fix: RPC error handling

feat: create and load wallets
This commit is contained in:
Sérgio Salgado 2022-03-29 17:38:52 +01:00
parent f9ae681453
commit 0135d50416
7 changed files with 84 additions and 33 deletions

View file

@ -41,11 +41,11 @@ function fetch (account = {}, method, params) {
return r.data.result
})
.catch(err => {
throw new Error(_.join(' ', [
'json-rpc::axios error:',
JSON.stringify(_.get('message', err, '')),
JSON.stringify(_.get('response.data.error', err, ''))
]))
throw new Error(JSON.stringify({
responseMessage: _.get('message', err),
message: _.get('response.data.error.message', err),
code: _.get('response.data.error.code', err)
}))
})
}

View file

@ -14,6 +14,16 @@ function fetch (method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BCH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
@ -50,10 +60,7 @@ function sendCoins (account, tx, settings, operatorId) {
txid: pickedObj.txid
}
})
.catch(err => {
if (err.code === -6) throw new E.InsufficientFundsError()
throw err
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {

View file

@ -17,21 +17,49 @@ function fetch (method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -4:
return loadWallet()
case -5:
return logger.error(`${err}`)
case -6:
throw new E.InsufficientFundsError()
case -18:
return createWallet()
default:
throw e
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function createWallet () {
return fetch('createwallet', ['wallet'])
.catch(errorHandle)
}
function loadWallet () {
return fetch('loadwallet', ['wallet', true])
.catch(errorHandle)
}
function accountBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
function accountUnconfirmedBalance (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
// We want a balance that includes all spends (0 conf) but only deposits that
@ -75,10 +103,7 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
txid: pickedObj.txid
}
})
.catch(err => {
if (err.code === -6) throw new E.InsufficientFundsError()
throw err
})
.catch(errorHandle)
}
function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
@ -98,20 +123,19 @@ function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
}))
.catch(err => {
if (err.code === -6) throw new E.InsufficientFundsError()
throw err
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
.catch(errorHandle)
}
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
function confirmedBalance (address, cryptoCode) {
@ -156,6 +180,7 @@ function newFunding (account, cryptoCode, settings, operatorId) {
fundingConfirmedBalance,
fundingAddress
}))
.catch(console.log)
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
@ -169,7 +194,7 @@ function fetchRBF (txId) {
return [txId, res['bip125-replaceable']]
})
.catch(err => {
if (err.code === -5) logger.error(`${err.message}`)
errorHandle(err)
return [txId, true]
})
}

View file

@ -15,6 +15,16 @@ function fetch (method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'DASH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
@ -52,10 +62,7 @@ function sendCoins (account, tx, settings, operatorId) {
txid: pickedObj.txid
}
})
.catch(err => {
if (err.code === -6) throw new E.InsufficientFundsError()
throw err
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {

View file

@ -15,6 +15,16 @@ function fetch (method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'LTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
@ -52,10 +62,7 @@ function sendCoins (account, tx, settings, operatorId) {
txid: pickedObj.txid
}
})
.catch(err => {
if (err.code === -6) throw new E.InsufficientFundsError()
throw err
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {

View file

@ -16,6 +16,16 @@ function fetch (method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'ZEC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
@ -78,10 +88,7 @@ function sendCoins (account, tx, settings, operatorId) {
txid: pickedObj.txid
}
})
.catch(err => {
if (err.code === -6) throw new E.InsufficientFundsError()
throw err
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {