diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 3a806e40..9010244e 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -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 { 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, diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 83075c89..d878941d 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -142,7 +142,7 @@ export class Pool extends PoolFactory { spender: string, amount: string, force = false - ): Promise { + ): Promise { 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 diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 38ce7ad7..f2871a62 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -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)) })