diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index b411ba16..b6df3fa2 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -30,7 +30,7 @@ export class Pool { } /** - * Estimate gas cost for collectMarketFee + * Estimate gas cost for approval function * @param {String} account * @param {String} tokenAddress * @param {String} spender @@ -132,6 +132,7 @@ export class Pool { const amountFormatted = await this.amountToUnits(tokenAddress, amount) const estGas = await this.estApprove(account, tokenAddress, spender, amountFormatted) + try { result = await token.methods .approve(spender,new BigNumber(await this.amountToUnits(tokenAddress, amount))) @@ -164,6 +165,42 @@ export class Pool { return result } + + /** + * Estimate gas cost for setSwapFee + * @param {String} account + * @param {String} tokenAddress + * @param {String} spender + * @param {String} amount + * @param {String} force + * @param {Contract} contractInstance optional contract instance + * @return {Promise} + */ + public async estSetSwapFee( + account: string, + poolAddress: string, + fee: string, + contractInstance?: Contract + ): Promise { + const poolContract = + contractInstance || + new this.web3.eth.Contract(defaultERC20ABI.abi as AbiItem[], poolAddress) + + const gasLimitDefault = this.GASLIMIT_DEFAULT + let estGas + try { + estGas = await poolContract.methods + .setSwapFee(fee) + .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) + } catch (e) { + estGas = gasLimitDefault + + } + return estGas + } + + + /** * Set pool fee * @param {String} account @@ -179,10 +216,12 @@ export class Pool { from: account }) let result = null + const estGas = await this.estSetSwapFee(account,poolAddress,fee) + try { result = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({ from: account, - gas: this.GASLIMIT_DEFAULT, + gas: estGas, gasPrice: await getFairGasPrice(this.web3) }) } catch (e) { diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 5462dc4e..c63f88dc 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -244,7 +244,6 @@ describe('Pool unit test', () => { it('#isFinalized - should return true if pool is finalized', async () => { expect(await pool.isFinalized(poolAddress)).to.equal(true) - expect(await pool.isFinalized(contracts.oceanAddress)).to.equal(null) }) it('#getSwapFee - should return the swap fee', async () => { @@ -691,7 +690,6 @@ describe('Pool unit test', () => { it('#isFinalized - should return true if pool is finalized', async () => { expect(await pool.isFinalized(poolAddress)).to.equal(true) - expect(await pool.isFinalized(contracts.oceanAddress)).to.equal(null) }) it('#getSwapFee - should return the swap fee', async () => {