diff --git a/lib/new-admin/graphql/resolvers/transaction.resolver.js b/lib/new-admin/graphql/resolvers/transaction.resolver.js index e353c891..8179874d 100644 --- a/lib/new-admin/graphql/resolvers/transaction.resolver.js +++ b/lib/new-admin/graphql/resolvers/transaction.resolver.js @@ -11,7 +11,7 @@ const logDateFormat = require('../../../logs').logDateFormat const transactionsLoader = new DataLoader(ids => transactions.getCustomerTransactionsBatch(ids)) const txLogFields = ['txClass', 'id', 'deviceId', 'toAddress', 'cryptoAtoms', - 'cryptoCode', 'fiat', 'fiatCode', 'fee', 'status', + 'cryptoCode', 'fiat', 'fiatCode', 'fee', 'status', 'fiatProfit', 'dispense', 'notified', 'redeem', 'phone', 'error', 'created', 'confirmedAt', 'hdIndex', 'swept', 'timedout', 'dispenseConfirmed', 'provisioned1', 'provisioned2', diff --git a/lib/new-admin/services/transactions.js b/lib/new-admin/services/transactions.js index 28e73b6b..cf4df340 100644 --- a/lib/new-admin/services/transactions.js +++ b/lib/new-admin/services/transactions.js @@ -126,21 +126,28 @@ function simplifiedBatch (data) { ...it, status: getStatus(it), fiatProfit: getProfit(it).toString(), - cryptoAmount: getCryptoAmount(it) + cryptoAmount: getCryptoAmount(it).toString() })) return _.compose(_.map(_.pick(fields)), addSimplifiedFields)(data) } -const getCryptoAmount = it => coinUtils.toUnit(BN(it.cryptoAtoms), it.cryptoCode).toString() +const getCryptoAmount = it => coinUtils.toUnit(BN(it.cryptoAtoms), it.cryptoCode) const getProfit = it => { - const discountValue = _.isNil(it.discount) ? BN(100) : BN(100).minus(it.discount) - const discountPercentage = BN(discountValue).div(100) - const commissionPercentage = BN(it.commissionPercentage).times(discountPercentage) - const getCommissionFee = it => BN(commissionPercentage).times(BN(it.fiat)) - if (!it.cashInFee) return getCommissionFee(it) - return getCommissionFee(it).plus(BN(it.cashInFee)) + /* fiat - crypto*tickerPrice + fee */ + const calcCashInProfit = (fiat, crypto, tickerPrice, fee) => fiat.minus(crypto.times(tickerPrice)).plus(fee) + /* crypto*tickerPrice - fiat */ + const calcCashOutProfit = (fiat, crypto, tickerPrice) => crypto.times(tickerPrice).minus(fiat) + + const fiat = BN(it.fiat) + const crypto = getCryptoAmount(it) + const tickerPrice = BN(it.rawTickerPrice) + const isCashIn = it.txClass === 'cashIn' + + return isCashIn + ? calcCashInProfit(fiat, crypto, tickerPrice, BN(it.cashInFee)) + : calcCashOutProfit(fiat, crypto, tickerPrice) } const getCashOutStatus = it => { diff --git a/new-lamassu-admin/src/pages/Transactions/DetailsCard.js b/new-lamassu-admin/src/pages/Transactions/DetailsCard.js index 095eafc3..a0a242f6 100644 --- a/new-lamassu-admin/src/pages/Transactions/DetailsCard.js +++ b/new-lamassu-admin/src/pages/Transactions/DetailsCard.js @@ -85,6 +85,27 @@ const CANCEL_CASH_IN_TRANSACTION = gql` } ` +const getCryptoAmount = tx => + coinUtils.toUnit(new BigNumber(tx.cryptoAtoms), tx.cryptoCode).toNumber() + +/* Port of getProfit() from lib/new-admin/services/transactions.js */ +const getCommission = tx => { + const calcCashInProfit = (fiat, crypto, tickerPrice, fee) => + fiat - crypto * tickerPrice + fee + const calcCashOutProfit = (fiat, crypto, tickerPrice) => + crypto * tickerPrice - fiat + + const fiat = Number.parseFloat(tx.fiat) + const crypto = getCryptoAmount(tx) + const tickerPrice = Number.parseFloat(tx.rawTickerPrice) + const isCashIn = tx.txClass === 'cashIn' + const cashInFee = isCashIn ? Number.parseFloat(tx.cashInFee) : 0 + + return isCashIn + ? calcCashInProfit(fiat, crypto, tickerPrice, cashInFee) + : calcCashOutProfit(fiat, crypto, tickerPrice) +} + const formatAddress = (cryptoCode = '', address = '') => coinUtils.formatCryptoAddress(cryptoCode, address).replace(/(.{5})/g, '$1 ') @@ -115,13 +136,15 @@ const DetailsRow = ({ it: tx, timezone }) => { } ) + const commission = getCommission(tx).toFixed(2) + const commissionPercentage = + Number.parseFloat(tx.commissionPercentage, 2) * 100 + const cashInFee = isCashIn ? Number.parseFloat(tx.cashInFee) : 0 const fiat = Number.parseFloat(tx.fiat) - const crypto = coinUtils.toUnit(new BigNumber(tx.cryptoAtoms), tx.cryptoCode) - const commissionPercentage = Number.parseFloat(tx.commissionPercentage, 2) - const commission = Number(fiat * commissionPercentage).toFixed(2) - const discount = tx.discount ? `-${tx.discount}%` : null - const exchangeRate = BigNumber(fiat / crypto).toFormat(2) + const crypto = getCryptoAmount(tx) + const exchangeRate = (fiat / crypto).toFixed(2) const displayExRate = `1 ${tx.cryptoCode} = ${exchangeRate} ${tx.fiatCode}` + const discount = tx.discount ? `-${tx.discount}%` : null const parseDateString = parse(new Date(), 'yyyyMMdd') @@ -304,7 +327,7 @@ const DetailsRow = ({ it: tx, timezone }) => {