1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Merge pull request #377 from oceanprotocol/fix/logger

Fix/LoggerLevel
This commit is contained in:
Ahmed Ali 2020-10-22 10:01:46 +02:00 committed by GitHub
commit b8c0a9fe15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 142 additions and 105 deletions

View File

@ -4,7 +4,8 @@ import { TransactionReceipt } from 'web3-core'
import { Pool } from './Pool'
import { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js'
import { SubscribablePromise } from '../utils'
import { SubscribablePromise, Logger } from '../utils'
declare type PoolTransactionType = 'swap' | 'join' | 'exit'
const POOL_MAX_AMOUNT_IN_LIMIT = 0.25 // maximum 1/4 of the pool reserve
@ -50,13 +51,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
}
@ -79,22 +81,22 @@ export class OceanPool extends Pool {
fee: string
): SubscribablePromise<PoolCreateProgressStep, TransactionReceipt> {
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
}
return new SubscribablePromise(async (observer) => {
observer.next(PoolCreateProgressStep.CreatingPool)
const createTxid = await super.createPool(account)
if (!createTxid) {
console.error('ERROR: Failed to call approve DT token')
this.logger.error('ERROR: Failed to call approve DT token')
return null
}
const address = createTxid.events.BPoolRegistered.returnValues[0]
@ -110,7 +112,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
}
observer.next(PoolCreateProgressStep.ApprovingOcean)
@ -121,7 +123,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
}
observer.next(PoolCreateProgressStep.SetupPool)
@ -137,7 +139,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 createTxid
@ -169,7 +171,7 @@ export class OceanPool extends Pool {
*/
public async getOceanReserve(poolAddress: string): Promise<string> {
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)
@ -434,7 +436,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}`)
}
}
@ -529,20 +531,20 @@ export class OceanPool extends Pool {
maxPrice?: string
): Promise<TransactionReceipt> {
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
@ -553,7 +555,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(
@ -584,7 +586,7 @@ export class OceanPool extends Pool {
maxPrice?: string
): Promise<TransactionReceipt> {
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)
@ -592,13 +594,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(
@ -608,7 +610,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(
@ -637,7 +639,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(
@ -647,7 +649,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(
@ -676,19 +678,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(
@ -713,12 +715,12 @@ export class OceanPool extends Pool {
amount: string
): Promise<TransactionReceipt> {
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(
@ -728,7 +730,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(
@ -755,24 +757,24 @@ export class OceanPool extends Pool {
maximumPoolShares: string
): Promise<TransactionReceipt> {
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(
@ -802,7 +804,7 @@ export class OceanPool extends Pool {
): Promise<TransactionReceipt> {
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
}
@ -816,7 +818,7 @@ export class OceanPool extends Pool {
*/
public async getDTPrice(poolAddress: string): Promise<string> {
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')

View File

@ -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'
@ -23,12 +23,13 @@ export class Pool extends PoolFactory {
constructor(
web3: Web3,
logger: Logger,
factoryABI: AbiItem | AbiItem[] = null,
poolABI: AbiItem | AbiItem[] = null,
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[]
}
@ -82,7 +83,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 +135,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 +153,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 +191,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 +216,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 +236,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 +252,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 +269,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 +285,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 +301,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 +317,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 +343,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 +360,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 +379,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 +396,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 +413,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 +431,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 +449,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 +466,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 +506,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 +546,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 +583,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 +617,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 +651,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 +686,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 +720,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 +754,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 +777,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 +800,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 +830,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 +859,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 +888,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 +917,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 +946,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 +975,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
}

View File

@ -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'
import { TransactionReceipt } from 'web3-core'
@ -8,9 +9,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
@ -23,6 +26,7 @@ export class PoolFactory {
this.factoryAddress = factoryAddress
}
if (gaslimit) this.GASLIMIT_DEFAULT = gaslimit
this.logger = logger
}
/**
@ -30,12 +34,12 @@ export class PoolFactory {
*/
async createPool(account: string): Promise<TransactionReceipt> {
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
}
@ -49,7 +53,7 @@ export class PoolFactory {
.send({ from: account, gas: this.GASLIMIT_DEFAULT })
// 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 txid
}

View File

@ -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
}
}

View File

@ -4,7 +4,7 @@ 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 {
@ -38,6 +38,7 @@ export class OceanFixedRateExchange {
public fixedRateExchangeABI: AbiItem | AbiItem[]
public web3: Web3
public contract: Contract = null
private logger: Logger
public datatokens: DataTokens
/**
@ -49,6 +50,7 @@ export class OceanFixedRateExchange {
*/
constructor(
web3: Web3,
logger: Logger,
fixedRateExchangeAddress: string = null,
fixedRateExchangeABI: AbiItem | AbiItem[] = null,
oceanAddress: string = null,
@ -65,6 +67,7 @@ export class OceanFixedRateExchange {
this.fixedRateExchangeABI,
this.fixedRateExchangeAddress
)
this.logger = logger
}
/**
@ -110,7 +113,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}`)
}
if (amount && exchangeId) {
observer.next(FixedRateCreateProgressStep.ApprovingDatatoken)
@ -168,7 +171,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
}
}
@ -244,6 +247,7 @@ export class OceanFixedRateExchange {
return estGas
})
} catch (e) {
this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`)
estGas = DEFAULT_GAS_LIMIT
}
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({
@ -278,6 +282,7 @@ export class OceanFixedRateExchange {
return estGas
})
} catch (e) {
this.logger.error(`ERROR: FixedPriceExchange: ${e.message}`)
estGas = DEFAULT_GAS_LIMIT
}
const trxReceipt = await this.contract.methods.toggleExchangeState(exchangeId).send({

View File

@ -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<TransactionReceipt> {
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<TransactionReceipt> {
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

View File

@ -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
}

View File

@ -504,7 +504,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 ' +
@ -522,7 +522,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
}

View File

@ -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
}
}

View File

@ -50,10 +50,12 @@ 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,
instanceConfig.logger,
instanceConfig.config.poolFactoryABI,
instanceConfig.config.poolABI,
instanceConfig.config.poolFactoryAddress,
@ -61,6 +63,7 @@ export class Ocean extends Instantiable {
)
instance.fixedRateExchange = new OceanFixedRateExchange(
instanceConfig.config.web3Provider,
instanceConfig.logger,
instanceConfig.config.fixedRateExchangeAddress,
instanceConfig.config.fixedRateExchangeAddressABI,
instanceConfig.config.oceanTokenAddress,
@ -68,6 +71,7 @@ export class Ocean extends Instantiable {
)
instance.OnChainMetadataCache = new OnChainMetadataCache(
instanceConfig.config.web3Provider,
instanceConfig.logger,
instanceConfig.config.metadataContractAddress,
instanceConfig.config.metadataContractABI
)

View File

@ -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,

View File

@ -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,

View File

@ -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')
})

View File

@ -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)
})

View File

@ -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) {
@ -74,7 +75,8 @@ describe('Balancer flow', () => {
contracts.factoryAddress,
factory.abi as AbiItem[],
datatokensTemplate.abi as AbiItem[],
web3
web3,
LoggerInstance
)
assert(datatoken !== null)
})
@ -89,7 +91,8 @@ describe('Balancer flow', () => {
contracts.factoryAddress,
factory.abi as AbiItem[],
datatokensTemplate.abi as AbiItem[],
web3
web3,
LoggerInstance
)
oceanTokenAddress = await oceandatatoken.create(
blob,
@ -102,6 +105,7 @@ describe('Balancer flow', () => {
it('should initialize OceanPool class', async () => {
Pool = new OceanPool(
web3,
LoggerInstance,
OceanPoolFactory.abi as AbiItem[],
OceanSPool.abi as AbiItem[],
OceanPoolFactoryAddress,

View File

@ -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 { LoggerInstance } from '../../../src/utils'
import Web3 from 'web3'
import factory from '@oceanprotocol/contracts/artifacts/DTFactory.json'
import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemplate.json'
@ -66,7 +66,8 @@ describe('FixedRateExchange flow', () => {
contracts.factoryAddress,
factory.abi as AbiItem[],
datatokensTemplate.abi as AbiItem[],
web3
web3,
LoggerInstance
)
assert(datatoken !== null)
})
@ -89,7 +90,8 @@ describe('FixedRateExchange flow', () => {
contracts.factoryAddress,
factory.abi as AbiItem[],
datatokensTemplate.abi as AbiItem[],
web3
web3,
LoggerInstance
)
oceanTokenAddress = await oceandatatoken.create(
blob,
@ -105,6 +107,7 @@ describe('FixedRateExchange flow', () => {
it('should initialize FixedExchangeRate class', async () => {
FixedRateClass = new OceanFixedRateExchange(
web3,
LoggerInstance,
FixedRateExchangeAddress,
FixedRateExchangeContract.abi as AbiItem[],
oceanTokenAddress,