From d47a9f02a65342a682b904b28100ac376c61d290 Mon Sep 17 00:00:00 2001 From: ssallam Date: Thu, 27 Aug 2020 09:39:02 +0200 Subject: [PATCH 1/5] use solidity setup pool function when creating new pool. --- src/balancer/OceanPool.ts | 44 +++++++++++++++++++++---------------- src/balancer/Pool.ts | 46 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 21 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index a9cb2493..4d63cebd 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -26,9 +26,10 @@ export class OceanPool extends Pool { /** * Create DataToken pool @param {String} account - * @param {String} token Data Token address - * @param {String} amount Data Token amount - * @param {String} weight Data Token weight + * @param {String} token DataToken address + * @param {String} amount DataToken amount + * @param {String} weight DataToken weight + * @param {String} fee Swap fee in Wei * @return {String} */ public async createDTPool( @@ -37,7 +38,6 @@ export class OceanPool extends Pool { amount: string, weight: string, fee: string, - finalize = true ): Promise { if (this.oceanAddress == null) { console.error('oceanAddress is not defined') @@ -50,22 +50,28 @@ export class OceanPool extends Pool { const address = await super.createPool(account) const oceanWeight = 10 - parseFloat(weight) const oceanAmount = (parseFloat(amount) * oceanWeight) / parseFloat(weight) - const tokens = [ - { - address: token, - amount: String(amount), - weight: String(weight) - }, - { - address: this.oceanAddress, - amount: String(oceanAmount), - weight: String(oceanWeight) - } - ] this.dtAddress = token - await super.addToPool(account, address, tokens) - await super.setSwapFee(account, address, fee) - if (finalize) await super.finalize(account, address) + + await this.approve( + account, + token, + address, + this.web3.utils.toWei(String(amount)) as any + ) + await this.approve( + account, + this.oceanAddress, + address, + this.web3.utils.toWei(String(oceanAmount)) as any + ) + + super.setup( + account, address, + token, String(amount), String(weight), + this.oceanAddress, String(oceanAmount), String(oceanWeight), + this.web3.utils.toWei(fee) + ) + return address } diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 2c76f194..a871a6a3 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -32,8 +32,50 @@ export class Pool extends PoolFactory { * Creates a new pool */ async createPool(account: string): Promise { - const pooladdress = await super.createPool(account) - return pooladdress + return await super.createPool(account) + } + + /** + * Setup a new pool by setting datatoken, base token, swap fee and + * finalizing the pool to make it public. + * + * @param account + * @param poolAddress + * @param dataToken + * @param dataTokenAmount + * @param dataTokenWeight + * @param baseToken + * @param baseTokenAmount + * @param baseTokenWeight + * @param swapFee + */ + async setup( + account: string, + poolAddress: string, + dataToken: string, + dataTokenAmount: string, + dataTokenWeight: string, + baseToken: string, + baseTokenAmount: string, + baseTokenWeight: string, + swapFee: string, + ): Promise { + const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { + from: account + }) + let result = null + try { + result = await pool.methods + .setup( + dataToken, dataTokenAmount, dataTokenWeight, + baseToken, baseTokenAmount, baseTokenWeight, + swapFee + ).send({ from: account, gas: this.GASLIMIT_DEFAULT }) + } catch (e) { + console.error(e) + } + return result + } /** From 7078113110526c9bbe2ed36d93eb500ce9e92480 Mon Sep 17 00:00:00 2001 From: ssallam Date: Thu, 27 Aug 2020 09:46:54 +0200 Subject: [PATCH 2/5] update method docs --- src/balancer/OceanPool.ts | 2 +- src/balancer/Pool.ts | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 4d63cebd..4b0b4c76 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -29,7 +29,7 @@ export class OceanPool extends Pool { * @param {String} token DataToken address * @param {String} amount DataToken amount * @param {String} weight DataToken weight - * @param {String} fee Swap fee in Wei + * @param {String} fee Swap fee (as float) * @return {String} */ public async createDTPool( diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index a871a6a3..d4a87687 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -39,15 +39,15 @@ export class Pool extends PoolFactory { * Setup a new pool by setting datatoken, base token, swap fee and * finalizing the pool to make it public. * - * @param account - * @param poolAddress - * @param dataToken - * @param dataTokenAmount - * @param dataTokenWeight - * @param baseToken - * @param baseTokenAmount - * @param baseTokenWeight - * @param swapFee + * @param {String} account ethereum address to use for sending this transaction + * @param {String} poolAddress address of new Balancer Pool + * @param {String} dataToken address of datatoken ERC20 contract + * @param {String} dataTokenAmount in wei + * @param {String} dataTokenWeight in wei + * @param {String} baseToken address of base token ERC20 contract + * @param {String} baseTokenAmount in wei + * @param {String} baseTokenWeight in wei + * @param {String} swapFee in wei */ async setup( account: string, From 8c7a701e6f3779bdf3a5db05507571f387c8d3dd Mon Sep 17 00:00:00 2001 From: ssallam Date: Thu, 27 Aug 2020 10:21:20 +0200 Subject: [PATCH 3/5] update ocean-contracts version to 0.3.5 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 93b9cb70..d4952dc6 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ }, "dependencies": { "@ethereum-navigator/navigator": "^0.5.0", - "@oceanprotocol/contracts": "^0.3.4", + "@oceanprotocol/contracts": "^0.3.5", "decimal.js": "^10.2.0", "fs": "0.0.1-security", "node-fetch": "^2.6.0", From afa2996768d9b69b2811ada0aead40f08bfc1b64 Mon Sep 17 00:00:00 2001 From: ssallam Date: Thu, 27 Aug 2020 11:01:15 +0200 Subject: [PATCH 4/5] Convert amount and weight to wei, fix tests. --- src/balancer/OceanPool.ts | 6 +++--- src/balancer/Pool.ts | 2 +- test/unit/balancer/Balancer.test.ts | 4 +++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 4b0b4c76..6bca604a 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -65,10 +65,10 @@ export class OceanPool extends Pool { this.web3.utils.toWei(String(oceanAmount)) as any ) - super.setup( + await super.setup( account, address, - token, String(amount), String(weight), - this.oceanAddress, String(oceanAmount), String(oceanWeight), + token, this.web3.utils.toWei(String(amount)), this.web3.utils.toWei(String(weight)), + this.oceanAddress, this.web3.utils.toWei(String(oceanAmount)), this.web3.utils.toWei(String(oceanWeight)), this.web3.utils.toWei(fee) ) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index d4a87687..5c849abd 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -72,7 +72,7 @@ export class Pool extends PoolFactory { swapFee ).send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(e) + console.error('Pool.setup failed:' + e) } return result diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index c8cf83d0..b719dffd 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -109,7 +109,9 @@ describe('Balancer flow', () => { }) it('Alice creates a new OceanPool pool', async () => { /// new pool with total DT = 45 , dt weight=90% with swap fee 2% - alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, 45, 9, '0.02') + alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, '45', '9', '0.02') + assert(await Pool.totalSupply(alicePoolAddress) == 100) + assert(await Pool.getNumTokens(alice, alicePoolAddress) == 2) }) it('Get pool information', async () => { const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress) From 5af50c4c9297c2b98124ae609ff61a0c32626958 Mon Sep 17 00:00:00 2001 From: ssallam Date: Thu, 27 Aug 2020 11:35:30 +0200 Subject: [PATCH 5/5] Fix lint issues. --- src/balancer/OceanPool.ts | 13 ++++++--- src/balancer/Pool.ts | 42 ++++++++++++++++------------- test/unit/balancer/Balancer.test.ts | 8 +++--- 3 files changed, 37 insertions(+), 26 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 6bca604a..ff8efe36 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -37,7 +37,7 @@ export class OceanPool extends Pool { token: string, amount: string, weight: string, - fee: string, + fee: string ): Promise { if (this.oceanAddress == null) { console.error('oceanAddress is not defined') @@ -66,9 +66,14 @@ export class OceanPool extends Pool { ) await super.setup( - account, address, - token, this.web3.utils.toWei(String(amount)), this.web3.utils.toWei(String(weight)), - this.oceanAddress, this.web3.utils.toWei(String(oceanAmount)), this.web3.utils.toWei(String(oceanWeight)), + account, + address, + token, + this.web3.utils.toWei(String(amount)), + this.web3.utils.toWei(String(weight)), + this.oceanAddress, + this.web3.utils.toWei(String(oceanAmount)), + this.web3.utils.toWei(String(oceanWeight)), this.web3.utils.toWei(fee) ) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 5c849abd..739ffa51 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -35,20 +35,20 @@ export class Pool extends PoolFactory { return await super.createPool(account) } - /** - * Setup a new pool by setting datatoken, base token, swap fee and - * finalizing the pool to make it public. - * - * @param {String} account ethereum address to use for sending this transaction - * @param {String} poolAddress address of new Balancer Pool - * @param {String} dataToken address of datatoken ERC20 contract - * @param {String} dataTokenAmount in wei - * @param {String} dataTokenWeight in wei - * @param {String} baseToken address of base token ERC20 contract - * @param {String} baseTokenAmount in wei - * @param {String} baseTokenWeight in wei - * @param {String} swapFee in wei - */ + /** + * Setup a new pool by setting datatoken, base token, swap fee and + * finalizing the pool to make it public. + * + * @param {String} account ethereum address to use for sending this transaction + * @param {String} poolAddress address of new Balancer Pool + * @param {String} dataToken address of datatoken ERC20 contract + * @param {String} dataTokenAmount in wei + * @param {String} dataTokenWeight in wei + * @param {String} baseToken address of base token ERC20 contract + * @param {String} baseTokenAmount in wei + * @param {String} baseTokenWeight in wei + * @param {String} swapFee in wei + */ async setup( account: string, poolAddress: string, @@ -58,7 +58,7 @@ export class Pool extends PoolFactory { baseToken: string, baseTokenAmount: string, baseTokenWeight: string, - swapFee: string, + swapFee: string ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress, { from: account @@ -67,15 +67,19 @@ export class Pool extends PoolFactory { try { result = await pool.methods .setup( - dataToken, dataTokenAmount, dataTokenWeight, - baseToken, baseTokenAmount, baseTokenWeight, + dataToken, + dataTokenAmount, + dataTokenWeight, + baseToken, + baseTokenAmount, + baseTokenWeight, swapFee - ).send({ from: account, gas: this.GASLIMIT_DEFAULT }) + ) + .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { console.error('Pool.setup failed:' + e) } return result - } /** diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index b719dffd..d1cdefa2 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -105,13 +105,15 @@ describe('Balancer flow', () => { await oceandatatoken.mint(oceanTokenAddress, alice, tokenAmount) }) it('Alice transfers 200 ocean token to Bob', async () => { - const ts = await datatoken.transfer(oceanTokenAddress, bob, transferAmount, alice) + await datatoken.transfer(oceanTokenAddress, bob, transferAmount, alice) }) it('Alice creates a new OceanPool pool', async () => { /// new pool with total DT = 45 , dt weight=90% with swap fee 2% alicePoolAddress = await Pool.createDTPool(alice, tokenAddress, '45', '9', '0.02') - assert(await Pool.totalSupply(alicePoolAddress) == 100) - assert(await Pool.getNumTokens(alice, alicePoolAddress) == 2) + const s = await Pool.totalSupply(alicePoolAddress) + assert(String(s) === '100', 'totalSupply does not match: ' + s) + const n = await Pool.getNumTokens(alice, alicePoolAddress) + assert(String(n) === '2', 'unexpected num tokens: ' + n) }) it('Get pool information', async () => { const currentTokens = await Pool.getCurrentTokens(alice, alicePoolAddress)