mirror of
https://github.com/oceanprotocol/ocean.js.git
synced 2024-11-26 20:39:05 +01:00
add test for liquidity view functions, fix decimals issue
This commit is contained in:
parent
5460b65e45
commit
475855d899
@ -756,14 +756,18 @@ export class Pool {
|
|||||||
defaultERC20ABI.abi as AbiItem[],
|
defaultERC20ABI.abi as AbiItem[],
|
||||||
token
|
token
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
decimals = await tokenContract.methods.decimals().call()
|
decimals = await tokenContract.methods.decimals().call()
|
||||||
|
if (decimals == 0){
|
||||||
|
decimals = 18
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
||||||
}
|
}
|
||||||
|
|
||||||
const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
|
const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
|
||||||
|
|
||||||
return amountFormatted.toString()
|
return amountFormatted.toString()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -775,10 +779,13 @@ export class Pool {
|
|||||||
)
|
)
|
||||||
try {
|
try {
|
||||||
decimals = await tokenContract.methods.decimals().call()
|
decimals = await tokenContract.methods.decimals().call()
|
||||||
|
if (decimals == 0){
|
||||||
|
decimals = 18
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18')
|
||||||
}
|
}
|
||||||
|
|
||||||
const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals)
|
const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals)
|
||||||
|
|
||||||
return amountFormatted.toString()
|
return amountFormatted.toString()
|
||||||
@ -1513,7 +1520,9 @@ export class Pool {
|
|||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted)
|
.getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted)
|
||||||
.call()
|
.call()
|
||||||
|
|
||||||
amount = await this.unitsToAmount(tokenOut, result)
|
amount = await this.unitsToAmount(tokenOut, result)
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error('ERROR: Failed to calcOutGivenIn')
|
this.logger.error('ERROR: Failed to calcOutGivenIn')
|
||||||
}
|
}
|
||||||
@ -1522,56 +1531,46 @@ export class Pool {
|
|||||||
|
|
||||||
public async calcPoolOutGivenSingleIn(
|
public async calcPoolOutGivenSingleIn(
|
||||||
poolAddress: string,
|
poolAddress: string,
|
||||||
tokenBalanceIn: string,
|
tokenIn: string,
|
||||||
tokenWeightIn: string,
|
tokenAmountIn: string
|
||||||
poolSupply: string,
|
|
||||||
totalWeight: string,
|
|
||||||
tokenAmountIn: string,
|
|
||||||
swapFee: 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 amount = null
|
let amount = null
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.calcPoolOutGivenSingleIn(
|
.calcPoolOutSingleIn(
|
||||||
this.web3.utils.toWei(tokenBalanceIn),
|
tokenIn,
|
||||||
this.web3.utils.toWei(tokenWeightIn),
|
await this.amountToUnits(tokenIn,tokenAmountIn)
|
||||||
this.web3.utils.toWei(poolSupply),
|
|
||||||
this.web3.utils.toWei(totalWeight),
|
|
||||||
this.web3.utils.toWei(tokenAmountIn),
|
|
||||||
this.web3.utils.toWei(swapFee)
|
|
||||||
)
|
)
|
||||||
.call()
|
.call()
|
||||||
amount = this.web3.utils.fromWei(result)
|
amount = await this.unitsToAmount(poolAddress,result)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`)
|
this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`)
|
||||||
}
|
}
|
||||||
return amount
|
return amount
|
||||||
}
|
}
|
||||||
|
|
||||||
public async calcSingleInGivenPoolOut(
|
public async calcSingleInGivenPoolOut(
|
||||||
poolAddress: string,
|
poolAddress: string,
|
||||||
tokenBalanceIn: string,
|
tokenIn:string,
|
||||||
tokenWeightIn: string,
|
|
||||||
poolSupply: string,
|
|
||||||
totalWeight: string,
|
|
||||||
poolAmountOut: string,
|
poolAmountOut: string,
|
||||||
swapFee: 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 amount = null
|
let amount = null
|
||||||
|
|
||||||
|
const amountFormatted = await this.amountToUnits(poolAddress,poolAmountOut)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.calcSingleInGivenPoolOut(
|
.calcSingleInPoolOut(
|
||||||
this.web3.utils.toWei(tokenBalanceIn),
|
tokenIn,
|
||||||
this.web3.utils.toWei(tokenWeightIn),
|
amountFormatted
|
||||||
this.web3.utils.toWei(poolSupply),
|
|
||||||
this.web3.utils.toWei(totalWeight),
|
|
||||||
this.web3.utils.toWei(poolAmountOut),
|
|
||||||
this.web3.utils.toWei(swapFee)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
.call()
|
.call()
|
||||||
amount = this.web3.utils.fromWei(result)
|
|
||||||
|
amount = await this.unitsToAmount(tokenIn,result)
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`)
|
this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`)
|
||||||
}
|
}
|
||||||
@ -1580,27 +1579,19 @@ export class Pool {
|
|||||||
|
|
||||||
public async calcSingleOutGivenPoolIn(
|
public async calcSingleOutGivenPoolIn(
|
||||||
poolAddress: string,
|
poolAddress: string,
|
||||||
tokenBalanceOut: string,
|
tokenOut:string,
|
||||||
tokenWeightOut: string,
|
poolAmountIn: string
|
||||||
poolSupply: string,
|
|
||||||
totalWeight: string,
|
|
||||||
poolAmountIn: string,
|
|
||||||
swapFee: 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 amount = null
|
let amount = null
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.calcSingleOutGivenPoolIn(
|
.calcSingleOutPoolIn(
|
||||||
this.web3.utils.toWei(tokenBalanceOut),
|
tokenOut,
|
||||||
this.web3.utils.toWei(tokenWeightOut),
|
await this.amountToUnits(poolAddress,poolAmountIn)
|
||||||
this.web3.utils.toWei(poolSupply),
|
|
||||||
this.web3.utils.toWei(totalWeight),
|
|
||||||
this.web3.utils.toWei(poolAmountIn),
|
|
||||||
this.web3.utils.toWei(swapFee)
|
|
||||||
)
|
)
|
||||||
.call()
|
.call()
|
||||||
amount = this.web3.utils.fromWei(result)
|
amount = await this.unitsToAmount(tokenOut,result)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`)
|
this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`)
|
||||||
}
|
}
|
||||||
@ -1608,28 +1599,21 @@ export class Pool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async calcPoolInGivenSingleOut(
|
public async calcPoolInGivenSingleOut(
|
||||||
poolAddress: string,
|
poolAddress:string,
|
||||||
tokenBalanceOut: string,
|
tokenOut:string,
|
||||||
tokenWeightOut: string,
|
tokenAmountOut: string
|
||||||
poolSupply: string,
|
|
||||||
totalWeight: string,
|
|
||||||
tokenAmountOut: string,
|
|
||||||
swapFee: 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 amount = null
|
let amount = null
|
||||||
try {
|
try {
|
||||||
const result = await pool.methods
|
const result = await pool.methods
|
||||||
.calcPoolInGivenSingleOut(
|
.calcPoolInSingleOut(
|
||||||
this.web3.utils.toWei(tokenBalanceOut),
|
tokenOut,
|
||||||
this.web3.utils.toWei(tokenWeightOut),
|
await this.amountToUnits(tokenOut,tokenAmountOut)
|
||||||
this.web3.utils.toWei(poolSupply),
|
|
||||||
this.web3.utils.toWei(totalWeight),
|
|
||||||
this.web3.utils.toWei(tokenAmountOut),
|
|
||||||
this.web3.utils.toWei(swapFee)
|
|
||||||
)
|
)
|
||||||
.call()
|
.call()
|
||||||
amount = this.web3.utils.fromWei(result)
|
|
||||||
|
amount = await this.unitsToAmount(poolAddress,result)
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`)
|
this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`)
|
||||||
}
|
}
|
||||||
|
@ -637,6 +637,46 @@ describe('Pool unit test', () => {
|
|||||||
expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0')
|
expect(await erc20Contract.methods.balanceOf(user2).call()).to.equal('0')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('#calcPoolOutGivenSingleIn - should get the amount of pool OUT for exact token IN', async () => {
|
||||||
|
// since rate is 1 and the pool is just created
|
||||||
|
// amount of pool out received for same amount of different token In is equal
|
||||||
|
const tokenInAmount = '10' // 10 USDC or 10 DTs
|
||||||
|
expect(await pool.calcPoolOutGivenSingleIn(poolAddress, erc20Token, tokenInAmount)).to.equal(
|
||||||
|
await pool.calcPoolOutGivenSingleIn(poolAddress, contracts.usdcAddress, tokenInAmount)
|
||||||
|
)
|
||||||
|
//console.log(await pool.calcPoolOutGivenSingleIn(poolAddress, erc20Token, tokenInAmount))
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
it('#calcSingleInGivenPoolOut - should get the amount of token IN for exact pool token OUT', async () => {
|
||||||
|
// since rate is 1 and the pool is just created
|
||||||
|
// amount of different token In for getting same pool amount out is equal
|
||||||
|
const poolAmountOut = '1'
|
||||||
|
expect(parseInt(await pool.calcSingleInGivenPoolOut(poolAddress, erc20Token, poolAmountOut))).to.be.closeTo(
|
||||||
|
parseInt(await pool.calcSingleInGivenPoolOut(poolAddress, contracts.usdcAddress, poolAmountOut)),1e9
|
||||||
|
)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#calcSingleOutGivenPoolIn - should get the amount of token OUT for exact pool token IN', async () => {
|
||||||
|
// since rate is 1 and the pool is just created
|
||||||
|
//amount amount of different token Out for rediming the same pool In is equal
|
||||||
|
const poolAmountIn = '10'
|
||||||
|
expect(await pool.calcSingleOutGivenPoolIn(poolAddress, erc20Token, poolAmountIn)).to.equal(
|
||||||
|
await pool.calcSingleOutGivenPoolIn(poolAddress, contracts.usdcAddress, poolAmountIn)
|
||||||
|
)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
it('#calcPoolInGivenSingleOut - should get the amount of pool IN for exact token OUT', async () => {
|
||||||
|
// since rate is 1 and the pool is just created
|
||||||
|
//amount of pool In for getting the same amount of different token Out is equal
|
||||||
|
const tokenAmountOut = '10'
|
||||||
|
expect(parseInt(await pool.calcPoolInGivenSingleOut(poolAddress, erc20Token,tokenAmountOut))).to.be.closeTo(
|
||||||
|
parseInt(await pool.calcPoolInGivenSingleOut(poolAddress, contracts.usdcAddress, tokenAmountOut)),1e9
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => {
|
it('#sharesBalance - should return user shares balance (datatoken balance, LPT balance, etc) ', async () => {
|
||||||
expect(await usdcContract.methods.balanceOf(user2).call()).to.equal(
|
expect(await usdcContract.methods.balanceOf(user2).call()).to.equal(
|
||||||
await pool.sharesBalance(user2, contracts.usdcAddress)
|
await pool.sharesBalance(user2, contracts.usdcAddress)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user