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

Remove try...catch from Pool

This commit is contained in:
Miquel A. Cabot 2022-06-21 12:19:42 +02:00
parent 6bd2913fe8
commit a3a2c157df

View File

@ -35,14 +35,9 @@ export class Pool extends SmartContract {
* @return {String} * @return {String}
*/ */
async sharesBalance(account: string, poolAddress: string): Promise<string> { async sharesBalance(account: string, poolAddress: string): Promise<string> {
let shares = null const token = this.getContract(poolAddress)
try { const balance = await token.methods.balanceOf(account).call()
const token = this.getContract(poolAddress) const shares = this.web3.utils.fromWei(balance)
const balance = await token.methods.balanceOf(account).call()
shares = this.web3.utils.fromWei(balance)
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get shares of pool : ${e.message}`)
}
return shares return shares
} }
@ -59,19 +54,15 @@ export class Pool extends SmartContract {
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
const pool = this.getContract(poolAddress, account) const pool = this.getContract(poolAddress, account)
let trxReceipt = null
const estGas = await calculateEstimatedGas(account, pool.methods.setSwapFee, fee) const estGas = await calculateEstimatedGas(account, pool.methods.setSwapFee, fee)
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({
trxReceipt = await pool.methods.setSwapFee(this.web3.utils.toWei(fee)).send({ from: account,
from: account, gas: estGas,
gas: estGas, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to set pool swap fee: ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -82,12 +73,7 @@ export class Pool extends SmartContract {
*/ */
async getNumTokens(poolAddress: string): Promise<string> { async getNumTokens(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let tokens = null const tokens = await pool.methods.getNumTokens().call()
try {
tokens = await pool.methods.getNumTokens().call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get number of tokens: ${e.message}`)
}
return tokens return tokens
} }
@ -98,15 +84,8 @@ export class Pool extends SmartContract {
*/ */
async getPoolSharesTotalSupply(poolAddress: string): Promise<string> { async getPoolSharesTotalSupply(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let amount = null const supply = await pool.methods.totalSupply().call()
try { const amount = this.web3.utils.fromWei(supply)
const supply = await pool.methods.totalSupply().call()
amount = this.web3.utils.fromWei(supply)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get total supply of pool shares: ${e.message}`
)
}
return amount return amount
} }
@ -118,14 +97,7 @@ export class Pool extends SmartContract {
*/ */
async getCurrentTokens(poolAddress: string): Promise<string[]> { async getCurrentTokens(poolAddress: string): Promise<string[]> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let tokens = null const tokens = await pool.methods.getCurrentTokens().call()
try {
tokens = await pool.methods.getCurrentTokens().call()
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get tokens composing this pool: ${e.message}`
)
}
return tokens return tokens
} }
@ -137,14 +109,7 @@ export class Pool extends SmartContract {
*/ */
async getFinalTokens(poolAddress: string): Promise<string[]> { async getFinalTokens(poolAddress: string): Promise<string[]> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let tokens = null const tokens = await pool.methods.getFinalTokens().call()
try {
tokens = await pool.methods.getFinalTokens().call()
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get the final tokens composing this pool ${e.message}`
)
}
return tokens return tokens
} }
@ -155,12 +120,7 @@ export class Pool extends SmartContract {
*/ */
async getController(poolAddress: string): Promise<string> { async getController(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let address = null const address = await pool.methods.getController().call()
try {
address = await pool.methods.getController().call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get pool controller address: ${e.message}`)
}
return address return address
} }
@ -171,12 +131,7 @@ export class Pool extends SmartContract {
*/ */
async getBasetoken(poolAddress: string): Promise<string> { async getBasetoken(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let address = null const address = await pool.methods.getBaseTokenAddress().call()
try {
address = await pool.methods.getBaseTokenAddress().call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get baseToken address: ${e.message}`)
}
return address return address
} }
@ -187,12 +142,7 @@ export class Pool extends SmartContract {
*/ */
async getDatatoken(poolAddress: string): Promise<string> { async getDatatoken(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let address = null const address = await pool.methods.getDatatokenAddress().call()
try {
address = await pool.methods.getDatatokenAddress().call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get datatoken address: ${e.message}`)
}
return address return address
} }
@ -203,12 +153,7 @@ export class Pool extends SmartContract {
*/ */
async getMarketFee(poolAddress: string): Promise<string> { async getMarketFee(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let fee = null const fee = await pool.methods.getMarketFee().call()
try {
fee = await pool.methods.getMarketFee().call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get getMarketFee: ${e.message}`)
}
return this.web3.utils.fromWei(fee).toString() return this.web3.utils.fromWei(fee).toString()
} }
@ -219,14 +164,7 @@ export class Pool extends SmartContract {
*/ */
async getMarketFeeCollector(poolAddress: string): Promise<string> { async getMarketFeeCollector(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let address = null const address = await pool.methods._publishMarketCollector().call()
try {
address = await pool.methods._publishMarketCollector().call()
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get marketFeeCollector address: ${e.message}`
)
}
return address return address
} }
@ -239,13 +177,7 @@ export class Pool extends SmartContract {
*/ */
async isBound(poolAddress: string, token: string): Promise<boolean> { async isBound(poolAddress: string, token: string): Promise<boolean> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let isBound = null const isBound = await pool.methods.isBound(token).call()
try {
isBound = await pool.methods.isBound(token).call()
} catch (e) {
LoggerInstance.error(`ERROR: Failed to check whether a token \
bounded to a pool. ${e.message}`)
}
return isBound return isBound
} }
@ -261,15 +193,9 @@ export class Pool extends SmartContract {
token: string, token: string,
tokenDecimals?: number tokenDecimals?: number
): Promise<string> { ): Promise<string> {
let amount = null const pool = this.getContract(poolAddress)
try { const balance = await pool.methods.getBalance(token).call()
const pool = this.getContract(poolAddress) const amount = await this.unitsToAmount(token, balance, tokenDecimals)
const balance = await pool.methods.getBalance(token).call()
amount = await this.unitsToAmount(token, balance, tokenDecimals)
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get how many tokens \
are in the pool: ${e.message}`)
}
return amount.toString() return amount.toString()
} }
@ -281,14 +207,7 @@ export class Pool extends SmartContract {
*/ */
async isFinalized(poolAddress: string): Promise<boolean> { async isFinalized(poolAddress: string): Promise<boolean> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let isFinalized = null const isFinalized = await pool.methods.isFinalized().call()
try {
isFinalized = await pool.methods.isFinalized().call()
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to check whether pool is finalized: ${e.message}`
)
}
return isFinalized return isFinalized
} }
@ -299,13 +218,8 @@ export class Pool extends SmartContract {
*/ */
async getSwapFee(poolAddress: string): Promise<string> { async getSwapFee(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let fee = null const swapFee = await pool.methods.getSwapFee().call()
try { const fee = this.web3.utils.fromWei(swapFee)
const swapFee = await pool.methods.getSwapFee().call()
fee = this.web3.utils.fromWei(swapFee)
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get pool fee: ${e.message}`)
}
return fee return fee
} }
@ -319,15 +233,8 @@ export class Pool extends SmartContract {
*/ */
async getNormalizedWeight(poolAddress: string, token: string): Promise<string> { async getNormalizedWeight(poolAddress: string, token: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let weight = null const normalizedWeight = await pool.methods.getNormalizedWeight(token).call()
try { const weight = this.web3.utils.fromWei(normalizedWeight)
const normalizedWeight = await pool.methods.getNormalizedWeight(token).call()
weight = this.web3.utils.fromWei(normalizedWeight)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get normalized weight of a token: ${e.message}`
)
}
return weight return weight
} }
@ -339,15 +246,8 @@ export class Pool extends SmartContract {
*/ */
async getDenormalizedWeight(poolAddress: string, token: string): Promise<string> { async getDenormalizedWeight(poolAddress: string, token: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let weight = null const denormalizedWeight = await pool.methods.getDenormalizedWeight(token).call()
try { const weight = this.web3.utils.fromWei(denormalizedWeight)
const denormalizedWeight = await pool.methods.getDenormalizedWeight(token).call()
weight = this.web3.utils.fromWei(denormalizedWeight)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get denormalized weight of a token in pool ${e.message}`
)
}
return weight return weight
} }
@ -359,15 +259,8 @@ export class Pool extends SmartContract {
*/ */
async getTotalDenormalizedWeight(poolAddress: string): Promise<string> { async getTotalDenormalizedWeight(poolAddress: string): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let weight = null const denormalizedWeight = await pool.methods.getTotalDenormalizedWeight().call()
try { const weight = this.web3.utils.fromWei(denormalizedWeight)
const denormalizedWeight = await pool.methods.getTotalDenormalizedWeight().call()
weight = this.web3.utils.fromWei(denormalizedWeight)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get total denormalized weight in pool ${e.message}`
)
}
return weight return weight
} }
@ -385,13 +278,8 @@ export class Pool extends SmartContract {
tokenDecimals?: number tokenDecimals?: number
): Promise<string> { ): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let weight = null const fee = await pool.methods.publishMarketFees(token).call()
try { const weight = await this.unitsToAmount(token, fee, tokenDecimals)
const fee = await pool.methods.publishMarketFees(token).call()
weight = await this.unitsToAmount(token, fee, tokenDecimals)
} catch (e) {
LoggerInstance.error(`ERROR: Failed to get market fees for a token: ${e.message}`)
}
return weight return weight
} }
@ -401,14 +289,8 @@ export class Pool extends SmartContract {
*/ */
async getCurrentMarketFees(poolAddress: string): Promise<CurrentFees> { async getCurrentMarketFees(poolAddress: string): Promise<CurrentFees> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
try { const currentMarketFees = await pool.methods.getCurrentOPCFees().call()
const currentMarketFees = await pool.methods.getCurrentOPCFees().call() return currentMarketFees
return currentMarketFees
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get community fees for a token: ${e.message}`
)
}
} }
/** /**
@ -417,14 +299,8 @@ export class Pool extends SmartContract {
*/ */
async getCurrentOPCFees(poolAddress: string): Promise<CurrentFees> { async getCurrentOPCFees(poolAddress: string): Promise<CurrentFees> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
try { const currentMarketFees = await pool.methods.getCurrentOPCFees().call()
const currentMarketFees = await pool.methods.getCurrentOPCFees().call() return currentMarketFees
return currentMarketFees
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get community fees for a token: ${e.message}`
)
}
} }
/** /**
@ -440,15 +316,8 @@ export class Pool extends SmartContract {
tokenDecimals?: number tokenDecimals?: number
): Promise<string> { ): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let weight = null const fee = await pool.methods.communityFees(token).call()
try { const weight = await this.unitsToAmount(token, fee, tokenDecimals)
const fee = await pool.methods.communityFees(token).call()
weight = await this.unitsToAmount(token, fee, tokenDecimals)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to get community fees for a token: ${e.message}`
)
}
return weight return weight
} }
@ -464,19 +333,15 @@ export class Pool extends SmartContract {
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let trxReceipt = null
const estGas = await calculateEstimatedGas(address, pool.methods.collectOPC) const estGas = await calculateEstimatedGas(address, pool.methods.collectOPC)
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods.collectOPC().send({
trxReceipt = await pool.methods.collectOPC().send({ from: address,
from: address, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to swap exact amount in : ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -496,19 +361,15 @@ export class Pool extends SmartContract {
throw new Error(`Caller is not MarketFeeCollector`) throw new Error(`Caller is not MarketFeeCollector`)
} }
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let trxReceipt = null
const estGas = await calculateEstimatedGas(address, pool.methods.collectMarketFee) const estGas = await calculateEstimatedGas(address, pool.methods.collectMarketFee)
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods.collectMarketFee().send({
trxReceipt = await pool.methods.collectMarketFee().send({ from: address,
from: address, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to swap exact amount in : ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -531,7 +392,6 @@ export class Pool extends SmartContract {
throw new Error(`Caller is not MarketFeeCollector`) throw new Error(`Caller is not MarketFeeCollector`)
} }
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let trxReceipt = null
const estGas = await calculateEstimatedGas( const estGas = await calculateEstimatedGas(
address, address,
@ -541,20 +401,16 @@ export class Pool extends SmartContract {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods
trxReceipt = await pool.methods .updatePublishMarketFee(
.updatePublishMarketFee( newPublishMarketAddress,
newPublishMarketAddress, this.web3.utils.toWei(newPublishMarketSwapFee)
this.web3.utils.toWei(newPublishMarketSwapFee) )
) .send({
.send({ from: address,
from: address, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to updatePublishMarketFee : ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -621,30 +477,25 @@ export class Pool extends SmartContract {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
let trxReceipt = null const trxReceipt = await pool.methods
try { .swapExactAmountIn(
trxReceipt = await pool.methods [
.swapExactAmountIn( tokenInOutMarket.tokenIn,
[ tokenInOutMarket.tokenOut,
tokenInOutMarket.tokenIn, tokenInOutMarket.marketFeeAddress
tokenInOutMarket.tokenOut, ],
tokenInOutMarket.marketFeeAddress [
], tokenAmountIn,
[ minAmountOut,
tokenAmountIn, maxPrice,
minAmountOut, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
maxPrice, ]
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) )
] .send({
) from: address,
.send({ gas: estGas + 1,
from: address, gasPrice: await this.getFairGasPrice()
gas: estGas + 1, })
gasPrice: await this.getFairGasPrice()
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to swap exact amount in : ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -665,7 +516,6 @@ export class Pool extends SmartContract {
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let trxReceipt = null
const maxSwap = await this.getMaxSwapExactOut(poolAddress, tokenInOutMarket.tokenOut) const maxSwap = await this.getMaxSwapExactOut(poolAddress, tokenInOutMarket.tokenOut)
if (new Decimal(amountsInOutMaxFee.tokenAmountOut).greaterThan(maxSwap)) { if (new Decimal(amountsInOutMaxFee.tokenAmountOut).greaterThan(maxSwap)) {
@ -708,29 +558,25 @@ export class Pool extends SmartContract {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods
trxReceipt = await pool.methods .swapExactAmountOut(
.swapExactAmountOut( [
[ tokenInOutMarket.tokenIn,
tokenInOutMarket.tokenIn, tokenInOutMarket.tokenOut,
tokenInOutMarket.tokenOut, tokenInOutMarket.marketFeeAddress
tokenInOutMarket.marketFeeAddress ],
], [
[ maxAmountIn,
maxAmountIn, tokenAmountOut,
tokenAmountOut, maxPrice,
maxPrice, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee)
this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) ]
] )
) .send({
.send({ from: account,
from: account, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to swap exact amount out: ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -754,7 +600,7 @@ export class Pool extends SmartContract {
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let trxReceipt = null
const tokenIn = await this.getBasetoken(poolAddress) const tokenIn = await this.getBasetoken(poolAddress)
const maxSwap = await this.getMaxAddLiquidity(poolAddress, tokenIn) const maxSwap = await this.getMaxAddLiquidity(poolAddress, tokenIn)
if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) { if (new Decimal(tokenAmountIn).greaterThan(maxSwap)) {
@ -774,21 +620,13 @@ export class Pool extends SmartContract {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods
trxReceipt = await pool.methods .joinswapExternAmountIn(amountInFormatted, this.web3.utils.toWei(minPoolAmountOut))
.joinswapExternAmountIn( .send({
amountInFormatted, from: account,
this.web3.utils.toWei(minPoolAmountOut) gas: estGas + 1,
) gasPrice: await this.getFairGasPrice()
.send({ })
from: account,
gas: estGas + 1,
gasPrice: await this.getFairGasPrice()
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to pay tokens in order to \
join the pool: ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -812,7 +650,7 @@ export class Pool extends SmartContract {
estimateGas?: G estimateGas?: G
): Promise<G extends false ? TransactionReceipt : number> { ): Promise<G extends false ? TransactionReceipt : number> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let trxReceipt = null
const tokenOut = await this.getBasetoken(poolAddress) const tokenOut = await this.getBasetoken(poolAddress)
const tokenAmountOut = await this.calcSingleOutGivenPoolIn( const tokenAmountOut = await this.calcSingleOutGivenPoolIn(
@ -839,17 +677,13 @@ export class Pool extends SmartContract {
) )
if (estimateGas) return estGas if (estimateGas) return estGas
try { const trxReceipt = await pool.methods
trxReceipt = await pool.methods .exitswapPoolAmountIn(this.web3.utils.toWei(poolAmountIn), minTokenOutFormatted)
.exitswapPoolAmountIn(this.web3.utils.toWei(poolAmountIn), minTokenOutFormatted) .send({
.send({ from: account,
from: account, gas: estGas + 1,
gas: estGas + 1, gasPrice: await this.getFairGasPrice()
gasPrice: await this.getFairGasPrice() })
})
} catch (e) {
LoggerInstance.error(`ERROR: Failed to pay pool shares into the pool: ${e.message}`)
}
return trxReceipt return trxReceipt
} }
@ -882,17 +716,10 @@ export class Pool extends SmartContract {
LoggerInstance.error(`ERROR: FAILED TO CALL DECIMALS(), USING 18 ${e.message}`) LoggerInstance.error(`ERROR: FAILED TO CALL DECIMALS(), USING 18 ${e.message}`)
} }
let price = null let price = await pool.methods
try { .getSpotPrice(tokenIn, tokenOut, this.web3.utils.toWei(swapMarketFee))
price = await pool.methods .call()
.getSpotPrice(tokenIn, tokenOut, this.web3.utils.toWei(swapMarketFee)) price = new BigNumber(price.toString())
.call()
price = new BigNumber(price.toString())
} catch (e) {
LoggerInstance.error(
'ERROR: Failed to get spot price of swapping tokenIn to tokenOut'
)
}
let decimalsDiff let decimalsDiff
if (decimalsTokenIn > decimalsTokenOut) { if (decimalsTokenIn > decimalsTokenOut) {
@ -942,46 +769,40 @@ export class Pool extends SmartContract {
tokenOutDecimals tokenOutDecimals
) )
let amount = null const amountIn = await pool.methods
.getAmountInExactOut(
try { tokenIn,
const amountIn = await pool.methods tokenOut,
.getAmountInExactOut( amountOutFormatted,
tokenIn, this.web3.utils.toWei(swapMarketFee)
tokenOut, )
amountOutFormatted, .call()
this.web3.utils.toWei(swapMarketFee) const amount = {
) tokenAmount: await this.unitsToAmount(
.call() tokenOut,
amount = { amountIn.tokenAmountIn,
tokenAmount: await this.unitsToAmount( tokenOutDecimals
tokenOut, ),
amountIn.tokenAmountIn, liquidityProviderSwapFeeAmount: await this.unitsToAmount(
tokenOutDecimals tokenIn,
), amountIn.lpFeeAmount,
liquidityProviderSwapFeeAmount: await this.unitsToAmount( tokenInDecimals
tokenIn, ),
amountIn.lpFeeAmount, oceanFeeAmount: await this.unitsToAmount(
tokenInDecimals tokenIn,
), amountIn.oceanFeeAmount,
oceanFeeAmount: await this.unitsToAmount( tokenInDecimals
tokenIn, ),
amountIn.oceanFeeAmount, publishMarketSwapFeeAmount: await this.unitsToAmount(
tokenInDecimals tokenIn,
), amountIn.publishMarketSwapFeeAmount,
publishMarketSwapFeeAmount: await this.unitsToAmount( tokenInDecimals
tokenIn, ),
amountIn.publishMarketSwapFeeAmount, consumeMarketSwapFeeAmount: await this.unitsToAmount(
tokenInDecimals tokenIn,
), amountIn.consumeMarketSwapFeeAmount,
consumeMarketSwapFeeAmount: await this.unitsToAmount( tokenInDecimals
tokenIn, )
amountIn.consumeMarketSwapFeeAmount,
tokenInDecimals
)
}
} catch (e) {
LoggerInstance.error(`ERROR: Failed to calcInGivenOut ${e.message}`)
} }
return amount return amount
} }
@ -1018,47 +839,41 @@ export class Pool extends SmartContract {
tokenInDecimals tokenInDecimals
) )
let amount = null const amountOut = await pool.methods
.getAmountOutExactIn(
tokenIn,
tokenOut,
amountInFormatted,
this.web3.utils.toWei(swapMarketFee)
)
.call()
try { const amount = {
const amountOut = await pool.methods tokenAmount: await this.unitsToAmount(
.getAmountOutExactIn( tokenOut,
tokenIn, amountOut.tokenAmountOut,
tokenOut, tokenOutDecimals
amountInFormatted, ),
this.web3.utils.toWei(swapMarketFee) liquidityProviderSwapFeeAmount: await this.unitsToAmount(
) tokenIn,
.call() amountOut.lpFeeAmount,
tokenInDecimals
amount = { ),
tokenAmount: await this.unitsToAmount( oceanFeeAmount: await this.unitsToAmount(
tokenOut, tokenIn,
amountOut.tokenAmountOut, amountOut.oceanFeeAmount,
tokenOutDecimals tokenInDecimals
), ),
liquidityProviderSwapFeeAmount: await this.unitsToAmount( publishMarketSwapFeeAmount: await this.unitsToAmount(
tokenIn, tokenIn,
amountOut.lpFeeAmount, amountOut.publishMarketSwapFeeAmount,
tokenInDecimals tokenInDecimals
), ),
oceanFeeAmount: await this.unitsToAmount( consumeMarketSwapFeeAmount: await this.unitsToAmount(
tokenIn, tokenIn,
amountOut.oceanFeeAmount, amountOut.consumeMarketSwapFeeAmount,
tokenInDecimals tokenInDecimals
), )
publishMarketSwapFeeAmount: await this.unitsToAmount(
tokenIn,
amountOut.publishMarketSwapFeeAmount,
tokenInDecimals
),
consumeMarketSwapFeeAmount: await this.unitsToAmount(
tokenIn,
amountOut.consumeMarketSwapFeeAmount,
tokenInDecimals
)
}
} catch (e) {
LoggerInstance.error(`ERROR: Failed to calcOutGivenIn ${e.message}`)
} }
return amount return amount
} }
@ -1078,22 +893,15 @@ export class Pool extends SmartContract {
tokenInDecimals?: number tokenInDecimals?: number
): Promise<string> { ): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let amount = null
try { const poolOut = await pool.methods
const poolOut = await pool.methods .calcPoolOutSingleIn(
.calcPoolOutSingleIn( tokenIn,
tokenIn, await this.amountToUnits(tokenIn, tokenAmountIn, tokenInDecimals)
await this.amountToUnits(tokenIn, tokenAmountIn, tokenInDecimals)
)
.call()
amount = await this.unitsToAmount(poolAddress, poolOut, poolDecimals)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to calculate PoolOutGivenSingleIn : ${e.message}`
) )
} .call()
const amount = await this.unitsToAmount(poolAddress, poolOut, poolDecimals)
return amount return amount
} }
@ -1112,23 +920,17 @@ export class Pool extends SmartContract {
tokenInDecimals?: number tokenInDecimals?: number
): Promise<string> { ): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let amount = null
const amountFormatted = await this.amountToUnits( const amountFormatted = await this.amountToUnits(
poolAddress, poolAddress,
poolAmountOut, poolAmountOut,
poolDecimals poolDecimals
) )
try { const singleIn = await pool.methods
const singleIn = await pool.methods .calcSingleInPoolOut(tokenIn, amountFormatted)
.calcSingleInPoolOut(tokenIn, amountFormatted) .call()
.call()
amount = await this.unitsToAmount(tokenIn, singleIn, tokenInDecimals) const amount = await this.unitsToAmount(tokenIn, singleIn, tokenInDecimals)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to calculate SingleInGivenPoolOut : ${e.message}`
)
}
return amount return amount
} }
@ -1147,19 +949,14 @@ export class Pool extends SmartContract {
tokenOutDecimals?: number tokenOutDecimals?: number
): Promise<string> { ): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let amount = null
try { const singleOut = await pool.methods
const singleOut = await pool.methods .calcSingleOutPoolIn(
.calcSingleOutPoolIn( tokenOut,
tokenOut, await this.amountToUnits(poolAddress, poolAmountIn, poolDecimals)
await this.amountToUnits(poolAddress, poolAmountIn, poolDecimals) )
) .call()
.call() const amount = await this.unitsToAmount(tokenOut, singleOut, tokenOutDecimals)
amount = await this.unitsToAmount(tokenOut, singleOut, tokenOutDecimals)
} catch (e) {
LoggerInstance.error(`ERROR: Failed to calculate SingleOutGivenPoolIn : ${e}`)
}
return amount return amount
} }
@ -1178,22 +975,15 @@ export class Pool extends SmartContract {
tokenOutDecimals?: number tokenOutDecimals?: number
): Promise<string> { ): Promise<string> {
const pool = this.getContract(poolAddress) const pool = this.getContract(poolAddress)
let amount = null
try { const poolIn = await pool.methods
const poolIn = await pool.methods .calcPoolInSingleOut(
.calcPoolInSingleOut( tokenOut,
tokenOut, await this.amountToUnits(tokenOut, tokenAmountOut, tokenOutDecimals)
await this.amountToUnits(tokenOut, tokenAmountOut, tokenOutDecimals)
)
.call()
amount = await this.unitsToAmount(poolAddress, poolIn, poolDecimals)
} catch (e) {
LoggerInstance.error(
`ERROR: Failed to calculate PoolInGivenSingleOut : ${e.message}`
) )
} .call()
const amount = await this.unitsToAmount(poolAddress, poolIn, poolDecimals)
return amount return amount
} }