diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 48921aa7..adef8b87 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -10,12 +10,17 @@ declare type PoolTransactionType = 'swap' | 'join' | 'exit' const POOL_MAX_AMOUNT_IN_LIMIT = 0.25 // maximum 1/4 of the pool reserve const POOL_MAX_AMOUNT_OUT_LIMIT = 0.25 // maximum 1/4 of the pool reserve - +const BPFACTORY_DEPLOY_BLOCK = 0 export interface PoolDetails { poolAddress: string tokens: string[] } +export interface AllPoolsShares { + poolAddress: string + shares: string +} + export interface TokensReceived { dtAmount: string oceanAmount: string @@ -849,7 +854,7 @@ export class OceanPool extends Pool { const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) const events = await factory.getPastEvents('BPoolRegistered', { filter: {}, - fromBlock: 0, + fromBlock: BPFACTORY_DEPLOY_BLOCK, toBlock: 'latest' }) events.sort((a, b) => (a.blockNumber > b.blockNumber ? 1 : -1)) @@ -886,7 +891,7 @@ export class OceanPool extends Pool { const events = await factory.getPastEvents('BPoolRegistered', { filter: account ? { registeredBy: account } : {}, - fromBlock: 0, + fromBlock: BPFACTORY_DEPLOY_BLOCK, toBlock: 'latest' }) @@ -897,6 +902,32 @@ export class OceanPool extends Pool { return result } + /** + * Search all pools in which a user has shares + * @param {String} account + * @return {AllPoolsShares[]} + */ + public async getPoolsSharesbyAddress(account: string): Promise { + const result: AllPoolsShares[] = [] + const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) + + const events = await factory.getPastEvents('BPoolRegistered', { + filter: account ? { registeredBy: account } : {}, + fromBlock: BPFACTORY_DEPLOY_BLOCK, + toBlock: 'latest' + }) + + for (let i = 0; i < events.length; i++) { + const shares = await super.sharesBalance(account, events[i].returnValues[0]) + if (shares) { + const onePool: AllPoolsShares = { shares, poolAddress: events[i].returnValues[0] } + result.push(onePool) + } + } + console.log(result) + return result + } + /** * Get pool details * @param {String} poolAddress Pool address @@ -926,7 +957,7 @@ export class OceanPool extends Pool { events = await pool.getPastEvents('LOG_SWAP', { filter, - fromBlock: 0, + fromBlock: BPFACTORY_DEPLOY_BLOCK, toBlock: 'latest' }) @@ -937,7 +968,7 @@ export class OceanPool extends Pool { events = await pool.getPastEvents('LOG_JOIN', { filter, - fromBlock: 0, + fromBlock: BPFACTORY_DEPLOY_BLOCK, toBlock: 'latest' }) @@ -948,7 +979,7 @@ export class OceanPool extends Pool { events = await pool.getPastEvents('LOG_EXIT', { filter, - fromBlock: 0, + fromBlock: BPFACTORY_DEPLOY_BLOCK, toBlock: 'latest' }) for (let i = 0; i < events.length; i++) { @@ -969,7 +1000,7 @@ export class OceanPool extends Pool { const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) const events = await factory.getPastEvents('BPoolRegistered', { filter: {}, - fromBlock: 0, + fromBlock: BPFACTORY_DEPLOY_BLOCK, toBlock: 'latest' }) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 261fb815..b57e6983 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -389,7 +389,6 @@ describe('Balancer flow', () => { if (consoleDebug) console.log('poolShares:' + poolShares) assert(parseFloat(poolShares) > 0) }) - it('Bob should remove Ocean liquidity from pool ', async () => { const currentDtReserve = await Pool.getOceanReserve(greatPool) const bobDtBalance = await datatoken.balance(oceanTokenAddress, bob) @@ -418,6 +417,11 @@ describe('Balancer flow', () => { assert(parseFloat(amounts.dtAmount) > 0) assert(parseFloat(amounts.oceanAmount) > 0) }) + it('ALice should get all her shares for all the pools', async () => { + const aliceShares = await Pool.getPoolsSharesbyAddress(alice) + assert(aliceShares.length > 0) + }) + it('ALice should remove all liquidity', async () => { const aliceShares = await Pool.sharesBalance(alice, greatPool) const aliceDtBalance = await datatoken.balance(tokenAddress, alice)