Skip to content

Commit

Permalink
Merge pull request #26 from consenlabs/hotfix/bignumber_precision_bug
Browse files Browse the repository at this point in the history
fix bignumber.js digits error
  • Loading branch information
focusj authored Aug 4, 2021
2 parents 8dcabe9 + fcd4641 commit 097a01d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 3 additions & 7 deletions src/quoting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ export const constructQuoteResponse = (indicativePrice: IndicativePriceApiResult
}

// Process buy amount for WYSIWY
function applyFeeToAmount(amount: number, feeFactor: number, precision: number) {
export function applyFeeToAmount(amount: number, feeFactor: number, precision: number): number {
if (isNil(amount)) return amount
return truncateAmount(
toBN(amount)
.dividedBy(1 - feeFactor / 10000)
.toString(),
precision
)
const rate = toBN(1).sub(toBN(feeFactor).div(10000))
return truncateAmount(toBN(amount).dividedBy(rate).toString(), precision)
}

function calcFeeFactorWhenBuy(tokenCfg: TokenConfig, factor: number | null): number {
Expand Down
10 changes: 10 additions & 0 deletions test/quoting.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import 'mocha'
import { assert } from 'chai'
import { applyFeeToAmount } from '../src/quoting'

describe('quoting.ts', function () {
it('applyFeeToAmount', function () {
const fee = applyFeeToAmount(104, 2553, 6)
assert.equal(fee, 139.653551)
})
})

0 comments on commit 097a01d

Please sign in to comment.