diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index 93a21fc7..6a0a1ea3 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -4,6 +4,7 @@ import { TransactionReceipt } from 'web3-core' import { Pool } from './Pool' import { EventData, Filter } from 'web3-eth-contract' import BigNumber from 'bignumber.js' +import { Logger } from '../utils' declare type PoolTransactionType = 'swap' | 'join' | 'exit' @@ -43,13 +44,14 @@ export class OceanPool extends Pool { constructor( web3: Web3, + logger: Logger, factoryABI: AbiItem | AbiItem[] = null, poolABI: AbiItem | AbiItem[] = null, factoryAddress: string = null, oceanAddress: string = null, gaslimit?: number ) { - super(web3, factoryABI, poolABI, factoryAddress, gaslimit) + super(web3, logger, factoryABI, poolABI, factoryAddress, gaslimit) if (oceanAddress) { this.oceanAddress = oceanAddress } @@ -72,15 +74,15 @@ export class OceanPool extends Pool { fee: string ): Promise { if (this.oceanAddress == null) { - console.error('ERROR: oceanAddress is not defined') + this.logger.error('ERROR: oceanAddress is not defined') return null } if (parseFloat(fee) > 0.1) { - console.error('ERROR: Swap fee too high. The maximum allowed swapFee is 0.1 (10%).') + this.logger.error('ERROR: Swap fee too high. The maximum allowed swapFee is 10%') return null } if (parseFloat(weight) > 9 || parseFloat(weight) < 1) { - console.error('ERROR: Weight out of bounds (min 1, max9)') + this.logger.error('ERROR: Weight out of bounds (min 1, max9)') return null } const address = await super.createPool(account) @@ -95,7 +97,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(String(amount)) ) if (!txid) { - console.error('ERROR: Failed to call approve DT token') + this.logger.error('ERROR: Failed to call approve DT token') return null } txid = await this.approve( @@ -105,7 +107,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(String(oceanAmount)) ) if (!txid) { - console.error('ERROR: Failed to call approve OCEAN token') + this.logger.error('ERROR: Failed to call approve OCEAN token') return null } txid = await super.setup( @@ -120,7 +122,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(fee) ) if (!txid) { - console.error('ERROR: Failed to create a new pool') + this.logger.error('ERROR: Failed to create a new pool') return null } return address @@ -151,7 +153,7 @@ export class OceanPool extends Pool { */ public async getOceanReserve(poolAddress: string): Promise { if (this.oceanAddress == null) { - console.error('ERROR: oceanAddress is not defined') + this.logger.error('ERROR: oceanAddress is not defined') return null } return super.getReserve(poolAddress, this.oceanAddress) @@ -416,7 +418,7 @@ export class OceanPool extends Pool { return { dtAmount, oceanAmount } } catch (e) { - console.error(`ERROR: Unable to get token info. ${e.message}`) + this.logger.error(`ERROR: Unable to get token info. ${e.message}`) } } @@ -511,20 +513,20 @@ export class OceanPool extends Pool { maxPrice?: string ): Promise { if (this.oceanAddress == null) { - console.error('ERROR: undefined ocean token contract address') + this.logger.error('ERROR: undefined ocean token contract address') return null } const dtAddress = await this.getDTAddress(poolAddress) if ( parseFloat(dtAmountWanted) > parseFloat(await this.getDTMaxBuyQuantity(poolAddress)) ) { - console.error('ERROR: Buy quantity exceeds quantity allowed') + this.logger.error('ERROR: Buy quantity exceeds quantity allowed') return null } const calcInGivenOut = await this.getOceanNeeded(poolAddress, dtAmountWanted) if (parseFloat(calcInGivenOut) > parseFloat(maxOceanAmount)) { - console.error('ERROR: Not enough Ocean Tokens') + this.logger.error('ERROR: Not enough Ocean Tokens') return null } // TODO - check balances first @@ -535,7 +537,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(maxOceanAmount) ) if (!txid) { - console.error('ERROR: OCEAN approve failed') + this.logger.error('ERROR: OCEAN approve failed') return null } return this.swapExactAmountOut( @@ -566,7 +568,7 @@ export class OceanPool extends Pool { maxPrice?: string ): Promise { if (this.oceanAddress == null) { - console.error('ERROR: oceanAddress is not defined') + this.logger.error('ERROR: oceanAddress is not defined') return null } const dtAddress = await this.getDTAddress(poolAddress) @@ -574,13 +576,13 @@ export class OceanPool extends Pool { parseFloat(oceanAmountWanted) > parseFloat(await this.getOceanMaxBuyQuantity(poolAddress)) ) { - console.error('ERROR: Buy quantity exceeds quantity allowed') + this.logger.error('ERROR: Buy quantity exceeds quantity allowed') return null } const calcOutGivenIn = await this.getOceanReceived(poolAddress, dtAmount) if (parseFloat(calcOutGivenIn) < parseFloat(oceanAmountWanted)) { - console.error('ERROR: Not enough datatokens') + this.logger.error('ERROR: Not enough datatokens') return null } const txid = await super.approve( @@ -590,7 +592,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(dtAmount) ) if (!txid) { - console.error('ERROR: DT approve failed') + this.logger.error('ERROR: DT approve failed') return null } return this.swapExactAmountIn( @@ -619,7 +621,7 @@ export class OceanPool extends Pool { const dtAddress = await this.getDTAddress(poolAddress) const maxAmount = await this.getMaxAddLiquidity(poolAddress, dtAddress) if (parseFloat(amount) > parseFloat(maxAmount)) { - console.error('ERROR: Too much reserve to add') + this.logger.error('ERROR: Too much reserve to add') return null } const txid = await super.approve( @@ -629,7 +631,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(amount) ) if (!txid) { - console.error('ERROR: DT approve failed') + this.logger.error('ERROR: DT approve failed') return null } const result = await super.joinswapExternAmountIn( @@ -658,19 +660,19 @@ export class OceanPool extends Pool { const dtAddress = await this.getDTAddress(poolAddress) const maxAmount = await this.getDTMaxRemoveLiquidity(poolAddress) if (parseFloat(amount) > parseFloat(maxAmount)) { - console.error('ERROR: Too much reserve to remove') + this.logger.error('ERROR: Too much reserve to remove') return null } const usershares = await this.sharesBalance(account, poolAddress) if (parseFloat(usershares) < parseFloat(maximumPoolShares)) { - console.error('ERROR: Not enough poolShares') + this.logger.error('ERROR: Not enough poolShares') return null } if ( parseFloat(maximumPoolShares) < parseFloat(await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount)) ) { - console.error('ERROR: Not enough poolShares') + this.logger.error('ERROR: Not enough poolShares') return null } return this.exitswapExternAmountOut( @@ -695,12 +697,12 @@ export class OceanPool extends Pool { amount: string ): Promise { if (this.oceanAddress == null) { - console.error('ERROR: oceanAddress is not defined') + this.logger.error('ERROR: oceanAddress is not defined') return null } const maxAmount = await this.getOceanMaxAddLiquidity(poolAddress) if (parseFloat(amount) > parseFloat(maxAmount)) { - console.error('ERROR: Too much reserve to add') + this.logger.error('ERROR: Too much reserve to add') return null } const txid = await super.approve( @@ -710,7 +712,7 @@ export class OceanPool extends Pool { this.web3.utils.toWei(amount) ) if (!txid) { - console.error('ERROR: OCEAN approve failed') + this.logger.error('ERROR: OCEAN approve failed') return null } const result = await super.joinswapExternAmountIn( @@ -737,24 +739,24 @@ export class OceanPool extends Pool { maximumPoolShares: string ): Promise { if (this.oceanAddress == null) { - console.error('ERROR: oceanAddress is not defined') + this.logger.error('ERROR: oceanAddress is not defined') return null } const maxAmount = await this.getOceanMaxRemoveLiquidity(poolAddress) if (parseFloat(amount) > parseFloat(maxAmount)) { - console.error('ERROR: Too much reserve to remove') + this.logger.error('ERROR: Too much reserve to remove') return null } const usershares = await this.sharesBalance(account, poolAddress) if (parseFloat(usershares) < parseFloat(maximumPoolShares)) { - console.error('ERROR: Not enough poolShares') + this.logger.error('ERROR: Not enough poolShares') return null } if ( parseFloat(maximumPoolShares) < parseFloat(await this.getPoolSharesRequiredToRemoveOcean(poolAddress, amount)) ) { - console.error('ERROR: Not enough poolShares') + this.logger.error('ERROR: Not enough poolShares') return null } return super.exitswapExternAmountOut( @@ -784,7 +786,7 @@ export class OceanPool extends Pool { ): Promise { const usershares = await this.sharesBalance(account, poolAddress) if (parseFloat(usershares) < parseFloat(poolShares)) { - console.error('ERROR: Not enough poolShares') + this.logger.error('ERROR: Not enough poolShares') return null } @@ -798,7 +800,7 @@ export class OceanPool extends Pool { */ public async getDTPrice(poolAddress: string): Promise { if (this.oceanAddress == null) { - console.error('ERROR: oceanAddress is not defined') + this.logger.error('ERROR: oceanAddress is not defined') return null } return this.getOceanNeeded(poolAddress, '1') diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 540eb8c5..2947cf2e 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -1,7 +1,7 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' import { TransactionReceipt } from 'web3-core' -import Decimal from 'decimal.js' +import { Logger } from '../utils' import BigNumber from 'bignumber.js' import jsonpoolABI from '@oceanprotocol/contracts/artifacts/BPool.json' import { PoolFactory } from './PoolFactory' @@ -20,9 +20,11 @@ export interface TokensToAdd { export class Pool extends PoolFactory { public poolABI: AbiItem | AbiItem[] + public logger: Logger constructor( web3: Web3, + logger: Logger, factoryABI: AbiItem | AbiItem[] = null, poolABI: AbiItem | AbiItem[] = null, factoryAddress: string = null, @@ -31,6 +33,7 @@ export class Pool extends PoolFactory { super(web3, factoryABI, factoryAddress, gaslimit) if (poolABI) this.poolABI = poolABI else this.poolABI = jsonpoolABI.abi as AbiItem[] + this.logger = logger } /** @@ -82,7 +85,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to setup a pool: ${e.message}`) + this.logger.error(`ERROR: Failed to setup a pool: ${e.message}`) } return result } @@ -134,7 +137,7 @@ export class Pool extends PoolFactory { .approve(spender, amount) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERRPR: Failed to approve spender to spend tokens : ${e.message}`) + this.logger.error(`ERRPR: Failed to approve spender to spend tokens : ${e.message}`) } return result } @@ -152,7 +155,7 @@ export class Pool extends PoolFactory { const balance = await token.methods.balanceOf(account).call() result = this.web3.utils.fromWei(balance) } catch (e) { - console.error(`ERROR: Failed to get shares of pool : ${e.message}`) + this.logger.error(`ERROR: Failed to get shares of pool : ${e.message}`) } return result } @@ -190,7 +193,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to add tokens to pool: ${e.message}`) + this.logger.error(`ERROR: Failed to add tokens to pool: ${e.message}`) } } } @@ -215,7 +218,7 @@ export class Pool extends PoolFactory { .setSwapFee(this.web3.utils.toWei(fee)) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to set pool swap fee: ${e.message}`) + this.logger.error(`ERROR: Failed to set pool swap fee: ${e.message}`) } return result } @@ -235,7 +238,7 @@ export class Pool extends PoolFactory { .finalize() .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to finalize pool: ${e.message}`) + this.logger.error(`ERROR: Failed to finalize pool: ${e.message}`) } return result } @@ -251,7 +254,7 @@ export class Pool extends PoolFactory { try { result = await pool.methods.getNumTokens().call() } catch (e) { - console.error(`ERROR: Failed to get number of tokens: ${e.message}`) + this.logger.error(`ERROR: Failed to get number of tokens: ${e.message}`) } return result } @@ -268,7 +271,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.totalSupply().call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to get total supply of pool shares: ${e.message}`) + this.logger.error(`ERROR: Failed to get total supply of pool shares: ${e.message}`) } return amount } @@ -284,7 +287,7 @@ export class Pool extends PoolFactory { try { result = await pool.methods.getCurrentTokens().call() } catch (e) { - console.error(`ERROR: Failed to get tokens composing this pool: ${e.message}`) + this.logger.error(`ERROR: Failed to get tokens composing this pool: ${e.message}`) } return result } @@ -300,7 +303,7 @@ export class Pool extends PoolFactory { try { result = await pool.methods.getFinalTokens().call() } catch (e) { - console.error(`ERROR: Failed to get the final tokens composing this pool`) + this.logger.error(`ERROR: Failed to get the final tokens composing this pool`) } return result } @@ -316,7 +319,7 @@ export class Pool extends PoolFactory { try { result = await pool.methods.getController().call() } catch (e) { - console.error(`ERROR: Failed to get pool controller address: ${e.message}`) + this.logger.error(`ERROR: Failed to get pool controller address: ${e.message}`) } return result } @@ -342,7 +345,7 @@ export class Pool extends PoolFactory { .setController(controllerAddress) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to set pool controller: ${e.message}`) + this.logger.error(`ERROR: Failed to set pool controller: ${e.message}`) } return result } @@ -359,7 +362,8 @@ export class Pool extends PoolFactory { try { result = await pool.methods.isBound(token).call() } catch (e) { - console.error(`ERROR: Failed to check if a token bounded to a pool: ${e.message}`) + this.logger.error(`ERROR: Failed to check whether a token \ + bounded to a pool. ${e.message}`) } return result } @@ -377,7 +381,8 @@ export class Pool extends PoolFactory { const result = await pool.methods.getBalance(token).call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to get how many tokens are in the pool: ${e.message}`) + this.logger.error(`ERROR: Failed to get how many tokens \ + are in the pool: ${e.message}`) } return amount } @@ -393,7 +398,7 @@ export class Pool extends PoolFactory { try { result = await pool.methods.isFinalized().call() } catch (e) { - console.error(`ERROR: Failed to check whether pool is finalized: ${e.message}`) + this.logger.error(`ERROR: Failed to check whether pool is finalized: ${e.message}`) } return result } @@ -410,7 +415,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.getSwapFee().call() fee = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to get pool fee: ${e.message}`) + this.logger.error(`ERROR: Failed to get pool fee: ${e.message}`) } return fee } @@ -428,7 +433,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.getNormalizedWeight(token).call() weight = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to get normalized weight of a token: ${e.message}`) + this.logger.error(`ERROR: Failed to get normalized weight of a token: ${e.message}`) } return weight } @@ -446,7 +451,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.getDenormalizedWeight(token).call() weight = this.web3.utils.fromWei(result) } catch (e) { - console.error('ERROR: Failed to get denormalized weight of a token in pool') + this.logger.error('ERROR: Failed to get denormalized weight of a token in pool') } return weight } @@ -463,7 +468,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.getTotalDenormalizedWeight().call() weight = this.web3.utils.fromWei(result) } catch (e) { - console.error('ERROR: Failed to get total denormalized weight in pool') + this.logger.error('ERROR: Failed to get total denormalized weight in pool') } return weight } @@ -503,7 +508,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to swap exact amount in : ${e.message}`) + this.logger.error(`ERROR: Failed to swap exact amount in : ${e.message}`) } return result } @@ -543,7 +548,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to swap exact amount out: ${e.message}`) + this.logger.error(`ERROR: Failed to swap exact amount out: ${e.message}`) } return result } @@ -580,7 +585,7 @@ export class Pool extends PoolFactory { .joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to join pool: ${e.message}`) + this.logger.error(`ERROR: Failed to join pool: ${e.message}`) } return result } @@ -614,7 +619,7 @@ export class Pool extends PoolFactory { .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to exit pool: ${e.message}`) + this.logger.error(`ERROR: Failed to exit pool: ${e.message}`) } return result } @@ -648,7 +653,8 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to pay tokens in order to join the pool: ${e.message}`) + this.logger.error(`ERROR: Failed to pay tokens in order to \ + join the pool: ${e.message}`) } return result } @@ -682,7 +688,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error('ERROR: Failed to join swap pool amount out') + this.logger.error('ERROR: Failed to join swap pool amount out') } return result } @@ -716,7 +722,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error(`ERROR: Failed to pay pool shares into the pool: ${e.message}`) + this.logger.error(`ERROR: Failed to pay pool shares into the pool: ${e.message}`) } return result } @@ -750,7 +756,7 @@ export class Pool extends PoolFactory { ) .send({ from: account, gas: this.GASLIMIT_DEFAULT }) } catch (e) { - console.error('ERROR: Failed to exitswapExternAmountOut') + this.logger.error('ERROR: Failed to exitswapExternAmountOut') } return result } @@ -773,7 +779,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.getSpotPrice(tokenIn, tokenOut).call() price = this.web3.utils.fromWei(result) } catch (e) { - console.error('ERROR: Failed to get spot price of swapping tokenIn to tokenOut') + this.logger.error('ERROR: Failed to get spot price of swapping tokenIn to tokenOut') } return price } @@ -796,7 +802,7 @@ export class Pool extends PoolFactory { const result = await pool.methods.getSpotPriceSansFee(tokenIn, tokenOut).call() price = this.web3.utils.fromWei(result) } catch (e) { - console.error('ERROR: Failed to getSpotPriceSansFee') + this.logger.error('ERROR: Failed to getSpotPriceSansFee') } return price } @@ -826,7 +832,7 @@ export class Pool extends PoolFactory { .call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error('ERROR: Failed to calcInGivenOut') + this.logger.error('ERROR: Failed to calcInGivenOut') } return amount } @@ -855,7 +861,7 @@ export class Pool extends PoolFactory { .call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error('ERROR: Failed to calcOutGivenIn') + this.logger.error('ERROR: Failed to calcOutGivenIn') } return amount } @@ -884,7 +890,7 @@ export class Pool extends PoolFactory { .call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`) + this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`) } return amount } @@ -913,7 +919,7 @@ export class Pool extends PoolFactory { .call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`) + this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`) } return amount } @@ -942,7 +948,7 @@ export class Pool extends PoolFactory { .call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`) + this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`) } return amount } @@ -971,7 +977,7 @@ export class Pool extends PoolFactory { .call() amount = this.web3.utils.fromWei(result) } catch (e) { - console.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`) + this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`) } return amount } diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 28df3a11..56176bc2 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -54,6 +54,7 @@ export class Ocean extends Instantiable { ) instance.pool = new OceanPool( instanceConfig.config.web3Provider, + instanceConfig.logger, instanceConfig.config.poolFactoryABI, instanceConfig.config.poolABI, instanceConfig.config.poolFactoryAddress, diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 85aa330c..3e6e2f25 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -12,6 +12,7 @@ import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemp // this will be replaced by our SFactory/SPool import OceanPoolFactory from '@oceanprotocol/contracts/artifacts/BFactory.json' import OceanSPool from '@oceanprotocol/contracts/artifacts/BPool.json' +import { LoggerInstance } from '../../../src/utils' const web3 = new Web3('http://127.0.0.1:8545') function sleep(ms: number) { @@ -102,6 +103,7 @@ describe('Balancer flow', () => { it('should initialize OceanPool class', async () => { Pool = new OceanPool( web3, + LoggerInstance, OceanPoolFactory.abi as AbiItem[], OceanSPool.abi as AbiItem[], OceanPoolFactoryAddress,