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

View File

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

View File

@ -216,6 +216,8 @@ describe('Balancer flow', () => {
await Pool.buyDT(bob, greatPool, '1', '4') await Pool.buyDT(bob, greatPool, '1', '4')
const bobDtBalance = await datatoken.balance(tokenAddress, bob) const bobDtBalance = await datatoken.balance(tokenAddress, bob)
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, 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(bobDtBalance) > 0)
assert(Number(bobOceanBalance) > 0) assert(Number(bobOceanBalance) > 0)
}) })
@ -223,6 +225,8 @@ describe('Balancer flow', () => {
await Pool.buyDTWithExactOcean(bob, greatPool, '0.1', '2') await Pool.buyDTWithExactOcean(bob, greatPool, '0.1', '2')
const bobDtBalance = await datatoken.balance(tokenAddress, bob) const bobDtBalance = await datatoken.balance(tokenAddress, bob)
const bobOceanBalance = await datatoken.balance(oceanTokenAddress, 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(bobDtBalance) > 0)
assert(Number(bobOceanBalance) > 0) assert(Number(bobOceanBalance) > 0)
}) })
@ -240,6 +244,8 @@ describe('Balancer flow', () => {
await Pool.sellDT(bob, greatPool, '1', '0.1') await Pool.sellDT(bob, greatPool, '1', '0.1')
const newbobDtBalance = await datatoken.balance(tokenAddress, bob) const newbobDtBalance = await datatoken.balance(tokenAddress, bob)
const newbobOceanBalance = await datatoken.balance(oceanTokenAddress, 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(newbobDtBalance) < Number(bobDtBalance))
assert(Number(newbobOceanBalance) > Number(bobOceanBalance)) assert(Number(newbobOceanBalance) > Number(bobOceanBalance))
}) })