From 163473a7ee383ea9c4865e69b2e6e340e4290a79 Mon Sep 17 00:00:00 2001 From: Davit Abulashvili Date: Fri, 4 Jan 2019 20:34:50 +0400 Subject: [PATCH] Log miner fee (#237) * Get miner fee written in some wallet plugins * Return txid and fee for geth. possible problem with txid (zero hash on pending transaction) * bitgo fee return * Wallet apis fees save in cryptoAtoms --- lib/cash-in/cash-in-tx.js | 5 +++-- lib/plugins/wallet/bitcoincashd/bitcoincashd.js | 9 +++++++++ lib/plugins/wallet/bitcoind/bitcoind.js | 9 +++++++++ lib/plugins/wallet/bitgo/bitgo.js | 5 ++++- lib/plugins/wallet/dashd/dashd.js | 9 +++++++++ lib/plugins/wallet/geth/base.js | 11 +++++++++++ lib/plugins/wallet/litecoind/litecoind.js | 9 +++++++++ lib/plugins/wallet/mock-wallet/mock-wallet.js | 2 +- lib/plugins/wallet/zcashd/zcashd.js | 9 +++++++++ 9 files changed, 64 insertions(+), 4 deletions(-) diff --git a/lib/cash-in/cash-in-tx.js b/lib/cash-in/cash-in-tx.js index 47aad2ad..889f5af9 100644 --- a/lib/cash-in/cash-in-tx.js +++ b/lib/cash-in/cash-in-tx.js @@ -57,8 +57,9 @@ function postProcess (r, pi) { if (!cashInLow.isClearToSend(r.dbTx, r.tx)) return Promise.resolve({}) return pi.sendCoins(r.tx) - .then(txHash => ({ - txHash, + .then(txObj => ({ + txHash: txObj.txid, + fee: txObj.fee, sendConfirmed: true, sendTime: 'now()^', sendPending: false, diff --git a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js index 949d4a67..b1d119b1 100644 --- a/lib/plugins/wallet/bitcoincashd/bitcoincashd.js +++ b/lib/plugins/wallet/bitcoincashd/bitcoincashd.js @@ -1,3 +1,4 @@ +const _ = require('lodash/fp') const jsonRpc = require('../../common/json-rpc') const BN = require('../../../bn') @@ -41,6 +42,14 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('sendtoaddress', [address, coins])) + .then((txId) => fetch('gettransaction', txId)) + .then((res) => _.pick(res, ['fee', 'txid'])) + .then((pickedObj) => { + return { + fee: BN(pickedObj.fee).abs().shift(unitScale).round(), + txid: pickedObj.txid + } + }) .catch(err => { if (err.code === -6) throw new E.InsufficientFundsError() throw err diff --git a/lib/plugins/wallet/bitcoind/bitcoind.js b/lib/plugins/wallet/bitcoind/bitcoind.js index ced3f636..a56b8ce6 100644 --- a/lib/plugins/wallet/bitcoind/bitcoind.js +++ b/lib/plugins/wallet/bitcoind/bitcoind.js @@ -1,3 +1,4 @@ +const _ = require('lodash/fp') const jsonRpc = require('../../common/json-rpc') const BN = require('../../../bn') @@ -41,6 +42,14 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('sendtoaddress', [address, coins])) + .then((txId) => fetch('gettransaction', txId)) + .then((res) => _.pick(res, ['fee', 'txid'])) + .then((pickedObj) => { + return { + fee: BN(pickedObj.fee).abs().shift(unitScale).round(), + txid: pickedObj.txid + } + }) .catch(err => { if (err.code === -6) throw new E.InsufficientFundsError() throw err diff --git a/lib/plugins/wallet/bitgo/bitgo.js b/lib/plugins/wallet/bitgo/bitgo.js index d3868b8d..74f383af 100644 --- a/lib/plugins/wallet/bitgo/bitgo.js +++ b/lib/plugins/wallet/bitgo/bitgo.js @@ -57,7 +57,10 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { return wallet.send(params) }) .then(result => { - return result.hash + let fee = parseFloat(result.transfer.feeString) + let txid = result.transfer.txid + + return { txid: txid, fee: BN(fee).round() } }) .catch(err => { if (err.message === 'insufficient funds') throw new E.InsufficientFundsError() diff --git a/lib/plugins/wallet/dashd/dashd.js b/lib/plugins/wallet/dashd/dashd.js index 89c3679c..ffaddeae 100644 --- a/lib/plugins/wallet/dashd/dashd.js +++ b/lib/plugins/wallet/dashd/dashd.js @@ -1,3 +1,4 @@ +const _ = require('lodash/fp') const jsonRpc = require('../../common/json-rpc') const coinUtils = require('../../../coin-utils') @@ -42,6 +43,14 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('sendtoaddress', [address, coins])) + .then((txId) => fetch('gettransaction', txId)) + .then((res) => _.pick(res, ['fee', 'txid'])) + .then((pickedObj) => { + return { + fee: BN(pickedObj.fee).abs().shift(unitScale).round(), + txid: pickedObj.txid + } + }) .catch(err => { if (err.code === -6) throw new E.InsufficientFundsError() throw err diff --git a/lib/plugins/wallet/geth/base.js b/lib/plugins/wallet/geth/base.js index a9879070..00264f4e 100644 --- a/lib/plugins/wallet/geth/base.js +++ b/lib/plugins/wallet/geth/base.js @@ -6,6 +6,7 @@ const hdkey = require('ethereumjs-wallet/hdkey') const Tx = require('ethereumjs-tx') const util = require('ethereumjs-util') const pify = require('pify') +const BN = require('../../../bn') const NAME = 'geth' exports.SUPPORTED_MODULES = ['wallet'] @@ -47,6 +48,16 @@ function isStrictAddress (cryptoCode, toAddress) { function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { return generateTx(toAddress, defaultWallet(account), cryptoAtoms, false) .then(pify(web3.eth.sendRawTransaction)) + .then(r => { + return pify(web3.eth.getTransactionByHash)(r.result) + .then(tx => { + if (!tx) return { txid: r.result } + + const fee = BN(tx.gas).multipliedBy(BN(tx.gasPrice)).round() + + return { txid: r.result, fee } + }) + }) } function checkCryptoCode (cryptoCode) { diff --git a/lib/plugins/wallet/litecoind/litecoind.js b/lib/plugins/wallet/litecoind/litecoind.js index ec117fa8..606d665f 100644 --- a/lib/plugins/wallet/litecoind/litecoind.js +++ b/lib/plugins/wallet/litecoind/litecoind.js @@ -1,3 +1,4 @@ +const _ = require('lodash/fp') const jsonRpc = require('../../common/json-rpc') const coinUtils = require('../../../coin-utils') @@ -42,6 +43,14 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('sendtoaddress', [address, coins])) + .then((txId) => fetch('gettransaction', txId)) + .then((res) => _.pick(res, ['fee', 'txid'])) + .then((pickedObj) => { + return { + fee: BN(pickedObj.fee).abs().shift(unitScale).round(), + txid: pickedObj.txid + } + }) .catch(err => { if (err.code === -6) throw new E.InsufficientFundsError() throw err diff --git a/lib/plugins/wallet/mock-wallet/mock-wallet.js b/lib/plugins/wallet/mock-wallet/mock-wallet.js index d18ac38f..2a15e7b0 100644 --- a/lib/plugins/wallet/mock-wallet/mock-wallet.js +++ b/lib/plugins/wallet/mock-wallet/mock-wallet.js @@ -51,7 +51,7 @@ function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) { console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s', cryptoCode, cryptoAtoms.toString(), toAddress) - return resolve('') + return resolve({ txid: '', fee: BN(0) }) }, 2000) }) } diff --git a/lib/plugins/wallet/zcashd/zcashd.js b/lib/plugins/wallet/zcashd/zcashd.js index 69ef7a67..fb05a951 100644 --- a/lib/plugins/wallet/zcashd/zcashd.js +++ b/lib/plugins/wallet/zcashd/zcashd.js @@ -1,3 +1,4 @@ +const _ = require('lodash/fp') const jsonRpc = require('../../common/json-rpc') const coinUtils = require('../../../coin-utils') @@ -42,6 +43,14 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) { return checkCryptoCode(cryptoCode) .then(() => fetch('sendtoaddress', [address, coins])) + .then((txId) => fetch('gettransaction', txId)) + .then((res) => _.pick(res, ['fee', 'txid'])) + .then((pickedObj) => { + return { + fee: BN(pickedObj.fee).abs().shift(unitScale).round(), + txid: pickedObj.txid + } + }) .catch(err => { if (err.code === -6) throw new E.InsufficientFundsError() throw err