From 578a39a7218ae46ce369d6182e4dd4e63a93034a Mon Sep 17 00:00:00 2001 From: Fabio Cigliano Date: Mon, 26 Nov 2018 12:07:15 +1300 Subject: [PATCH] log raw ticker price within tx --- .../src/Common/TransactionTypes.elm | 2 + lamassu-admin-elm/src/Transaction/Decoder.elm | 2 + lamassu-admin-elm/src/Transaction/View.elm | 2 + lib/cash-out/cash-out-helper.js | 3 +- lib/plugins.js | 12 +- lib/tx.js | 22 +++- migrations/1543182139555-tx-ticker-price.js | 16 +++ public/elm.js | 112 +++++++++++++----- 8 files changed, 132 insertions(+), 39 deletions(-) create mode 100644 migrations/1543182139555-tx-ticker-price.js diff --git a/lamassu-admin-elm/src/Common/TransactionTypes.elm b/lamassu-admin-elm/src/Common/TransactionTypes.elm index d256509f..b7ccc82e 100644 --- a/lamassu-admin-elm/src/Common/TransactionTypes.elm +++ b/lamassu-admin-elm/src/Common/TransactionTypes.elm @@ -19,6 +19,7 @@ type alias CashInTxRec = , cryptoCode : CryptoCode , fiat : Float , commissionPercentage : Maybe Float + , rawTickerPrice : Maybe Float , fiatCode : String , txHash : Maybe String , phone : Maybe String @@ -39,6 +40,7 @@ type alias CashOutTxRec = , cryptoCode : CryptoCode , fiat : Float , commissionPercentage : Maybe Float + , rawTickerPrice : Maybe Float , fiatCode : String , status : String , dispense : Bool diff --git a/lamassu-admin-elm/src/Transaction/Decoder.elm b/lamassu-admin-elm/src/Transaction/Decoder.elm index b9cfa652..d58ae06a 100644 --- a/lamassu-admin-elm/src/Transaction/Decoder.elm +++ b/lamassu-admin-elm/src/Transaction/Decoder.elm @@ -66,6 +66,7 @@ cashInTxDecoder = |> required "cryptoCode" cryptoCodeDecoder |> required "fiat" floatString |> required "commissionPercentage" (nullable floatString) + |> required "rawTickerPrice" (nullable floatString) |> required "fiatCode" string |> required "txHash" (nullable string) |> required "phone" (nullable string) @@ -93,6 +94,7 @@ cashOutTxDecoder = |> required "cryptoCode" cryptoCodeDecoder |> required "fiat" floatString |> required "commissionPercentage" (nullable floatString) + |> required "rawTickerPrice" (nullable floatString) |> required "fiatCode" string |> required "status" string |> required "dispense" bool diff --git a/lamassu-admin-elm/src/Transaction/View.elm b/lamassu-admin-elm/src/Transaction/View.elm index ec71a8f8..c9eb48c0 100644 --- a/lamassu-admin-elm/src/Transaction/View.elm +++ b/lamassu-admin-elm/src/Transaction/View.elm @@ -43,6 +43,7 @@ cashInTxView tx = [ div [] [ text tx.id ] , div [] [ text "This is a cash-in transaction" ] , div [] [ text ("Fiat: " ++ (format "0,0.00" tx.fiat)) ] + , div [] [ text ("Raw ticker price: " ++ (format "0,0.00" (Maybe.withDefault 0.0 tx.rawTickerPrice))) ] , div [] [ text ("Status: " ++ cancelStatus) ] , div [] [ text error ] , cancelButtonDiv @@ -64,6 +65,7 @@ cashOutTxView tx = [ div [] [ text tx.id ] , div [] [ text "This is a cash-out transaction" ] , div [] [ text ("Fiat: " ++ (format "0,0.00" tx.fiat)) ] + , div [] [ text ("Raw ticker price: " ++ (format "0,0.00" (Maybe.withDefault 0.0 tx.rawTickerPrice))) ] , div [] [ text error ] ] diff --git a/lib/cash-out/cash-out-helper.js b/lib/cash-out/cash-out-helper.js index 1241ebd3..b9e059f2 100644 --- a/lib/cash-out/cash-out-helper.js +++ b/lib/cash-out/cash-out-helper.js @@ -14,7 +14,8 @@ function convertBigNumFields (obj) { const convert = (value, key) => _.includes(key, [ 'cryptoAtoms', 'fiat', - 'commissionPercentage' + 'commissionPercentage', + 'rawTickerPrice' ]) ? value.toString() : value diff --git a/lib/plugins.js b/lib/plugins.js index 0a1ec5f3..e8bd4204 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -41,6 +41,15 @@ function plugins (settings, deviceId) { } } + async function getRawTickerPrice (fiatCode, cryptoCode) { + const tickers = await ticker.getRates(settings, fiatCode, cryptoCode) + + return { + cashIn: _.get(['rates', 'ask'], tickers), + cashOut: _.get(['rates', 'bid'], tickers) + } + } + function buildRates (tickers) { const config = configManager.machineScoped(deviceId, settings.config) const cryptoCodes = config.cryptoCurrencies @@ -793,7 +802,8 @@ function plugins (settings, deviceId) { sell, notificationsEnabled, notifyOperator, - getCommissionPercentage + getCommissionPercentage, + getRawTickerPrice } } diff --git a/lib/tx.js b/lib/tx.js index 71c84f19..db31b034 100644 --- a/lib/tx.js +++ b/lib/tx.js @@ -3,8 +3,8 @@ const BN = require('./bn') const CashInTx = require('./cash-in/cash-in-tx') const CashOutTx = require('./cash-out/cash-out-tx') -function process (tx, pi) { - const mtx = massage(tx, pi) +async function process (tx, pi) { + const mtx = await massage(tx, pi) if (mtx.direction === 'cashIn') return CashInTx.post(mtx, pi) if (mtx.direction === 'cashOut') return CashOutTx.post(mtx, pi) @@ -16,15 +16,22 @@ function post (tx, pi) { .then(_.set('dirty', false)) } -function massage (tx, pi) { +async function massage (tx, pi) { const direction = _.get('direction', tx) const cryptoCode = _.get('cryptoCode', tx) + const fiatCode = _.get('fiatCode', tx) const isDateField = r => r === 'created' || _.endsWith('_time', r) const transformDate = (v, k) => isDateField(k) ? new Date(v) : v const mapValuesWithKey = _.mapValues.convert({'cap': false}) const transformDates = r => mapValuesWithKey(transformDate, r) const logCommission = r => _.assign(r, { - commissionPercentage: _.get(direction, pi.getCommissionPercentage(cryptoCode)) + commissionPercentage: _.get(direction + , pi.getCommissionPercentage(cryptoCode)) + }) + + const tickerPrice = await pi.getRawTickerPrice(fiatCode, cryptoCode) + const logRawTickerPrice = r => _.assign(r, { + rawTickerPrice: _.get(direction, tickerPrice) }) const mapBN = r => { @@ -44,7 +51,12 @@ function massage (tx, pi) { return _.assign(r, update) } - const mapper = _.flow(transformDates, logCommission, mapBN, _.unset('dirty')) + const mapper = _.flow( + transformDates, + mapBN, + logCommission, + logRawTickerPrice, + _.unset('dirty')) return mapper(tx) } diff --git a/migrations/1543182139555-tx-ticker-price.js b/migrations/1543182139555-tx-ticker-price.js new file mode 100644 index 00000000..fd468a96 --- /dev/null +++ b/migrations/1543182139555-tx-ticker-price.js @@ -0,0 +1,16 @@ +'use strict' + +const db = require('./db') + +exports.up = function (next) { + var sql = [ + 'ALTER TABLE cash_in_txs ADD COLUMN raw_ticker_price numeric(14, 5) null DEFAULT null', + 'ALTER TABLE cash_out_txs ADD COLUMN raw_ticker_price numeric(14, 5) null DEFAULT null' + ] + + db.multi(sql, next) +} + +exports.down = function (next) { + next() +} diff --git a/public/elm.js b/public/elm.js index 315eddde..a2b662e2 100644 --- a/public/elm.js +++ b/public/elm.js @@ -28478,7 +28478,9 @@ var _user$project$Common_TransactionTypes$CashInTxRec = function (a) { return function (n) { return function (o) { return function (p) { - return {id: a, machineName: b, toAddress: c, cryptoAtoms: d, cryptoCode: e, fiat: f, commissionPercentage: g, fiatCode: h, txHash: i, phone: j, error: k, operatorCompleted: l, send: m, sendConfirmed: n, expired: o, created: p}; + return function (q) { + return {id: a, machineName: b, toAddress: c, cryptoAtoms: d, cryptoCode: e, fiat: f, commissionPercentage: g, rawTickerPrice: h, fiatCode: i, txHash: j, phone: k, error: l, operatorCompleted: m, send: n, sendConfirmed: o, expired: p, created: q}; + }; }; }; }; @@ -28511,7 +28513,9 @@ var _user$project$Common_TransactionTypes$CashOutTxRec = function (a) { return function (n) { return function (o) { return function (p) { - return {id: a, machineName: b, toAddress: c, cryptoAtoms: d, cryptoCode: e, fiat: f, commissionPercentage: g, fiatCode: h, status: i, dispense: j, notified: k, redeemed: l, phone: m, error: n, created: o, confirmed: p}; + return function (q) { + return {id: a, machineName: b, toAddress: c, cryptoAtoms: d, cryptoCode: e, fiat: f, commissionPercentage: g, rawTickerPrice: h, fiatCode: i, status: j, dispense: k, notified: l, redeemed: m, phone: n, error: o, created: p, confirmed: q}; + }; }; }; }; @@ -33990,33 +33994,37 @@ var _user$project$Transaction_Decoder$cashInTxDecoder = A3( _elm_lang$core$Json_Decode$string, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'commissionPercentage', + 'rawTickerPrice', _elm_lang$core$Json_Decode$nullable(_user$project$Transaction_Decoder$floatString), A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'fiat', - _user$project$Transaction_Decoder$floatString, + 'commissionPercentage', + _elm_lang$core$Json_Decode$nullable(_user$project$Transaction_Decoder$floatString), A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'cryptoCode', - _user$project$Transaction_Decoder$cryptoCodeDecoder, + 'fiat', + _user$project$Transaction_Decoder$floatString, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'cryptoAtoms', - _user$project$Transaction_Decoder$intString, + 'cryptoCode', + _user$project$Transaction_Decoder$cryptoCodeDecoder, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'toAddress', - _elm_lang$core$Json_Decode$string, + 'cryptoAtoms', + _user$project$Transaction_Decoder$intString, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'machineName', + 'toAddress', _elm_lang$core$Json_Decode$string, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'id', + 'machineName', _elm_lang$core$Json_Decode$string, - _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Common_TransactionTypes$CashInTxRec))))))))))))))))); + A3( + _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, + 'id', + _elm_lang$core$Json_Decode$string, + _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Common_TransactionTypes$CashInTxRec)))))))))))))))))); var _user$project$Transaction_Decoder$cashOutTxDecoder = A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, 'confirmedAt', @@ -34055,33 +34063,37 @@ var _user$project$Transaction_Decoder$cashOutTxDecoder = A3( _elm_lang$core$Json_Decode$string, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'commissionPercentage', + 'rawTickerPrice', _elm_lang$core$Json_Decode$nullable(_user$project$Transaction_Decoder$floatString), A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'fiat', - _user$project$Transaction_Decoder$floatString, + 'commissionPercentage', + _elm_lang$core$Json_Decode$nullable(_user$project$Transaction_Decoder$floatString), A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'cryptoCode', - _user$project$Transaction_Decoder$cryptoCodeDecoder, + 'fiat', + _user$project$Transaction_Decoder$floatString, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'cryptoAtoms', - _user$project$Transaction_Decoder$intString, + 'cryptoCode', + _user$project$Transaction_Decoder$cryptoCodeDecoder, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'toAddress', - _elm_lang$core$Json_Decode$string, + 'cryptoAtoms', + _user$project$Transaction_Decoder$intString, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'machineName', + 'toAddress', _elm_lang$core$Json_Decode$string, A3( _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, - 'id', + 'machineName', _elm_lang$core$Json_Decode$string, - _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Common_TransactionTypes$CashOutTxRec))))))))))))))))); + A3( + _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$required, + 'id', + _elm_lang$core$Json_Decode$string, + _NoRedInk$elm_decode_pipeline$Json_Decode_Pipeline$decode(_user$project$Common_TransactionTypes$CashOutTxRec)))))))))))))))))); var _user$project$Transaction_Decoder$txDecode = function (txClass) { var _p4 = txClass; switch (_p4) { @@ -39084,10 +39096,28 @@ var _user$project$Transaction_View$cashOutTxView = function (tx) { {ctor: '[]'}, { ctor: '::', - _0: _elm_lang$html$Html$text(error), + _0: _elm_lang$html$Html$text( + A2( + _elm_lang$core$Basics_ops['++'], + 'Raw ticker price: ', + A2( + _ggb$numeral_elm$Numeral$format, + '0,0.00', + A2(_elm_lang$core$Maybe$withDefault, 0.0, tx.rawTickerPrice)))), _1: {ctor: '[]'} }), - _1: {ctor: '[]'} + _1: { + ctor: '::', + _0: A2( + _elm_lang$html$Html$div, + {ctor: '[]'}, + { + ctor: '::', + _0: _elm_lang$html$Html$text(error), + _1: {ctor: '[]'} + }), + _1: {ctor: '[]'} + } } } } @@ -39165,7 +39195,13 @@ var _user$project$Transaction_View$cashInTxView = function (tx) { { ctor: '::', _0: _elm_lang$html$Html$text( - A2(_elm_lang$core$Basics_ops['++'], 'Status: ', cancelStatus)), + A2( + _elm_lang$core$Basics_ops['++'], + 'Raw ticker price: ', + A2( + _ggb$numeral_elm$Numeral$format, + '0,0.00', + A2(_elm_lang$core$Maybe$withDefault, 0.0, tx.rawTickerPrice)))), _1: {ctor: '[]'} }), _1: { @@ -39175,13 +39211,25 @@ var _user$project$Transaction_View$cashInTxView = function (tx) { {ctor: '[]'}, { ctor: '::', - _0: _elm_lang$html$Html$text(error), + _0: _elm_lang$html$Html$text( + A2(_elm_lang$core$Basics_ops['++'], 'Status: ', cancelStatus)), _1: {ctor: '[]'} }), _1: { ctor: '::', - _0: cancelButtonDiv, - _1: {ctor: '[]'} + _0: A2( + _elm_lang$html$Html$div, + {ctor: '[]'}, + { + ctor: '::', + _0: _elm_lang$html$Html$text(error), + _1: {ctor: '[]'} + }), + _1: { + ctor: '::', + _0: cancelButtonDiv, + _1: {ctor: '[]'} + } } } }