From 87dd5fcfa10f6379862b82ff504cd4b1f7501191 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Mon, 19 Oct 2020 20:57:17 +0200 Subject: [PATCH 01/13] fix FixedRateExchange --- src/exchange/FixedRateExchange.ts | 16 ++++++++++------ src/ocean/Ocean.ts | 3 ++- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 2e3343e3..84c91c3c 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -3,6 +3,7 @@ import BigNumber from 'bignumber.js' import { TransactionReceipt } from 'web3-core' import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' +import { Logger } from '../utils' import Web3 from 'web3' export interface FixedPriceExchange { @@ -30,6 +31,7 @@ export class OceanFixedRateExchange { public fixedRateExchangeABI: AbiItem | AbiItem[] public web3: Web3 public contract: Contract = null + private logger: Logger /** * Instantiate FixedRateExchange @@ -42,7 +44,8 @@ export class OceanFixedRateExchange { web3: Web3, fixedRateExchangeAddress: string = null, fixedRateExchangeABI: AbiItem | AbiItem[] = null, - oceanAddress: string = null + oceanAddress: string = null, + logger: Logger ) { this.web3 = web3 this.fixedRateExchangeAddress = fixedRateExchangeAddress @@ -54,6 +57,7 @@ export class OceanFixedRateExchange { this.fixedRateExchangeABI, this.fixedRateExchangeAddress ) + this.logger = logger } /** @@ -91,7 +95,7 @@ export class OceanFixedRateExchange { }) exchangeId = trxReceipt.events.ExchangeCreated.returnValues[0] } catch (e) { - console.error(`ERROR: Failed to create new exchange: ${e.message}`) + this.logger.error(`ERROR: Failed to create new exchange: ${e.message}`) } return exchangeId } @@ -144,7 +148,7 @@ export class OceanFixedRateExchange { }) return trxReceipt } catch (e) { - console.error(`ERROR: Failed to buy datatokens: ${e.message}`) + this.logger.error(`ERROR: Failed to buy datatokens: ${e.message}`) return null } } @@ -178,7 +182,7 @@ export class OceanFixedRateExchange { .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .estimateGas(function (err, estGas) { if (err) { - console.error(`ERROR: FixedPriceExchange: ${err.message}`) + this.logger.error(`ERROR: FixedPriceExchange: ${err.message}`) return DEFAULT_GAS_LIMIT } return estGas @@ -214,7 +218,7 @@ export class OceanFixedRateExchange { .toggleExchangeState(exchangeId) .estimateGas(function (err, estGas) { if (err) { - console.error(`ERROR: FixedPriceExchange: ${err.message}`) + this.logger.error(`ERROR: FixedPriceExchange: ${err.message}`) estGas = DEFAULT_GAS_LIMIT } return estGas @@ -248,7 +252,7 @@ export class OceanFixedRateExchange { .toggleExchangeState(exchangeId) .estimateGas(function (err, estGas) { if (err) { - console.error(`ERROR: FixedPriceExchange: ${err.message}`) + this.logger.error(`ERROR: FixedPriceExchange: ${err.message}`) estGas = DEFAULT_GAS_LIMIT } return estGas diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 1c4d4377..0ff9b0a5 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -63,7 +63,8 @@ export class Ocean extends Instantiable { instanceConfig.config.web3Provider, instanceConfig.config.fixedRateExchangeAddress, instanceConfig.config.fixedRateExchangeAddressABI, - instanceConfig.config.oceanTokenAddress + instanceConfig.config.oceanTokenAddress, + instanceConfig.logger ) instance.OnChainMetadataCache = new OnChainMetadataCache( instanceConfig.config.web3Provider, From b01908a7bd9c0d8f54b1d67ed90906b779c89e38 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Mon, 19 Oct 2020 21:33:17 +0200 Subject: [PATCH 02/13] fix test for FixedRateExchange --- test/unit/exchanges/FixedPriceExchange.test.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index ea4cc205..1908f505 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -4,7 +4,7 @@ import { TestContractHandler } from '../../TestContractHandler' import { FixedPricedContractHandler } from '../../FixedPriceContractHandler' import { DataTokens } from '../../../src/datatokens/Datatokens' import { OceanFixedRateExchange } from '../../../src/exchange/FixedRateExchange' - +import { Logger } from '../../../src/utils' import Web3 from 'web3' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' @@ -107,7 +107,8 @@ describe('FixedRateExchange flow', () => { web3, FixedRateExchangeAddress, FixedRateExchangeContract.abi as AbiItem[], - oceanTokenAddress + oceanTokenAddress, + new Logger() ) assert(FixedRateClass !== null) }) From ca733f2a064224b9df261d4c114ad3971cd89999 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Tue, 20 Oct 2020 11:45:06 +0200 Subject: [PATCH 03/13] fix error messages --- src/exchange/FixedRateExchange.ts | 14 ++++++++------ src/ocean/Ocean.ts | 4 ++-- test/unit/exchanges/FixedPriceExchange.test.ts | 7 ++++--- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 84c91c3c..5c7f9f2a 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -1,9 +1,9 @@ +import { Logger } from '../utils' import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' import BigNumber from 'bignumber.js' import { TransactionReceipt } from 'web3-core' import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' -import { Logger } from '../utils' import Web3 from 'web3' export interface FixedPriceExchange { @@ -42,10 +42,10 @@ export class OceanFixedRateExchange { */ constructor( web3: Web3, + logger: Logger, fixedRateExchangeAddress: string = null, fixedRateExchangeABI: AbiItem | AbiItem[] = null, - oceanAddress: string = null, - logger: Logger + oceanAddress: string = null ) { this.web3 = web3 this.fixedRateExchangeAddress = fixedRateExchangeAddress @@ -182,7 +182,7 @@ export class OceanFixedRateExchange { .setRate(exchangeId, this.web3.utils.toWei(String(newRate))) .estimateGas(function (err, estGas) { if (err) { - this.logger.error(`ERROR: FixedPriceExchange: ${err.message}`) + console.error(`ERROR: FixedPriceExchange: ${err.message}`) return DEFAULT_GAS_LIMIT } return estGas @@ -218,12 +218,13 @@ export class OceanFixedRateExchange { .toggleExchangeState(exchangeId) .estimateGas(function (err, estGas) { if (err) { - this.logger.error(`ERROR: FixedPriceExchange: ${err.message}`) + console.error(`ERROR: FixedPriceExchange: ${err.message}`) estGas = DEFAULT_GAS_LIMIT } return estGas }) } catch (e) { + this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`) estGas = DEFAULT_GAS_LIMIT } const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ @@ -252,12 +253,13 @@ export class OceanFixedRateExchange { .toggleExchangeState(exchangeId) .estimateGas(function (err, estGas) { if (err) { - this.logger.error(`ERROR: FixedPriceExchange: ${err.message}`) + console.error(`ERROR: FixedPriceExchange: ${err.message}`) estGas = DEFAULT_GAS_LIMIT } return estGas }) } catch (e) { + this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`) estGas = DEFAULT_GAS_LIMIT } const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({ diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 0ff9b0a5..28df3a11 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -61,10 +61,10 @@ export class Ocean extends Instantiable { ) instance.fixedRateExchange = new OceanFixedRateExchange( instanceConfig.config.web3Provider, + instanceConfig.logger, instanceConfig.config.fixedRateExchangeAddress, instanceConfig.config.fixedRateExchangeAddressABI, - instanceConfig.config.oceanTokenAddress, - instanceConfig.logger + instanceConfig.config.oceanTokenAddress ) instance.OnChainMetadataCache = new OnChainMetadataCache( instanceConfig.config.web3Provider, diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 1908f505..1b6fd336 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -4,7 +4,7 @@ import { TestContractHandler } from '../../TestContractHandler' import { FixedPricedContractHandler } from '../../FixedPriceContractHandler' import { DataTokens } from '../../../src/datatokens/Datatokens' import { OceanFixedRateExchange } from '../../../src/exchange/FixedRateExchange' -import { Logger } from '../../../src/utils' +import { LoggerInstance } from '../../../src/utils' import Web3 from 'web3' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' @@ -105,10 +105,10 @@ describe('FixedRateExchange flow', () => { it('should initialize FixedExchangeRate class', async () => { FixedRateClass = new OceanFixedRateExchange( web3, + LoggerInstance, FixedRateExchangeAddress, FixedRateExchangeContract.abi as AbiItem[], - oceanTokenAddress, - new Logger() + oceanTokenAddress ) assert(FixedRateClass !== null) }) @@ -208,6 +208,7 @@ describe('FixedRateExchange flow', () => { if (consoleDebug) console.log('Bob ocean balance:' + balance) }) it('Alice should update the rate', async () => { + // console.log(await FixedRateClass.getLogger()) const tx = await FixedRateClass.setRate(aliceExchangeId, updatedPriceRate, alice) assert(tx !== null) }) From 08019706e9d46c7409b338d77f4b2f50d4787aab Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Tue, 20 Oct 2020 14:59:49 +0200 Subject: [PATCH 04/13] fix Balancer pool --- src/balancer/OceanPool.ts | 64 +++++++++++------------ src/balancer/Pool.ts | 78 ++++++++++++++++------------- src/ocean/Ocean.ts | 1 + test/unit/balancer/Balancer.test.ts | 2 + 4 files changed, 78 insertions(+), 67 deletions(-) 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, From b0ef8d7262c677c5e1b3d4ac615f971400249fc0 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Tue, 20 Oct 2020 15:35:23 +0200 Subject: [PATCH 05/13] fix Balancer PoolFactory --- src/balancer/Pool.ts | 4 +--- src/balancer/PoolFactory.ts | 10 +++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/balancer/Pool.ts b/src/balancer/Pool.ts index 2947cf2e..3f84d08b 100644 --- a/src/balancer/Pool.ts +++ b/src/balancer/Pool.ts @@ -20,7 +20,6 @@ export interface TokensToAdd { export class Pool extends PoolFactory { public poolABI: AbiItem | AbiItem[] - public logger: Logger constructor( web3: Web3, @@ -30,10 +29,9 @@ export class Pool extends PoolFactory { factoryAddress: string = null, gaslimit?: number ) { - super(web3, factoryABI, factoryAddress, gaslimit) + super(web3, logger, factoryABI, factoryAddress, gaslimit) if (poolABI) this.poolABI = poolABI else this.poolABI = jsonpoolABI.abi as AbiItem[] - this.logger = logger } /** diff --git a/src/balancer/PoolFactory.ts b/src/balancer/PoolFactory.ts index 19c614d9..77ba5d99 100644 --- a/src/balancer/PoolFactory.ts +++ b/src/balancer/PoolFactory.ts @@ -1,5 +1,6 @@ import Web3 from 'web3' import { AbiItem } from 'web3-utils/types' +import { Logger } from '../utils' import jsonFactoryABI from '@oceanprotocol/contracts/artifacts/BFactory.json' export class PoolFactory { @@ -7,9 +8,11 @@ export class PoolFactory { public web3: Web3 = null public factoryABI: AbiItem | AbiItem[] public factoryAddress: string + public logger: Logger constructor( web3: Web3, + logger: Logger, factoryABI: AbiItem | AbiItem[] = null, factoryAddress: string = null, gaslimit?: number @@ -22,6 +25,7 @@ export class PoolFactory { this.factoryAddress = factoryAddress } if (gaslimit) this.GASLIMIT_DEFAULT = gaslimit + this.logger = logger } /** @@ -29,12 +33,12 @@ export class PoolFactory { */ async createPool(account: string): Promise { if (this.web3 === null) { - console.error('ERROR: Web3 object is null') + this.logger.error('ERROR: Web3 object is null') return null } if (this.factoryAddress === null) { - console.error('ERROR: bfactoryAddress is null') + this.logger.error('ERROR: bfactoryAddress is null') return null } @@ -51,7 +55,7 @@ export class PoolFactory { try { pooladdress = transactiondata.events.BPoolRegistered.returnValues[0] } catch (e) { - console.error(`ERROR: Failed to create new pool: ${e.message}`) + this.logger.error(`ERROR: Failed to create new pool: ${e.message}`) } return pooladdress } From c4d6b48896a115fd72c0cfd0f5a507ad2187ed1a Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 09:52:43 +0200 Subject: [PATCH 06/13] fix DataTokens --- src/datatokens/Datatokens.ts | 12 +++++++----- src/ocean/Ocean.ts | 3 ++- test/unit/Datatokens.test.ts | 5 +++-- test/unit/balancer/Balancer.test.ts | 6 ++++-- test/unit/exchanges/FixedPriceExchange.test.ts | 8 +++++--- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/datatokens/Datatokens.ts b/src/datatokens/Datatokens.ts index d5dd1c05..648aa8a9 100644 --- a/src/datatokens/Datatokens.ts +++ b/src/datatokens/Datatokens.ts @@ -3,7 +3,7 @@ import { AbiItem } from 'web3-utils/types' import defaultFactoryABI from '@oceanprotocol/contracts/artifacts/DTFactory.json' import defaultDatatokensABI from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' - +import { Logger } from '../utils' import wordListDefault from '../data/words.json' import { TransactionReceipt } from 'web3-core' import BigNumber from 'bignumber.js' @@ -16,7 +16,7 @@ export class DataTokens { public factoryABI: AbiItem | AbiItem[] public datatokensABI: AbiItem | AbiItem[] public web3: Web3 - + private logger: Logger /** * Instantiate DataTokens (independently of Ocean). * @param {String} factoryAddress @@ -28,12 +28,14 @@ export class DataTokens { factoryAddress: string, factoryABI: AbiItem | AbiItem[], datatokensABI: AbiItem | AbiItem[], - web3: Web3 + web3: Web3, + logger: Logger ) { this.factoryAddress = factoryAddress this.factoryABI = factoryABI || (defaultFactoryABI.abi as AbiItem[]) this.datatokensABI = datatokensABI || (defaultDatatokensABI.abi as AbiItem[]) this.web3 = web3 + this.logger = logger } /** @@ -108,7 +110,7 @@ export class DataTokens { try { tokenAddress = trxReceipt.events.TokenCreated.returnValues[0] } catch (e) { - console.error(`ERROR: Failed to create datatoken : ${e.message}`) + this.logger.error(`ERROR: Failed to create datatoken : ${e.message}`) } return tokenAddress } @@ -373,7 +375,7 @@ export class DataTokens { .send({ from: address, gas: 600000 }) return trxReceipt } catch (e) { - console.error(`ERROR: Failed to start order : ${e.message}`) + this.logger.error(`ERROR: Failed to start order : ${e.message}`) return null } } diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 56176bc2..05afb676 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -50,7 +50,8 @@ export class Ocean extends Instantiable { instanceConfig.config.factoryAddress, instanceConfig.config.factoryABI, instanceConfig.config.datatokensABI, - instanceConfig.config.web3Provider + instanceConfig.config.web3Provider, + instanceConfig.logger ) instance.pool = new OceanPool( instanceConfig.config.web3Provider, diff --git a/test/unit/Datatokens.test.ts b/test/unit/Datatokens.test.ts index f760f04c..a4de8765 100644 --- a/test/unit/Datatokens.test.ts +++ b/test/unit/Datatokens.test.ts @@ -2,7 +2,7 @@ import { assert } from 'chai' import { AbiItem } from 'web3-utils/types' import { TestContractHandler } from '../TestContractHandler' import { DataTokens } from '../../src/datatokens/Datatokens' - +import { LoggerInstance } from '../../src/utils' import Web3 from 'web3' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' @@ -38,7 +38,8 @@ describe('DataTokens', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) assert(datatoken !== null) }) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 3e6e2f25..4bcc2592 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -75,7 +75,8 @@ describe('Balancer flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) assert(datatoken !== null) }) @@ -90,7 +91,8 @@ describe('Balancer flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) oceanTokenAddress = await oceandatatoken.create( blob, diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 1b6fd336..6dabf765 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -10,6 +10,7 @@ import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' import FixedRateExchangeContract = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') +import { Logger } from '../../../src/lib' const web3 = new Web3('http://127.0.0.1:8545') describe('FixedRateExchange flow', () => { @@ -66,7 +67,8 @@ describe('FixedRateExchange flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) assert(datatoken !== null) }) @@ -89,7 +91,8 @@ describe('FixedRateExchange flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) oceanTokenAddress = await oceandatatoken.create( blob, @@ -208,7 +211,6 @@ describe('FixedRateExchange flow', () => { if (consoleDebug) console.log('Bob ocean balance:' + balance) }) it('Alice should update the rate', async () => { - // console.log(await FixedRateClass.getLogger()) const tx = await FixedRateClass.setRate(aliceExchangeId, updatedPriceRate, alice) assert(tx !== null) }) From 418305c0f10281d73fb26f7af8c89a1a78d419b7 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 10:16:31 +0200 Subject: [PATCH 07/13] minor test cleanup --- test/unit/balancer/Balancer.test.ts | 10 +++++----- test/unit/exchanges/FixedPriceExchange.test.ts | 1 - 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 4bcc2592..7c5dc294 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -15,11 +15,11 @@ 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) { - return new Promise((resolve) => { - setTimeout(resolve, ms) - }) -} +// function sleep(ms: number) { +// return new Promise((resolve) => { +// setTimeout(resolve, ms) +// }) +// } describe('Balancer flow', () => { let oceanTokenAddress: string diff --git a/test/unit/exchanges/FixedPriceExchange.test.ts b/test/unit/exchanges/FixedPriceExchange.test.ts index 6dabf765..eae10258 100644 --- a/test/unit/exchanges/FixedPriceExchange.test.ts +++ b/test/unit/exchanges/FixedPriceExchange.test.ts @@ -10,7 +10,6 @@ import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' import FixedRateExchangeContract = require('@oceanprotocol/contracts/artifacts/FixedRateExchange.json') -import { Logger } from '../../../src/lib' const web3 = new Web3('http://127.0.0.1:8545') describe('FixedRateExchange flow', () => { From 9cb6ea3dbe7eae44bb31f4c31e57bd57355c9979 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 10:21:51 +0200 Subject: [PATCH 08/13] fix more datatokens related tests --- test/integration/ComputeFlow.test.ts | 4 +++- test/integration/Marketplaceflow.test.ts | 4 +++- test/integration/Simpleflow.test.ts | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/test/integration/ComputeFlow.test.ts b/test/integration/ComputeFlow.test.ts index 3b33088b..4f377981 100644 --- a/test/integration/ComputeFlow.test.ts +++ b/test/integration/ComputeFlow.test.ts @@ -10,6 +10,7 @@ import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' import { Account, DDO, Metadata } from '../../src/lib' import { Cluster, Container, Server } from '../../src/ocean/Compute' +import { LoggerInstance } from '../../src/utils' const web3 = new Web3('http://127.0.0.1:8545') function sleep(ms: number) { @@ -87,7 +88,8 @@ describe('Compute flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) tokenAddress = await datatoken.create( blob, diff --git a/test/integration/Marketplaceflow.test.ts b/test/integration/Marketplaceflow.test.ts index bef949b7..4de24496 100644 --- a/test/integration/Marketplaceflow.test.ts +++ b/test/integration/Marketplaceflow.test.ts @@ -9,6 +9,7 @@ import { Account, EditableMetadata, Service, ServiceAccess } from '../../src/lib import { Ocean } from '../../src/ocean/Ocean' import { ConfigHelper } from '../../src/utils/ConfigHelper' import { TestContractHandler } from '../TestContractHandler' +import { LoggerInstance } from '../../src/utils' const web3 = new Web3('http://127.0.0.1:8545') @@ -65,7 +66,8 @@ describe('Marketplace flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) tokenAddress = await datatoken.create( blob, diff --git a/test/integration/Simpleflow.test.ts b/test/integration/Simpleflow.test.ts index 6ec1389e..c615bd8c 100644 --- a/test/integration/Simpleflow.test.ts +++ b/test/integration/Simpleflow.test.ts @@ -5,6 +5,7 @@ import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json' import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json' +import { LoggerInstance } from '../../src/utils' const web3 = new Web3('http://127.0.0.1:8545') describe('Simple flow', () => { @@ -40,7 +41,8 @@ describe('Simple flow', () => { contracts.factoryAddress, factory.abi as AbiItem[], datatokensTemplate.abi as AbiItem[], - web3 + web3, + LoggerInstance ) tokenAddress = await datatoken.create(blob, alice, '10000000000', 'AliceDT', 'DTA') }) From 5047347e21af7aab1bf348fcf86ab2276e9596f4 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 10:33:23 +0200 Subject: [PATCH 09/13] fix OnChainMetadataCache --- src/metadatacache/OnChainMetaDataCache.ts | 18 ++++++++++-------- src/ocean/Ocean.ts | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/metadatacache/OnChainMetaDataCache.ts b/src/metadatacache/OnChainMetaDataCache.ts index 93b7babf..9c421103 100644 --- a/src/metadatacache/OnChainMetaDataCache.ts +++ b/src/metadatacache/OnChainMetaDataCache.ts @@ -4,8 +4,7 @@ import { Contract } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' import defaultDDOContractABI from '@oceanprotocol/contracts/artifacts/Metadata.json' -import { didZeroX } from '../utils' - +import { didZeroX, Logger } from '../utils' // Using limited, compress-only version // See https://github.com/LZMA-JS/LZMA-JS#but-i-dont-want-to-use-web-workers import { LZMA } from 'lzma/src/lzma-c' @@ -19,11 +18,13 @@ export class OnChainMetadataCache { public DDOContractABI: AbiItem | AbiItem[] public web3: Web3 public DDOContract: Contract = null + private logger: Logger /** * Instantiate OnChainMetadata Store for on-chain interaction. */ constructor( web3: Web3, + logger: Logger, DDOContractAddress: string = null, DDOContractABI: AbiItem | AbiItem[] = null ) { @@ -35,6 +36,7 @@ export class OnChainMetadataCache { this.DDOContractABI, this.DDOContractAddress ) + this.logger = logger } /** @@ -100,7 +102,7 @@ export class OnChainMetadataCache { consumerAccount: string ): Promise { if (!this.DDOContract) { - console.error('ERROR: Missing DDOContract') + this.logger.error('ERROR: Missing DDOContract') return null } try { @@ -116,7 +118,7 @@ export class OnChainMetadataCache { .send({ from: consumerAccount }) return trxReceipt } catch (e) { - console.error(`ERROR: Failed to publish raw DDO : ${e.message}`) + this.logger.error(`ERROR: Failed to publish raw DDO : ${e.message}`) return null } } @@ -136,7 +138,7 @@ export class OnChainMetadataCache { consumerAccount: string ): Promise { if (!this.DDOContract) { - console.error('ERROR: Missing DDOContract') + this.logger.error('ERROR: Missing DDOContract') return null } try { @@ -145,7 +147,7 @@ export class OnChainMetadataCache { .send({ from: consumerAccount }) return trxReceipt } catch (e) { - console.error(`ERROR: Failed to update raw DDO : ${e.message}`) + this.logger.error(`ERROR: Failed to update raw DDO : ${e.message}`) return null } } @@ -171,7 +173,7 @@ export class OnChainMetadataCache { }) return trxReceipt } catch (e) { - console.error(`ERROR: Failed to transfer DDO ownership : ${e.message}`) + this.logger.error(`ERROR: Failed to transfer DDO ownership : ${e.message}`) return null } } @@ -201,7 +203,7 @@ export class OnChainMetadataCache { hex += '' + hexChar[(message[i] >> 4) & 0x0f] + hexChar[message[i] & 0x0f] } } catch (e) { - console.error(`ERROR: Failed to get hex message value : ${e.message}`) + this.logger.error(`ERROR: Failed to get hex message value : ${e.message}`) } const hexMessage = '0x' + hex return hexMessage diff --git a/src/ocean/Ocean.ts b/src/ocean/Ocean.ts index 05afb676..a9f4a732 100644 --- a/src/ocean/Ocean.ts +++ b/src/ocean/Ocean.ts @@ -70,6 +70,7 @@ export class Ocean extends Instantiable { ) instance.OnChainMetadataCache = new OnChainMetadataCache( instanceConfig.config.web3Provider, + instanceConfig.logger, instanceConfig.config.metadataContractAddress, instanceConfig.config.metadataContractABI ) From bdd1483668b6f42213896d7ebeec4ed7c3023c3a Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 10:35:20 +0200 Subject: [PATCH 10/13] uncomment the sleep function --- test/unit/balancer/Balancer.test.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/unit/balancer/Balancer.test.ts b/test/unit/balancer/Balancer.test.ts index 7c5dc294..4bcc2592 100644 --- a/test/unit/balancer/Balancer.test.ts +++ b/test/unit/balancer/Balancer.test.ts @@ -15,11 +15,11 @@ 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) { -// return new Promise((resolve) => { -// setTimeout(resolve, ms) -// }) -// } +function sleep(ms: number) { + return new Promise((resolve) => { + setTimeout(resolve, ms) + }) +} describe('Balancer flow', () => { let oceanTokenAddress: string From d3d1667093c52350ef2a75aa38b08265e13f4343 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 10:55:40 +0200 Subject: [PATCH 11/13] fix more log level errors --- src/ocean/Account.ts | 2 +- src/ocean/Assets.ts | 4 ++-- src/ocean/Compute.ts | 4 ++-- src/utils/ConfigHelper.ts | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ocean/Account.ts b/src/ocean/Account.ts index 4ff84ce3..16da86d0 100644 --- a/src/ocean/Account.ts +++ b/src/ocean/Account.ts @@ -111,7 +111,7 @@ export default class Account extends Instantiable { }) result = this.web3.utils.fromWei(await token.methods.balanceOf(this.id).call()) } catch (e) { - console.error(`ERROR: Failed to get the balance: ${e.message}`) + this.logger.error(`ERROR: Failed to get the balance: ${e.message}`) } return result } diff --git a/src/ocean/Assets.ts b/src/ocean/Assets.ts index f5b22be5..c64d160a 100644 --- a/src/ocean/Assets.ts +++ b/src/ocean/Assets.ts @@ -497,7 +497,7 @@ export class Assets extends Instantiable { ) const totalCost = new BigNumber(String(providerData.numTokens)) if (balance.isLessThan(totalCost)) { - console.error( + this.logger.error( 'ERROR: Not enough funds Needed ' + totalCost.toString() + ' but balance is ' + @@ -515,7 +515,7 @@ export class Assets extends Instantiable { ) if (txid) return txid.transactionHash } catch (e) { - console.error(`ERROR: Failed to order: ${e.message}`) + this.logger.error(`ERROR: Failed to order: ${e.message}`) } return null } diff --git a/src/ocean/Compute.ts b/src/ocean/Compute.ts index 3961e864..c69713d8 100644 --- a/src/ocean/Compute.ts +++ b/src/ocean/Compute.ts @@ -395,7 +395,7 @@ export class Compute extends Instantiable { // check if raw algo is allowed if (service.attributes.main.privacy) if (!service.attributes.main.privacy.allowRawAlgorithm) { - console.error('ERROR: This service does not allow raw algorithm') + this.logger.error('ERROR: This service does not allow raw algorithm') return null } } @@ -407,7 +407,7 @@ export class Compute extends Instantiable { if ( !service.attributes.main.privacy.trustedAlgorithms.includes(algorithmDid) ) { - console.error('ERROR: This service does not allow this algorithm') + this.logger.error('ERROR: This service does not allow this algorithm') return null } } diff --git a/src/utils/ConfigHelper.ts b/src/utils/ConfigHelper.ts index 8d76f9e0..d6088d54 100644 --- a/src/utils/ConfigHelper.ts +++ b/src/utils/ConfigHelper.ts @@ -91,7 +91,7 @@ export class ConfigHelper { return configAddresses } catch (e) { - console.error(`ERROR: Could not load local contract address file: ${e.message}`) + Logger.error(`ERROR: Could not load local contract address file: ${e.message}`) return null } } From 7d9426e7ff0d8367fec89ec60d36f270e7860976 Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 11:05:01 +0200 Subject: [PATCH 12/13] update travis --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2521ab75..b8ce90e6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,9 @@ dist: xenial sudo: required language: node_js -node_js: node - +node_js: + - 14 + services: - docker From b9f038411d861b61d56f3f9298fe2d7f5608032f Mon Sep 17 00:00:00 2001 From: Ahmed Ali Date: Wed, 21 Oct 2020 15:13:29 +0200 Subject: [PATCH 13/13] fix minor linting errors --- src/balancer/OceanPool.ts | 3 +-- src/exchange/FixedRateExchange.ts | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/balancer/OceanPool.ts b/src/balancer/OceanPool.ts index d09bcbd4..6a680fc3 100644 --- a/src/balancer/OceanPool.ts +++ b/src/balancer/OceanPool.ts @@ -4,8 +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' -import { SubscribablePromise } from '../utils' +import { SubscribablePromise, Logger } from '../utils' declare type PoolTransactionType = 'swap' | 'join' | 'exit' diff --git a/src/exchange/FixedRateExchange.ts b/src/exchange/FixedRateExchange.ts index 2e6e004e..03bdc83b 100644 --- a/src/exchange/FixedRateExchange.ts +++ b/src/exchange/FixedRateExchange.ts @@ -1,11 +1,10 @@ -import { Logger } from '../utils' import defaultFixedRateExchangeABI from '@oceanprotocol/contracts/artifacts/FixedRateExchange.json' import BigNumber from 'bignumber.js' import { TransactionReceipt } from 'web3-core' import { Contract, EventData } from 'web3-eth-contract' import { AbiItem } from 'web3-utils/types' import Web3 from 'web3' -import { SubscribablePromise } from '../utils' +import { SubscribablePromise, Logger } from '../utils' import { DataTokens } from '../datatokens/Datatokens' export interface FixedPriceExchange {