1
0
mirror of https://github.com/oceanprotocol/ocean.js.git synced 2024-11-26 20:39:05 +01:00

Remove redundant get in calcBaseInGivenOutDT (#1283)

* fix

* fix unitsToAmount

* fix
This commit is contained in:
mihaisc 2022-02-14 13:34:46 +02:00 committed by GitHub
parent 9782af59a2
commit 3845335251
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 20 deletions

View File

@ -539,41 +539,29 @@ export class FixedRateExchange {
exchangeId: string, exchangeId: string,
datatokenAmount: string datatokenAmount: string
): Promise<PriceAndFees> { ): Promise<PriceAndFees> {
const fixedRateExchange = await this.getExchange(exchangeId)
const result = await this.contract.methods const result = await this.contract.methods
.calcBaseInGivenOutDT( .calcBaseInGivenOutDT(
exchangeId, exchangeId,
await this.amountToUnits( await this.amountToUnits(fixedRateExchange.datatoken, datatokenAmount)
(
await this.getExchange(exchangeId)
).datatoken,
datatokenAmount
)
) )
.call() .call()
const priceAndFees = { const priceAndFees = {
baseTokenAmount: await this.unitsToAmount( baseTokenAmount: await this.unitsToAmount(
( fixedRateExchange.baseToken,
await this.getExchange(exchangeId)
).baseToken,
result.baseTokenAmount result.baseTokenAmount
), ),
baseTokenAmountBeforeFee: await this.unitsToAmount( baseTokenAmountBeforeFee: await this.unitsToAmount(
( fixedRateExchange.baseToken,
await this.getExchange(exchangeId)
).baseToken,
result.baseTokenAmountBeforeFee result.baseTokenAmountBeforeFee
), ),
marketFeeAmount: await this.unitsToAmount( marketFeeAmount: await this.unitsToAmount(
( fixedRateExchange.baseToken,
await this.getExchange(exchangeId)
).baseToken,
result.marketFeeAmount result.marketFeeAmount
), ),
oceanFeeAmount: await this.unitsToAmount( oceanFeeAmount: await this.unitsToAmount(
( fixedRateExchange.baseToken,
await this.getExchange(exchangeId)
).baseToken,
result.oceanFeeAmount result.oceanFeeAmount
) )
} as PriceAndFees } as PriceAndFees

View File

@ -121,7 +121,11 @@ export async function unitsToAmount(
if (decimals === '0') { if (decimals === '0') {
decimals = 18 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 }) BigNumber.config({ EXPONENTIAL_AT: 50 })
return amountFormatted.toString() return amountFormatted.toString()
} catch (e) { } catch (e) {
@ -140,8 +144,12 @@ export async function amountToUnits(
if (decimals === '0') { if (decimals === '0') {
decimals = 18 decimals = 18
} }
const amountFormatted = new BigNumber(parseInt(amount) * 10 ** decimals)
BigNumber.config({ EXPONENTIAL_AT: 50 }) BigNumber.config({ EXPONENTIAL_AT: 50 })
const amountFormatted = new BigNumber(amount).times(
new BigNumber(10).exponentiatedBy(decimals)
)
return amountFormatted.toString() return amountFormatted.toString()
} catch (e) { } catch (e) {
LoggerInstance.error(`ERROR: FAILED TO CALL DECIMALS(), USING 18', ${e.message}`) LoggerInstance.error(`ERROR: FAILED TO CALL DECIMALS(), USING 18', ${e.message}`)