Skip to content

Commit

Permalink
fix redundancy (#1278)
Browse files Browse the repository at this point in the history
* fix redundancy

* convert to wei fixed price

* fix test
  • Loading branch information
mihaisc authored Feb 11, 2022
1 parent 88c0b30 commit 6f12e28
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
35 changes: 5 additions & 30 deletions src/pools/fixedRate/FixedRateExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
LoggerInstance,
getFairGasPrice,
configHelperNetworks,
setContractDefaults
setContractDefaults,
amountToUnits,
unitsToAmount
} from '../../utils'
import { Config } from '../../models/index.js'
import { PriceAndFees } from '../..'
Expand Down Expand Up @@ -89,38 +91,11 @@ export class FixedRateExchange {
}

async amountToUnits(token: string, amount: string): Promise<string> {
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<string> {
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)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Expand Down
4 changes: 2 additions & 2 deletions test/unit/pools/fixedRate/FixedRateExchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6f12e28

Please sign in to comment.