diff --git a/src/pools/fixedRate/FixedRateExchange.ts b/src/pools/fixedRate/FixedRateExchange.ts index a4ea7d08..dff8f585 100644 --- a/src/pools/fixedRate/FixedRateExchange.ts +++ b/src/pools/fixedRate/FixedRateExchange.ts @@ -9,7 +9,9 @@ import { LoggerInstance, getFairGasPrice, configHelperNetworks, - setContractDefaults + setContractDefaults, + amountToUnits, + unitsToAmount } from '../../utils' import { Config } from '../../models/index.js' import { PriceAndFees } from '../..' @@ -89,38 +91,11 @@ export class FixedRateExchange { } async amountToUnits(token: string, amount: string): Promise { - let decimals = 18 - const tokenContract = setContractDefaults( - new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], token), - this.config - ) - - try { - decimals = await tokenContract.methods.decimals().call() - } catch (e) { - LoggerInstance.error('ERROR: FAILED TO CALL DECIMALS(), USING 18') - } - - const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals) - - return amountFormatted.toString() + return amountToUnits(this.web3, token, amount) } async unitsToAmount(token: string, amount: string): Promise { - let decimals = 18 - const tokenContract = setContractDefaults( - new this.web3.eth.Contract(defaultErc20Abi.abi as AbiItem[], token), - this.config - ) - try { - decimals = await tokenContract.methods.decimals().call() - } catch (e) { - LoggerInstance.error('ERROR: FAILED TO CALL DECIMALS(), USING 18') - } - - const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals) - - return amountFormatted.toString() + return unitsToAmount(this.web3, token, amount) } /** diff --git a/src/utils/ContractUtils.ts b/src/utils/ContractUtils.ts index 5d2880be..3c37eece 100644 --- a/src/utils/ContractUtils.ts +++ b/src/utils/ContractUtils.ts @@ -80,7 +80,7 @@ export function getFreCreationParams(freParams: FreCreationParams): any { uints: [ freParams.baseTokenDecimals, freParams.datatokenDecimals, - freParams.fixedRate, + Web3.utils.toWei(freParams.fixedRate), Web3.utils.toWei(freParams.marketFee), withMint ] diff --git a/test/unit/pools/fixedRate/FixedRateExchange.test.ts b/test/unit/pools/fixedRate/FixedRateExchange.test.ts index e39811e6..6959e017 100644 --- a/test/unit/pools/fixedRate/FixedRateExchange.test.ts +++ b/test/unit/pools/fixedRate/FixedRateExchange.test.ts @@ -121,7 +121,7 @@ describe('Fixed Rate unit test', () => { marketFeeCollector: user3, baseTokenDecimals: 18, datatokenDecimals: 18, - fixedRate: web3.utils.toWei('1'), + fixedRate: '1', marketFee: '0.001', allowedConsumer: ADDRESS_ZERO, withMint: false @@ -453,7 +453,7 @@ describe('Fixed Rate unit test', () => { marketFeeCollector: user3, baseTokenDecimals: 6, datatokenDecimals: 18, - fixedRate: web3.utils.toWei('1'), + fixedRate: '1', marketFee: '0.001', allowedConsumer: ADDRESS_ZERO, withMint: false