From 00257c3d60061f5e96f70f4e44a224b58e7055d3 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 20 Aug 2020 13:33:59 +0200 Subject: [PATCH 1/4] add ocean.pool.totalSupply() --- src/balancer/Pool.ts | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index f2103dd9..cf14585e 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -89,7 +89,7 @@ export class Pool extends PoolFactory { } /** - * Get Pool shares + * Get user shares of pool tokens * @param {String} account * @param {String} poolAddress * @return {String} @@ -132,6 +132,42 @@ export class Pool extends PoolFactory { return result } + /** + * Get total supply of pool tokens + * @param {String} poolAddress + * @return {String} + */ + async totalSupply(poolAddress: string): Promise { + const minABI = [ + { + constant: true, + name: 'totalSupply', + outputs: [ + { + name: 'balance', + type: 'uint256' + } + ], + payable: false, + stateMutability: 'view', + type: 'function' + } + ] as AbiItem[] + + const token = new this.web3.eth.Contract(minABI, poolAddress) + // const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) + + let result = null + + try { + const totalSupply = await token.methods.totalSupply().call() + result = this.web3.utils.fromWei(totalSupply) + } catch (e) { + console.error(e) + } + return result + } + /** * Adds tokens to pool * @param {String} account From a3e43c157843907d377b7134a8b9067d3e28f581 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 20 Aug 2020 13:42:19 +0200 Subject: [PATCH 2/4] add totalSupply unit test --- test/unit/balancer/Balancer.test.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index fd399495..c8cf83d0 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -133,6 +133,10 @@ describe('Balancer flow', () => { const currentOceanReserve = await Pool.getOceanReserve(alice, alicePoolAddress) assert(currentOceanReserve > 0) }) + it('Get total supply of pool tokens', async () => { + const totalSupply = await Pool.totalSupply(alicePoolAddress) + assert(totalSupply > 0) + }) it('Get amount of Ocean needed to buy 1 dtToken', async () => { const requiredOcean = await Pool.getOceanNeeded(alice, alicePoolAddress, '1') assert(requiredOcean > 0) From af7964ca84394574fb78fece1ca75a6799746fc8 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 20 Aug 2020 17:47:52 +0200 Subject: [PATCH 3/4] refactor --- src/balancer/Pool.ts | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index cf14585e..8196d10a 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -116,16 +116,17 @@ export class Pool extends PoolFactory { type: 'function' } ] as AbiItem[] - const token = new this.web3.eth.Contract(minABI, poolAddress, { - from: account - }) + let result = null + try { - result = this.web3.utils.fromWei( - await token.methods - .balanceOf(account) - .call({ from: account, gas: this.GASLIMIT_DEFAULT }) - ) + const token = new this.web3.eth.Contract(minABI, poolAddress, { + from: account + }) + const balance = await token.methods + .balanceOf(account) + .call({ from: account, gas: this.GASLIMIT_DEFAULT }) + result = this.web3.utils.fromWei(balance) } catch (e) { console.error(e) } @@ -154,12 +155,10 @@ export class Pool extends PoolFactory { } ] as AbiItem[] - const token = new this.web3.eth.Contract(minABI, poolAddress) - // const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) - let result = null try { + const token = new this.web3.eth.Contract(minABI, poolAddress) const totalSupply = await token.methods.totalSupply().call() result = this.web3.utils.fromWei(totalSupply) } catch (e) { From dee24e87a3081373e055422702cff7633ad95131 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Thu, 20 Aug 2020 17:54:16 +0200 Subject: [PATCH 4/4] simplification, make it work --- src/balancer/Pool.ts | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 8196d10a..2c76f194 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -139,27 +139,11 @@ export class Pool extends PoolFactory { * @return {String} */ async totalSupply(poolAddress: string): Promise { - const minABI = [ - { - constant: true, - name: 'totalSupply', - outputs: [ - { - name: 'balance', - type: 'uint256' - } - ], - payable: false, - stateMutability: 'view', - type: 'function' - } - ] as AbiItem[] - let result = null try { - const token = new this.web3.eth.Contract(minABI, poolAddress) - const totalSupply = await token.methods.totalSupply().call() + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) + const totalSupply = await pool.methods.totalSupply().call() result = this.web3.utils.fromWei(totalSupply) } catch (e) { console.error(e)