Skip to content

Commit

Permalink
Remove redundant get in calcBaseInGivenOutDT (#1283)
Browse files Browse the repository at this point in the history
* fix

* fix unitsToAmount

* fix
  • Loading branch information
mihaisc authored Feb 14, 2022
1 parent 9782af5 commit 3845335
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
24 changes: 6 additions & 18 deletions src/pools/fixedRate/FixedRateExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -539,41 +539,29 @@ export class FixedRateExchange {
exchangeId: string,
datatokenAmount: string
): Promise<PriceAndFees> {
const fixedRateExchange = await this.getExchange(exchangeId)
const result = await this.contract.methods
.calcBaseInGivenOutDT(
exchangeId,
await this.amountToUnits(
(
await this.getExchange(exchangeId)
).datatoken,
datatokenAmount
)
await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount)
)
.call()

const priceAndFees = {
baseTokenAmount: await this.unitsToAmount(
(
await this.getExchange(exchangeId)
).baseToken,
fixedRateExchange.baseToken,
result.baseTokenAmount
),
baseTokenAmountBeforeFee: await this.unitsToAmount(
(
await this.getExchange(exchangeId)
).baseToken,
fixedRateExchange.baseToken,
result.baseTokenAmountBeforeFee
),
marketFeeAmount: await this.unitsToAmount(
(
await this.getExchange(exchangeId)
).baseToken,
fixedRateExchange.baseToken,
result.marketFeeAmount
),
oceanFeeAmount: await this.unitsToAmount(
(
await this.getExchange(exchangeId)
).baseToken,
fixedRateExchange.baseToken,
result.oceanFeeAmount
)
} as PriceAndFees
Expand Down
12 changes: 10 additions & 2 deletions src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,11 @@ export async function unitsToAmount(
if (decimals === '0') {
decimals = 18
}
const amountFormatted = new BigNumber(parseInt(amount) / 10 ** decimals)

const amountFormatted = new BigNumber(amount).div(
new BigNumber(10).exponentiatedBy(decimals)
)

BigNumber.config({ EXPONENTIAL_AT: 50 })
return amountFormatted.toString()
} catch (e) {
Expand All @@ -140,8 +144,12 @@ export async function amountToUnits(
if (decimals === '0') {
decimals = 18
}
const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
BigNumber.config({ EXPONENTIAL_AT: 50 })

const amountFormatted = new BigNumber(amount).times(
new BigNumber(10).exponentiatedBy(decimals)
)

return amountFormatted.toString()
} catch (e) {
LoggerInstance.error(`ERROR: FAILED TO CALL DECIMALS(), USING 18', ${e.message}`)
Expand Down

0 comments on commit 3845335

Please sign in to comment.