From 121f7d958747fdf07293ed1259fce6e6508bb510 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Salgado?= Date: Tue, 15 Feb 2022 05:41:46 +0000 Subject: [PATCH] fix: remove getStatus inner function calls fix: add method string to error handler fix: get mempool transaction balance --- lib/plugins/wallet/monerod/monerod.js | 58 +++++++++------------------ 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/lib/plugins/wallet/monerod/monerod.js b/lib/plugins/wallet/monerod/monerod.js index bc315bb4..3d070dee 100644 --- a/lib/plugins/wallet/monerod/monerod.js +++ b/lib/plugins/wallet/monerod/monerod.js @@ -34,7 +34,7 @@ function fetch (method, params) { return jsonRpc.fetchDigest(rpcConfig(), method, params) } -function handleError (error) { +function handleError (error, method) { switch(error.code) { case -13: { @@ -57,7 +57,7 @@ function handleError (error) { default: throw new Error( _.join(' ', [ - 'json-rpc::got error:', + `json-rpc::${method} error:`, JSON.stringify(_.get('message', error, '')), JSON.stringify(_.get('response.data.error', error, '')) ]) @@ -91,7 +91,7 @@ function accountBalance (cryptoCode) { .then(res => { return BN(res.unlocked_balance).decimalPlaces(0) }) - .catch(err => handleError(err)) + .catch(err => handleError(err, 'accountBalance')) } function balance (account, cryptoCode, settings, operatorId) { @@ -118,56 +118,34 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) { fee: BN(res.fee_list[0]).abs(), txid: res.tx_hash_list[0] })) - .catch(err => handleError(err)) + .catch(err => handleError(err, 'sendCoins')) } function newAddress (account, info, tx, settings, operatorId) { return checkCryptoCode(info.cryptoCode) .then(() => fetch('create_address', { account_index: 0 })) .then(res => res.address) - .catch(err => handleError(err)) -} - -function addressBalance (address, confirmations) { - return fetch('get_address_index', { address: address }) - .then(addressRes => fetch('get_balance', { account_index: addressRes.index.major, address_indices: [addressRes.index.minor] })) - .then(res => { - const addressInfo = _.find(it => it.address === address, res.per_subaddress) - return confirmations > 0 ? BN(addressInfo.unlocked_balance) : BN(addressInfo.balance) - }) - .catch(err => handleError(err)) -} - -function confirmedBalance (address, cryptoCode) { - return checkCryptoCode(cryptoCode) - .then(() => refreshWallet()) - .then(() => addressBalance(address, 1)) - .catch(err => handleError(err)) -} - -function pendingBalance (address, cryptoCode) { - return checkCryptoCode(cryptoCode) - .then(() => refreshWallet()) - .then(() => addressBalance(address, 0)) - .catch(err => handleError(err)) + .catch(err => handleError(err, 'newAddress')) } function getStatus (account, tx, requested, settings, operatorId) { const { toAddress, cryptoCode } = tx return checkCryptoCode(cryptoCode) .then(() => refreshWallet()) - .then(() => confirmedBalance(toAddress, cryptoCode)) - .then(confirmed => { - if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } + .then(() => fetch('get_address_index', { address: toAddress })) + .then(addressRes => fetch('get_transfers', { in: true, pool: true, account_index: addressRes.index.major, address_indices: [addressRes.index.minor] })) + .then(transferRes => { + const confirmedToAddress = _.filter(it => it.address === toAddress, transferRes.in ?? []) + const pendingToAddress = _.filter(it => it.address === toAddress, transferRes.pool ?? []) + const confirmed = _.reduce((acc, value) => acc.plus(value.amount), BN(0), confirmedToAddress) + const pending = _.reduce((acc, value) => acc.plus(value.amount), BN(0), pendingToAddress) - return pendingBalance(toAddress, cryptoCode) - .then(pending => { - if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' } - if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' } - return { receivedCryptoAtoms: pending, status: 'notSeen' } - }) + if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } + if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' } + if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' } + return { receivedCryptoAtoms: pending, status: 'notSeen' } }) - .catch(err => handleError(err)) + .catch(err => handleError(err, 'getStatus')) } function newFunding (account, cryptoCode, settings, operatorId) { @@ -191,7 +169,7 @@ function newFunding (account, cryptoCode, settings, operatorId) { fundingAddress: addressRes.address } }) - .catch(err => handleError(err)) + .catch(err => handleError(err, 'newFunding')) } function cryptoNetwork (account, cryptoCode, settings, operatorId) {