From 65ab6c2c23e71290d0bcde26abeeaf8a06e24e50 Mon Sep 17 00:00:00 2001 From: lacoop6tu Date: Tue, 9 Nov 2021 08:28:41 -0500 Subject: [PATCH] fix lint --- src/pools/balancer/Pool.ts | 69 +++++++++++--------------- test/unit/pools/balancer/Pool.test.ts | 71 +++++++++++++++++++-------- 2 files changed, 80 insertions(+), 60 deletions(-) diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index 8aceedb4..692148c0 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -756,18 +756,18 @@ export class Pool { defaultERC20ABI.abi as AbiItem[], token ) - + try { decimals = await tokenContract.methods.decimals().call() - if (decimals == 0){ + if (decimals === 0) { decimals = 18 } } catch (e) { this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18') } - + const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals) - + return amountFormatted.toString() } @@ -779,13 +779,13 @@ export class Pool { ) try { decimals = await tokenContract.methods.decimals().call() - if (decimals == 0){ + if (decimals === 0) { decimals = 18 } } catch (e) { this.logger.error('ERROR: FAILED TO CALL DECIMALS(), USING 18') } - + const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals) return amountFormatted.toString() @@ -1520,9 +1520,8 @@ export class Pool { const result = await pool.methods .getAmountOutExactIn(tokenIn, tokenOut, amountInFormatted) .call() - + amount = await this.unitsToAmount(tokenOut, result) - } catch (e) { this.logger.error('ERROR: Failed to calcOutGivenIn') } @@ -1538,39 +1537,32 @@ export class Pool { let amount = null try { const result = await pool.methods - .calcPoolOutSingleIn( - tokenIn, - await this.amountToUnits(tokenIn,tokenAmountIn) - ) + .calcPoolOutSingleIn(tokenIn, await this.amountToUnits(tokenIn, tokenAmountIn)) .call() - amount = await this.unitsToAmount(poolAddress,result) + amount = await this.unitsToAmount(poolAddress, result) } catch (e) { this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`) } return amount } - + public async calcSingleInGivenPoolOut( poolAddress: string, - tokenIn:string, - poolAmountOut: string, + tokenIn: string, + poolAmountOut: string ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let amount = null - - const amountFormatted = await this.amountToUnits(poolAddress,poolAmountOut) - + + const amountFormatted = await this.amountToUnits(poolAddress, poolAmountOut) + try { const result = await pool.methods - .calcSingleInPoolOut( - tokenIn, - amountFormatted - ) - + .calcSingleInPoolOut(tokenIn, amountFormatted) + .call() - - amount = await this.unitsToAmount(tokenIn,result) - + + amount = await this.unitsToAmount(tokenIn, result) } catch (e) { this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`) } @@ -1579,7 +1571,7 @@ export class Pool { public async calcSingleOutGivenPoolIn( poolAddress: string, - tokenOut:string, + tokenOut: string, poolAmountIn: string ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) @@ -1587,11 +1579,11 @@ export class Pool { try { const result = await pool.methods .calcSingleOutPoolIn( - tokenOut, - await this.amountToUnits(poolAddress,poolAmountIn) + tokenOut, + await this.amountToUnits(poolAddress, poolAmountIn) ) .call() - amount = await this.unitsToAmount(tokenOut,result) + amount = await this.unitsToAmount(tokenOut, result) } catch (e) { this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`) } @@ -1599,21 +1591,18 @@ export class Pool { } public async calcPoolInGivenSingleOut( - poolAddress:string, - tokenOut:string, - tokenAmountOut: string + poolAddress: string, + tokenOut: string, + tokenAmountOut: string ): Promise { const pool = new this.web3.eth.Contract(this.poolABI, poolAddress) let amount = null try { const result = await pool.methods - .calcPoolInSingleOut( - tokenOut, - await this.amountToUnits(tokenOut,tokenAmountOut) - ) + .calcPoolInSingleOut(tokenOut, await this.amountToUnits(tokenOut, tokenAmountOut)) .call() - - amount = await this.unitsToAmount(poolAddress,result) + + amount = await this.unitsToAmount(poolAddress, result) } catch (e) { this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`) } diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index 0962fd09..6fb8929b 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -638,42 +638,73 @@ describe('Pool unit test', () => { }) 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 + // 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) + expect( + await pool.calcPoolOutGivenSingleIn(poolAddress, erc20Token, tokenInAmount) + ).to.equal( + await pool.calcPoolOutGivenSingleIn( + poolAddress, + contracts.usdcAddress, + tokenInAmount + ) ) - //console.log(await pool.calcPoolOutGivenSingleIn(poolAddress, erc20Token, 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 + // 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 + 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 + // 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) + 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 + // 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 + expect( + parseInt( + await pool.calcPoolInGivenSingleOut(poolAddress, erc20Token, tokenAmountOut) + ) + ).to.be.closeTo( + parseInt( + await pool.calcPoolInGivenSingleOut( + poolAddress, + contracts.usdcAddress, + tokenAmountOut + ) + ), + 1e11 ) })