diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index d2b57733..21c77c73 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -372,4 +372,167 @@ export class OceanPool extends Pool { swapFee ) } + + /** + * Search all pools created by an address + * @param {String} account If empty, will return all pools ever created by anybody + * @return {String[]} + */ + public async searchPoolsbyCreator(account?: string): Promise { + const result: string[] = [] + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { + from: account + }) + let myFilter + if (account) { + myFilter = { registeredBy: account } + } else { + myFilter = {} + } + const events = await factory.getPastEvents('BPoolRegistered', { + filter: myFilter, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + if (account) { + if (events[i].returnValues[1] === account) { + result.push(events[i].returnValues[0]) + } + } else result.push(events[i].returnValues[0]) + } + return result + } + + /** + * Get all actions from a pool (join,exit,swap) + * @param {String} poolAddress Pool address + * @param {String} account + * @param {Boolean} swaps Include swaps + * @param {Boolean} joins Include joins + * @param {Boolean} exists Include exits + * @return {String[]} + */ + public async getPoolLogs( + poolAddress: string, + account?: string, + swaps?: boolean, + joins?: boolean, + exits?: boolean + ): Promise { + const results = [] + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { + from: account + }) + let events + let myFilter + if (account) myFilter = { caller: account } + else myFilter = {} + if (swaps) { + events = await pool.getPastEvents('LOG_SWAP', { + filter: myFilter, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + if (account) { + if (events[i].returnValues[0] === account) { + results.push(this.getEventData('swap', poolAddress, events[i])) + } + } else { + results.push(this.getEventData('swap', poolAddress, events[i])) + } + } + } + if (joins) { + events = await pool.getPastEvents('LOG_JOIN', { + filter: myFilter, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + if (account) { + if (events[i].returnValues[0] === account) { + results.push(this.getEventData('join', poolAddress, events[i])) + } + } else { + results.push(this.getEventData('join', poolAddress, events[i])) + } + } + } + if (exits) { + events = await pool.getPastEvents('LOG_EXIT', { + filter: myFilter, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + if (account) { + if (events[i].returnValues[0] === account) { + results.push(this.getEventData('exit', poolAddress, events[i])) + } + } else { + results.push(this.getEventData('exit', poolAddress, events[i])) + } + } + } + return results + } + + /** + * Get all logs on all pools for a specific address + * @param {String} account + * @param {Boolean} swaps Include swaps + * @param {Boolean} joins Include joins + * @param {Boolean} exists Include exits + * @return {String[]} + */ + public async getAllPoolLogs( + account: string, + swaps?: boolean, + joins?: boolean, + exits?: boolean + ): Promise { + const results = [] + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { + from: account + }) + const events = await factory.getPastEvents('BPoolRegistered', { + filter: {}, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + results.push( + await this.getPoolLogs(events[i].returnValues[0], account, swaps, joins, exits) + ) + } + return results + } + + private getEventData(action: string, poolAddress: string, data: any) { + const result = Object() + result.action = action + result.poolAddress = poolAddress + result.caller = data.returnValues[0] + result.transactionHash = data.transactionHash + result.blockNumber = data.blockNumber + switch (action) { + case 'swap': + result.tokenIn = data.returnValues[1] + result.tokenOut = data.returnValues[2] + result.tokenAmountIn = data.returnValues[3] + result.tokenAmountOut = data.returnValues[4] + break + case 'join': + result.tokenIn = data.returnValues[1] + result.tokenAmountIn = data.returnValues[2] + break + case 'exit': + result.tokenOut = data.returnValues[1] + result.tokenAmountOut = data.returnValues[2] + break + } + return result + } } diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 99c236fe..4e093e1e 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -15,7 +15,7 @@ export interface TokensToAdd { } export class Pool extends PoolFactory { - private poolABI: AbiItem | AbiItem[] + public poolABI: AbiItem | AbiItem[] constructor( web3: Web3, diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 944d88c6..bee7ebc9 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -357,4 +357,76 @@ export class OceanFixedRateExchange { } return result } + + /** + * Get all exchanges, filtered by creator(if any) + * @param {String} account + * @return {Promise} + */ + public async searchExchangesbyCreator( + account?: string + ): Promise { + const result: FixedPricedExchange[] = [] + const events = await this.contract.getPastEvents('ExchangeCreated', { + filter: {}, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + if (account) { + if (events[i].returnValues[3] === account) + result.push(await this.getExchange(events[i].returnValues[0])) + } else result.push(await this.getExchange(events[i].returnValues[0])) + } + return result + } + + /** + * Get all swaps for an exchange, filtered by account(if any) + * @param {String} exchangeId + * @param {String} account + * @return {Promise} + */ + public async searchExchangesSwaps(exchangeId: string, account?: string): Promise { + const result: FixedPricedExchange[] = [] + const events = await this.contract.getPastEvents('Swapped', { + filter: { exchangeId: exchangeId }, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + if (account) { + if (events[i].returnValues[1] === account) + result.push(await this.getEventData(events[i])) + } else result.push(await this.getEventData(events[i])) + } + return result + } + + /** + * Get all swaps for an account + * @param {String} account + * @return {Promise} + */ + public async searchAllExchangesSwaps(account: string): Promise { + const result = [] + const events = await this.contract.getPastEvents('ExchangeCreated', { + filter: {}, + fromBlock: 0, + toBlock: 'latest' + }) + for (let i = 0; i < events.length; i++) { + result.push(await this.searchExchangesSwaps(events[i].returnValues[0], account)) + } + return result + } + + private getEventData(data: any) { + const result = Object() + result.exchangeID = data.returnValues[0] + result.caller = data.returnValues[1] + result.baseTokenAmount = data.returnValues[2] + result.dataTokenAmount = data.returnValues[3] + return result + } } diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index a76a403a..f38760e6 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -270,4 +270,19 @@ describe('Balancer flow', () => { assert(parseFloat(bobDtBalance) < parseFloat(newbobDtBalance)) assert(parseFloat(poolShares) > parseFloat(newpoolShares)) }) + + it('ALice should get all the pools that she created', async () => { + const alicePools = await Pool.searchPoolsbyCreator(alice) + assert(alicePools.length > 0) + }) + + it('ALice should get the logs for her pool', async () => { + const poolLogs = await Pool.getPoolLogs(greatPool, null, true, true, true) + assert(poolLogs.length > 0) + }) + it('Bob should get the logs for all his activities', async () => { + const poolLogs = await Pool.getAllPoolLogs(bob, true, true, true) + assert(poolLogs.length > 0) + }) + }) diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 7e6e7f30..00f1e795 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -226,4 +226,12 @@ describe('FixedRateExchange flow', () => { const exchangeDetails = await FixedRateClass.searchforDT(tokenAddress, tokenAmount) assert(exchangeDetails.length === 0) }) + it('Bob should find all the exchanges created by Alice', async () => { + const exchangeDetails = await FixedRateClass.searchExchangesbyCreator(alice) + assert(exchangeDetails.length > 0) + }) + it('Bob should find all his swaps', async () => { + const exchangeDetails = await FixedRateClass.searchAllExchangesSwaps(bob) + assert(exchangeDetails.length > 0) + }) })