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)
}
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) {