diff --git a/src/factories/NFTFactory.ts b/src/factories/NFTFactory.ts index 9d9365e6..5af4822f 100644 --- a/src/factories/NFTFactory.ts +++ b/src/factories/NFTFactory.ts @@ -705,7 +705,7 @@ export class NftFactory { let estGas try { const ercCreateData = getErcCreationParams(ercParams) - const poolData = getPoolCreationParams(poolParams) + const poolData = await getPoolCreationParams(this.web3, poolParams) estGas = await this.factory721.methods .createNftWithErc20WithPool(nftCreateData, ercCreateData, poolData) .estimateGas({ from: address }, (err, estGas) => (err ? gasLimitDefault : estGas)) @@ -738,7 +738,7 @@ export class NftFactory { poolParams ) const ercCreateData = getErcCreationParams(ercParams) - const poolData = getPoolCreationParams(poolParams) + const poolData = await getPoolCreationParams(this.web3, poolParams) // Invoke createToken function of the contract const trxReceipt = await this.factory721.methods @@ -769,8 +769,7 @@ export class NftFactory { let estGas const ercCreateData = getErcCreationParams(ercParams) - - const fixedData = getFreCreationParams(freParams) + const fixedData = await getFreCreationParams(freParams) try { estGas = await this.factory721.methods diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index b8161d54..3a936956 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -802,8 +802,24 @@ export class Pool { this.config ) + const tokenAmountIn = await amountToUnits( + this.web3, + tokenInOutMarket.tokenIn, + amountsInOutMaxFee.tokenAmountIn + ) + + const minAmountOut = await amountToUnits( + this.web3, + tokenInOutMarket.tokenOut, + amountsInOutMaxFee.minAmountOut + ) + const maxPrice = amountsInOutMaxFee.maxPrice - ? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice) + ? amountToUnits( + this.web3, + await this.getBaseToken(poolAddress), + amountsInOutMaxFee.maxPrice + ) : MaxUint256 const gasLimitDefault = this.GASLIMIT_DEFAULT @@ -817,8 +833,8 @@ export class Pool { tokenInOutMarket.marketFeeAddress ], [ - amountsInOutMaxFee.tokenAmountIn, - amountsInOutMaxFee.minAmountOut, + tokenAmountIn, + minAmountOut, maxPrice, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) ] @@ -859,20 +875,6 @@ export class Pool { throw new Error(`tokenAmountIn is greater than ${maxSwap.toString()}`) } - amountsInOutMaxFee.tokenAmountIn = await amountToUnits( - this.web3, - tokenInOutMarket.tokenIn, - amountsInOutMaxFee.tokenAmountIn - ) - - amountsInOutMaxFee.minAmountOut = await amountToUnits( - this.web3, - tokenInOutMarket.tokenOut, - amountsInOutMaxFee.minAmountOut - ) - - let result = null - const estGas = await this.estSwapExactAmountIn( address, poolAddress, @@ -880,8 +882,26 @@ export class Pool { amountsInOutMaxFee ) + const tokenAmountIn = await amountToUnits( + this.web3, + tokenInOutMarket.tokenIn, + amountsInOutMaxFee.tokenAmountIn + ) + + const minAmountOut = await amountToUnits( + this.web3, + tokenInOutMarket.tokenOut, + amountsInOutMaxFee.minAmountOut + ) + + let result = null + const maxPrice = amountsInOutMaxFee.maxPrice - ? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice) + ? await amountToUnits( + this.web3, + await this.getBaseToken(poolAddress), + amountsInOutMaxFee.maxPrice + ) : MaxUint256 try { @@ -893,8 +913,8 @@ export class Pool { tokenInOutMarket.marketFeeAddress ], [ - amountsInOutMaxFee.tokenAmountIn, - amountsInOutMaxFee.minAmountOut, + tokenAmountIn, + minAmountOut, maxPrice, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) ] @@ -936,8 +956,24 @@ export class Pool { const gasLimitDefault = this.GASLIMIT_DEFAULT + const maxAmountIn = await amountToUnits( + this.web3, + tokenInOutMarket.tokenIn, + amountsInOutMaxFee.maxAmountIn + ) + + const tokenAmountOut = await amountToUnits( + this.web3, + tokenInOutMarket.tokenOut, + amountsInOutMaxFee.tokenAmountOut + ) + const maxPrice = amountsInOutMaxFee.maxPrice - ? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice) + ? await amountToUnits( + this.web3, + await this.getBaseToken(poolAddress), + amountsInOutMaxFee.maxPrice + ) : MaxUint256 let estGas @@ -950,8 +986,8 @@ export class Pool { tokenInOutMarket.marketFeeAddress ], [ - amountsInOutMaxFee.maxAmountIn, - amountsInOutMaxFee.tokenAmountOut, + maxAmountIn, + tokenAmountOut, maxPrice, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) ] @@ -988,18 +1024,6 @@ export class Pool { throw new Error(`tokenAmountOut is greater than ${maxSwap.toString()}`) } - amountsInOutMaxFee.maxAmountIn = await amountToUnits( - this.web3, - tokenInOutMarket.tokenIn, - amountsInOutMaxFee.maxAmountIn - ) - - amountsInOutMaxFee.tokenAmountOut = await amountToUnits( - this.web3, - tokenInOutMarket.tokenOut, - amountsInOutMaxFee.tokenAmountOut - ) - const estGas = await this.estSwapExactAmountOut( account, poolAddress, @@ -1007,8 +1031,24 @@ export class Pool { amountsInOutMaxFee ) + const maxAmountIn = await amountToUnits( + this.web3, + tokenInOutMarket.tokenIn, + amountsInOutMaxFee.maxAmountIn + ) + + const tokenAmountOut = await amountToUnits( + this.web3, + tokenInOutMarket.tokenOut, + amountsInOutMaxFee.tokenAmountOut + ) + const maxPrice = amountsInOutMaxFee.maxPrice - ? this.web3.utils.toWei(amountsInOutMaxFee.maxPrice) + ? amountToUnits( + this.web3, + await this.getBaseToken(poolAddress), + amountsInOutMaxFee.maxPrice + ) : MaxUint256 try { @@ -1020,8 +1060,8 @@ export class Pool { tokenInOutMarket.marketFeeAddress ], [ - amountsInOutMaxFee.maxAmountIn, - amountsInOutMaxFee.tokenAmountOut, + maxAmountIn, + tokenAmountOut, maxPrice, this.web3.utils.toWei(amountsInOutMaxFee.swapMarketFee) ] diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index 3c6f87ac..3883ec96 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -171,17 +171,17 @@ export class FixedRateExchange { consumeMarketAddress: string = '0x0000000000000000000000000000000000000000', consumeMarketFee: string = '0' ): Promise { - const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee) + const exchange = await this.getExchange(exchangeId) + const consumeMarketFeeFormatted = await this.amountToUnits( + exchange.baseToken, + consumeMarketFee + ) const dtAmountFormatted = await this.amountToUnits( - ( - await this.getExchange(exchangeId) - ).datatoken, + exchange.datatoken, datatokenAmount ) const maxBtFormatted = await this.amountToUnits( - ( - await this.getExchange(exchangeId) - ).baseToken, + exchange.baseToken, maxBaseTokenAmount ) @@ -270,17 +270,17 @@ export class FixedRateExchange { consumeMarketAddress: string = '0x0000000000000000000000000000000000000000', consumeMarketFee: string = '0' ): Promise { - const consumeMarketFeeFormatted = await this.web3.utils.toWei(consumeMarketFee) + const exchange = await this.getExchange(exchangeId) + const consumeMarketFeeFormatted = await this.amountToUnits( + exchange.baseToken, + consumeMarketFee + ) const dtAmountFormatted = await this.amountToUnits( - ( - await this.getExchange(exchangeId) - ).datatoken, + exchange.datatoken, datatokenAmount ) const minBtFormatted = await this.amountToUnits( - ( - await this.getExchange(exchangeId) - ).baseToken, + exchange.baseToken, minBaseTokenAmount ) const estGas = await this.estBuyDT( @@ -327,7 +327,7 @@ export class FixedRateExchange { * Estimate gas cost for setRate * @param {String} account * @param {String} exchangeId ExchangeId - * @param {Number} newRate New rate + * @param {String} newRate New rate * @param {Contract} contractInstance optional contract instance * @return {Promise} */ @@ -342,7 +342,7 @@ export class FixedRateExchange { let estGas try { estGas = await fixedRate.methods - .setRate(exchangeId, newRate) + .setRate(exchangeId, await this.web3.utils.toWei(newRate)) .estimateGas({ from: account }, (err, estGas) => (err ? gasLimitDefault : estGas)) } catch (e) { estGas = gasLimitDefault @@ -353,7 +353,7 @@ export class FixedRateExchange { /** * Set new rate * @param {String} exchangeId ExchangeId - * @param {Number} newRate New rate + * @param {String} newRate New rate * @param {String} address User account * @return {Promise} transaction receipt */ @@ -362,11 +362,7 @@ export class FixedRateExchange { exchangeId: string, newRate: string ): Promise { - const estGas = await this.estSetRate( - address, - exchangeId, - this.web3.utils.toWei(String(newRate)) - ) + const estGas = await this.estSetRate(address, exchangeId, newRate) const trxReceipt = await this.contract.methods .setRate(exchangeId, this.web3.utils.toWei(newRate)) .send({ @@ -533,7 +529,8 @@ export class FixedRateExchange { */ public async getRate(exchangeId: string): Promise { const weiRate = await this.contract.methods.getRate(exchangeId).call() - return this.web3.utils.fromWei(weiRate) + const rate = await this.web3.utils.fromWei(weiRate) + return rate } /** @@ -592,7 +589,7 @@ export class FixedRateExchange { .calcBaseInGivenOutDT( exchangeId, await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount), - this.web3.utils.toWei(consumeMarketFee) + await this.amountToUnits(fixedRateExchange.baseToken, consumeMarketFee) ) .call() @@ -629,16 +626,12 @@ export class FixedRateExchange { datatokenAmount: string, consumeMarketFee: string = '0' ): Promise { + const exchange = await this.getExchange(exchangeId) const result = await this.contract.methods .calcBaseOutGivenInDT( exchangeId, - await this.amountToUnits( - ( - await this.getExchange(exchangeId) - ).datatoken, - datatokenAmount - ), - this.web3.utils.toWei(consumeMarketFee) + await this.amountToUnits(exchange.datatoken, datatokenAmount), + await this.amountToUnits(exchange.baseToken, consumeMarketFee) ) .call() diff --git a/src/provider/Provider.ts b/src/provider/Provider.ts index 283647e1..de2517fd 100644 --- a/src/provider/Provider.ts +++ b/src/provider/Provider.ts @@ -100,7 +100,7 @@ export class Provider { }, signal: signal }) - return String((await response.json()).nonce) + return (await response.json()).nonce.toString() } catch (e) { LoggerInstance.error(e) throw new Error('HTTP request failed') @@ -623,7 +623,7 @@ export class Provider { let signatureMessage = accountId signatureMessage += jobId - signatureMessage += String(index) + signatureMessage += index.toString() signatureMessage += nonce const signature = await this.createHashSignature(web3, accountId, signatureMessage) diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index cd00106e..20bbca70 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -87,7 +87,10 @@ export function getFreCreationParams(freParams: FreCreationParams): any { } } -export function getPoolCreationParams(poolParams: PoolCreationParams): any { +export async function getPoolCreationParams( + web3: Web3, + poolParams: PoolCreationParams +): Promise { return { addresses: [ poolParams.ssContract, @@ -102,7 +105,11 @@ export function getPoolCreationParams(poolParams: PoolCreationParams): any { poolParams.baseTokenDecimals, Web3.utils.toWei(poolParams.vestingAmount), poolParams.vestedBlocks, - Web3.utils.toWei(poolParams.initialBaseTokenLiquidity) + await amountToUnits( + web3, + poolParams.baseTokenAddress, + poolParams.initialBaseTokenLiquidity + ) ], swapFees: [ Web3.utils.toWei(poolParams.swapFeeLiquidityProvider), diff --git a/test/unit/pools/balancer/Pool.test.ts b/test/unit/pools/balancer/Pool.test.ts index e65e2cb3..0095cd74 100644 --- a/test/unit/pools/balancer/Pool.test.ts +++ b/test/unit/pools/balancer/Pool.test.ts @@ -12,7 +12,13 @@ import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispen import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { allowance, amountToUnits, approve, LoggerInstance } from '../../../../src/utils' +import { + allowance, + amountToUnits, + approve, + LoggerInstance, + unitsToAmount +} from '../../../../src/utils' import { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory' import { Pool } from '../../../../src/pools/balancer/Pool' import { @@ -606,7 +612,9 @@ describe('Pool unit test', () => { baseTokenDecimals: await usdcContract.methods.decimals().call(), vestingAmount: '10000', vestedBlocks: 2500000, - initialBaseTokenLiquidity: web3.utils.fromWei( + initialBaseTokenLiquidity: await unitsToAmount( + web3, + contracts.usdcAddress, await amountToUnits(web3, contracts.usdcAddress, '2000') ), swapFeeLiquidityProvider: '0.001', diff --git a/test/unit/pools/ssContracts/SideStaking.test.ts b/test/unit/pools/ssContracts/SideStaking.test.ts index 7865f4fa..b118299e 100644 --- a/test/unit/pools/ssContracts/SideStaking.test.ts +++ b/test/unit/pools/ssContracts/SideStaking.test.ts @@ -12,7 +12,13 @@ import Dispenser from '@oceanprotocol/contracts/artifacts/contracts/pools/dispen import FixedRate from '@oceanprotocol/contracts/artifacts/contracts/pools/fixedRate/FixedRateExchange.sol/FixedRateExchange.json' import PoolTemplate from '@oceanprotocol/contracts/artifacts/contracts/pools/balancer/BPool.sol/BPool.json' import OPFCollector from '@oceanprotocol/contracts/artifacts/contracts/communityFee/OPFCommunityFeeCollector.sol/OPFCommunityFeeCollector.json' -import { allowance, amountToUnits, approve, LoggerInstance } from '../../../../src/utils' +import { + allowance, + amountToUnits, + approve, + LoggerInstance, + unitsToAmount +} from '../../../../src/utils' import { NftFactory, NftCreateData } from '../../../../src/factories/NFTFactory' import { Pool } from '../../../../src/pools/balancer/Pool' import { SideStaking } from '../../../../src/pools/ssContracts/SideStaking' @@ -410,7 +416,9 @@ describe('SideStaking unit test', () => { baseTokenDecimals: await usdcContract.methods.decimals().call(), vestingAmount: '10000', vestedBlocks: 2500000, - initialBaseTokenLiquidity: web3.utils.fromWei( + initialBaseTokenLiquidity: await unitsToAmount( + web3, + contracts.usdcAddress, await amountToUnits(web3, contracts.usdcAddress, '2000') ), swapFeeLiquidityProvider: '0.001',