From 237e9481078f28e23295cc19d97d38c7440bdfad Mon Sep 17 00:00:00 2001 From: mihaisc Date: Fri, 24 Jun 2022 07:22:18 -0700 Subject: [PATCH] Proper conversion to wei of base token when ordering (#1527) * proper conversion to wei Signed-off-by: mihaisc * fix conversion in pool Signed-off-by: mihaisc * fix test Signed-off-by: mihaisc * fix lint Signed-off-by: mihaisc --- src/@types/FixedPrice.ts | 2 ++ src/pools/balancer/Pool.ts | 2 +- src/tokens/Datatoken.ts | 2 +- src/utils/ContractUtils.ts | 19 ++++++++++++++++--- test/unit/tokens/Datatoken.test.ts | 2 ++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/@types/FixedPrice.ts b/src/@types/FixedPrice.ts index 3fe6ea439..ebb65e350 100644 --- a/src/@types/FixedPrice.ts +++ b/src/@types/FixedPrice.ts @@ -15,6 +15,8 @@ export interface FreOrderParams { exchangeContract: string exchangeId: string maxBaseTokenAmount: string + baseTokenAddress: string + baseTokenDecimals?: number swapMarketFee: string marketFeeAddress: string } diff --git a/src/pools/balancer/Pool.ts b/src/pools/balancer/Pool.ts index 74dfe0193..a3d7b6e6c 100644 --- a/src/pools/balancer/Pool.ts +++ b/src/pools/balancer/Pool.ts @@ -1363,7 +1363,7 @@ export class Pool { tokenAmount: await this.unitsToAmount( tokenOut, result.tokenAmountIn, - tokenOutDecimals + tokenInDecimals ), liquidityProviderSwapFeeAmount: await this.unitsToAmount( tokenIn, diff --git a/src/tokens/Datatoken.ts b/src/tokens/Datatoken.ts index 5c155d5da..f94beac21 100644 --- a/src/tokens/Datatoken.ts +++ b/src/tokens/Datatoken.ts @@ -1037,7 +1037,7 @@ export class Datatoken { ): Promise { const dtContract = new this.web3.eth.Contract(this.datatokensEnterpriseAbi, dtAddress) try { - const freContractParams = getFreOrderParams(freParams) + const freContractParams = await getFreOrderParams(this.web3, freParams) const estGas = await estimateGas( address, diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 94012c761..e4bb3c0d2 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -55,12 +55,25 @@ export function getErcCreationParams(ercParams: Erc20CreateParams): any { } } -export function getFreOrderParams(freParams: FreOrderParams): any { +export async function getFreOrderParams( + web3: Web3, + freParams: FreOrderParams +): Promise { return { exchangeContract: freParams.exchangeContract, exchangeId: freParams.exchangeId, - maxBaseTokenAmount: Web3.utils.toWei(freParams.maxBaseTokenAmount), - swapMarketFee: Web3.utils.toWei(freParams.swapMarketFee), + maxBaseTokenAmount: await amountToUnits( + web3, + freParams.baseTokenAddress, + freParams.maxBaseTokenAmount, + freParams.baseTokenDecimals + ), + swapMarketFee: await amountToUnits( + web3, + freParams.baseTokenAddress, + freParams.swapMarketFee, + freParams.baseTokenDecimals + ), marketFeeAddress: freParams.marketFeeAddress } } diff --git a/test/unit/tokens/Datatoken.test.ts b/test/unit/tokens/Datatoken.test.ts index 226b87da5..e862e5502 100644 --- a/test/unit/tokens/Datatoken.test.ts +++ b/test/unit/tokens/Datatoken.test.ts @@ -486,6 +486,8 @@ describe('Datatoken', () => { exchangeContract: fixedRateAddress, exchangeId: exchangeId, maxBaseTokenAmount: '1', + baseTokenAddress: contracts.daiAddress, + baseTokenDecimals: 18, swapMarketFee: '0.1', marketFeeAddress: ZERO_ADDRESS }