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

Fix token amount - token allowance comparison (#1012)

* fixed allowence check in approve and added allowance check in addOceanLiquidity method

* added more dtAllowance checks

* renamed constants and some code cleanup

* return allowance if bigger than amount and delete upper methods checks
This commit is contained in:
Bogdan Fazakas 2021-09-22 10:28:45 +03:00 committed by GitHub
parent 61ef169a12
commit 7e88ef9fd4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 46 deletions

View File

@ -120,34 +120,31 @@ export class OceanPool extends Pool {
const oceanWeight = 10 - parseFloat(dtWeight)
this.dtAddress = dtAddress
let txid
const dtAllowance = await this.allowance(dtAddress, account, address)
if (new Decimal(dtAllowance).lt(dtAmount)) {
observer.next(PoolCreateProgressStep.ApprovingDatatoken)
txid = await this.approve(
account,
dtAddress,
address,
this.web3.utils.toWei(String(dtAmount))
)
if (!txid) {
this.logger.error('ERROR: Failed to call approve DT token')
throw new Error('ERROR: Failed to call approve DT token')
}
observer.next(PoolCreateProgressStep.ApprovingDatatoken)
txid = await this.approve(
account,
dtAddress,
address,
this.web3.utils.toWei(String(dtAmount))
)
if (!txid) {
this.logger.error('ERROR: Failed to call approve DT token')
throw new Error('ERROR: Failed to call approve DT token')
}
const oceanAllowance = await this.allowance(this.oceanAddress, account, address)
if (new Decimal(oceanAllowance).lt(oceanAmount)) {
observer.next(PoolCreateProgressStep.ApprovingOcean)
txid = await this.approve(
account,
this.oceanAddress,
address,
this.web3.utils.toWei(String(oceanAmount))
)
if (!txid) {
this.logger.error('ERROR: Failed to call approve OCEAN token')
throw new Error('ERROR: Failed to call approve OCEAN token')
}
observer.next(PoolCreateProgressStep.ApprovingOcean)
txid = await this.approve(
account,
this.oceanAddress,
address,
this.web3.utils.toWei(String(oceanAmount))
)
if (!txid) {
this.logger.error('ERROR: Failed to call approve OCEAN token')
throw new Error('ERROR: Failed to call approve OCEAN token')
}
observer.next(PoolCreateProgressStep.SetupPool)
txid = await super.setup(
account,
@ -562,7 +559,7 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough Ocean Tokens')
return null
}
// TODO - check balances first
const txid = await super.approve(
account,
this.oceanAddress,
@ -570,9 +567,10 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(maxOceanAmount)
)
if (!txid) {
this.logger.error('ERROR: OCEAN approve failed')
return null
this.logger.error('ERROR: Failed to call approve OCEAN token')
throw new Error('ERROR: Failed to call approve OCEAN token')
}
const tx = await super.swapExactAmountOut(
account,
poolAddress,
@ -598,7 +596,7 @@ export class OceanPool extends Pool {
account: string,
poolAddress: string,
minimumdtAmountWanted: string,
OceanAmount: string,
oceanAmount: string,
maxPrice?: string
): Promise<TransactionReceipt> {
if (this.oceanAddress == null) {
@ -615,26 +613,27 @@ export class OceanPool extends Pool {
return null
}
const calcInGivenOut = await this.getOceanNeeded(poolAddress, minimumdtAmountWanted)
if (new Decimal(calcInGivenOut).greaterThan(OceanAmount)) {
if (new Decimal(calcInGivenOut).greaterThan(oceanAmount)) {
this.logger.error('ERROR: Not enough Ocean Tokens')
return null
}
// TODO - check balances first
const txid = await super.approve(
account,
this.oceanAddress,
poolAddress,
this.web3.utils.toWei(OceanAmount)
this.web3.utils.toWei(oceanAmount)
)
if (!txid) {
this.logger.error('ERROR: OCEAN approve failed')
return null
this.logger.error('ERROR: Failed to call approve OCEAN token')
throw new Error('ERROR: Failed to call approve OCEAN token')
}
const tx = await super.swapExactAmountIn(
account,
poolAddress,
this.oceanAddress,
OceanAmount,
oceanAmount,
dtAddress,
minimumdtAmountWanted,
maxPrice
@ -676,6 +675,7 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Not enough datatokens')
return null
}
const txid = await super.approve(
account,
dtAddress,
@ -683,9 +683,10 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(dtAmount)
)
if (!txid) {
this.logger.error('ERROR: DT approve failed')
return null
this.logger.error('ERROR: Failed to call approve DT token')
throw new Error('ERROR: Failed to call approve DT token')
}
const tx = await super.swapExactAmountIn(
account,
poolAddress,
@ -716,6 +717,7 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Too much reserve to add')
return null
}
const txid = await super.approve(
account,
dtAddress,
@ -723,9 +725,10 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(amount)
)
if (!txid) {
this.logger.error('ERROR: DT approve failed')
return null
this.logger.error('ERROR: Failed to call approve DT token')
throw new Error('ERROR: Failed to call approve DT token')
}
const result = await super.joinswapExternAmountIn(
account,
poolAddress,
@ -800,6 +803,7 @@ export class OceanPool extends Pool {
this.logger.error('ERROR: Too much reserve to add')
return null
}
const txid = await super.approve(
account,
this.oceanAddress,
@ -807,9 +811,10 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(amount)
)
if (!txid) {
this.logger.error('ERROR: OCEAN approve failed')
return null
this.logger.error('ERROR: Failed to call approve OCEAN token')
throw new Error('ERROR: Failed to call approve OCEAN token')
}
const result = await super.joinswapExternAmountIn(
account,
poolAddress,

View File

@ -142,7 +142,7 @@ export class Pool extends PoolFactory {
spender: string,
amount: string,
force = false
): Promise<TransactionReceipt> {
): Promise<TransactionReceipt | string> {
const minABI = [
{
constant: false,
@ -173,9 +173,10 @@ export class Pool extends PoolFactory {
})
if (!force) {
const currentAllowence = await this.allowance(tokenAddress, account, spender)
if (new Decimal(currentAllowence).greaterThanOrEqualTo(amount)) {
// we have enough
return null
if (
new Decimal(this.web3.utils.toWei(currentAllowence)).greaterThanOrEqualTo(amount)
) {
return currentAllowence
}
}
let result = null

View File

@ -216,6 +216,8 @@ describe('Balancer flow', () => {
await Pool.buyDT(bob, greatPool, '1', '4')
const bobDtBalance = await datatoken.balance(tokenAddress, bob)
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
if (consoleDebug) console.log('BOB DT Balance:' + bobDtBalance)
if (consoleDebug) console.log('BOB Ocean Balance:' + bobOceanBalance)
assert(Number(bobDtBalance) > 0)
assert(Number(bobOceanBalance) > 0)
})
@ -223,6 +225,8 @@ describe('Balancer flow', () => {
await Pool.buyDTWithExactOcean(bob, greatPool, '0.1', '2')
const bobDtBalance = await datatoken.balance(tokenAddress, bob)
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
if (consoleDebug) console.log('BOB DT Balance:' + bobDtBalance)
if (consoleDebug) console.log('BOB Ocean Balance:' + bobOceanBalance)
assert(Number(bobDtBalance) > 0)
assert(Number(bobOceanBalance) > 0)
})
@ -240,6 +244,8 @@ describe('Balancer flow', () => {
await Pool.sellDT(bob, greatPool, '1', '0.1')
const newbobDtBalance = await datatoken.balance(tokenAddress, bob)
const newbobOceanBalance = await datatoken.balance(oceanTokenAddress, bob)
if (consoleDebug) console.log('BOB Dt Balance:' + newbobDtBalance)
if (consoleDebug) console.log('BOB ocean Balance:' + newbobOceanBalance)
assert(Number(newbobDtBalance) < Number(bobDtBalance))
assert(Number(newbobOceanBalance) > Number(bobOceanBalance))
})