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

fix estGas functions in Pool and SideStaking

This commit is contained in:
lacoop6tu 2021-11-02 17:49:35 -05:00
parent 52794336cb
commit eb73bf553e
3 changed files with 37 additions and 144 deletions

View File

@ -25,6 +25,7 @@
"changelog": "auto-changelog -p", "changelog": "auto-changelog -p",
"prepublishOnly": "npm run build", "prepublishOnly": "npm run build",
"test:ss": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/ssContracts/SideStaking.test.ts'", "test:ss": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/ssContracts/SideStaking.test.ts'",
"test:pool": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/balancer/Pool.test.ts'",
"test:router": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/Router.test.ts'", "test:router": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/pools/Router.test.ts'",
"test:unit": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/**/*.test.ts'", "test:unit": "mocha --config=test/unit/.mocharc.json --node-env=test --exit 'test/unit/**/*.test.ts'",
"test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit", "test:unit:cover": "nyc --report-dir coverage/unit npm run test:unit",

View File

@ -4,6 +4,7 @@ import { TransactionReceipt } from 'web3-core'
import { Contract } from 'web3-eth-contract' import { Contract } from 'web3-eth-contract'
import { Logger, getFairGasPrice } from '../../utils' import { Logger, getFairGasPrice } from '../../utils'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
const BN = require('bn.js');
import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json'
import defaultPool from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json' import defaultPool from '@oceanprotocol/contracts/artifacts/contracts/pools/FactoryRouter.sol/FactoryRouter.json'
import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json' import defaultERC20ABI from '@oceanprotocol/contracts/artifacts/contracts/templates/ERC20Template.sol/ERC20Template.json'
@ -53,10 +54,11 @@ export class Pool {
let estGas let estGas
try { try {
estGas = await tokenContract.methods estGas = await tokenContract.methods
.approve(spender, amount) .approve(spender, new BigNumber(amount))
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
estGas = gasLimitDefault estGas = gasLimitDefault
console.log(e)
} }
return estGas return estGas
} }
@ -129,10 +131,10 @@ export class Pool {
let result = null let result = null
const amountFormatted = await this.amountToUnits(tokenAddress, amount) const amountFormatted = await this.amountToUnits(tokenAddress, amount)
const estGas = await this.estApprove(account, tokenAddress, spender, amountFormatted) const estGas = await this.estApprove(account, tokenAddress, spender, amountFormatted)
try { try {
result = await token.methods result = await token.methods
.approve(spender, new BigNumber(await this.amountToUnits(tokenAddress, amount))) .approve(spender,new BigNumber(await this.amountToUnits(tokenAddress, amount)))
.send({ .send({
from: account, from: account,
gas: estGas + 1, gas: estGas + 1,
@ -531,7 +533,7 @@ export class Pool {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let result = null let result = null
const estGas = await this.estCollectOPF(address, poolAddress) const estGas = await this.estCollectOPF(address, poolAddress)
try { try {
result = await pool.methods.collectOPF().send({ result = await pool.methods.collectOPF().send({
from: address, from: address,
@ -592,7 +594,7 @@ export class Pool {
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
let result = null let result = null
const estGas = await this.estCollectMarketFee(address, poolAddress, to) const estGas = await this.estCollectMarketFee(address, poolAddress, to)
try { try {
result = await pool.methods.collectMarketFee(to).send({ result = await pool.methods.collectMarketFee(to).send({
from: address, from: address,
@ -657,7 +659,7 @@ export class Pool {
poolAddress, poolAddress,
newCollector newCollector
) )
try { try {
result = await pool.methods.updateMarketFeeCollector(newCollector).send({ result = await pool.methods.updateMarketFeeCollector(newCollector).send({
from: address, from: address,
@ -705,7 +707,7 @@ export class Pool {
tokenAmountIn, tokenAmountIn,
tokenOut, tokenOut,
minAmountOut, minAmountOut,
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? maxPrice : MaxUint256
) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
@ -785,6 +787,7 @@ export class Pool {
minAmountOutFormatted.toString(), minAmountOutFormatted.toString(),
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
//console.log(minAmountOutFormatted, 'minamoutnoutformatted') //console.log(minAmountOutFormatted, 'minamoutnoutformatted')
try { try {
result = await pool.methods result = await pool.methods
@ -842,7 +845,7 @@ export class Pool {
maxAmountIn, maxAmountIn,
tokenOut, tokenOut,
amountOut, amountOut,
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? maxPrice : MaxUint256
) )
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
@ -885,7 +888,7 @@ export class Pool {
amountOutFormatted, amountOutFormatted,
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256 maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
) )
try { try {
result = await pool.methods result = await pool.methods
.swapExactAmountOut( .swapExactAmountOut(
@ -930,7 +933,7 @@ export class Pool {
let estGas let estGas
try { try {
estGas = await poolContract.methods estGas = await poolContract.methods
.joinPool(this.web3.utils.toWei(poolAmountOut), maxAmountsIn) .joinPool(poolAmountOut, maxAmountsIn)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
estGas = gasLimitDefault estGas = gasLimitDefault
@ -970,7 +973,7 @@ export class Pool {
this.web3.utils.toWei(poolAmountOut), this.web3.utils.toWei(poolAmountOut),
weiMaxAmountsIn weiMaxAmountsIn
) )
try { try {
result = await pool.methods result = await pool.methods
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn) .joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
@ -1009,7 +1012,7 @@ export class Pool {
let estGas let estGas
try { try {
estGas = await poolContract.methods estGas = await poolContract.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), minAmountsOut) .exitPool(poolAmountIn, minAmountsOut)
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) { } catch (e) {
estGas = gasLimitDefault estGas = gasLimitDefault
@ -1046,6 +1049,7 @@ export class Pool {
this.web3.utils.toWei(poolAmountIn), this.web3.utils.toWei(poolAmountIn),
weiMinAmountsOut weiMinAmountsOut
) )
try { try {
result = await pool.methods result = await pool.methods
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
@ -1117,7 +1121,7 @@ export class Pool {
amountInFormatted, amountInFormatted,
this.web3.utils.toWei(minPoolAmountOut) this.web3.utils.toWei(minPoolAmountOut)
) )
try { try {
result = await pool.methods result = await pool.methods
.joinswapExternAmountIn( .joinswapExternAmountIn(
@ -1198,6 +1202,7 @@ export class Pool {
this.web3.utils.toWei(poolAmountOut), this.web3.utils.toWei(poolAmountOut),
maxAmountInFormatted maxAmountInFormatted
) )
try { try {
result = await pool.methods result = await pool.methods
.joinswapPoolAmountOut( .joinswapPoolAmountOut(
@ -1277,6 +1282,7 @@ export class Pool {
this.web3.utils.toWei(poolAmountIn), this.web3.utils.toWei(poolAmountIn),
minTokenOutFormatted minTokenOutFormatted
) )
try { try {
result = await pool.methods result = await pool.methods
.exitswapPoolAmountIn( .exitswapPoolAmountIn(
@ -1355,6 +1361,7 @@ export class Pool {
this.web3.utils.toWei(tokenAmountOut), this.web3.utils.toWei(tokenAmountOut),
this.web3.utils.toWei(maxPoolAmountIn) this.web3.utils.toWei(maxPoolAmountIn)
) )
try { try {
result = await pool.methods result = await pool.methods
.exitswapExternAmountOut( .exitswapExternAmountOut(

View File

@ -14,7 +14,7 @@ const MaxUint256 =
/** /**
* Provides an interface to Ocean friendly fork from Balancer BPool * Provides an interface to Ocean friendly fork from Balancer BPool
*/ */
// TODO: Add decimals handling
export class SideStaking { export class SideStaking {
public ssABI: AbiItem | AbiItem[] public ssABI: AbiItem | AbiItem[]
public web3: Web3 public web3: Web3
@ -63,124 +63,9 @@ export class SideStaking {
} }
/** /**
* Estimate gas cost for collectMarketFee * Get DTs in circulation (amount vested not accounted)
* @param {String} account
* @param {String} tokenAddress
* @param {String} spender
* @param {String} amount
* @param {String} force
* @param {Contract} contractInstance optional contract instance
* @return {Promise<number>}
*/
public async estApprove(
account: string,
tokenAddress: string,
spender: string,
amount: string,
contractInstance?: Contract
): Promise<number> {
const tokenContract =
contractInstance ||
new this.web3.eth.Contract(defaultERC20ABI.abi as AbiItem[], tokenAddress)
const gasLimitDefault = this.GASLIMIT_DEFAULT
let estGas
try {
estGas = await tokenContract.methods
.approve(spender, amount)
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
} catch (e) {
estGas = gasLimitDefault
}
return estGas
}
/**
* Get Alloance for both DataToken and Ocean
* @param {String } tokenAdress
* @param {String} owner
* @param {String} spender
*/
public async allowance(
tokenAddress: string,
owner: string,
spender: string
): Promise<string> {
const tokenAbi = defaultERC20ABI.abi as AbiItem[]
const datatoken = new this.web3.eth.Contract(tokenAbi, tokenAddress)
const trxReceipt = await datatoken.methods.allowance(owner, spender).call()
return await this.unitsToAmount(tokenAddress, trxReceipt)
}
/**
* Approve spender to spent amount tokens
* @param {String} account
* @param {String} tokenAddress
* @param {String} spender
* @param {String} amount (always expressed as wei)
* @param {String} force if true, will overwrite any previous allowence. Else, will check if allowence is enough and will not send a transaction if it's not needed
*/
async approve(
account: string,
tokenAddress: string,
spender: string,
amount: string,
force = false
): Promise<TransactionReceipt | string> {
const minABI = [
{
constant: false,
inputs: [
{
name: '_spender',
type: 'address'
},
{
name: '_value',
type: 'uint256'
}
],
name: 'approve',
outputs: [
{
name: '',
type: 'bool'
}
],
payable: false,
stateMutability: 'nonpayable',
type: 'function'
}
] as AbiItem[]
const token = new this.web3.eth.Contract(minABI, tokenAddress)
if (!force) {
const currentAllowence = await this.allowance(tokenAddress, account, spender)
if (new Decimal(currentAllowence).greaterThanOrEqualTo(amount)) {
return currentAllowence
}
}
let result = null
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)))
.send({
from: account,
gas: estGas + 1,
gasPrice: await getFairGasPrice(this.web3)
})
} catch (e) {
this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${e.message}`)
}
return result
}
/**
* Get
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatoken address
* @return {String} * @return {String}
*/ */
async getDataTokenCirculatingSupply( async getDataTokenCirculatingSupply(
@ -200,8 +85,9 @@ export class SideStaking {
} }
/** /**
* Get * Get Publisher address
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatoken address
* @return {String} * @return {String}
*/ */
async getPublisherAddress( async getPublisherAddress(
@ -236,7 +122,7 @@ export class SideStaking {
} }
/** /**
* Get * Get Pool Address
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String}
@ -253,7 +139,7 @@ export class SideStaking {
} }
/** /**
* Get * Get basetoken balance in the contract
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String}
@ -273,7 +159,7 @@ export class SideStaking {
} }
/** /**
* Get * Get dt balance in the staking contract available for being added as liquidity
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String}
@ -294,10 +180,10 @@ export class SideStaking {
} }
/** /**
* Get * Get block when vesting ends
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String} end block for vesting amount
*/ */
async getvestingEndBlock(ssAddress: string, datatokenAddress: string): Promise<string> { async getvestingEndBlock(ssAddress: string, datatokenAddress: string): Promise<string> {
const sideStaking = new this.web3.eth.Contract(this.ssABI, ssAddress) const sideStaking = new this.web3.eth.Contract(this.ssABI, ssAddress)
@ -311,10 +197,10 @@ export class SideStaking {
} }
/** /**
* Get * Get total amount vesting
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String}
*/ */
async getvestingAmount(ssAddress: string, datatokenAddress: string): Promise<string> { async getvestingAmount(ssAddress: string, datatokenAddress: string): Promise<string> {
const sideStaking = new this.web3.eth.Contract(this.ssABI, ssAddress) const sideStaking = new this.web3.eth.Contract(this.ssABI, ssAddress)
@ -329,7 +215,7 @@ export class SideStaking {
} }
/** /**
* Get * Get last block publisher got some vested tokens
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String}
@ -349,7 +235,7 @@ export class SideStaking {
} }
/** /**
* Get * Get how much has been taken from the vesting amount
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
* @return {String} * @return {String}
@ -370,7 +256,7 @@ export class SideStaking {
} }
/** /**
* Estimate gas cost for collectMarketFee * Estimate gas cost for getVesting
* @param {String} account * @param {String} account
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
* @param {String} datatokenAddress datatokenAddress * @param {String} datatokenAddress datatokenAddress
@ -398,7 +284,7 @@ export class SideStaking {
return estGas return estGas
} }
/** /**Send vested tokens available to the publisher address, can be called by anyone
* *
* @param {String} account * @param {String} account
* @param {String} ssAddress side staking contract address * @param {String} ssAddress side staking contract address
@ -419,7 +305,6 @@ export class SideStaking {
datatokenAddress, datatokenAddress,
sideStaking sideStaking
) )
try { try {
result = await sideStaking.methods.getVesting(datatokenAddress).send({ result = await sideStaking.methods.getVesting(datatokenAddress).send({
from: account, from: account,