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

fix Balancer pool

This commit is contained in:
Ahmed Ali 2020-10-20 14:59:49 +02:00
parent ca733f2a06
commit 08019706e9
4 changed files with 78 additions and 67 deletions

View File

@ -4,6 +4,7 @@ import { TransactionReceipt } from 'web3-core'
import { Pool } from './Pool' import { Pool } from './Pool'
import { EventData, Filter } from 'web3-eth-contract' import { EventData, Filter } from 'web3-eth-contract'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import { Logger } from '../utils'
declare type PoolTransactionType = 'swap' | 'join' | 'exit' declare type PoolTransactionType = 'swap' | 'join' | 'exit'
@ -43,13 +44,14 @@ export class OceanPool extends Pool {
constructor( constructor(
web3: Web3, web3: Web3,
logger: Logger,
factoryABI: AbiItem | AbiItem[] = null, factoryABI: AbiItem | AbiItem[] = null,
poolABI: AbiItem | AbiItem[] = null, poolABI: AbiItem | AbiItem[] = null,
factoryAddress: string = null, factoryAddress: string = null,
oceanAddress: string = null, oceanAddress: string = null,
gaslimit?: number gaslimit?: number
) { ) {
super(web3, factoryABI, poolABI, factoryAddress, gaslimit) super(web3, logger, factoryABI, poolABI, factoryAddress, gaslimit)
if (oceanAddress) { if (oceanAddress) {
this.oceanAddress = oceanAddress this.oceanAddress = oceanAddress
} }
@ -72,15 +74,15 @@ export class OceanPool extends Pool {
fee: string fee: string
): Promise<string> { ): Promise<string> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null return null
} }
if (parseFloat(fee) > 0.1) { 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 return null
} }
if (parseFloat(weight) > 9 || parseFloat(weight) < 1) { 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 null
} }
const address = await super.createPool(account) const address = await super.createPool(account)
@ -95,7 +97,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(String(amount)) this.web3.utils.toWei(String(amount))
) )
if (!txid) { if (!txid) {
console.error('ERROR: Failed to call approve DT token') this.logger.error('ERROR: Failed to call approve DT token')
return null return null
} }
txid = await this.approve( txid = await this.approve(
@ -105,7 +107,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(String(oceanAmount)) this.web3.utils.toWei(String(oceanAmount))
) )
if (!txid) { if (!txid) {
console.error('ERROR: Failed to call approve OCEAN token') this.logger.error('ERROR: Failed to call approve OCEAN token')
return null return null
} }
txid = await super.setup( txid = await super.setup(
@ -120,7 +122,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(fee) this.web3.utils.toWei(fee)
) )
if (!txid) { if (!txid) {
console.error('ERROR: Failed to create a new pool') this.logger.error('ERROR: Failed to create a new pool')
return null return null
} }
return address return address
@ -151,7 +153,7 @@ export class OceanPool extends Pool {
*/ */
public async getOceanReserve(poolAddress: string): Promise<string> { public async getOceanReserve(poolAddress: string): Promise<string> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null return null
} }
return super.getReserve(poolAddress, this.oceanAddress) return super.getReserve(poolAddress, this.oceanAddress)
@ -416,7 +418,7 @@ export class OceanPool extends Pool {
return { dtAmount, oceanAmount } return { dtAmount, oceanAmount }
} catch (e) { } 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 maxPrice?: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: undefined ocean token contract address') this.logger.error('ERROR: undefined ocean token contract address')
return null return null
} }
const dtAddress = await this.getDTAddress(poolAddress) const dtAddress = await this.getDTAddress(poolAddress)
if ( if (
parseFloat(dtAmountWanted) > parseFloat(await this.getDTMaxBuyQuantity(poolAddress)) 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 return null
} }
const calcInGivenOut = await this.getOceanNeeded(poolAddress, dtAmountWanted) const calcInGivenOut = await this.getOceanNeeded(poolAddress, dtAmountWanted)
if (parseFloat(calcInGivenOut) > parseFloat(maxOceanAmount)) { if (parseFloat(calcInGivenOut) > parseFloat(maxOceanAmount)) {
console.error('ERROR: Not enough Ocean Tokens') this.logger.error('ERROR: Not enough Ocean Tokens')
return null return null
} }
// TODO - check balances first // TODO - check balances first
@ -535,7 +537,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(maxOceanAmount) this.web3.utils.toWei(maxOceanAmount)
) )
if (!txid) { if (!txid) {
console.error('ERROR: OCEAN approve failed') this.logger.error('ERROR: OCEAN approve failed')
return null return null
} }
return this.swapExactAmountOut( return this.swapExactAmountOut(
@ -566,7 +568,7 @@ export class OceanPool extends Pool {
maxPrice?: string maxPrice?: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null return null
} }
const dtAddress = await this.getDTAddress(poolAddress) const dtAddress = await this.getDTAddress(poolAddress)
@ -574,13 +576,13 @@ export class OceanPool extends Pool {
parseFloat(oceanAmountWanted) > parseFloat(oceanAmountWanted) >
parseFloat(await this.getOceanMaxBuyQuantity(poolAddress)) parseFloat(await this.getOceanMaxBuyQuantity(poolAddress))
) { ) {
console.error('ERROR: Buy quantity exceeds quantity allowed') this.logger.error('ERROR: Buy quantity exceeds quantity allowed')
return null return null
} }
const calcOutGivenIn = await this.getOceanReceived(poolAddress, dtAmount) const calcOutGivenIn = await this.getOceanReceived(poolAddress, dtAmount)
if (parseFloat(calcOutGivenIn) < parseFloat(oceanAmountWanted)) { if (parseFloat(calcOutGivenIn) < parseFloat(oceanAmountWanted)) {
console.error('ERROR: Not enough datatokens') this.logger.error('ERROR: Not enough datatokens')
return null return null
} }
const txid = await super.approve( const txid = await super.approve(
@ -590,7 +592,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(dtAmount) this.web3.utils.toWei(dtAmount)
) )
if (!txid) { if (!txid) {
console.error('ERROR: DT approve failed') this.logger.error('ERROR: DT approve failed')
return null return null
} }
return this.swapExactAmountIn( return this.swapExactAmountIn(
@ -619,7 +621,7 @@ export class OceanPool extends Pool {
const dtAddress = await this.getDTAddress(poolAddress) const dtAddress = await this.getDTAddress(poolAddress)
const maxAmount = await this.getMaxAddLiquidity(poolAddress, dtAddress) const maxAmount = await this.getMaxAddLiquidity(poolAddress, dtAddress)
if (parseFloat(amount) > parseFloat(maxAmount)) { if (parseFloat(amount) > parseFloat(maxAmount)) {
console.error('ERROR: Too much reserve to add') this.logger.error('ERROR: Too much reserve to add')
return null return null
} }
const txid = await super.approve( const txid = await super.approve(
@ -629,7 +631,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(amount) this.web3.utils.toWei(amount)
) )
if (!txid) { if (!txid) {
console.error('ERROR: DT approve failed') this.logger.error('ERROR: DT approve failed')
return null return null
} }
const result = await super.joinswapExternAmountIn( const result = await super.joinswapExternAmountIn(
@ -658,19 +660,19 @@ export class OceanPool extends Pool {
const dtAddress = await this.getDTAddress(poolAddress) const dtAddress = await this.getDTAddress(poolAddress)
const maxAmount = await this.getDTMaxRemoveLiquidity(poolAddress) const maxAmount = await this.getDTMaxRemoveLiquidity(poolAddress)
if (parseFloat(amount) > parseFloat(maxAmount)) { if (parseFloat(amount) > parseFloat(maxAmount)) {
console.error('ERROR: Too much reserve to remove') this.logger.error('ERROR: Too much reserve to remove')
return null return null
} }
const usershares = await this.sharesBalance(account, poolAddress) const usershares = await this.sharesBalance(account, poolAddress)
if (parseFloat(usershares) < parseFloat(maximumPoolShares)) { if (parseFloat(usershares) < parseFloat(maximumPoolShares)) {
console.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
if ( if (
parseFloat(maximumPoolShares) < parseFloat(maximumPoolShares) <
parseFloat(await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount)) parseFloat(await this.getPoolSharesRequiredToRemoveDT(poolAddress, amount))
) { ) {
console.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
return this.exitswapExternAmountOut( return this.exitswapExternAmountOut(
@ -695,12 +697,12 @@ export class OceanPool extends Pool {
amount: string amount: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null return null
} }
const maxAmount = await this.getOceanMaxAddLiquidity(poolAddress) const maxAmount = await this.getOceanMaxAddLiquidity(poolAddress)
if (parseFloat(amount) > parseFloat(maxAmount)) { if (parseFloat(amount) > parseFloat(maxAmount)) {
console.error('ERROR: Too much reserve to add') this.logger.error('ERROR: Too much reserve to add')
return null return null
} }
const txid = await super.approve( const txid = await super.approve(
@ -710,7 +712,7 @@ export class OceanPool extends Pool {
this.web3.utils.toWei(amount) this.web3.utils.toWei(amount)
) )
if (!txid) { if (!txid) {
console.error('ERROR: OCEAN approve failed') this.logger.error('ERROR: OCEAN approve failed')
return null return null
} }
const result = await super.joinswapExternAmountIn( const result = await super.joinswapExternAmountIn(
@ -737,24 +739,24 @@ export class OceanPool extends Pool {
maximumPoolShares: string maximumPoolShares: string
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null return null
} }
const maxAmount = await this.getOceanMaxRemoveLiquidity(poolAddress) const maxAmount = await this.getOceanMaxRemoveLiquidity(poolAddress)
if (parseFloat(amount) > parseFloat(maxAmount)) { if (parseFloat(amount) > parseFloat(maxAmount)) {
console.error('ERROR: Too much reserve to remove') this.logger.error('ERROR: Too much reserve to remove')
return null return null
} }
const usershares = await this.sharesBalance(account, poolAddress) const usershares = await this.sharesBalance(account, poolAddress)
if (parseFloat(usershares) < parseFloat(maximumPoolShares)) { if (parseFloat(usershares) < parseFloat(maximumPoolShares)) {
console.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
if ( if (
parseFloat(maximumPoolShares) < parseFloat(maximumPoolShares) <
parseFloat(await this.getPoolSharesRequiredToRemoveOcean(poolAddress, amount)) parseFloat(await this.getPoolSharesRequiredToRemoveOcean(poolAddress, amount))
) { ) {
console.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
return super.exitswapExternAmountOut( return super.exitswapExternAmountOut(
@ -784,7 +786,7 @@ export class OceanPool extends Pool {
): Promise<TransactionReceipt> { ): Promise<TransactionReceipt> {
const usershares = await this.sharesBalance(account, poolAddress) const usershares = await this.sharesBalance(account, poolAddress)
if (parseFloat(usershares) < parseFloat(poolShares)) { if (parseFloat(usershares) < parseFloat(poolShares)) {
console.error('ERROR: Not enough poolShares') this.logger.error('ERROR: Not enough poolShares')
return null return null
} }
@ -798,7 +800,7 @@ export class OceanPool extends Pool {
*/ */
public async getDTPrice(poolAddress: string): Promise<string> { public async getDTPrice(poolAddress: string): Promise<string> {
if (this.oceanAddress == null) { if (this.oceanAddress == null) {
console.error('ERROR: oceanAddress is not defined') this.logger.error('ERROR: oceanAddress is not defined')
return null return null
} }
return this.getOceanNeeded(poolAddress, '1') return this.getOceanNeeded(poolAddress, '1')

View File

@ -1,7 +1,7 @@
import Web3 from 'web3' import Web3 from 'web3'
import { AbiItem } from 'web3-utils/types' import { AbiItem } from 'web3-utils/types'
import { TransactionReceipt } from 'web3-core' import { TransactionReceipt } from 'web3-core'
import Decimal from 'decimal.js' import { Logger } from '../utils'
import BigNumber from 'bignumber.js' import BigNumber from 'bignumber.js'
import jsonpoolABI from '@oceanprotocol/contracts/artifacts/BPool.json' import jsonpoolABI from '@oceanprotocol/contracts/artifacts/BPool.json'
import { PoolFactory } from './PoolFactory' import { PoolFactory } from './PoolFactory'
@ -20,9 +20,11 @@ export interface TokensToAdd {
export class Pool extends PoolFactory { export class Pool extends PoolFactory {
public poolABI: AbiItem | AbiItem[] public poolABI: AbiItem | AbiItem[]
public logger: Logger
constructor( constructor(
web3: Web3, web3: Web3,
logger: Logger,
factoryABI: AbiItem | AbiItem[] = null, factoryABI: AbiItem | AbiItem[] = null,
poolABI: AbiItem | AbiItem[] = null, poolABI: AbiItem | AbiItem[] = null,
factoryAddress: string = null, factoryAddress: string = null,
@ -31,6 +33,7 @@ export class Pool extends PoolFactory {
super(web3, factoryABI, factoryAddress, gaslimit) super(web3, factoryABI, factoryAddress, gaslimit)
if (poolABI) this.poolABI = poolABI if (poolABI) this.poolABI = poolABI
else this.poolABI = jsonpoolABI.abi as AbiItem[] 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 }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -134,7 +137,7 @@ export class Pool extends PoolFactory {
.approve(spender, amount) .approve(spender, amount)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -152,7 +155,7 @@ export class Pool extends PoolFactory {
const balance = await token.methods.balanceOf(account).call() const balance = await token.methods.balanceOf(account).call()
result = this.web3.utils.fromWei(balance) result = this.web3.utils.fromWei(balance)
} catch (e) { } 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 return result
} }
@ -190,7 +193,7 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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)) .setSwapFee(this.web3.utils.toWei(fee))
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -235,7 +238,7 @@ export class Pool extends PoolFactory {
.finalize() .finalize()
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to finalize pool: ${e.message}`) this.logger.error(`ERROR: Failed to finalize pool: ${e.message}`)
} }
return result return result
} }
@ -251,7 +254,7 @@ export class Pool extends PoolFactory {
try { try {
result = await pool.methods.getNumTokens().call() result = await pool.methods.getNumTokens().call()
} catch (e) { } 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 return result
} }
@ -268,7 +271,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.totalSupply().call() const result = await pool.methods.totalSupply().call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } 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 return amount
} }
@ -284,7 +287,7 @@ export class Pool extends PoolFactory {
try { try {
result = await pool.methods.getCurrentTokens().call() result = await pool.methods.getCurrentTokens().call()
} catch (e) { } 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 return result
} }
@ -300,7 +303,7 @@ export class Pool extends PoolFactory {
try { try {
result = await pool.methods.getFinalTokens().call() result = await pool.methods.getFinalTokens().call()
} catch (e) { } 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 return result
} }
@ -316,7 +319,7 @@ export class Pool extends PoolFactory {
try { try {
result = await pool.methods.getController().call() result = await pool.methods.getController().call()
} catch (e) { } 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 return result
} }
@ -342,7 +345,7 @@ export class Pool extends PoolFactory {
.setController(controllerAddress) .setController(controllerAddress)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -359,7 +362,8 @@ export class Pool extends PoolFactory {
try { try {
result = await pool.methods.isBound(token).call() result = await pool.methods.isBound(token).call()
} catch (e) { } 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 return result
} }
@ -377,7 +381,8 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getBalance(token).call() const result = await pool.methods.getBalance(token).call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } 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 return amount
} }
@ -393,7 +398,7 @@ export class Pool extends PoolFactory {
try { try {
result = await pool.methods.isFinalized().call() result = await pool.methods.isFinalized().call()
} catch (e) { } 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 return result
} }
@ -410,7 +415,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getSwapFee().call() const result = await pool.methods.getSwapFee().call()
fee = this.web3.utils.fromWei(result) fee = this.web3.utils.fromWei(result)
} catch (e) { } 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 return fee
} }
@ -428,7 +433,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getNormalizedWeight(token).call() const result = await pool.methods.getNormalizedWeight(token).call()
weight = this.web3.utils.fromWei(result) weight = this.web3.utils.fromWei(result)
} catch (e) { } 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 return weight
} }
@ -446,7 +451,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getDenormalizedWeight(token).call() const result = await pool.methods.getDenormalizedWeight(token).call()
weight = this.web3.utils.fromWei(result) weight = this.web3.utils.fromWei(result)
} catch (e) { } 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 return weight
} }
@ -463,7 +468,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getTotalDenormalizedWeight().call() const result = await pool.methods.getTotalDenormalizedWeight().call()
weight = this.web3.utils.fromWei(result) weight = this.web3.utils.fromWei(result)
} catch (e) { } 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 return weight
} }
@ -503,7 +508,7 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -543,7 +548,7 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -580,7 +585,7 @@ export class Pool extends PoolFactory {
.joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn) .joinPool(this.web3.utils.toWei(poolAmountOut), weiMaxAmountsIn)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to join pool: ${e.message}`) this.logger.error(`ERROR: Failed to join pool: ${e.message}`)
} }
return result return result
} }
@ -614,7 +619,7 @@ export class Pool extends PoolFactory {
.exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut) .exitPool(this.web3.utils.toWei(poolAmountIn), weiMinAmountsOut)
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to exit pool: ${e.message}`) this.logger.error(`ERROR: Failed to exit pool: ${e.message}`)
} }
return result return result
} }
@ -648,7 +653,8 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -682,7 +688,7 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -716,7 +722,7 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } 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 return result
} }
@ -750,7 +756,7 @@ export class Pool extends PoolFactory {
) )
.send({ from: account, gas: this.GASLIMIT_DEFAULT }) .send({ from: account, gas: this.GASLIMIT_DEFAULT })
} catch (e) { } catch (e) {
console.error('ERROR: Failed to exitswapExternAmountOut') this.logger.error('ERROR: Failed to exitswapExternAmountOut')
} }
return result return result
} }
@ -773,7 +779,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getSpotPrice(tokenIn, tokenOut).call() const result = await pool.methods.getSpotPrice(tokenIn, tokenOut).call()
price = this.web3.utils.fromWei(result) price = this.web3.utils.fromWei(result)
} catch (e) { } 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 return price
} }
@ -796,7 +802,7 @@ export class Pool extends PoolFactory {
const result = await pool.methods.getSpotPriceSansFee(tokenIn, tokenOut).call() const result = await pool.methods.getSpotPriceSansFee(tokenIn, tokenOut).call()
price = this.web3.utils.fromWei(result) price = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error('ERROR: Failed to getSpotPriceSansFee') this.logger.error('ERROR: Failed to getSpotPriceSansFee')
} }
return price return price
} }
@ -826,7 +832,7 @@ export class Pool extends PoolFactory {
.call() .call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error('ERROR: Failed to calcInGivenOut') this.logger.error('ERROR: Failed to calcInGivenOut')
} }
return amount return amount
} }
@ -855,7 +861,7 @@ export class Pool extends PoolFactory {
.call() .call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error('ERROR: Failed to calcOutGivenIn') this.logger.error('ERROR: Failed to calcOutGivenIn')
} }
return amount return amount
} }
@ -884,7 +890,7 @@ export class Pool extends PoolFactory {
.call() .call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`) this.logger.error(`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`)
} }
return amount return amount
} }
@ -913,7 +919,7 @@ export class Pool extends PoolFactory {
.call() .call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`) this.logger.error(`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`)
} }
return amount return amount
} }
@ -942,7 +948,7 @@ export class Pool extends PoolFactory {
.call() .call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`) this.logger.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e.message}`)
} }
return amount return amount
} }
@ -971,7 +977,7 @@ export class Pool extends PoolFactory {
.call() .call()
amount = this.web3.utils.fromWei(result) amount = this.web3.utils.fromWei(result)
} catch (e) { } catch (e) {
console.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`) this.logger.error(`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`)
} }
return amount return amount
} }

View File

@ -54,6 +54,7 @@ export class Ocean extends Instantiable {
) )
instance.pool = new OceanPool( instance.pool = new OceanPool(
instanceConfig.config.web3Provider, instanceConfig.config.web3Provider,
instanceConfig.logger,
instanceConfig.config.poolFactoryABI, instanceConfig.config.poolFactoryABI,
instanceConfig.config.poolABI, instanceConfig.config.poolABI,
instanceConfig.config.poolFactoryAddress, instanceConfig.config.poolFactoryAddress,

View File

@ -12,6 +12,7 @@ import datatokensTemplate from '@oceanprotocol/contracts/artifacts/DataTokenTemp
// this will be replaced by our SFactory/SPool // this will be replaced by our SFactory/SPool
import OceanPoolFactory from '@oceanprotocol/contracts/artifacts/BFactory.json' import OceanPoolFactory from '@oceanprotocol/contracts/artifacts/BFactory.json'
import OceanSPool from '@oceanprotocol/contracts/artifacts/BPool.json' import OceanSPool from '@oceanprotocol/contracts/artifacts/BPool.json'
import { LoggerInstance } from '../../../src/utils'
const web3 = new Web3('http://127.0.0.1:8545') const web3 = new Web3('http://127.0.0.1:8545')
function sleep(ms: number) { function sleep(ms: number) {
@ -102,6 +103,7 @@ describe('Balancer flow', () => {
it('should initialize OceanPool class', async () => { it('should initialize OceanPool class', async () => {
Pool = new OceanPool( Pool = new OceanPool(
web3, web3,
LoggerInstance,
OceanPoolFactory.abi as AbiItem[], OceanPoolFactory.abi as AbiItem[],
OceanSPool.abi as AbiItem[], OceanSPool.abi as AbiItem[],
OceanPoolFactoryAddress, OceanPoolFactoryAddress,