add support for funding
This commit is contained in:
parent
8440fa3b1d
commit
bf0dc3f7f3
15 changed files with 731 additions and 356 deletions
|
|
@ -27,6 +27,7 @@ const login = require('./login')
|
|||
const pairing = require('./pairing')
|
||||
const server = require('./server')
|
||||
const transactions = require('./transactions')
|
||||
const funding = require('./funding')
|
||||
|
||||
const NEVER = new Date(Date.now() + 100 * T.years)
|
||||
const REAUTHENTICATE_INTERVAL = T.minute
|
||||
|
|
@ -122,6 +123,18 @@ app.post('/api/machines', (req, res) => {
|
|||
.then(() => dbNotify())
|
||||
})
|
||||
|
||||
app.get('/api/funding', (req, res) => {
|
||||
return funding.getFunding()
|
||||
.then(r => res.json(r))
|
||||
})
|
||||
|
||||
app.get('/api/funding/:cryptoCode', (req, res) => {
|
||||
const cryptoCode = req.params.cryptoCode
|
||||
|
||||
return funding.getFunding(cryptoCode)
|
||||
.then(r => res.json(r))
|
||||
})
|
||||
|
||||
app.get('/api/status', (req, res, next) => {
|
||||
return Promise.all([server.status(), config.validateCurrentConfig()])
|
||||
.then(([serverStatus, invalidConfigGroups]) => res.send({
|
||||
|
|
|
|||
61
lib/admin/funding.js
Normal file
61
lib/admin/funding.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
const BN = require('../bn')
|
||||
const settingsLoader = require('../settings-loader')
|
||||
const configManager = require('../config-manager')
|
||||
const wallet = require('../wallet')
|
||||
const ticker = require('../ticker')
|
||||
const coinUtils = require('../coin-utils')
|
||||
|
||||
module.exports = {getFunding}
|
||||
|
||||
function computeCrypto (cryptoCode, _balance) {
|
||||
const unitScale = coinUtils.coins[cryptoCode].unitScale
|
||||
|
||||
return BN(_balance).shift(-unitScale).round(5)
|
||||
}
|
||||
|
||||
function computeFiat (rate, cryptoCode, _balance) {
|
||||
const unitScale = coinUtils.coins[cryptoCode].unitScale
|
||||
|
||||
return BN(_balance).shift(-unitScale).mul(rate).round(5)
|
||||
}
|
||||
|
||||
function getFunding (cryptoCode) {
|
||||
cryptoCode = cryptoCode || 'BTC'
|
||||
const cryptoDisplays = coinUtils.cryptoDisplays
|
||||
|
||||
if (!coinUtils.coins[cryptoCode]) throw new Error(`Unsupported coin: ${cryptoCode}`)
|
||||
return settingsLoader.loadLatest()
|
||||
.then(settings => {
|
||||
const config = configManager.unscoped(settings.config)
|
||||
const fiatCode = config.fiatCurrency
|
||||
|
||||
const promises = [
|
||||
wallet.newFunding(settings, cryptoCode),
|
||||
ticker.getRates(settings, fiatCode, cryptoCode)
|
||||
]
|
||||
|
||||
return Promise.all(promises)
|
||||
.then(([fundingRec, ratesRec]) => {
|
||||
const rates = ratesRec.rates
|
||||
const rate = (rates.ask.add(rates.bid)).div(2)
|
||||
const fundingConfirmedBalance = fundingRec.fundingConfirmedBalance
|
||||
const fiatConfirmedBalance = computeFiat(rate, cryptoCode, fundingConfirmedBalance)
|
||||
const pending = fundingRec.fundingPendingBalance.sub(fundingConfirmedBalance)
|
||||
const fiatPending = computeFiat(rate, cryptoCode, pending)
|
||||
const fundingAddress = fundingRec.fundingAddress
|
||||
const fundingAddressUrl = coinUtils.buildUrl(cryptoCode, fundingAddress)
|
||||
|
||||
return {
|
||||
cryptoCode,
|
||||
cryptoDisplays,
|
||||
fundingAddress,
|
||||
fundingAddressUrl,
|
||||
confirmedBalance: computeCrypto(cryptoCode, fundingConfirmedBalance).toFormat(5),
|
||||
pending: computeCrypto(cryptoCode, pending).toFormat(5),
|
||||
fiatConfirmedBalance: fiatConfirmedBalance.toFormat(2),
|
||||
fiatPending: fiatPending.toFormat(2),
|
||||
fiatCode
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
19
lib/coin-utils.js
Normal file
19
lib/coin-utils.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
const coins = {
|
||||
BTC: {unitScale: 8},
|
||||
ETH: {unitScale: 18}
|
||||
}
|
||||
|
||||
const cryptoDisplays = [
|
||||
{cryptoCode: 'BTC', display: 'Bitcoin'},
|
||||
{cryptoCode: 'ETH', display: 'Ethereum'}
|
||||
]
|
||||
|
||||
module.exports = {coins, cryptoDisplays, buildUrl}
|
||||
|
||||
function buildUrl (cryptoCode, address) {
|
||||
switch (cryptoCode) {
|
||||
case 'BTC': return `bitcoin:${address}`
|
||||
case 'ETH': return `ethereum:${address}`
|
||||
default: throw new Error(`Unsupported crypto: ${cryptoCode}`)
|
||||
}
|
||||
}
|
||||
|
|
@ -19,6 +19,8 @@ function load (type, pluginCode) {
|
|||
throw new Error(`Unallowed plugin type: ${type}`)
|
||||
}
|
||||
|
||||
if (!pluginCode) throw new Error(`No plugin defined for ${type}`)
|
||||
|
||||
if (pluginCode.search(/[a-z0-9\-]/) === -1) {
|
||||
throw new Error(`Unallowed plugin name: ${pluginCode}`)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ const sms = require('./sms')
|
|||
const email = require('./email')
|
||||
const cashOutHelper = require('./cash-out-helper')
|
||||
const machineLoader = require('./machine-loader')
|
||||
const coinUtils = require('./coin-utils')
|
||||
|
||||
const mapValuesWithKey = _.mapValues.convert({cap: false})
|
||||
|
||||
|
|
@ -26,10 +27,7 @@ const STALE_BALANCE = 3 * T.minutes
|
|||
const PONG_TTL = '1 week'
|
||||
const tradesQueues = {}
|
||||
|
||||
const coins = {
|
||||
BTC: {unitScale: 8},
|
||||
ETH: {unitScale: 18}
|
||||
}
|
||||
const coins = coinUtils.coins
|
||||
|
||||
function plugins (settings, deviceId) {
|
||||
function buildRates (tickers) {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -68,6 +68,16 @@ function newAddress (settings, info) {
|
|||
.then(r => r.wallet.newAddress(r.account, info))
|
||||
}
|
||||
|
||||
function newFunding (settings, cryptoCode, address) {
|
||||
return fetchWallet(settings, cryptoCode)
|
||||
.then(r => {
|
||||
const wallet = r.wallet
|
||||
const account = r.account
|
||||
|
||||
return wallet.newFunding(account, cryptoCode)
|
||||
})
|
||||
}
|
||||
|
||||
function getWalletStatus (settings, tx) {
|
||||
return fetchWallet(settings, tx.cryptoCode)
|
||||
.then(r => r.wallet.getStatus(r.account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode))
|
||||
|
|
@ -135,5 +145,6 @@ module.exports = {
|
|||
newAddress,
|
||||
getStatus,
|
||||
sweep,
|
||||
isHd
|
||||
isHd,
|
||||
newFunding
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue