fix: review
This commit is contained in:
parent
772805269f
commit
5045821593
9 changed files with 62 additions and 172 deletions
41
lib/commission-math.js
Normal file
41
lib/commission-math.js
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
const BN = require('./bn')
|
||||
const configManager = require('./new-config-manager')
|
||||
const coinUtils = require('./coin-utils')
|
||||
|
||||
function truncateCrypto (cryptoAtoms, cryptoCode) {
|
||||
const DECIMAL_PLACES = 3
|
||||
if (cryptoAtoms.eq(0)) return cryptoAtoms
|
||||
|
||||
const scale = 5 // TODO: change this to coins.displayScale when coins have that attribute
|
||||
const scaleFactor = BN(10).pow(scale)
|
||||
|
||||
return BN(cryptoAtoms).truncated().div(scaleFactor)
|
||||
.round(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 = BN(tx.rawTickerPrice)
|
||||
const discount = getDiscountRate(tx.discount, commissions[tx.direction])
|
||||
const rate = tickerRate.mul(discount).round(5)
|
||||
const unitScale = coinUtils.getCryptoCurrency(tx.cryptoCode).unitScale
|
||||
const unitScaleFactor = BN(10).pow(unitScale)
|
||||
|
||||
return truncateCrypto(BN(usableFiat).div(rate.div(unitScaleFactor)), tx.cryptoCode)
|
||||
}
|
||||
|
||||
function getDiscountRate (discount, commission) {
|
||||
const bnDiscount = discount ? BN(discount) : BN(0)
|
||||
const bnCommission = BN(commission)
|
||||
const percentageDiscount = BN(1).sub(bnDiscount.div(100))
|
||||
const percentageCommission = bnCommission.div(100)
|
||||
return BN(1).add(percentageDiscount.mul(percentageCommission))
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
truncateCrypto,
|
||||
fiatToCrypto,
|
||||
getDiscountRate
|
||||
}
|
||||
|
|
@ -21,4 +21,9 @@ function softDeleteCoupon (couponId) {
|
|||
return db.none(sql, [couponId])
|
||||
}
|
||||
|
||||
module.exports = { getAvailableCoupons, getCoupon, createCoupon, softDeleteCoupon }
|
||||
function getNumberOfAvailableCoupons () {
|
||||
const sql = `SELECT COUNT(id) FROM coupons WHERE soft_deleted=false`
|
||||
return db.one(sql).then(res => res.count)
|
||||
}
|
||||
|
||||
module.exports = { getAvailableCoupons, getCoupon, createCoupon, softDeleteCoupon, getNumberOfAvailableCoupons }
|
||||
|
|
@ -13,7 +13,7 @@ const settingsLoader = require('../../new-settings-loader')
|
|||
// const tokenManager = require('../../token-manager')
|
||||
const blacklist = require('../../blacklist')
|
||||
const machineEventsByIdBatch = require("../../postgresql_interface").machineEventsByIdBatch
|
||||
const couponManager = require('../../coupon-manager')
|
||||
const couponManager = require('../../coupons')
|
||||
|
||||
const serverVersion = require('../../../package.json').version
|
||||
|
||||
|
|
@ -179,7 +179,6 @@ const typeDefs = gql`
|
|||
id: ID!
|
||||
code: String!
|
||||
discount: Int!
|
||||
soft_deleted: Boolean
|
||||
}
|
||||
|
||||
type Transaction {
|
||||
|
|
@ -295,7 +294,7 @@ const typeDefs = gql`
|
|||
deleteBlacklistRow(cryptoCode: String!, address: String!): Blacklist
|
||||
insertBlacklistRow(cryptoCode: String!, address: String!): Blacklist
|
||||
createCoupon(code: String!, discount: Int!): Coupon
|
||||
softDeleteCoupon(couponId: ID!): Coupon
|
||||
deleteCoupon(couponId: ID!): Coupon
|
||||
}
|
||||
`
|
||||
|
||||
|
|
@ -370,7 +369,7 @@ const resolvers = {
|
|||
blacklist.insertIntoBlacklist(cryptoCode, address),
|
||||
// revokeToken: (...[, { token }]) => tokenManager.revokeToken(token)
|
||||
createCoupon: (...[, { code, discount }]) => couponManager.createCoupon(code, discount),
|
||||
softDeleteCoupon: (...[, { couponId }]) => couponManager.softDeleteCoupon(couponId)
|
||||
deleteCoupon: (...[, { couponId }]) => couponManager.deleteCoupon(couponId)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ const cashOutHelper = require('./cash-out/cash-out-helper')
|
|||
const machineLoader = require('./machine-loader')
|
||||
const customers = require('./customers')
|
||||
const coinUtils = require('./coin-utils')
|
||||
const commissionMath = require('./commission-math')
|
||||
const coupons = require('./coupons')
|
||||
|
||||
const mapValuesWithKey = _.mapValues.convert({
|
||||
cap: false
|
||||
|
|
@ -190,11 +192,6 @@ function plugins (settings, deviceId) {
|
|||
.then(row => row.id)
|
||||
}
|
||||
|
||||
function getNumberOfAvailableCoupons () {
|
||||
const sql = `SELECT COUNT(id) FROM coupons WHERE soft_deleted=false`
|
||||
return db.one(sql).then(res => res.count)
|
||||
}
|
||||
|
||||
function mapCoinSettings (coinParams) {
|
||||
const cryptoCode = coinParams[0]
|
||||
const cryptoNetwork = coinParams[1]
|
||||
|
|
@ -227,7 +224,7 @@ function plugins (settings, deviceId) {
|
|||
const testnetPromises = cryptoCodes.map(c => wallet.cryptoNetwork(settings, c))
|
||||
const pingPromise = recordPing(deviceTime, machineVersion, machineModel)
|
||||
const currentConfigVersionPromise = fetchCurrentConfigVersion()
|
||||
const currentAvailableCoupons = getNumberOfAvailableCoupons()
|
||||
const currentAvailableCoupons = coupons.getNumberOfAvailableCoupons()
|
||||
|
||||
const promises = [
|
||||
buildAvailableCassettes(),
|
||||
|
|
@ -459,7 +456,7 @@ function plugins (settings, deviceId) {
|
|||
function buyAndSell (rec, doBuy, tx) {
|
||||
const cryptoCode = rec.cryptoCode
|
||||
const fiatCode = rec.fiatCode
|
||||
const cryptoAtoms = doBuy ? fiatToCrypto(tx, rec) : rec.cryptoAtoms.neg()
|
||||
const cryptoAtoms = doBuy ? commissionMath.fiatToCrypto(tx, rec, deviceId, settings.config) : rec.cryptoAtoms.neg()
|
||||
|
||||
const market = [fiatCode, cryptoCode].join('')
|
||||
|
||||
|
|
@ -475,38 +472,6 @@ function plugins (settings, deviceId) {
|
|||
})
|
||||
}
|
||||
|
||||
function truncateCrypto (cryptoAtoms, cryptoCode) {
|
||||
const DECIMAL_PLACES = 3
|
||||
if (cryptoAtoms.eq(0)) return cryptoAtoms
|
||||
|
||||
const scale = 5 // TODO: change this to coins.displayScale when coins have that attribute
|
||||
const scaleFactor = BN(10).pow(scale)
|
||||
|
||||
return BN(cryptoAtoms).truncated().div(scaleFactor)
|
||||
.round(DECIMAL_PLACES).times(scaleFactor)
|
||||
}
|
||||
|
||||
function fiatToCrypto (tx, rec) {
|
||||
const usableFiat = rec.fiat - rec.cashInFee
|
||||
|
||||
const commissions = configManager.getCommissions(tx.cryptoCode, deviceId, settings.config)
|
||||
const tickerRate = BN(tx.rawTickerPrice)
|
||||
const discount = getDiscountRate(tx.discount, commissions[tx.direction])
|
||||
const rate = tickerRate.mul(discount).round(5)
|
||||
const unitScale = coinUtils.getCryptoCurrency(tx.cryptoCode).unitScale
|
||||
const unitScaleFactor = BN(10).pow(unitScale)
|
||||
|
||||
return truncateCrypto(BN(usableFiat).div(rate.div(unitScaleFactor)), tx.cryptoCode)
|
||||
}
|
||||
|
||||
function getDiscountRate (discount, commission) {
|
||||
const bnDiscount = discount ? BN(discount) : BN(0)
|
||||
const bnCommission = BN(commission)
|
||||
const percentageDiscount = BN(1).sub(bnDiscount.div(100))
|
||||
const percentageCommission = bnCommission.div(100)
|
||||
return BN(1).add(percentageDiscount.mul(percentageCommission))
|
||||
}
|
||||
|
||||
function consolidateTrades (cryptoCode, fiatCode) {
|
||||
const market = [fiatCode, cryptoCode].join('')
|
||||
|
||||
|
|
|
|||
|
|
@ -25,8 +25,9 @@ const E = require('./error')
|
|||
const customers = require('./customers')
|
||||
const logs = require('./logs')
|
||||
const compliance = require('./compliance')
|
||||
const couponManager = require('./coupon-manager')
|
||||
const couponManager = require('./coupons')
|
||||
const BN = require('./bn')
|
||||
const commissionMath = require('./commission-math')
|
||||
|
||||
const version = require('../package.json').version
|
||||
|
||||
|
|
@ -223,7 +224,7 @@ function verifyCoupon (req, res, next) {
|
|||
const transaction = req.body.tx
|
||||
const commissions = configManager.getCommissions(transaction.cryptoCode, req.deviceId, req.settings.config)
|
||||
const tickerRate = BN(transaction.rawTickerPrice)
|
||||
const discount = getDiscountRate(coupon.discount, commissions[transaction.direction])
|
||||
const discount = commissionMath.getDiscountRate(coupon.discount, commissions[transaction.direction])
|
||||
const rates = {
|
||||
[transaction.cryptoCode]: {
|
||||
[transaction.direction]: (transaction.direction === 'cashIn')
|
||||
|
|
@ -240,12 +241,6 @@ function verifyCoupon (req, res, next) {
|
|||
.catch(next)
|
||||
}
|
||||
|
||||
function getDiscountRate (discount, commission) {
|
||||
const percentageDiscount = BN(1).sub(BN(discount).div(100))
|
||||
const percentageCommission = BN(commission).div(100)
|
||||
return BN(1).add(percentageDiscount.mul(percentageCommission))
|
||||
}
|
||||
|
||||
function addOrUpdateCustomer (req) {
|
||||
const customerData = req.body
|
||||
const machineVersion = req.query.version
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue