From b815fbf0a135e0825674b45b64f1d911c03e6a59 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 16 Sep 2020 15:36:44 -0700 Subject: [PATCH 01/23] add stats --- src/balancer/OceanPool.ts | 163 ++++++++++++++++++ src/balancer/Pool.ts | 2 +- src/exchange/FixedRateExchange.ts | 72 ++++++++ test/unit/balancer/Balancer.test.ts | 15 ++ .../unit/exchanges/FixedPriceExchange.test.ts | 8 + 5 files changed, 259 insertions(+), 1 deletion(-) 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) + }) }) From fe570b7865e361bf1adb3f0132deb72661765f1b Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 16 Sep 2020 15:47:57 -0700 Subject: [PATCH 02/23] fix lint --- test/unit/balancer/Balancer.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index f38760e6..92be1aa2 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -284,5 +284,4 @@ describe('Balancer flow', () => { const poolLogs = await Pool.getAllPoolLogs(bob, true, true, true) assert(poolLogs.length > 0) }) - }) From c6a9f7d69b8ac60f349e62a3dbc161b835b4fb41 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 16 Sep 2020 15:36:44 -0700 Subject: [PATCH 03/23] add stats --- src/balancer/OceanPool.ts | 163 ++++++++++++++++++ src/balancer/Pool.ts | 2 +- src/exchange/FixedRateExchange.ts | 72 ++++++++ test/unit/balancer/Balancer.test.ts | 15 ++ .../unit/exchanges/FixedPriceExchange.test.ts | 8 + 5 files changed, 259 insertions(+), 1 deletion(-) 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) + }) }) From 34daf5cfe96cf97447b3ea1de27e4e654385ed0e Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 16 Sep 2020 15:47:57 -0700 Subject: [PATCH 04/23] fix lint --- test/unit/balancer/Balancer.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index f38760e6..92be1aa2 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -284,5 +284,4 @@ describe('Balancer flow', () => { const poolLogs = await Pool.getAllPoolLogs(bob, true, true, true) assert(poolLogs.length > 0) }) - }) From 1f15a67d649392a07c0189681a17e91961cfd435 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 24 Sep 2020 22:15:33 -0700 Subject: [PATCH 05/23] add getPoolsbyCreator --- src/balancer/OceanPool.ts | 27 ++++++++++++++++++--------- src/balancer/Pool.ts | 14 ++++---------- test/unit/balancer/Balancer.test.ts | 4 ++-- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 21c77c73..f6935d73 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -3,6 +3,11 @@ import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' +export interface PoolDetails { + poolAddress: string + tokens: string[] +} + /** * Ocean Pools submodule exposed under ocean.pool */ @@ -84,7 +89,7 @@ export class OceanPool extends Pool { */ public async getDTAddress(account: string, poolAddress: string): Promise { this.dtAddress = null - const tokens = await this.getCurrentTokens(account, poolAddress) + const tokens = await this.getCurrentTokens(poolAddress) let token: string for (token of tokens) { @@ -335,10 +340,7 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - const constituents = await super.getCurrentTokens( - account, - events[i].returnValues[0] - ) + const constituents = await super.getCurrentTokens(events[i].returnValues[0]) if (constituents.includes(dtAddress)) result.push(events[i].returnValues[0]) } return result @@ -378,8 +380,8 @@ export class OceanPool extends Pool { * @param {String} account If empty, will return all pools ever created by anybody * @return {String[]} */ - public async searchPoolsbyCreator(account?: string): Promise { - const result: string[] = [] + public async getPoolsbyCreator(account?: string): Promise { + const result: PoolDetails[] = [] const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) @@ -397,13 +399,20 @@ export class OceanPool extends Pool { for (let i = 0; i < events.length; i++) { if (account) { if (events[i].returnValues[1] === account) { - result.push(events[i].returnValues[0]) + result.push(await this.getPoolDetails(events[i].returnValues[0])) } - } else result.push(events[i].returnValues[0]) + } else result.push(await this.getPoolDetails(events[i].returnValues[0])) } return result } + public async getPoolDetails(poolAddress: string): Promise { + const details: PoolDetails = null + details.poolAddress = poolAddress + details.tokens = await super.getFinalTokens(poolAddress) + return details + } + /** * Get all actions from a pool (join,exit,swap) * @param {String} poolAddress Pool address diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 4e093e1e..cb930518 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -275,14 +275,11 @@ export class Pool extends PoolFactory { /** * Get tokens composing this pool - * @param {String} account * @param {String} poolAddress * @return {String[]} */ - async getCurrentTokens(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getCurrentTokens(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null try { result = await pool.methods.getCurrentTokens().call() @@ -294,14 +291,11 @@ export class Pool extends PoolFactory { /** * Get the final tokens composing this pool - * @param {String} account * @param {String} poolAddress * @return {String[]} */ - async getFinalTokens(account: string, poolAddress: string): Promise { - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) + async getFinalTokens(poolAddress: string): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let result = null try { result = await pool.methods.getFinalTokens().call() diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 92be1aa2..8133de13 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -122,7 +122,7 @@ describe('Balancer flow', () => { assert(String(n) === '2', 'unexpected num tokens: ' + n) }) it('Get pool information', async () => { - const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress) + const currentTokens = await Pool.getCurrentTokens(alicePoolAddress) assert(currentTokens.length === 2) assert(currentTokens.includes(tokenAddress)) assert(currentTokens.includes(oceanTokenAddress)) @@ -272,7 +272,7 @@ describe('Balancer flow', () => { }) it('ALice should get all the pools that she created', async () => { - const alicePools = await Pool.searchPoolsbyCreator(alice) + const alicePools = await Pool.getPoolsbyCreator(alice) assert(alicePools.length > 0) }) From 43dee2fca7fca7f63b3ff7d2d7cc276f5adfdf88 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 25 Sep 2020 00:49:19 -0700 Subject: [PATCH 06/23] fix tests --- src/balancer/OceanPool.ts | 11 +++-- src/exchange/FixedRateExchange.ts | 44 +++++++++++++------ .../unit/exchanges/FixedPriceExchange.test.ts | 4 +- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index f6935d73..3a9ffb30 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -382,9 +382,7 @@ export class OceanPool extends Pool { */ public async getPoolsbyCreator(account?: string): Promise { const result: PoolDetails[] = [] - const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { - from: account - }) + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) let myFilter if (account) { myFilter = { registeredBy: account } @@ -405,9 +403,14 @@ export class OceanPool extends Pool { } return result } + /** + * Get pool details + * @param {String} poolAddress Pool address + * @return {PoolDetails[]} + */ public async getPoolDetails(poolAddress: string): Promise { - const details: PoolDetails = null + const details: PoolDetails = { poolAddress: null, tokens: null } details.poolAddress = poolAddress details.tokens = await super.getFinalTokens(poolAddress) return details diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index bee7ebc9..00c1481e 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -15,6 +15,12 @@ export interface FixedPricedExchange { supply: string } +export interface FixedPricedSwap { + exchangeID: string + caller: string + baseTokenAmount: string + dataTokenAmount: string +} const DEFAULT_GAS_LIMIT = 300000 export class OceanFixedRateExchange { @@ -363,9 +369,7 @@ export class OceanFixedRateExchange { * @param {String} account * @return {Promise} */ - public async searchExchangesbyCreator( - account?: string - ): Promise { + public async getExchangesbyCreator(account?: string): Promise { const result: FixedPricedExchange[] = [] const events = await this.contract.getPastEvents('ExchangeCreated', { filter: {}, @@ -385,10 +389,13 @@ export class OceanFixedRateExchange { * Get all swaps for an exchange, filtered by account(if any) * @param {String} exchangeId * @param {String} account - * @return {Promise} + * @return {Promise} */ - public async searchExchangesSwaps(exchangeId: string, account?: string): Promise { - const result: FixedPricedExchange[] = [] + public async getExchangeSwaps( + exchangeId: string, + account?: string + ): Promise { + const result: FixedPricedSwap[] = [] const events = await this.contract.getPastEvents('Swapped', { filter: { exchangeId: exchangeId }, fromBlock: 0, @@ -406,23 +413,34 @@ export class OceanFixedRateExchange { /** * Get all swaps for an account * @param {String} account - * @return {Promise} + * @return {Promise} */ - public async searchAllExchangesSwaps(account: string): Promise { - const result = [] + public async getAllExchangesSwaps(account: string): Promise { + const result: FixedPricedSwap[] = [] 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)) - } + const swaps: FixedPricedSwap[] = await this.getExchangeSwaps( + events[i].returnValues[0], + account + ) + swaps.forEach((swap) => { + result.push(swap) + }) + } return result } - private getEventData(data: any) { - const result = Object() + private getEventData(data: any): FixedPricedSwap { + const result: FixedPricedSwap = { + exchangeID: null, + caller: null, + baseTokenAmount: null, + dataTokenAmount: null + } result.exchangeID = data.returnValues[0] result.caller = data.returnValues[1] result.baseTokenAmount = data.returnValues[2] diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 00f1e795..ea4cc205 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -227,11 +227,11 @@ describe('FixedRateExchange flow', () => { assert(exchangeDetails.length === 0) }) it('Bob should find all the exchanges created by Alice', async () => { - const exchangeDetails = await FixedRateClass.searchExchangesbyCreator(alice) + const exchangeDetails = await FixedRateClass.getExchangesbyCreator(alice) assert(exchangeDetails.length > 0) }) it('Bob should find all his swaps', async () => { - const exchangeDetails = await FixedRateClass.searchAllExchangesSwaps(bob) + const exchangeDetails = await FixedRateClass.getAllExchangesSwaps(bob) assert(exchangeDetails.length > 0) }) }) From ee53e5f41a829b907000d4d3dba18fd0635c66f7 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Fri, 25 Sep 2020 01:17:41 -0700 Subject: [PATCH 07/23] add interface for PoolLogs & Actions --- src/balancer/OceanPool.ts | 59 +++++++++++++++++++++-------- src/exchange/FixedRateExchange.ts | 4 +- test/unit/balancer/Balancer.test.ts | 4 +- 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 3a9ffb30..cca5bcc2 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -8,6 +8,22 @@ export interface PoolDetails { tokens: string[] } +export interface PoolAction { + poolAddress: string + caller: string + transactionHash: string + blockNumber: number + tokenIn?: string + tokenOut?: string + tokenAmountIn?: string + tokenAmountOut?: string +} + +export interface PoolLogs { + joins?: PoolAction[] + exists?: PoolAction[] + swaps?: PoolAction[] +} /** * Ocean Pools submodule exposed under ocean.pool */ @@ -423,7 +439,7 @@ export class OceanPool extends Pool { * @param {Boolean} swaps Include swaps * @param {Boolean} joins Include joins * @param {Boolean} exists Include exits - * @return {String[]} + * @return {PoolLogs[]} */ public async getPoolLogs( poolAddress: string, @@ -431,8 +447,8 @@ export class OceanPool extends Pool { swaps?: boolean, joins?: boolean, exits?: boolean - ): Promise { - const results = [] + ): Promise { + const results: PoolLogs = { joins: [], exists: [], swaps: [] } const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) @@ -449,10 +465,10 @@ export class OceanPool extends Pool { for (let i = 0; i < events.length; i++) { if (account) { if (events[i].returnValues[0] === account) { - results.push(this.getEventData('swap', poolAddress, events[i])) + results.swaps.push(this.getEventData('swap', poolAddress, events[i])) } } else { - results.push(this.getEventData('swap', poolAddress, events[i])) + results.swaps.push(this.getEventData('swap', poolAddress, events[i])) } } } @@ -465,10 +481,10 @@ export class OceanPool extends Pool { for (let i = 0; i < events.length; i++) { if (account) { if (events[i].returnValues[0] === account) { - results.push(this.getEventData('join', poolAddress, events[i])) + results.joins.push(this.getEventData('join', poolAddress, events[i])) } } else { - results.push(this.getEventData('join', poolAddress, events[i])) + results.joins.push(this.getEventData('join', poolAddress, events[i])) } } } @@ -481,10 +497,10 @@ export class OceanPool extends Pool { for (let i = 0; i < events.length; i++) { if (account) { if (events[i].returnValues[0] === account) { - results.push(this.getEventData('exit', poolAddress, events[i])) + results.exists.push(this.getEventData('exit', poolAddress, events[i])) } } else { - results.push(this.getEventData('exit', poolAddress, events[i])) + results.exists.push(this.getEventData('exit', poolAddress, events[i])) } } } @@ -497,15 +513,15 @@ export class OceanPool extends Pool { * @param {Boolean} swaps Include swaps * @param {Boolean} joins Include joins * @param {Boolean} exists Include exits - * @return {String[]} + * @return {PoolLogs} */ public async getAllPoolLogs( account: string, swaps?: boolean, joins?: boolean, exits?: boolean - ): Promise { - const results = [] + ): Promise { + const results: PoolLogs = { joins: [], exists: [], swaps: [] } const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) @@ -515,14 +531,27 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - results.push( - await this.getPoolLogs(events[i].returnValues[0], account, swaps, joins, exits) + const logs = await this.getPoolLogs( + events[i].returnValues[0], + account, + swaps, + joins, + exits ) + logs.joins.forEach((log) => { + results.joins.push(log) + }) + logs.exists.forEach((log) => { + results.exists.push(log) + }) + logs.swaps.forEach((log) => { + results.swaps.push(log) + }) } return results } - private getEventData(action: string, poolAddress: string, data: any) { + private getEventData(action: string, poolAddress: string, data: any): PoolAction { const result = Object() result.action = action result.poolAddress = poolAddress diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 00c1481e..99a41f91 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -429,8 +429,8 @@ export class OceanFixedRateExchange { ) swaps.forEach((swap) => { result.push(swap) - }) - } + }) + } return result } diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 8133de13..14920507 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -278,10 +278,10 @@ describe('Balancer flow', () => { it('ALice should get the logs for her pool', async () => { const poolLogs = await Pool.getPoolLogs(greatPool, null, true, true, true) - assert(poolLogs.length > 0) + assert(poolLogs.joins.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) + assert(poolLogs.swaps.length > 0) }) }) From 122fc40f63596fc5f8ea0c4bf2603c79e96b8c77 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 29 Sep 2020 17:40:50 +0200 Subject: [PATCH 08/23] typo --- src/balancer/OceanPool.ts | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index cca5bcc2..afdd9d1b 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -2,6 +2,7 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' +import { EventData } from 'web3-eth-contract' export interface PoolDetails { poolAddress: string @@ -21,7 +22,7 @@ export interface PoolAction { export interface PoolLogs { joins?: PoolAction[] - exists?: PoolAction[] + exits?: PoolAction[] swaps?: PoolAction[] } /** @@ -438,7 +439,7 @@ export class OceanPool extends Pool { * @param {String} account * @param {Boolean} swaps Include swaps * @param {Boolean} joins Include joins - * @param {Boolean} exists Include exits + * @param {Boolean} exits Include exits * @return {PoolLogs[]} */ public async getPoolLogs( @@ -448,14 +449,17 @@ export class OceanPool extends Pool { joins?: boolean, exits?: boolean ): Promise { - const results: PoolLogs = { joins: [], exists: [], swaps: [] } + const results: PoolLogs = { joins: [], exits: [], swaps: [] } const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) - let events + + let events: EventData[] let myFilter + if (account) myFilter = { caller: account } else myFilter = {} + if (swaps) { events = await pool.getPastEvents('LOG_SWAP', { filter: myFilter, @@ -497,10 +501,10 @@ export class OceanPool extends Pool { for (let i = 0; i < events.length; i++) { if (account) { if (events[i].returnValues[0] === account) { - results.exists.push(this.getEventData('exit', poolAddress, events[i])) + results.exits.push(this.getEventData('exit', poolAddress, events[i])) } } else { - results.exists.push(this.getEventData('exit', poolAddress, events[i])) + results.exits.push(this.getEventData('exit', poolAddress, events[i])) } } } @@ -512,7 +516,7 @@ export class OceanPool extends Pool { * @param {String} account * @param {Boolean} swaps Include swaps * @param {Boolean} joins Include joins - * @param {Boolean} exists Include exits + * @param {Boolean} exits Include exits * @return {PoolLogs} */ public async getAllPoolLogs( @@ -521,7 +525,7 @@ export class OceanPool extends Pool { joins?: boolean, exits?: boolean ): Promise { - const results: PoolLogs = { joins: [], exists: [], swaps: [] } + const results: PoolLogs = { joins: [], exits: [], swaps: [] } const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) @@ -541,8 +545,8 @@ export class OceanPool extends Pool { logs.joins.forEach((log) => { results.joins.push(log) }) - logs.exists.forEach((log) => { - results.exists.push(log) + logs.exits.forEach((log) => { + results.exits.push(log) }) logs.swaps.forEach((log) => { results.swaps.push(log) From 63e01ee85813de3de70a04c5bc59cd64e42e5091 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 29 Sep 2020 21:48:48 +0200 Subject: [PATCH 09/23] getEventData refactor --- src/balancer/OceanPool.ts | 40 +++++++++++++++++++------------ src/exchange/FixedRateExchange.ts | 16 +++++-------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index afdd9d1b..9042f43f 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -555,27 +555,37 @@ export class OceanPool extends Pool { return results } - private getEventData(action: string, poolAddress: string, data: any): PoolAction { - const result = Object() - result.action = action - result.poolAddress = poolAddress - result.caller = data.returnValues[0] - result.transactionHash = data.transactionHash - result.blockNumber = data.blockNumber + private getEventData(action: string, poolAddress: string, data: EventData): PoolAction { + let result: PoolAction = { + poolAddress, + caller: data.returnValues[0], + transactionHash: data.transactionHash, + 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] + result = { + ...result, + tokenIn: data.returnValues[1], + tokenOut: data.returnValues[2], + tokenAmountIn: data.returnValues[3], + tokenAmountOut: data.returnValues[4] + } break case 'join': - result.tokenIn = data.returnValues[1] - result.tokenAmountIn = data.returnValues[2] + result = { + ...result, + tokenIn: data.returnValues[1], + tokenAmountIn: data.returnValues[2] + } break case 'exit': - result.tokenOut = data.returnValues[1] - result.tokenAmountOut = data.returnValues[2] + result = { + ...result, + tokenOut: data.returnValues[1], + tokenAmountOut: data.returnValues[2] + } break } return result diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 99a41f91..7910875a 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -1,7 +1,7 @@ import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' import BigNumber from 'bignumber.js' import { TransactionReceipt } from 'web3-core' -import { Contract } from 'web3-eth-contract' +import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' @@ -434,17 +434,13 @@ export class OceanFixedRateExchange { return result } - private getEventData(data: any): FixedPricedSwap { + private getEventData(data: EventData): FixedPricedSwap { const result: FixedPricedSwap = { - exchangeID: null, - caller: null, - baseTokenAmount: null, - dataTokenAmount: null + exchangeID: data.returnValues[0], + caller: data.returnValues[1], + baseTokenAmount: data.returnValues[2], + dataTokenAmount: data.returnValues[3] } - result.exchangeID = data.returnValues[0] - result.caller = data.returnValues[1] - result.baseTokenAmount = data.returnValues[2] - result.dataTokenAmount = data.returnValues[3] return result } } From 54cb529ff3a712c942452897cc415e5e1d5ef988 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 29 Sep 2020 21:58:39 +0200 Subject: [PATCH 10/23] typing updates, small refactor --- src/balancer/OceanPool.ts | 41 +++++++++++++------------------ src/exchange/FixedRateExchange.ts | 4 +-- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 9042f43f..8d013747 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -2,7 +2,7 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' -import { EventData } from 'web3-eth-contract' +import { EventData, Filter } from 'web3-eth-contract' export interface PoolDetails { poolAddress: string @@ -395,22 +395,18 @@ export class OceanPool extends Pool { /** * Search all pools created by an address * @param {String} account If empty, will return all pools ever created by anybody - * @return {String[]} + * @return {PoolDetails[]} */ public async getPoolsbyCreator(account?: string): Promise { const result: PoolDetails[] = [] const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) - let myFilter - if (account) { - myFilter = { registeredBy: account } - } else { - myFilter = {} - } + const events = await factory.getPastEvents('BPoolRegistered', { - filter: myFilter, + filter: account ? { registeredBy: account } : {}, fromBlock: 0, toBlock: 'latest' }) + for (let i = 0; i < events.length; i++) { if (account) { if (events[i].returnValues[1] === account) { @@ -420,16 +416,15 @@ export class OceanPool extends Pool { } return result } + /** * Get pool details * @param {String} poolAddress Pool address - * @return {PoolDetails[]} + * @return {PoolDetails} */ - public async getPoolDetails(poolAddress: string): Promise { - const details: PoolDetails = { poolAddress: null, tokens: null } - details.poolAddress = poolAddress - details.tokens = await super.getFinalTokens(poolAddress) + const tokens = await super.getFinalTokens(poolAddress) + const details: PoolDetails = { poolAddress, tokens } return details } @@ -440,7 +435,7 @@ export class OceanPool extends Pool { * @param {Boolean} swaps Include swaps * @param {Boolean} joins Include joins * @param {Boolean} exits Include exits - * @return {PoolLogs[]} + * @return {PoolLogs} */ public async getPoolLogs( poolAddress: string, @@ -450,15 +445,9 @@ export class OceanPool extends Pool { exits?: boolean ): Promise { const results: PoolLogs = { joins: [], exits: [], swaps: [] } - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { - from: account - }) - + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) + const myFilter: Filter = account ? { caller: account } : {} let events: EventData[] - let myFilter - - if (account) myFilter = { caller: account } - else myFilter = {} if (swaps) { events = await pool.getPastEvents('LOG_SWAP', { @@ -555,7 +544,11 @@ export class OceanPool extends Pool { return results } - private getEventData(action: string, poolAddress: string, data: EventData): PoolAction { + private getEventData( + action: 'swap' | 'join' | 'exit', + poolAddress: string, + data: EventData + ): PoolAction { let result: PoolAction = { poolAddress, caller: data.returnValues[0], diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 7910875a..35add8f6 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -404,8 +404,8 @@ export class OceanFixedRateExchange { 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])) + result.push(this.getEventData(events[i])) + } else result.push(this.getEventData(events[i])) } return result } From 6f645d6e8fd134a69b56e862e9652a830308df12 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 02:59:15 -0700 Subject: [PATCH 11/23] small refactor --- src/balancer/OceanPool.ts | 152 ++++++++++++---------------- test/unit/balancer/Balancer.test.ts | 8 +- 2 files changed, 68 insertions(+), 92 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 8d013747..0e3c99e6 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -9,22 +9,20 @@ export interface PoolDetails { tokens: string[] } -export interface PoolAction { +export interface PoolTransaction { poolAddress: string + dtAddress: string caller: string transactionHash: string blockNumber: number + timestamp: number tokenIn?: string tokenOut?: string tokenAmountIn?: string tokenAmountOut?: string + type: 'join' | 'exit' | 'swap' } -export interface PoolLogs { - joins?: PoolAction[] - exits?: PoolAction[] - swaps?: PoolAction[] -} /** * Ocean Pools submodule exposed under ocean.pool */ @@ -104,7 +102,7 @@ export class OceanPool extends Pool { * @param {String} poolAddress * @return {string} */ - public async getDTAddress(account: string, poolAddress: string): Promise { + public async getDTAddress(poolAddress: string): Promise { this.dtAddress = null const tokens = await this.getCurrentTokens(poolAddress) let token: string @@ -137,7 +135,7 @@ export class OceanPool extends Pool { * @return {String} */ public async getDTReserve(account: string, poolAddress: string): Promise { - await this.getDTAddress(account, poolAddress) + await this.getDTAddress(poolAddress) return super.getReserve(account, poolAddress, this.dtAddress) } @@ -161,7 +159,7 @@ export class OceanPool extends Pool { console.error('oceanAddress is not defined') return null } - await this.getDTAddress(account, poolAddress) + await this.getDTAddress(poolAddress) // TODO - check balances first await super.approve( @@ -202,7 +200,7 @@ export class OceanPool extends Pool { console.error('oceanAddress is not defined') return null } - await this.getDTAddress(account, poolAddress) + await this.getDTAddress(poolAddress) return this.swapExactAmountOut( account, poolAddress, @@ -226,7 +224,7 @@ export class OceanPool extends Pool { poolAddress: string, amount: string ): Promise { - await this.getDTAddress(account, poolAddress) + await this.getDTAddress(poolAddress) await super.approve( account, this.dtAddress, @@ -256,7 +254,7 @@ export class OceanPool extends Pool { amount: string, maximumPoolShares: string ): Promise { - await this.getDTAddress(account, poolAddress) + await this.getDTAddress(poolAddress) // TODO Check balance of PoolShares before doing exit return this.exitswapExternAmountOut( account, @@ -368,7 +366,7 @@ export class OceanPool extends Pool { poolAddress: string, dtRequired: string ): Promise { - await this.getDTAddress(account, poolAddress) + await this.getDTAddress(poolAddress) const tokenBalanceIn = await this.getReserve(account, poolAddress, this.oceanAddress) const tokenWeightIn = await this.getDenormalizedWeight( account, @@ -439,82 +437,69 @@ export class OceanPool extends Pool { */ public async getPoolLogs( poolAddress: string, - account?: string, - swaps?: boolean, - joins?: boolean, - exits?: boolean - ): Promise { - const results: PoolLogs = { joins: [], exits: [], swaps: [] } + account?: string + ): Promise { + const results: PoolTransaction[] = [] const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) + const dtAddress = await this.getDTAddress(poolAddress) const myFilter: Filter = account ? { caller: account } : {} let events: EventData[] - 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.swaps.push(this.getEventData('swap', poolAddress, events[i])) - } - } else { - results.swaps.push(this.getEventData('swap', poolAddress, events[i])) + 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(await this.getEventData('swap', poolAddress, dtAddress, events[i])) } + } else { + results.push(await this.getEventData('swap', poolAddress, dtAddress, 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.joins.push(this.getEventData('join', poolAddress, events[i])) - } - } else { - results.joins.push(this.getEventData('join', poolAddress, events[i])) + + 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(await this.getEventData('join', poolAddress, dtAddress, events[i])) } + } else { + results.push(await this.getEventData('join', poolAddress, dtAddress, 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.exits.push(this.getEventData('exit', poolAddress, events[i])) - } - } else { - results.exits.push(this.getEventData('exit', poolAddress, events[i])) + + 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(await this.getEventData('exit', poolAddress, dtAddress, events[i])) } + } else { + results.push(await this.getEventData('exit', poolAddress, dtAddress, 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} exits Include exits * @return {PoolLogs} */ - public async getAllPoolLogs( - account: string, - swaps?: boolean, - joins?: boolean, - exits?: boolean - ): Promise { - const results: PoolLogs = { joins: [], exits: [], swaps: [] } + public async getAllPoolLogs(account: string): Promise { + const results: PoolTransaction[] = [] const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { from: account }) @@ -524,36 +509,27 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - const logs = await this.getPoolLogs( - events[i].returnValues[0], - account, - swaps, - joins, - exits - ) - logs.joins.forEach((log) => { - results.joins.push(log) - }) - logs.exits.forEach((log) => { - results.exits.push(log) - }) - logs.swaps.forEach((log) => { - results.swaps.push(log) - }) + const logs = await this.getPoolLogs(events[i].returnValues[0], account) + for (let j = 0; j < logs.length; j++) results.push(logs[j]) } return results } - private getEventData( + private async getEventData( action: 'swap' | 'join' | 'exit', poolAddress: string, + dtAddress: string, data: EventData - ): PoolAction { - let result: PoolAction = { + ): Promise { + const blockDetails = await this.web3.eth.getBlock(data.blockNumber) + let result: PoolTransaction = { poolAddress, + dtAddress, caller: data.returnValues[0], transactionHash: data.transactionHash, - blockNumber: data.blockNumber + blockNumber: data.blockNumber, + timestamp: parseInt(String(blockDetails.timestamp)), + type: action } switch (action) { diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 14920507..063c56b7 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -277,11 +277,11 @@ describe('Balancer flow', () => { }) it('ALice should get the logs for her pool', async () => { - const poolLogs = await Pool.getPoolLogs(greatPool, null, true, true, true) - assert(poolLogs.joins.length > 0) + const poolLogs = await Pool.getPoolLogs(greatPool, null) + 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.swaps.length > 0) + const poolLogs = await Pool.getAllPoolLogs(bob) + assert(poolLogs.length > 0) }) }) From a1916b0e04d921dd13b12b8e2c7d0688e04aa1d2 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 03:23:42 -0700 Subject: [PATCH 12/23] optimizations --- src/balancer/OceanPool.ts | 34 +++++++--------------------------- 1 file changed, 7 insertions(+), 27 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 0e3c99e6..7231a0f0 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -406,11 +406,8 @@ export class OceanPool extends Pool { }) for (let i = 0; i < events.length; i++) { - if (account) { - if (events[i].returnValues[1] === account) { - result.push(await this.getPoolDetails(events[i].returnValues[0])) - } - } else result.push(await this.getPoolDetails(events[i].returnValues[0])) + if (!account || events[i].returnValues[1] === account) + result.push(await this.getPoolDetails(events[i].returnValues[0])) } return result } @@ -440,7 +437,7 @@ export class OceanPool extends Pool { account?: string ): Promise { const results: PoolTransaction[] = [] - const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account }) + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const dtAddress = await this.getDTAddress(poolAddress) const myFilter: Filter = account ? { caller: account } : {} let events: EventData[] @@ -451,13 +448,8 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (account) { - if (events[i].returnValues[0] === account) { - results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) - } - } else { + if (!account || events[i].returnValues[0] === account) results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) - } } events = await pool.getPastEvents('LOG_JOIN', { @@ -466,13 +458,8 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (account) { - if (events[i].returnValues[0] === account) { - results.push(await this.getEventData('join', poolAddress, dtAddress, events[i])) - } - } else { + if (!account || events[i].returnValues[0] === account) results.push(await this.getEventData('join', poolAddress, dtAddress, events[i])) - } } events = await pool.getPastEvents('LOG_EXIT', { @@ -481,13 +468,8 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (account) { - if (events[i].returnValues[0] === account) { - results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) - } - } else { + if (!account || events[i].returnValues[0] === account) results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) - } } return results @@ -500,9 +482,7 @@ export class OceanPool extends Pool { */ public async getAllPoolLogs(account: string): Promise { const results: PoolTransaction[] = [] - const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress, { - from: account - }) + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) const events = await factory.getPastEvents('BPoolRegistered', { filter: {}, fromBlock: 0, From ca5d2c2a54dc5400e81c76a4b1b84427745a0d27 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 03:41:56 -0700 Subject: [PATCH 13/23] optimizations --- src/balancer/OceanPool.ts | 7 ++----- src/exchange/FixedRateExchange.ts | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 7231a0f0..e8fdc49b 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -427,10 +427,7 @@ export class OceanPool extends Pool { * 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} exits Include exits - * @return {PoolLogs} + * @return {PoolTransaction[]} */ public async getPoolLogs( poolAddress: string, @@ -478,7 +475,7 @@ export class OceanPool extends Pool { /** * Get all logs on all pools for a specific address * @param {String} account - * @return {PoolLogs} + * @return {PoolTransaction[]} */ public async getAllPoolLogs(account: string): Promise { const results: PoolTransaction[] = [] diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 35add8f6..f7db94af 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -5,7 +5,7 @@ import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' -export interface FixedPricedExchange { +export interface FixedPriceExchange { exchangeID?: string exchangeOwner: string dataToken: string @@ -15,7 +15,7 @@ export interface FixedPricedExchange { supply: string } -export interface FixedPricedSwap { +export interface FixedPriceSwap { exchangeID: string caller: string baseTokenAmount: string @@ -298,8 +298,8 @@ export class OceanFixedRateExchange { * @param {String} exchangeId ExchangeId * @return {Promise} Exchange details */ - public async getExchange(exchangeId: string): Promise { - const result: FixedPricedExchange = await this.contract.methods + public async getExchange(exchangeId: string): Promise { + const result: FixedPriceExchange = await this.contract.methods .getExchange(exchangeId) .call() return result @@ -343,8 +343,8 @@ export class OceanFixedRateExchange { public async searchforDT( dataTokenAddress: string, minSupply: string - ): Promise { - const result: FixedPricedExchange[] = [] + ): Promise { + const result: FixedPriceExchange[] = [] const events = await this.contract.getPastEvents('ExchangeCreated', { filter: { datatoken: dataTokenAddress }, fromBlock: 0, @@ -369,8 +369,8 @@ export class OceanFixedRateExchange { * @param {String} account * @return {Promise} */ - public async getExchangesbyCreator(account?: string): Promise { - const result: FixedPricedExchange[] = [] + public async getExchangesbyCreator(account?: string): Promise { + const result: FixedPriceExchange[] = [] const events = await this.contract.getPastEvents('ExchangeCreated', { filter: {}, fromBlock: 0, @@ -394,8 +394,8 @@ export class OceanFixedRateExchange { public async getExchangeSwaps( exchangeId: string, account?: string - ): Promise { - const result: FixedPricedSwap[] = [] + ): Promise { + const result: FixedPriceSwap[] = [] const events = await this.contract.getPastEvents('Swapped', { filter: { exchangeId: exchangeId }, fromBlock: 0, @@ -415,15 +415,15 @@ export class OceanFixedRateExchange { * @param {String} account * @return {Promise} */ - public async getAllExchangesSwaps(account: string): Promise { - const result: FixedPricedSwap[] = [] + public async getAllExchangesSwaps(account: string): Promise { + const result: FixedPriceSwap[] = [] const events = await this.contract.getPastEvents('ExchangeCreated', { filter: {}, fromBlock: 0, toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - const swaps: FixedPricedSwap[] = await this.getExchangeSwaps( + const swaps: FixedPriceSwap[] = await this.getExchangeSwaps( events[i].returnValues[0], account ) @@ -434,8 +434,8 @@ export class OceanFixedRateExchange { return result } - private getEventData(data: EventData): FixedPricedSwap { - const result: FixedPricedSwap = { + private getEventData(data: EventData): FixedPriceSwap { + const result: FixedPriceSwap = { exchangeID: data.returnValues[0], caller: data.returnValues[1], baseTokenAmount: data.returnValues[2], From 9195cfbcca8cc55c3464ed5d3dedc8ce4534ec98 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 03:46:18 -0700 Subject: [PATCH 14/23] optimizations --- src/balancer/OceanPool.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index e8fdc49b..6bb2c2cd 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -493,7 +493,7 @@ export class OceanPool extends Pool { } private async getEventData( - action: 'swap' | 'join' | 'exit', + type: 'swap' | 'join' | 'exit', poolAddress: string, dtAddress: string, data: EventData @@ -506,10 +506,10 @@ export class OceanPool extends Pool { transactionHash: data.transactionHash, blockNumber: data.blockNumber, timestamp: parseInt(String(blockDetails.timestamp)), - type: action + type } - switch (action) { + switch (type) { case 'swap': result = { ...result, From 24e5e371012db95bef10e55c63c05925b63b074c Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 03:51:08 -0700 Subject: [PATCH 15/23] optimizations --- src/balancer/OceanPool.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 6bb2c2cd..458fe606 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -4,6 +4,8 @@ import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' import { EventData, Filter } from 'web3-eth-contract' +declare type PoolTransactionType = 'swap' | 'join' | 'exit' + export interface PoolDetails { poolAddress: string tokens: string[] @@ -493,7 +495,7 @@ export class OceanPool extends Pool { } private async getEventData( - type: 'swap' | 'join' | 'exit', + type: PoolTransactionType, poolAddress: string, dtAddress: string, data: EventData From 25673f9593a546540fa635ff1a93dd57aa726c12 Mon Sep 17 00:00:00 2001 From: Alex Coseru Date: Wed, 30 Sep 2020 13:52:51 +0300 Subject: [PATCH 16/23] Update src/balancer/OceanPool.ts Co-authored-by: Matthias Kretschmann --- src/balancer/OceanPool.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 458fe606..2eab8d11 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -22,7 +22,7 @@ export interface PoolTransaction { tokenOut?: string tokenAmountIn?: string tokenAmountOut?: string - type: 'join' | 'exit' | 'swap' + type: PoolTransactionType } /** From a2fbbb37654886bf96ead1e335fc24425eb9fdc0 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 04:07:04 -0700 Subject: [PATCH 17/23] lowercase account testing --- src/balancer/OceanPool.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 2eab8d11..58041c97 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -408,7 +408,7 @@ export class OceanPool extends Pool { }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[1] === account) + if (!account || events[i].returnValues[1].toLowerCase() === account.toLowerCase()) result.push(await this.getPoolDetails(events[i].returnValues[0])) } return result @@ -447,7 +447,7 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0] === account) + if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) } @@ -457,7 +457,7 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0] === account) + if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('join', poolAddress, dtAddress, events[i])) } @@ -467,7 +467,7 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0] === account) + if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) } From bad9f6641e4f62e5110518f0932cd3fb83d04814 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 30 Sep 2020 13:33:15 +0200 Subject: [PATCH 18/23] debug --- src/balancer/OceanPool.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 58041c97..32650969 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -438,31 +438,33 @@ export class OceanPool extends Pool { const results: PoolTransaction[] = [] const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const dtAddress = await this.getDTAddress(poolAddress) - const myFilter: Filter = account ? { caller: account } : {} + const filter: Filter = account ? { caller: account } : {} let events: EventData[] events = await pool.getPastEvents('LOG_SWAP', { - filter: myFilter, + filter, fromBlock: 0, toBlock: 'latest' }) + for (let i = 0; i < events.length; i++) { if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) } events = await pool.getPastEvents('LOG_JOIN', { - filter: myFilter, + filter, fromBlock: 0, toBlock: 'latest' }) + console.log(events) 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: myFilter, + filter, fromBlock: 0, toBlock: 'latest' }) @@ -487,6 +489,7 @@ export class OceanPool extends Pool { fromBlock: 0, toBlock: 'latest' }) + console.log(events) for (let i = 0; i < events.length; i++) { const logs = await this.getPoolLogs(events[i].returnValues[0], account) for (let j = 0; j < logs.length; j++) results.push(logs[j]) From e7c140ef1406af8f454074cead7c08fccd0e52c0 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Wed, 30 Sep 2020 13:52:56 +0200 Subject: [PATCH 19/23] remove toLowerCase() workaround --- src/balancer/OceanPool.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 32650969..6c374d91 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -448,7 +448,7 @@ export class OceanPool extends Pool { }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) + if (!account || events[i].returnValues[0] === account) results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) } @@ -457,9 +457,9 @@ export class OceanPool extends Pool { fromBlock: 0, toBlock: 'latest' }) - console.log(events) + for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) + if (!account || events[i].returnValues[0] === account) results.push(await this.getEventData('join', poolAddress, dtAddress, events[i])) } @@ -469,7 +469,7 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) + if (!account || events[i].returnValues[0] === account) results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) } @@ -489,7 +489,7 @@ export class OceanPool extends Pool { fromBlock: 0, toBlock: 'latest' }) - console.log(events) + for (let i = 0; i < events.length; i++) { const logs = await this.getPoolLogs(events[i].returnValues[0], account) for (let j = 0; j < logs.length; j++) results.push(logs[j]) From 8eaaaa3bac0204972f3ee15d2b7141888fc88d5c Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 05:06:48 -0700 Subject: [PATCH 20/23] values in number, not Wei --- src/balancer/OceanPool.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 6c374d91..5deaea9b 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -528,14 +528,14 @@ export class OceanPool extends Pool { result = { ...result, tokenIn: data.returnValues[1], - tokenAmountIn: data.returnValues[2] + tokenAmountIn: this.web3.utils.fromWei(data.returnValues[2]) } break case 'exit': result = { ...result, tokenOut: data.returnValues[1], - tokenAmountOut: data.returnValues[2] + tokenAmountOut: this.web3.utils.fromWei(data.returnValues[2]) } break } From 815668720b81adf496d70743fc411e67fd5f5423 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 05:08:08 -0700 Subject: [PATCH 21/23] values in number, not Wei --- src/balancer/OceanPool.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 5deaea9b..f75b8293 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -520,8 +520,8 @@ export class OceanPool extends Pool { ...result, tokenIn: data.returnValues[1], tokenOut: data.returnValues[2], - tokenAmountIn: data.returnValues[3], - tokenAmountOut: data.returnValues[4] + tokenAmountIn: this.web3.utils.fromWei(data.returnValues[3]), + tokenAmountOut: this.web3.utils.fromWei(data.returnValues[4]) } break case 'join': From dfceb09d5e172b621c7ec0b6346648950414a1cb Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 05:18:23 -0700 Subject: [PATCH 22/23] optimizations in FRE --- src/exchange/FixedRateExchange.ts | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index f7db94af..ceab6efc 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -377,10 +377,8 @@ export class OceanFixedRateExchange { 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])) + if (!account || events[i].returnValues[3] === account) + result.push(await this.getExchange(events[i].returnValues[0])) } return result } @@ -402,10 +400,8 @@ export class OceanFixedRateExchange { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (account) { - if (events[i].returnValues[1] === account) - result.push(this.getEventData(events[i])) - } else result.push(this.getEventData(events[i])) + if (!account || events[i].returnValues[1] === account) + result.push(this.getEventData(events[i])) } return result } From a82a8e6a8627bf57221c6c923480da5185a9715c Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Wed, 30 Sep 2020 05:22:33 -0700 Subject: [PATCH 23/23] moved all address checks to lowercase --- src/balancer/OceanPool.ts | 6 +++--- src/exchange/FixedRateExchange.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index f75b8293..0a7ac1f6 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -448,7 +448,7 @@ export class OceanPool extends Pool { }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0] === account) + if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('swap', poolAddress, dtAddress, events[i])) } @@ -459,7 +459,7 @@ export class OceanPool extends Pool { }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0] === account) + if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('join', poolAddress, dtAddress, events[i])) } @@ -469,7 +469,7 @@ export class OceanPool extends Pool { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[0] === account) + if (!account || events[i].returnValues[0].toLowerCase() === account.toLowerCase()) results.push(await this.getEventData('exit', poolAddress, dtAddress, events[i])) } diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index ceab6efc..0bb5b6eb 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -377,7 +377,7 @@ export class OceanFixedRateExchange { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[3] === account) + if (!account || events[i].returnValues[3].toLowerCase() === account.toLowerCase()) result.push(await this.getExchange(events[i].returnValues[0])) } return result @@ -400,7 +400,7 @@ export class OceanFixedRateExchange { toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { - if (!account || events[i].returnValues[1] === account) + if (!account || events[i].returnValues[1].toLowerCase() === account.toLowerCase()) result.push(this.getEventData(events[i])) } return result