add support for funding

This commit is contained in:
Josh Harvey 2017-06-17 01:38:51 +03:00
parent 8440fa3b1d
commit bf0dc3f7f3
15 changed files with 731 additions and 356 deletions

View file

@ -50,25 +50,30 @@ function parseConf (confPath) {
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode)
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance (account, cryptoCode, confirmations) {
return checkCryptoCode(cryptoCode)
.then(() => {
const rpc = initRpc()
return new Promise((resolve, reject) => {
rpc.getBalance(pluginConfig.account, confirmations, (err, result) => {
if (err) return reject(err)
if (result.error) reject(new Error(err))
return resolve(BN(result.result).shift(SATOSHI_SHIFT).round())
})
})
})
}
// We want a balance that includes all spends (0 conf) but only deposits that
// have at least 1 confirmation. getbalance does this for us automatically.
function balance (account, cryptoCode) {
return new Promise((resolve, reject) => {
checkCryptoCode(cryptoCode)
const rpc = initRpc()
rpc.getBalance(pluginConfig.account, 1, (err, result) => {
if (err) return reject(err)
if (result.error) {
return reject(err)
}
resolve(BN(result.result).shift(SATOSHI_SHIFT).round())
})
})
return accountBalance(account, cryptoCode, 1)
}
function sendCoins (account, address, cryptoAtoms, cryptoCode) {
@ -76,26 +81,30 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const confirmations = 1
const bitcoins = cryptoAtoms.shift(-SATOSHI_SHIFT).toFixed(8)
return new Promise((resolve, reject) => {
checkCryptoCode(cryptoCode)
rpc.sendFrom(pluginConfig.account, address, bitcoins, confirmations, (err, result) => {
if (err) {
if (err.code === -6) return reject(new E.InsufficientFundsError())
return reject(err)
}
return checkCryptoCode(cryptoCode)
.then(() => {
return new Promise((resolve, reject) => {
rpc.sendFrom(pluginConfig.account, address, bitcoins, confirmations, (err, result) => {
if (err) {
if (err.code === -6) return reject(new E.InsufficientFundsError())
return reject(err)
}
resolve(result.result)
resolve(result.result)
})
})
})
}
function newAddress (account, info) {
return new Promise((resolve, reject) => {
checkCryptoCode(info.cryptoCode)
const rpc = initRpc()
rpc.getNewAddress((err, result) => {
if (err) return reject(err)
resolve(result.result)
return checkCryptoCode(info.cryptoCode)
.then(() => {
return new Promise((resolve, reject) => {
const rpc = initRpc()
rpc.getNewAddress((err, result) => {
if (err) return reject(err)
resolve(result.result)
})
})
})
}
@ -106,17 +115,23 @@ function addressBalance (address, confs) {
.then(r => BN(r.result).shift(SATOSHI_SHIFT).round())
}
const confirmedBalance = address => addressBalance(address, 1)
const pendingBalance = address => addressBalance(address, 0)
function confirmedBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 1))
}
function pendingBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 0))
}
function getStatus (account, toAddress, requested, cryptoCode) {
return Promise.resolve()
.then(() => checkCryptoCode(cryptoCode))
.then(() => confirmedBalance(toAddress))
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested)) return {status: 'confirmed'}
return pendingBalance(toAddress)
return pendingBalance(toAddress, cryptoCode)
.then(pending => {
if (pending.gte(requested)) return {status: 'authorized'}
if (pending.gt(0)) return {status: 'insufficientFunds'}
@ -125,10 +140,31 @@ function getStatus (account, toAddress, requested, cryptoCode) {
})
}
function newFunding (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountBalance(account, cryptoCode, 0),
accountBalance(account, cryptoCode, 1),
newAddress(account, {cryptoCode})
]
return Promise.all(promises)
})
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
}
module.exports = {
NAME,
balance,
pendingBalance,
confirmedBalance,
sendCoins,
newAddress,
getStatus
getStatus,
newFunding
}