feat: add loyalty panel screen and structure

feat: add coupons table

feat: add coupons to schema

fix: coupon schema

feat: coupon table

feat: add coupon top button

feat: add first coupon button

feat: delete coupon

feat: coupon modal

fix: clear discount on modal close

fix: modal input formatting

feat: add new coupons

fix: button positioning

fix: remove loyalty panel sidebar

fix: coupon screen matching specs

fix: coupon modal

feat: send coupon data to machine on poll

fix: available coupons bool

feat: coupon endpoint

feat: transaction discount migration

feat: post-discount rates

refactor: bills

feat: version string

fix: bill saving on db

feat: coupon soft-delete

fix: coupon soft delete

fix: bill receiving

feat: remove cryptoAtoms update during tx

fix: tx trading

fix: bills

feat: start trades rework

fix: remove code

fix: code review
This commit is contained in:
Sérgio Salgado 2020-10-28 11:15:20 +00:00 committed by Josh Harvey
parent 64315cfd80
commit 7fe8799edc
18 changed files with 622 additions and 13 deletions

View file

@ -190,6 +190,11 @@ function plugins (settings, deviceId) {
.then(row => row.id)
}
function fetchCurrentAvailableCoupons () {
const sql = `select * from coupons where soft_deleted=false`
return db.any(sql).then(v => v.length)
}
function mapCoinSettings (coinParams) {
const cryptoCode = coinParams[0]
const cryptoNetwork = coinParams[1]
@ -222,11 +227,13 @@ function plugins (settings, deviceId) {
const testnetPromises = cryptoCodes.map(c => wallet.cryptoNetwork(settings, c))
const pingPromise = recordPing(deviceTime, machineVersion, machineModel)
const currentConfigVersionPromise = fetchCurrentConfigVersion()
const currentAvailableCouponsPromise = fetchCurrentAvailableCoupons()
const promises = [
buildAvailableCassettes(),
pingPromise,
currentConfigVersionPromise
currentConfigVersionPromise,
currentAvailableCouponsPromise
].concat(tickerPromises, balancePromises, testnetPromises)
return Promise.all(promises)
@ -236,16 +243,18 @@ function plugins (settings, deviceId) {
const cryptoCodesCount = cryptoCodes.length
const tickers = arr.slice(3, cryptoCodesCount + 3)
const balances = arr.slice(cryptoCodesCount + 3, 2 * cryptoCodesCount + 3)
const testNets = arr.slice(2 * cryptoCodesCount + 3)
const testNets = arr.slice(2 * cryptoCodesCount + 3, arr.length - 1)
const coinParams = _.zip(cryptoCodes, testNets)
const coinsWithoutRate = _.map(mapCoinSettings, coinParams)
const areThereAvailableCoupons = arr[arr.length - 1] > 0
return {
cassettes,
rates: buildRates(tickers),
balances: buildBalances(balances),
coins: _.zipWith(_.assign, coinsWithoutRate, tickers),
configVersion
configVersion,
areThereAvailableCoupons
}
})
}
@ -440,18 +449,18 @@ function plugins (settings, deviceId) {
* Trader functions
*/
function buy (rec) {
return buyAndSell(rec, true)
function buy (rec, tx) {
return buyAndSell(rec, true, tx)
}
function sell (rec) {
return buyAndSell(rec, false)
}
function buyAndSell (rec, doBuy) {
function buyAndSell (rec, doBuy, tx) {
const cryptoCode = rec.cryptoCode
const fiatCode = rec.fiatCode
const cryptoAtoms = doBuy ? rec.cryptoAtoms : rec.cryptoAtoms.neg()
const cryptoAtoms = doBuy ? fiatToCrypto(tx, rec) : rec.cryptoAtoms.neg()
const market = [fiatCode, cryptoCode].join('')
@ -467,6 +476,38 @@ 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('')