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