fix: remove getStatus inner function calls

fix: add method string to error handler
fix: get mempool transaction balance
This commit is contained in:
Sérgio Salgado 2022-02-15 05:41:46 +00:00
parent 2c9a8705db
commit 121f7d9587

View file

@ -34,7 +34,7 @@ function fetch (method, params) {
return jsonRpc.fetchDigest(rpcConfig(), method, params) return jsonRpc.fetchDigest(rpcConfig(), method, params)
} }
function handleError (error) { function handleError (error, method) {
switch(error.code) { switch(error.code) {
case -13: case -13:
{ {
@ -57,7 +57,7 @@ function handleError (error) {
default: default:
throw new Error( throw new Error(
_.join(' ', [ _.join(' ', [
'json-rpc::got error:', `json-rpc::${method} error:`,
JSON.stringify(_.get('message', error, '')), JSON.stringify(_.get('message', error, '')),
JSON.stringify(_.get('response.data.error', error, '')) JSON.stringify(_.get('response.data.error', error, ''))
]) ])
@ -91,7 +91,7 @@ function accountBalance (cryptoCode) {
.then(res => { .then(res => {
return BN(res.unlocked_balance).decimalPlaces(0) return BN(res.unlocked_balance).decimalPlaces(0)
}) })
.catch(err => handleError(err)) .catch(err => handleError(err, 'accountBalance'))
} }
function balance (account, cryptoCode, settings, operatorId) { function balance (account, cryptoCode, settings, operatorId) {
@ -118,56 +118,34 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
fee: BN(res.fee_list[0]).abs(), fee: BN(res.fee_list[0]).abs(),
txid: res.tx_hash_list[0] txid: res.tx_hash_list[0]
})) }))
.catch(err => handleError(err)) .catch(err => handleError(err, 'sendCoins'))
} }
function newAddress (account, info, tx, settings, operatorId) { function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode) return checkCryptoCode(info.cryptoCode)
.then(() => fetch('create_address', { account_index: 0 })) .then(() => fetch('create_address', { account_index: 0 }))
.then(res => res.address) .then(res => res.address)
.catch(err => handleError(err)) .catch(err => handleError(err, 'newAddress'))
}
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))
} }
function getStatus (account, tx, requested, settings, operatorId) { function getStatus (account, tx, requested, settings, operatorId) {
const { toAddress, cryptoCode } = tx const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => refreshWallet()) .then(() => refreshWallet())
.then(() => confirmedBalance(toAddress, cryptoCode)) .then(() => fetch('get_address_index', { address: toAddress }))
.then(confirmed => { .then(addressRes => fetch('get_transfers', { in: true, pool: true, account_index: addressRes.index.major, address_indices: [addressRes.index.minor] }))
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' } .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) if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
.then(pending => { if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' } if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' } return { receivedCryptoAtoms: pending, status: 'notSeen' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
}) })
.catch(err => handleError(err)) .catch(err => handleError(err, 'getStatus'))
} }
function newFunding (account, cryptoCode, settings, operatorId) { function newFunding (account, cryptoCode, settings, operatorId) {
@ -191,7 +169,7 @@ function newFunding (account, cryptoCode, settings, operatorId) {
fundingAddress: addressRes.address fundingAddress: addressRes.address
} }
}) })
.catch(err => handleError(err)) .catch(err => handleError(err, 'newFunding'))
} }
function cryptoNetwork (account, cryptoCode, settings, operatorId) { function cryptoNetwork (account, cryptoCode, settings, operatorId) {