mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
fix lint, add estApprove function
This commit is contained in:
parent
22746a10e0
commit
30151b7694
@ -28,6 +28,39 @@ export class Pool {
|
|||||||
this.logger = logger
|
this.logger = logger
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Estimate gas cost for collectMarketFee
|
||||||
|
* @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
|
* Get Alloance for both DataToken and Ocean
|
||||||
* @param {String } tokenAdress
|
* @param {String } tokenAdress
|
||||||
@ -42,7 +75,8 @@ export class Pool {
|
|||||||
const tokenAbi = defaultERC20ABI.abi as AbiItem[]
|
const tokenAbi = defaultERC20ABI.abi as AbiItem[]
|
||||||
const datatoken = new this.web3.eth.Contract(tokenAbi, tokenAddress)
|
const datatoken = new this.web3.eth.Contract(tokenAbi, tokenAddress)
|
||||||
const trxReceipt = await datatoken.methods.allowance(owner, spender).call()
|
const trxReceipt = await datatoken.methods.allowance(owner, spender).call()
|
||||||
return (await this.unitsToAmount(tokenAddress, trxReceipt)).toString()
|
|
||||||
|
return await this.unitsToAmount(tokenAddress, trxReceipt)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,31 +119,20 @@ export class Pool {
|
|||||||
type: 'function'
|
type: 'function'
|
||||||
}
|
}
|
||||||
] as AbiItem[]
|
] as AbiItem[]
|
||||||
const token = new this.web3.eth.Contract(minABI, tokenAddress, {
|
const token = new this.web3.eth.Contract(minABI, tokenAddress)
|
||||||
from: account
|
|
||||||
})
|
|
||||||
if (!force) {
|
if (!force) {
|
||||||
const currentAllowence = await this.allowance(tokenAddress, account, spender)
|
const currentAllowence = await this.allowance(tokenAddress, account, spender)
|
||||||
if (
|
if (new Decimal(currentAllowence).greaterThanOrEqualTo(amount)) {
|
||||||
new Decimal(this.web3.utils.toWei(currentAllowence)).greaterThanOrEqualTo(amount)
|
|
||||||
) {
|
|
||||||
return currentAllowence
|
return currentAllowence
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let result = null
|
let result = null
|
||||||
const gasLimitDefault = this.GASLIMIT_DEFAULT
|
const amountFormatted = await this.amountToUnits(tokenAddress, amount)
|
||||||
let estGas
|
const estGas = await this.estApprove(account, tokenAddress, spender, amountFormatted)
|
||||||
try {
|
|
||||||
estGas = await token.methods
|
|
||||||
.approve(spender, await this.amountToUnits(tokenAddress, amount))
|
|
||||||
.estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
|
||||||
} catch (e) {
|
|
||||||
estGas = gasLimitDefault
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
result = await token.methods
|
result = await token.methods
|
||||||
.approve(spender, 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,
|
||||||
@ -445,7 +468,7 @@ export class Pool {
|
|||||||
let weight = null
|
let weight = null
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods.marketFees(token).call()
|
const result = await pool.methods.marketFees(token).call()
|
||||||
weight = this.web3.utils.fromWei(result)
|
weight = await this.unitsToAmount(token, result)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to get market fees for a token: ${e.message}`)
|
this.logger.error(`ERROR: Failed to get market fees for a token: ${e.message}`)
|
||||||
}
|
}
|
||||||
@ -463,7 +486,7 @@ export class Pool {
|
|||||||
let weight = null
|
let weight = null
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods.communityFees(token).call()
|
const result = await pool.methods.communityFees(token).call()
|
||||||
weight = this.web3.utils.fromWei(result)
|
weight = await this.unitsToAmount(token, result)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to get community fees for a token: ${e.message}`)
|
this.logger.error(`ERROR: Failed to get community fees for a token: ${e.message}`)
|
||||||
}
|
}
|
||||||
@ -691,9 +714,8 @@ export class Pool {
|
|||||||
return estGas
|
return estGas
|
||||||
}
|
}
|
||||||
|
|
||||||
async amountToUnits(token: string, amount: string): Promise<number> {
|
async amountToUnits(token: string, amount: string): Promise<string> {
|
||||||
let decimals = 18
|
let decimals = 18
|
||||||
let amountFormatted
|
|
||||||
const tokenContract = new this.web3.eth.Contract(
|
const tokenContract = new this.web3.eth.Contract(
|
||||||
defaultERC20ABI.abi as AbiItem[],
|
defaultERC20ABI.abi as AbiItem[],
|
||||||
token
|
token
|
||||||
@ -704,14 +726,13 @@ export class Pool {
|
|||||||
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
||||||
}
|
}
|
||||||
|
|
||||||
amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
|
const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
|
||||||
|
|
||||||
return amountFormatted
|
return amountFormatted.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
async unitsToAmount(token: string, amount: string): Promise<number> {
|
async unitsToAmount(token: string, amount: string): Promise<string> {
|
||||||
let decimals = 18
|
let decimals = 18
|
||||||
let amountFormatted
|
|
||||||
const tokenContract = new this.web3.eth.Contract(
|
const tokenContract = new this.web3.eth.Contract(
|
||||||
defaultERC20ABI.abi as AbiItem[],
|
defaultERC20ABI.abi as AbiItem[],
|
||||||
token
|
token
|
||||||
@ -722,9 +743,9 @@ export class Pool {
|
|||||||
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
||||||
}
|
}
|
||||||
|
|
||||||
amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals)
|
const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals)
|
||||||
|
|
||||||
return amountFormatted
|
return amountFormatted.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -748,17 +769,10 @@ export class Pool {
|
|||||||
maxPrice?: string
|
maxPrice?: string
|
||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
||||||
const tokenInContract = new this.web3.eth.Contract(
|
|
||||||
defaultERC20ABI.abi as AbiItem[],
|
|
||||||
tokenIn
|
|
||||||
)
|
|
||||||
|
|
||||||
let amountInFormatted
|
const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn)
|
||||||
let minAmountOutFormatted
|
|
||||||
|
|
||||||
amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn)
|
const minAmountOutFormatted = await this.amountToUnits(tokenOut, minAmountOut)
|
||||||
|
|
||||||
minAmountOutFormatted = await this.amountToUnits(tokenOut, minAmountOut)
|
|
||||||
|
|
||||||
let result = null
|
let result = null
|
||||||
|
|
||||||
@ -768,7 +782,7 @@ export class Pool {
|
|||||||
tokenIn,
|
tokenIn,
|
||||||
amountInFormatted,
|
amountInFormatted,
|
||||||
tokenOut,
|
tokenOut,
|
||||||
minAmountOutFormatted,
|
minAmountOutFormatted.toString(),
|
||||||
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
|
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
|
||||||
)
|
)
|
||||||
console.log(minAmountOutFormatted, 'minamoutnoutformatted')
|
console.log(minAmountOutFormatted, 'minamoutnoutformatted')
|
||||||
@ -825,9 +839,9 @@ export class Pool {
|
|||||||
estGas = await poolContract.methods
|
estGas = await poolContract.methods
|
||||||
.swapExactAmountOut(
|
.swapExactAmountOut(
|
||||||
tokenIn,
|
tokenIn,
|
||||||
this.web3.utils.toWei(maxAmountIn),
|
maxAmountIn,
|
||||||
tokenOut,
|
tokenOut,
|
||||||
this.web3.utils.toWei(amountOut),
|
amountOut,
|
||||||
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
|
maxPrice ? this.web3.utils.toWei(maxPrice) : MaxUint256
|
||||||
)
|
)
|
||||||
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
.estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas))
|
||||||
@ -859,10 +873,9 @@ export class Pool {
|
|||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
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
|
||||||
let maxAmountInFormatted
|
|
||||||
let amountOutFormatted
|
const maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn)
|
||||||
maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn)
|
const amountOutFormatted = await this.amountToUnits(tokenOut, amountOut)
|
||||||
amountOutFormatted = await this.amountToUnits(tokenOut, amountOut)
|
|
||||||
const estGas = await this.estSwapExactAmountOut(
|
const estGas = await this.estSwapExactAmountOut(
|
||||||
account,
|
account,
|
||||||
poolAddress,
|
poolAddress,
|
||||||
@ -1095,8 +1108,8 @@ export class Pool {
|
|||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
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
|
||||||
let amountInFormatted
|
|
||||||
amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn)
|
const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn)
|
||||||
const estGas = await this.estJoinswapExternAmountIn(
|
const estGas = await this.estJoinswapExternAmountIn(
|
||||||
account,
|
account,
|
||||||
poolAddress,
|
poolAddress,
|
||||||
@ -1176,8 +1189,8 @@ export class Pool {
|
|||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
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
|
||||||
let maxAmountInFormatted
|
|
||||||
maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn)
|
const maxAmountInFormatted = await this.amountToUnits(tokenIn, maxAmountIn)
|
||||||
const estGas = await this.estJoinswapPoolAmountOut(
|
const estGas = await this.estJoinswapPoolAmountOut(
|
||||||
account,
|
account,
|
||||||
poolAddress,
|
poolAddress,
|
||||||
@ -1255,8 +1268,8 @@ export class Pool {
|
|||||||
): Promise<TransactionReceipt> {
|
): Promise<TransactionReceipt> {
|
||||||
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
|
||||||
let minTokenOutFormatted
|
|
||||||
minTokenOutFormatted = await this.amountToUnits(tokenOut, minTokenAmountOut)
|
const minTokenOutFormatted = await this.amountToUnits(tokenOut, minTokenAmountOut)
|
||||||
const estGas = await this.estExitswapPoolAmountIn(
|
const estGas = await this.estExitswapPoolAmountIn(
|
||||||
account,
|
account,
|
||||||
poolAddress,
|
poolAddress,
|
||||||
@ -1427,8 +1440,8 @@ export class Pool {
|
|||||||
tokenAmountOut: string
|
tokenAmountOut: string
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
||||||
let amountOutFormatted
|
|
||||||
amountOutFormatted = await this.amountToUnits(tokenOut, tokenAmountOut)
|
const amountOutFormatted = await this.amountToUnits(tokenOut, tokenAmountOut)
|
||||||
|
|
||||||
let amount = null
|
let amount = null
|
||||||
|
|
||||||
@ -1451,11 +1464,10 @@ export class Pool {
|
|||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
const pool = new this.web3.eth.Contract(this.poolABI, poolAddress)
|
||||||
|
|
||||||
let amountInFormatted
|
const amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn)
|
||||||
amountInFormatted = await this.amountToUnits(tokenIn, tokenAmountIn)
|
|
||||||
|
|
||||||
let amount = null
|
let amount = null
|
||||||
// console.log(amountInFormatted)
|
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted)
|
.getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted)
|
||||||
|
@ -17,8 +17,6 @@ import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/community
|
|||||||
import { LoggerInstance } from '../../../../src/utils'
|
import { LoggerInstance } from '../../../../src/utils'
|
||||||
import { NFTFactory } from '../../../../src/factories/NFTFactory'
|
import { NFTFactory } from '../../../../src/factories/NFTFactory'
|
||||||
import { Pool } from '../../../../src/pools/balancer/Pool'
|
import { Pool } from '../../../../src/pools/balancer/Pool'
|
||||||
import { CONNREFUSED } from 'dns'
|
|
||||||
import exp from 'constants'
|
|
||||||
const { keccak256 } = require('@ethersproject/keccak256')
|
const { keccak256 } = require('@ethersproject/keccak256')
|
||||||
const web3 = new Web3('http://127.0.0.1:8545')
|
const web3 = new Web3('http://127.0.0.1:8545')
|
||||||
const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75'
|
const communityCollector = '0xeE9300b7961e0a01d9f0adb863C7A227A07AaD75'
|
||||||
@ -96,7 +94,13 @@ describe('Pool unit test', () => {
|
|||||||
contracts.factory721Address,
|
contracts.factory721Address,
|
||||||
'10000'
|
'10000'
|
||||||
)
|
)
|
||||||
|
console.log(
|
||||||
|
await pool.allowance(
|
||||||
|
contracts.daiAddress,
|
||||||
|
contracts.accounts[0],
|
||||||
|
contracts.factory721Address
|
||||||
|
)
|
||||||
|
)
|
||||||
expect(
|
expect(
|
||||||
await pool.allowance(
|
await pool.allowance(
|
||||||
contracts.daiAddress,
|
contracts.daiAddress,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user