1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

complete refactor getPoolLogs & getEventData

This commit is contained in:
alexcos20 2020-11-06 23:49:08 -08:00
parent 31f8c38ca8
commit 1d717cb3b0
2 changed files with 75 additions and 42 deletions

View File

@ -1,6 +1,6 @@
import Web3 from 'web3' import Web3 from 'web3'
import { AbiItem } from 'web3-utils/types' import { AbiItem } from 'web3-utils/types'
import { TransactionReceipt } from 'web3-core' import { TransactionReceipt, Log } from 'web3-core'
import { Pool } from './Pool' import { Pool } from './Pool'
import { EventData, Filter } from 'web3-eth-contract' import { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
@ -987,43 +987,34 @@ export class OceanPool extends Pool {
account?: string account?: string
): Promise<PoolTransaction[]> { ): Promise<PoolTransaction[]> {
const results: PoolTransaction[] = [] const results: PoolTransaction[] = []
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
const dtAddress = await this.getDTAddress(poolAddress) const dtAddress = await this.getDTAddress(poolAddress)
const filter: Filter = account ? { caller: account } : {}
let events: EventData[]
if (startBlock === 0) startBlock = this.startBlock if (startBlock === 0) startBlock = this.startBlock
events = await pool.getPastEvents('LOG_SWAP', { const swapTopic = super.getSwapEventSignature()
filter, const joinTopic = super.getJoinEventSignature()
fromBlock: startBlock, const exitTopic = super.getExitEventSignature()
toBlock: 'latest' let addressTopic
}) if (account)
addressTopic = '0x000000000000000000000000' + account.substring(2).toLowerCase()
for (let i = 0; i < events.length; i++) { else addressTopic = null
if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) const events = await this.web3.eth.getPastLogs({
results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) address: poolAddress,
} topics: [[swapTopic, joinTopic, exitTopic], addressTopic],
events = await pool.getPastEvents('LOG_JOIN', {
filter,
fromBlock: startBlock,
toBlock: 'latest'
})
for (let i = 0; i < events.length; i++) {
if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase())
results.push(await this.getEventData('join', poolAddress, dtAddress, events[i]))
}
events = await pool.getPastEvents('LOG_EXIT', {
filter,
fromBlock: startBlock, fromBlock: startBlock,
toBlock: 'latest' toBlock: 'latest'
}) })
for (let i = 0; i < events.length; i++) { for (let i = 0; i < events.length; i++) {
if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) switch (events[i].topics[0]) {
results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) case swapTopic:
results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i]))
break
case joinTopic:
results.push(await this.getEventData('join', poolAddress, dtAddress, events[i]))
break
case exitTopic:
results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i]))
break
}
} }
return results return results
} }
@ -1055,41 +1046,44 @@ export class OceanPool extends Pool {
type: PoolTransactionType, type: PoolTransactionType,
poolAddress: string, poolAddress: string,
dtAddress: string, dtAddress: string,
data: EventData data: Log
): Promise<PoolTransaction> { ): Promise<PoolTransaction> {
const blockDetails = await this.web3.eth.getBlock(data.blockNumber) const blockDetails = await this.web3.eth.getBlock(data.blockNumber)
let result: PoolTransaction = { let result: PoolTransaction = {
poolAddress, poolAddress,
dtAddress, dtAddress,
caller: data.returnValues[0], caller: data.topics[1],
transactionHash: data.transactionHash, transactionHash: data.transactionHash,
blockNumber: data.blockNumber, blockNumber: data.blockNumber,
timestamp: parseInt(String(blockDetails.timestamp)), timestamp: parseInt(String(blockDetails.timestamp)),
type type
} }
let params
switch (type) { switch (type) {
case 'swap': case 'swap':
params = this.web3.eth.abi.decodeParameters(['uint256', 'uint256'], data.data)
result = { result = {
...result, ...result,
tokenIn: data.returnValues[1], tokenIn: '0x' + data.topics[2].substring(data.topics[2].length - 40),
tokenOut: data.returnValues[2], tokenOut: '0x' + data.topics[2].substring(data.topics[3].length - 40),
tokenAmountIn: this.web3.utils.fromWei(data.returnValues[3]), tokenAmountIn: this.web3.utils.fromWei(params[0]),
tokenAmountOut: this.web3.utils.fromWei(data.returnValues[4]) tokenAmountOut: this.web3.utils.fromWei(params[1])
} }
break break
case 'join': case 'join':
params = this.web3.eth.abi.decodeParameters(['uint256'], data.data)
result = { result = {
...result, ...result,
tokenIn: data.returnValues[1], tokenIn: '0x' + data.topics[2].substring(data.topics[2].length - 40),
tokenAmountIn: this.web3.utils.fromWei(data.returnValues[2]) tokenAmountIn: this.web3.utils.fromWei(params[0])
} }
break break
case 'exit': case 'exit':
params = this.web3.eth.abi.decodeParameters(['uint256'], data.data)
result = { result = {
...result, ...result,
tokenOut: data.returnValues[1], tokenOut: '0x' + data.topics[2].substring(data.topics[2].length - 40),
tokenAmountOut: this.web3.utils.fromWei(data.returnValues[2]) tokenAmountOut: this.web3.utils.fromWei(params[0])
} }
break break
} }

View File

@ -1149,4 +1149,43 @@ export class Pool extends PoolFactory {
} }
return amount return amount
} }
/**
* Get LOG_SWAP encoded topic
* @return {String}
*/
public getSwapEventSignature(): string {
const abi = this.poolABI as AbiItem[]
const eventdata = abi.find(function (o) {
if (o.name === 'LOG_SWAP' && o.type === 'event') return o
})
const topic = this.web3.eth.abi.encodeEventSignature(eventdata as any)
return topic
}
/**
* Get LOG_JOIN encoded topic
* @return {String}
*/
public getJoinEventSignature(): string {
const abi = this.poolABI as AbiItem[]
const eventdata = abi.find(function (o) {
if (o.name === 'LOG_JOIN' && o.type === 'event') return o
})
const topic = this.web3.eth.abi.encodeEventSignature(eventdata as any)
return topic
}
/**
* Get LOG_EXIT encoded topic
* @return {String}
*/
public getExitEventSignature(): string {
const abi = this.poolABI as AbiItem[]
const eventdata = abi.find(function (o) {
if (o.name === 'LOG_EXIT' && o.type === 'event') return o
})
const topic = this.web3.eth.abi.encodeEventSignature(eventdata as any)
return topic
}
} }