Merge pull request #1110 from chaotixkilla/fix-xmr-cashout-address-lookup

Fix XMR cash-out address balance lookup
This commit is contained in:
Rafael Taranto 2022-02-17 10:35:58 +00:00 committed by GitHub
commit a6c95f0f61

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,53 +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 => BN(res.unlocked_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) {
@ -175,12 +156,20 @@ function newFunding (account, cryptoCode, settings, operatorId) {
balanceRes,
fetch('create_address', { account_index: 0 })
]))
.then(([balanceRes, addressRes]) => ({
fundingPendingBalance: BN(balanceRes.balance).minus(balanceRes.unlocked_balance),
fundingConfirmedBalance: BN(balanceRes.unlocked_balance),
fundingAddress: addressRes.address
}))
.catch(err => handleError(err))
.then(([balanceRes, addressRes]) => Promise.all([
balanceRes,
addressRes,
fetch('get_transfers', { pool: true, account_index: 0 })
]))
.then(([balanceRes, addressRes, transferRes]) => {
const memPoolBalance = _.reduce((acc, value) => acc.plus(value.amount), BN(0), transferRes.pool)
return {
fundingPendingBalance: BN(balanceRes.balance).minus(balanceRes.unlocked_balance).plus(memPoolBalance),
fundingConfirmedBalance: BN(balanceRes.unlocked_balance),
fundingAddress: addressRes.address
}
})
.catch(err => handleError(err, 'newFunding'))
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {