From 087b021c17ba00e5a3b8d914642a6ac0527e14a2 Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 29 Oct 2020 04:35:30 -0700 Subject: [PATCH 1/2] add getPoolsSharesbyAddress --- src/balancer/OceanPool.ts | 45 ++++++++++++++++++++++++----- test/unit/balancer/Balancer.test.ts | 6 +++- 2 files changed, 43 insertions(+), 8 deletions(-) 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) From 6ba0ae6808641e9e8cc048deae3a4fe3a721afac Mon Sep 17 00:00:00 2001 From: alexcos20 Date: Thu, 29 Oct 2020 04:50:47 -0700 Subject: [PATCH 2/2] fix namings and add did --- src/balancer/OceanPool.ts | 15 ++++++++++----- test/unit/balancer/Balancer.test.ts | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index adef8b87..80a7bfb4 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -4,7 +4,7 @@ import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' import { EventData, Filter } from 'web3-eth-contract' import BigNumber from 'bignumber.js' -import { SubscribablePromise, Logger } from '../utils' +import { SubscribablePromise, Logger, didNoZeroX, didPrefixed } from '../utils' declare type PoolTransactionType = 'swap' | 'join' | 'exit' @@ -16,9 +16,10 @@ export interface PoolDetails { tokens: string[] } -export interface AllPoolsShares { +export interface PoolShare { poolAddress: string shares: string + did: string } export interface TokensReceived { @@ -907,8 +908,8 @@ export class OceanPool extends Pool { * @param {String} account * @return {AllPoolsShares[]} */ - public async getPoolsSharesbyAddress(account: string): Promise { - const result: AllPoolsShares[] = [] + public async getPoolSharesByAddress(account: string): Promise { + const result: PoolShare[] = [] const factory = new this.web3.eth.Contract(this.factoryABI, this.factoryAddress) const events = await factory.getPastEvents('BPoolRegistered', { @@ -920,7 +921,11 @@ export class OceanPool extends Pool { 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] } + const onePool: PoolShare = { + shares, + poolAddress: events[i].returnValues[0], + did: didPrefixed(didNoZeroX(await this.getDTAddress(events[i].returnValues[0]))) + } result.push(onePool) } } diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index b57e6983..9a866de4 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -418,7 +418,7 @@ describe('Balancer flow', () => { assert(parseFloat(amounts.oceanAmount) > 0) }) it('ALice should get all her shares for all the pools', async () => { - const aliceShares = await Pool.getPoolsSharesbyAddress(alice) + const aliceShares = await Pool.getPoolSharesByAddress(alice) assert(aliceShares.length > 0) })