handle cash-in errors
This commit is contained in:
parent
48dd23c11d
commit
86adfd0a63
7 changed files with 40 additions and 24 deletions
|
|
@ -5,6 +5,7 @@ const fs = require('fs')
|
|||
const pify = require('pify')
|
||||
|
||||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
|
||||
const NAME = 'Bitcoind'
|
||||
|
||||
|
|
@ -28,12 +29,6 @@ function initRpc () {
|
|||
return new RpcClient(rpcConfig)
|
||||
}
|
||||
|
||||
function richError (msg, name) {
|
||||
const err = new Error(msg)
|
||||
err.name = name
|
||||
return err
|
||||
}
|
||||
|
||||
/*
|
||||
* initialize RpcClient
|
||||
*/
|
||||
|
|
@ -68,7 +63,7 @@ function balance (account, cryptoCode) {
|
|||
if (err) return reject(err)
|
||||
|
||||
if (result.error) {
|
||||
return reject(richError(result.error, 'bitcoindError'))
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
resolve(BN(result.result).shift(SATOSHI_SHIFT).round())
|
||||
|
|
@ -85,15 +80,8 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
checkCryptoCode(cryptoCode)
|
||||
rpc.sendFrom(pluginConfig.account, address, bitcoins, confirmations, (err, result) => {
|
||||
if (err) {
|
||||
if (err.code === -6) {
|
||||
return reject(richError('Insufficient funds', 'InsufficientFunds'))
|
||||
}
|
||||
|
||||
if (err instanceof Error) {
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
return reject(richError(err.message, 'bitcoindError'))
|
||||
if (err.code === -6) return reject(new E.InsufficientFundsError())
|
||||
return reject(err)
|
||||
}
|
||||
|
||||
resolve(result.result)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
const BitGo = require('bitgo')
|
||||
const BN = require('../../../bn')
|
||||
|
||||
const E = require('../../../error')
|
||||
|
||||
const pjson = require('../../../../package.json')
|
||||
const userAgent = 'Lamassu-Server/' + pjson.version
|
||||
|
||||
|
|
@ -38,9 +40,7 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
|
|||
return result.hash
|
||||
})
|
||||
.catch(err => {
|
||||
if (err.message === 'Insufficient funds') {
|
||||
err.name = 'InsufficientFunds'
|
||||
}
|
||||
if (err.message === 'Insufficient funds') throw new E.InsufficientFundsError()
|
||||
throw err
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
const BN = require('../../../bn')
|
||||
const E = require('../../../error')
|
||||
|
||||
const NAME = 'FakeWallet'
|
||||
|
||||
|
|
@ -18,12 +19,18 @@ function balance (account, cryptoCode) {
|
|||
})
|
||||
}
|
||||
|
||||
function isInsufficient (cryptoCode) {
|
||||
if (cryptoCode === 'BTC') return BN(1e5 * 10)
|
||||
if (cryptoCode === 'ETH') return BN(1e18 * 0.25)
|
||||
throw new Error('Unsupported crypto: ' + cryptoCode)
|
||||
}
|
||||
function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
|
||||
return new Promise(resolve => {
|
||||
return new Promise((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s',
|
||||
cryptoCode, cryptoAtoms.toString(), toAddress)
|
||||
resolve('<txHash>')
|
||||
if (isInsufficient(cryptoCode)) return reject(new E.InsufficientFundsError())
|
||||
return resolve('<txHash>')
|
||||
}, 2000)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue