1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Merge pull request #420 from oceanprotocol/feature/gas_optimisation

gas cost optimisations
This commit is contained in:
Alex Coseru 2020-10-27 21:20:35 +02:00 committed by GitHub
commit 20a99d98ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -130,10 +130,26 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
let estGas
try {
estGas = await token.methods
.approve(spender, amount)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await token.methods result = await token.methods
.approve(spender, amount) .approve(spender, amount)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${e.message}`) this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${e.message}`)
} }
@ -495,6 +511,26 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
let estGas
try {
estGas = await pool.methods
.swapExactAmountIn(
tokenIn,
this.web3.utils.toWei(tokenAmountIn),
tokenOut,
this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountIn( .swapExactAmountIn(
@ -504,7 +540,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(minAmountOut), this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`) this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`)
} }
@ -535,6 +571,26 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
let estGas
try {
estGas = await pool.methods
.swapExactAmountOut(
tokenIn,
this.web3.utils.toWei(maxAmountIn),
tokenOut,
this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountOut( .swapExactAmountOut(
@ -544,7 +600,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(minAmountOut), this.web3.utils.toWei(minAmountOut),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to swap exact amount out: ${e.message}`) this.logger.error(`ERROR: Failed to swap exact amount out: ${e.message}`)
} }
@ -642,6 +698,24 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
let estGas
try {
estGas = await await pool.methods
.joinswapExternAmountIn(
tokenIn,
this.web3.utils.toWei(tokenAmountIn),
this.web3.utils.toWei(minPoolAmountOut)
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.joinswapExternAmountIn( .joinswapExternAmountIn(
@ -649,7 +723,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(tokenAmountIn), this.web3.utils.toWei(tokenAmountIn),
this.web3.utils.toWei(minPoolAmountOut) this.web3.utils.toWei(minPoolAmountOut)
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error(`ERROR: Failed to pay tokens in order to \ this.logger.error(`ERROR: Failed to pay tokens in order to \
join the pool: ${e.message}`) join the pool: ${e.message}`)
@ -745,6 +819,24 @@ export class Pool extends PoolFactory {
from: account from: account
}) })
let result = null let result = null
let estGas
try {
estGas = await pool.methods
.exitswapExternAmountOut(
tokenOut,
this.web3.utils.toWei(tokenAmountOut),
this.web3.utils.toWei(maxPoolAmountIn)
)
.estimateGas(function (err, estGas) {
if (err) {
// console.error('ERROR: OnChainMetadataCacheEstimateGas: ' + err)
return this.GASLIMIT_DEFAULT
}
return estGas
})
} catch (e) {
estGas = this.GASLIMIT_DEFAULT
}
try { try {
result = await pool.methods result = await pool.methods
.exitswapExternAmountOut( .exitswapExternAmountOut(
@ -752,7 +844,7 @@ export class Pool extends PoolFactory {
this.web3.utils.toWei(tokenAmountOut), this.web3.utils.toWei(tokenAmountOut),
this.web3.utils.toWei(maxPoolAmountIn) this.web3.utils.toWei(maxPoolAmountIn)
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: estGas + 1 })
} catch (e) { } catch (e) {
this.logger.error('ERROR: Failed to exitswapExternAmountOut') this.logger.error('ERROR: Failed to exitswapExternAmountOut')
} }