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
}

View file

@ -84,10 +84,30 @@ function getStatus (account, toAddress, requested, cryptoCode) {
})
}
function newFunding (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
return getWallet(account)
.then(wallet => {
return wallet.createAddress()
.then(result => {
const fundingAddress = result.address
return wallet.setLabel({address: fundingAddress, label: 'Funding Address'})
.then(() => ({
fundingPendingBalance: BN(wallet.wallet.balance),
fundingConfirmedBalance: BN(wallet.wallet.confirmedBalance),
fundingAddress
}))
})
})
})
}
module.exports = {
NAME,
balance,
sendCoins,
newAddress,
getStatus
getStatus,
newFunding
}

View file

@ -21,7 +21,8 @@ module.exports = {
getStatus,
sweep,
defaultAddress,
supportsHd: true
supportsHd: true,
newFunding
}
if (!web3.isConnected()) {
@ -36,13 +37,13 @@ function sendCoins (account, toAddress, cryptoAtoms, cryptoCode) {
.then(pify(web3.eth.sendRawTransaction))
}
function validateCrypto (cryptoCode) {
function checkCryptoCode (cryptoCode) {
if (cryptoCode === 'ETH') return Promise.resolve()
return Promise.reject(new Error('cryptoCode must be ETH'))
}
function balance (account, cryptoCode) {
return validateCrypto(cryptoCode)
return checkCryptoCode(cryptoCode)
.then(() => pendingBalance(defaultAddress(account)))
}
@ -136,7 +137,7 @@ function newAddress (account, info) {
}
function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
return validateCrypto(cryptoCode)
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress))
.then(confirmed => {
if (confirmed.gte(cryptoAtoms)) return {status: 'confirmed'}
@ -164,3 +165,22 @@ function defaultHdNode (account) {
const key = hdkey.fromMasterSeed(masterSeed)
return key.derivePath(defaultPrefixPath)
}
function newFunding (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const fundingAddress = defaultAddress(account)
const promises = [
pendingBalance(fundingAddress),
confirmedBalance(fundingAddress)
]
return Promise.all(promises)
.then(([fundingPendingBalance, fundingConfirmedBalance]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
})
}

View file

@ -19,6 +19,24 @@ function balance (account, cryptoCode) {
})
}
function pendingBalance (account, cryptoCode) {
return Promise.resolve()
.then(() => {
if (cryptoCode === 'BTC') return BN(1e8 * 10.1)
if (cryptoCode === 'ETH') return BN(1e18 * 10.1)
throw new Error('Unsupported crypto: ' + cryptoCode)
})
}
function confirmedBalance (account, cryptoCode) {
return Promise.resolve()
.then(() => {
if (cryptoCode === 'BTC') return BN(1e8 * 10)
if (cryptoCode === 'ETH') return BN(1e18 * 10)
throw new Error('Unsupported crypto: ' + cryptoCode)
})
}
// Note: This makes it easier to test insufficient funds errors
let sendCount = 0
@ -50,6 +68,21 @@ function newAddress () {
return Promise.resolve('<Fake address, don\'t send>')
}
function newFunding (account, cryptoCode) {
const promises = [
pendingBalance(account, cryptoCode),
confirmedBalance(account, cryptoCode),
newAddress(account, {cryptoCode})
]
return Promise.all(promises)
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
}
function getStatus (account, toAddress, cryptoAtoms, cryptoCode) {
const elapsed = Date.now() - t0
@ -66,5 +99,6 @@ module.exports = {
balance,
sendCoins,
newAddress,
getStatus
getStatus,
newFunding
}