chore: use monorepo organization
This commit is contained in:
parent
deaf7d6ecc
commit
a687827f7e
1099 changed files with 8184 additions and 11535 deletions
40
packages/server/lib/commission-math.js
Normal file
40
packages/server/lib/commission-math.js
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
const BN = require('./bn')
|
||||
const configManager = require('./new-config-manager')
|
||||
const { utils: coinUtils } = require('@lamassu/coins')
|
||||
|
||||
function truncateCrypto (cryptoAtoms, cryptoCode) {
|
||||
const DECIMAL_PLACES = 6
|
||||
if (cryptoAtoms.eq(0)) return cryptoAtoms
|
||||
|
||||
const scale = coinUtils.getCryptoCurrency(cryptoCode).unitScale
|
||||
const scaleFactor = BN(10).pow(scale)
|
||||
|
||||
return new BN(cryptoAtoms).integerValue(BN.ROUND_DOWN).div(scaleFactor)
|
||||
.decimalPlaces(DECIMAL_PLACES).times(scaleFactor)
|
||||
}
|
||||
|
||||
function fiatToCrypto (tx, rec, deviceId, config) {
|
||||
const usableFiat = rec.fiat - rec.cashInFee
|
||||
|
||||
const commissions = configManager.getCommissions(tx.cryptoCode, deviceId, config)
|
||||
const tickerRate = new BN(tx.rawTickerPrice)
|
||||
const discount = getDiscountRate(tx.discount, commissions[tx.direction])
|
||||
const rate = tickerRate.times(discount).decimalPlaces(5)
|
||||
const unitScale = coinUtils.getCryptoCurrency(tx.cryptoCode).unitScale
|
||||
const unitScaleFactor = new BN(10).pow(unitScale)
|
||||
|
||||
return truncateCrypto(new BN(usableFiat).div(rate.div(unitScaleFactor)), tx.cryptoCode)
|
||||
}
|
||||
|
||||
function getDiscountRate (discount, commission) {
|
||||
const bnDiscount = discount ? new BN(discount) : new BN(0)
|
||||
const bnCommission = new BN(commission)
|
||||
const percentageDiscount = new BN(1).minus(bnDiscount.div(100))
|
||||
const percentageCommission = bnCommission.div(100)
|
||||
return new BN(1).plus(percentageDiscount.times(percentageCommission))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fiatToCrypto,
|
||||
getDiscountRate
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue