chore: server code formatting

This commit is contained in:
Rafael Taranto 2025-05-12 15:35:00 +01:00
parent aedabcbdee
commit 68517170e2
234 changed files with 9824 additions and 6195 deletions

View file

@ -10,11 +10,11 @@ const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch (method, params) {
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
@ -24,115 +24,130 @@ function errorHandle (e) {
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BCH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'BCH')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance (cryptoCode) {
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance (cryptoCode) {
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// 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, settings, operatorId) {
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 1))
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 0))
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode)
.then(pending => {
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode })
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main',
)
}
function checkBlockchainStatus (cryptoCode) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready')
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress (cryptoCode, address) {
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('listreceivedbyaddress', [0, true, true, address]))
.then(txsByAddress => Promise.all(_.map(id => fetch('getrawtransaction', [id]), _.flatMap(it => it.txids, txsByAddress))))
.then(txsByAddress =>
Promise.all(
_.map(
id => fetch('getrawtransaction', [id]),
_.flatMap(it => it.txids, txsByAddress),
),
),
)
.then(_.map(({ hash }) => hash))
}
@ -144,5 +159,5 @@ module.exports = {
newFunding,
cryptoNetwork,
checkBlockchainStatus,
getTxHashesByAddress
getTxHashesByAddress,
}

View file

@ -6,7 +6,6 @@ const BN = require('../../../bn')
const E = require('../../../error')
const logger = require('../../../logger')
const { utils: coinUtils } = require('@lamassu/coins')
const { isDevMode } = require('../../../environment-helper')
const cryptoRec = coinUtils.getCryptoCurrency('BTC')
const unitScale = cryptoRec.unitScale
@ -15,11 +14,11 @@ const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
const SUPPORTS_BATCHING = true
function fetch (method, params) {
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -5:
@ -31,32 +30,40 @@ function errorHandle (e) {
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'BTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'BTC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance (cryptoCode) {
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalances'))
.then(({ mine }) => new BN(mine.trusted).shiftedBy(unitScale).decimalPlaces(0))
.then(({ mine }) =>
new BN(mine.trusted).shiftedBy(unitScale).decimalPlaces(0),
)
.catch(errorHandle)
}
function accountUnconfirmedBalance (cryptoCode) {
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalances'))
.then(({ mine }) => new BN(mine.untrusted_pending).plus(mine.immature).shiftedBy(unitScale).decimalPlaces(0))
.then(({ mine }) =>
new BN(mine.untrusted_pending)
.plus(mine.immature)
.shiftedBy(unitScale)
.decimalPlaces(0),
)
.catch(errorHandle)
}
// 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, settings, operatorId) {
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function estimateFee () {
function estimateFee() {
return getSatBEstimateFee()
.then(result => BN(result))
.catch(err => {
@ -64,26 +71,29 @@ function estimateFee () {
})
}
function calculateFeeDiscount (feeMultiplier = 1, unitScale) {
function calculateFeeDiscount(feeMultiplier = 1, unitScale) {
// 0 makes bitcoind do automatic fee selection
const AUTOMATIC_FEE = 0
return estimateFee()
.then(estimatedFee => {
if (!estimatedFee) {
logger.info('failure estimating fee, using bitcoind automatic fee selection')
return AUTOMATIC_FEE
}
// transform from sat/vB to BTC/kvB and apply the multipler
const newFee = estimatedFee.shiftedBy(-unitScale+3).times(feeMultiplier)
if (newFee.lt(0.00001) || newFee.gt(0.1)) {
logger.info('fee outside safety parameters, defaulting to automatic fee selection')
return AUTOMATIC_FEE
}
return newFee.toFixed(8)
})
return estimateFee().then(estimatedFee => {
if (!estimatedFee) {
logger.info(
'failure estimating fee, using bitcoind automatic fee selection',
)
return AUTOMATIC_FEE
}
// transform from sat/vB to BTC/kvB and apply the multipler
const newFee = estimatedFee.shiftedBy(-unitScale + 3).times(feeMultiplier)
if (newFee.lt(0.00001) || newFee.gt(0.1)) {
logger.info(
'fee outside safety parameters, defaulting to automatic fee selection',
)
return AUTOMATIC_FEE
}
return newFee.toFixed(8)
})
}
function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
function sendCoins(account, tx, settings, operatorId, feeMultiplier) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
@ -91,102 +101,113 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
.then(() => calculateFeeDiscount(feeMultiplier, unitScale))
.then(newFee => fetch('settxfee', [newFee]))
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function sendCoinsBatch (account, txs, cryptoCode, feeMultiplier) {
function sendCoinsBatch(account, txs, cryptoCode, feeMultiplier) {
return checkCryptoCode(cryptoCode)
.then(() => calculateFeeDiscount(feeMultiplier, unitScale))
.then(newFee => fetch('settxfee', [newFee]))
.then(() => _.reduce((acc, value) => ({
...acc,
[value.toAddress]: _.isNil(acc[value.toAddress])
? BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8)
: BN(acc[value.toAddress]).plus(BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8))
}), {}, txs))
.then((obj) => fetch('sendmany', ['', obj]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => ({
.then(() =>
_.reduce(
(acc, value) => ({
...acc,
[value.toAddress]: _.isNil(acc[value.toAddress])
? BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8)
: BN(acc[value.toAddress]).plus(
BN(value.cryptoAtoms).shiftedBy(-unitScale).toFixed(8),
),
}),
{},
txs,
),
)
.then(obj => fetch('sendmany', ['', obj]))
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => ({
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
txid: pickedObj.txid,
}))
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
.catch(errorHandle)
}
function addressBalance (address, confs) {
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
.catch(errorHandle)
}
function confirmedBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 1))
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 0))
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode)
.then(pending => {
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode })
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
.catch(errorHandle)
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main',
)
}
function fetchRBF (txId) {
function fetchRBF(txId) {
return fetch('getmempoolentry', [txId])
.then((res) => {
.then(res => {
return [txId, res['bip125-replaceable']]
})
.catch(err => {
@ -195,16 +216,23 @@ function fetchRBF (txId) {
})
}
function checkBlockchainStatus (cryptoCode) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready')
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress (cryptoCode, address) {
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('listreceivedbyaddress', [0, true, true, address]))
.then(txsByAddress => Promise.all(_.map(id => fetch('getrawtransaction', [id]), _.flatMap(it => it.txids, txsByAddress))))
.then(txsByAddress =>
Promise.all(
_.map(
id => fetch('getrawtransaction', [id]),
_.flatMap(it => it.txids, txsByAddress),
),
),
)
.then(_.map(({ hash }) => hash))
}
@ -220,5 +248,5 @@ module.exports = {
checkBlockchainStatus,
getTxHashesByAddress,
fetch,
SUPPORTS_BATCHING
SUPPORTS_BATCHING,
}

View file

@ -36,7 +36,7 @@ const getWallet = (account, cryptoCode) => {
return bitgo.coin(coin).wallets().get({ id: walletId })
}
function checkCryptoCode (cryptoCode) {
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
@ -44,26 +44,26 @@ function checkCryptoCode (cryptoCode) {
return Promise.resolve()
}
function getLegacyAddress (address, cryptoCode) {
function getLegacyAddress(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
return toLegacyAddress(address)
}
function getCashAddress (address, cryptoCode) {
function getCashAddress(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
return toCashAddress(address)
}
function formatToGetStatus (address, cryptoCode) {
function formatToGetStatus(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
const [part1, part2] = getLegacyAddress(address, cryptoCode).split(':')
return part2 || part1
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
@ -72,7 +72,7 @@ function sendCoins (account, tx, settings, operatorId) {
address: getLegacyAddress(toAddress, cryptoCode),
amount: cryptoAtoms.toNumber(),
walletPassphrase: account[`${cryptoCode}WalletPassphrase`],
enforceMinConfirmsForChange: false
enforceMinConfirmsForChange: false,
}
return wallet.send(params)
})
@ -83,51 +83,55 @@ function sendCoins (account, tx, settings, operatorId) {
return { txid: txid, fee: new BN(fee).decimalPlaces(0) }
})
.catch(err => {
if (err.message === 'insufficient funds') throw new E.InsufficientFundsError()
if (err.message === 'insufficient funds')
throw new E.InsufficientFundsError()
throw err
})
}
function balance (account, cryptoCode, settings, operatorId) {
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => new BN(wallet._wallet.spendableBalanceString))
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => getWallet(account, info.cryptoCode))
.then(wallet => {
return wallet.createAddress()
.then(result => {
const address = result.address
return wallet.createAddress().then(result => {
const address = result.address
// If a label was provided, set the label
if (info.label) {
return wallet.updateAddress({ address: address, label: info.label })
.then(() => getCashAddress(address, info.cryptoCode))
}
// If a label was provided, set the label
if (info.label) {
return wallet
.updateAddress({ address: address, label: info.label })
.then(() => getCashAddress(address, info.cryptoCode))
}
return getCashAddress(address, info.cryptoCode)
})
return getCashAddress(address, info.cryptoCode)
})
})
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => wallet.transfers({
type: 'receive',
address: formatToGetStatus(toAddress, cryptoCode)
}))
.then(wallet =>
wallet.transfers({
type: 'receive',
address: formatToGetStatus(toAddress, cryptoCode),
}),
)
.then(({ transfers }) => {
const filterConfirmed = _.filter(it =>
it.state === 'confirmed' && it.type === 'receive'
const filterConfirmed = _.filter(
it => it.state === 'confirmed' && it.type === 'receive',
)
const filterPending = _.filter(it =>
(it.state === 'confirmed' || it.state === 'unconfirmed') &&
it.type === 'receive'
const filterPending = _.filter(
it =>
(it.state === 'confirmed' || it.state === 'unconfirmed') &&
it.type === 'receive',
)
const sum = _.reduce((acc, val) => val.plus(acc), new BN(0))
@ -136,40 +140,43 @@ function getStatus (account, tx, requested, settings, operatorId) {
const confirmed = _.compose(sum, toBn, filterConfirmed)(transfers)
const pending = _.compose(sum, toBn, filterPending)(transfers)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => {
return getWallet(account, cryptoCode)
.then(wallet => {
return wallet.createAddress()
.then(result => {
const fundingAddress = result.address
return wallet.updateAddress({ address: fundingAddress, label: 'Funding Address' })
.then(() => ({
fundingPendingBalance: new BN(wallet._wallet.balance).minus(wallet._wallet.confirmedBalance),
fundingConfirmedBalance: new BN(wallet._wallet.confirmedBalance),
fundingAddress: getCashAddress(fundingAddress, cryptoCode)
}))
})
})
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => {
return getWallet(account, cryptoCode).then(wallet => {
return wallet.createAddress().then(result => {
const fundingAddress = result.address
return wallet
.updateAddress({ address: fundingAddress, label: 'Funding Address' })
.then(() => ({
fundingPendingBalance: new BN(wallet._wallet.balance).minus(
wallet._wallet.confirmedBalance,
),
fundingConfirmedBalance: new BN(wallet._wallet.confirmedBalance),
fundingAddress: getCashAddress(fundingAddress, cryptoCode),
}))
})
})
})
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => account.environment === 'test' ? 'test' : 'main')
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => Promise.resolve('ready'))
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
@ -180,5 +187,5 @@ module.exports = {
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus
checkBlockchainStatus,
}

View file

@ -11,11 +11,11 @@ const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch (method, params) {
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
@ -25,110 +25,124 @@ function errorHandle (e) {
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'DASH') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'DASH')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance (cryptoCode) {
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance (cryptoCode) {
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// 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, settings, operatorId) {
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 1))
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 0))
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode)
.then(pending => {
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode })
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus (cryptoCode) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready')
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress (cryptoCode, address) {
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('listreceivedbyaddress', [0, true, true, true, address]))
.then(txsByAddress => Promise.all(_.map(id => fetch('getrawtransaction', [id]), _.flatMap(it => it.txids, txsByAddress))))
.then(txsByAddress =>
Promise.all(
_.map(
id => fetch('getrawtransaction', [id]),
_.flatMap(it => it.txids, txsByAddress),
),
),
)
.then(_.map(({ hash }) => hash))
}
@ -139,5 +153,5 @@ module.exports = {
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress
getTxHashesByAddress,
}

View file

@ -7,16 +7,16 @@ const SUPPORTED_COINS = ['LN']
const BN = require('../../../bn')
function request (graphqlQuery, token, endpoint) {
function request(graphqlQuery, token, endpoint) {
const headers = {
'content-type': 'application/json',
'X-API-KEY': token
'X-API-KEY': token,
}
return axios({
method: 'post',
url: endpoint,
headers: headers,
data: graphqlQuery
data: graphqlQuery,
})
.then(r => {
if (r.error) throw r.error
@ -27,7 +27,7 @@ function request (graphqlQuery, token, endpoint) {
})
}
function checkCryptoCode (cryptoCode) {
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
@ -35,10 +35,10 @@ function checkCryptoCode (cryptoCode) {
return Promise.resolve()
}
function getTransactionsByAddress (token, endpoint, walletId, address) {
function getTransactionsByAddress(token, endpoint, walletId, address) {
const accountInfo = {
'operationName': 'me',
'query': `query me($walletId: WalletId!, , $address: OnChainAddress!) {
operationName: 'me',
query: `query me($walletId: WalletId!, , $address: OnChainAddress!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
@ -55,7 +55,7 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
}
}
}`,
'variables': { walletId, address }
variables: { walletId, address },
}
return request(accountInfo, token, endpoint)
.then(r => {
@ -66,10 +66,10 @@ function getTransactionsByAddress (token, endpoint, walletId, address) {
})
}
function getGaloyWallet (token, endpoint, walletId) {
function getGaloyWallet(token, endpoint, walletId) {
const accountInfo = {
'operationName': 'me',
'query': `query me($walletId: WalletId!) {
operationName: 'me',
query: `query me($walletId: WalletId!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
@ -80,7 +80,7 @@ function getGaloyWallet (token, endpoint, walletId) {
}
}
}`,
'variables': { walletId }
variables: { walletId },
}
return request(accountInfo, token, endpoint)
.then(r => {
@ -91,18 +91,18 @@ function getGaloyWallet (token, endpoint, walletId) {
})
}
function isLnInvoice (address) {
function isLnInvoice(address) {
return address.toLowerCase().startsWith('lnbc')
}
function isLnurl (address) {
function isLnurl(address) {
return address.toLowerCase().startsWith('lnurl')
}
function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
function sendFundsOnChain(walletId, address, cryptoAtoms, token, endpoint) {
const sendOnChain = {
'operationName': 'onChainPaymentSend',
'query': `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
operationName: 'onChainPaymentSend',
query: `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
onChainPaymentSend(input: $input) {
errors {
message
@ -111,18 +111,17 @@ function sendFundsOnChain (walletId, address, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { address, amount: cryptoAtoms.toString(), walletId } }
variables: { input: { address, amount: cryptoAtoms.toString(), walletId } },
}
return request(sendOnChain, token, endpoint)
.then(result => {
return result.data.onChainPaymentSend
})
return request(sendOnChain, token, endpoint).then(result => {
return result.data.onChainPaymentSend
})
}
function sendFundsLNURL (walletId, lnurl, cryptoAtoms, token, endpoint) {
function sendFundsLNURL(walletId, lnurl, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnurlPaymentSend',
'query': `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
operationName: 'lnurlPaymentSend',
query: `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
lnurlPaymentSend(input: $input) {
errors {
message
@ -131,15 +130,23 @@ function sendFundsLNURL (walletId, lnurl, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { 'lnurl': `${lnurl}`, 'walletId': `${walletId}`, 'amount': `${cryptoAtoms}` } }
variables: {
input: {
lnurl: `${lnurl}`,
walletId: `${walletId}`,
amount: `${cryptoAtoms}`,
},
},
}
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnurlPaymentSend)
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnurlPaymentSend,
)
}
function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
function sendFundsLN(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
'operationName': 'lnNoAmountInvoicePaymentSend',
'query': `mutation lnNoAmountInvoicePaymentSend($input: LnNoAmountInvoicePaymentInput!) {
operationName: 'lnNoAmountInvoicePaymentSend',
query: `mutation lnNoAmountInvoicePaymentSend($input: LnNoAmountInvoicePaymentInput!) {
lnNoAmountInvoicePaymentSend(input: $input) {
errors {
message
@ -148,15 +155,23 @@ function sendFundsLN (walletId, invoice, cryptoAtoms, token, endpoint) {
status
}
}`,
'variables': { 'input': { 'paymentRequest': invoice, walletId, amount: cryptoAtoms.toString() } }
variables: {
input: {
paymentRequest: invoice,
walletId,
amount: cryptoAtoms.toString(),
},
},
}
return request(sendLnNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoicePaymentSend)
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnNoAmountInvoicePaymentSend,
)
}
function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
function sendProbeRequest(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendProbeNoAmount = {
'operationName': 'lnNoAmountInvoiceFeeProbe',
'query': `mutation lnNoAmountInvoiceFeeProbe($input: LnNoAmountInvoiceFeeProbeInput!) {
operationName: 'lnNoAmountInvoiceFeeProbe',
query: `mutation lnNoAmountInvoiceFeeProbe($input: LnNoAmountInvoiceFeeProbeInput!) {
lnNoAmountInvoiceFeeProbe(input: $input) {
amount
errors {
@ -165,22 +180,48 @@ function sendProbeRequest (walletId, invoice, cryptoAtoms, token, endpoint) {
}
}
}`,
'variables': { 'input': { paymentRequest: invoice, walletId, amount: cryptoAtoms.toString() } }
variables: {
input: {
paymentRequest: invoice,
walletId,
amount: cryptoAtoms.toString(),
},
},
}
return request(sendProbeNoAmount, token, endpoint).then(result => result.data.lnNoAmountInvoiceFeeProbe)
return request(sendProbeNoAmount, token, endpoint).then(
result => result.data.lnNoAmountInvoiceFeeProbe,
)
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => {
if (isLnInvoice(toAddress)) {
return sendFundsLN(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsLN(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
if (isLnurl(toAddress)) {
return sendFundsLNURL(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsLNURL(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
return sendFundsOnChain(account.walletId, toAddress, cryptoAtoms, account.apiSecret, account.endpoint)
return sendFundsOnChain(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
})
.then(result => {
switch (result.status) {
@ -193,25 +234,33 @@ function sendCoins (account, tx, settings, operatorId) {
case 'PENDING':
return '<galoy transaction>'
default:
throw new Error(`Transaction failed: ${_.head(result.errors).message}`)
throw new Error(
`Transaction failed: ${_.head(result.errors).message}`,
)
}
})
}
function probeLN (account, cryptoCode, invoice) {
function probeLN(account, cryptoCode, invoice) {
const probeHardLimits = [200000, 1000000, 2000000]
const promises = probeHardLimits.map(limit => {
return sendProbeRequest(account.walletId, invoice, limit, account.apiSecret, account.endpoint)
.then(r => _.isEmpty(r.errors))
return sendProbeRequest(
account.walletId,
invoice,
limit,
account.apiSecret,
account.endpoint,
).then(r => _.isEmpty(r.errors))
})
return Promise.all(promises)
.then(results => _.zipObject(probeHardLimits, results))
return Promise.all(promises).then(results =>
_.zipObject(probeHardLimits, results),
)
}
function newOnChainAddress (walletId, token, endpoint) {
function newOnChainAddress(walletId, token, endpoint) {
const createOnChainAddress = {
'operationName': 'onChainAddressCreate',
'query': `mutation onChainAddressCreate($input: OnChainAddressCreateInput!) {
operationName: 'onChainAddressCreate',
query: `mutation onChainAddressCreate($input: OnChainAddressCreateInput!) {
onChainAddressCreate(input: $input) {
address
errors {
@ -220,41 +269,17 @@ function newOnChainAddress (walletId, token, endpoint) {
}
}
}`,
'variables': { 'input': { walletId } }
variables: { input: { walletId } },
}
return request(createOnChainAddress, token, endpoint)
.then(result => {
return result.data.onChainAddressCreate.address
})
return request(createOnChainAddress, token, endpoint).then(result => {
return result.data.onChainAddressCreate.address
})
}
function newNoAmountInvoice (walletId, token, endpoint) {
function newInvoice(walletId, cryptoAtoms, token, endpoint) {
const createInvoice = {
'operationName': 'lnNoAmountInvoiceCreate',
'query': `mutation lnNoAmountInvoiceCreate($input: LnNoAmountInvoiceCreateInput!) {
lnNoAmountInvoiceCreate(input: $input) {
errors {
message
path
}
invoice {
paymentRequest
}
}
}`,
'variables': { 'input': { walletId } }
}
return request(createInvoice, token, endpoint)
.then(result => {
return result.data.lnNoAmountInvoiceCreate.invoice.paymentRequest
})
}
function newInvoice (walletId, cryptoAtoms, token, endpoint) {
const createInvoice = {
'operationName': 'lnInvoiceCreate',
'query': `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
operationName: 'lnInvoiceCreate',
query: `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
lnInvoiceCreate(input: $input) {
errors {
message
@ -265,37 +290,44 @@ function newInvoice (walletId, cryptoAtoms, token, endpoint) {
}
}
}`,
'variables': { 'input': { walletId, amount: cryptoAtoms.toString() } }
variables: { input: { walletId, amount: cryptoAtoms.toString() } },
}
return request(createInvoice, token, endpoint)
.then(result => {
return result.data.lnInvoiceCreate.invoice.paymentRequest
})
return request(createInvoice, token, endpoint).then(result => {
return result.data.lnInvoiceCreate.invoice.paymentRequest
})
}
function balance (account, cryptoCode, settings, operatorId) {
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return new BN(wallet.balance || 0)
})
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info, tx) {
const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => newInvoice(account.walletId, cryptoAtoms, account.apiSecret, account.endpoint))
return checkCryptoCode(cryptoCode).then(() =>
newInvoice(
account.walletId,
cryptoAtoms,
account.apiSecret,
account.endpoint,
),
)
}
function getInvoiceStatus (token, endpoint, address) {
function getInvoiceStatus(token, endpoint, address) {
const query = {
'operationName': 'lnInvoicePaymentStatus',
'query': `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
operationName: 'lnInvoicePaymentStatus',
query: `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
lnInvoicePaymentStatus(input: $input) {
status
}
}`,
'variables': { input: { paymentRequest: address } }
variables: { input: { paymentRequest: address } },
}
return request(query, token, endpoint)
.then(r => {
@ -306,62 +338,89 @@ function getInvoiceStatus (token, endpoint, address) {
})
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const getBalance = _.reduce((acc, value) => {
acc[value.node.status] = acc[value.node.status].plus(new BN(value.node.settlementAmount))
return acc
}, { SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) })
const getBalance = _.reduce(
(acc, value) => {
acc[value.node.status] = acc[value.node.status].plus(
new BN(value.node.settlementAmount),
)
return acc
},
{ SUCCESS: new BN(0), PENDING: new BN(0), FAILURE: new BN(0) },
)
return checkCryptoCode(cryptoCode)
.then(() => {
const address = coinUtils.parseUrl(cryptoCode, account.environment, toAddress, false)
if (isLnInvoice(address)) {
return getInvoiceStatus(account.apiSecret, account.endpoint, address)
.then(it => {
const isPaid = it === 'PAID'
if (isPaid) return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
return { receivedCryptoAtoms: BN(0), status: 'notSeen' }
})
}
// On-chain and intra-ledger transactions
return getTransactionsByAddress(account.apiSecret, account.endpoint, account.walletId, address)
.then(transactions => {
const { SUCCESS: confirmed, PENDING: pending } = getBalance(transactions.edges)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return checkCryptoCode(cryptoCode).then(() => {
const address = coinUtils.parseUrl(
cryptoCode,
account.environment,
toAddress,
false,
)
if (isLnInvoice(address)) {
return getInvoiceStatus(
account.apiSecret,
account.endpoint,
address,
).then(it => {
const isPaid = it === 'PAID'
if (isPaid)
return { receivedCryptoAtoms: cryptoAtoms, status: 'confirmed' }
return { receivedCryptoAtoms: BN(0), status: 'notSeen' }
})
}
// On-chain and intra-ledger transactions
return getTransactionsByAddress(
account.apiSecret,
account.endpoint,
account.walletId,
address,
).then(transactions => {
const { SUCCESS: confirmed, PENDING: pending } = getBalance(
transactions.edges,
)
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
// Regular BTC address
return checkCryptoCode(cryptoCode)
.then(() => getGaloyWallet(account.apiSecret, account.endpoint, account.walletId))
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return newOnChainAddress(account.walletId, account.apiSecret, account.endpoint)
.then(onChainAddress => [onChainAddress, wallet.balance])
return newOnChainAddress(
account.walletId,
account.apiSecret,
account.endpoint,
).then(onChainAddress => [onChainAddress, wallet.balance])
})
.then(([onChainAddress, balance]) => {
return {
// with the old api is not possible to get pending balance
fundingPendingBalance: new BN(0),
fundingConfirmedBalance: new BN(balance),
fundingAddress: onChainAddress
fundingAddress: onChainAddress,
}
})
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => account.environment === 'test' ? 'test' : 'main')
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => Promise.resolve('ready'))
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
@ -373,5 +432,5 @@ module.exports = {
newFunding,
cryptoNetwork,
checkBlockchainStatus,
probeLN
probeLN,
}

View file

@ -6,7 +6,6 @@ const web3 = new Web3()
const hdkey = require('ethereumjs-wallet/hdkey')
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx')
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')
const Tx = require('ethereumjs-tx')
const { default: PQueue } = require('p-queue')
const util = require('ethereumjs-util')
const coins = require('@lamassu/coins')
@ -34,7 +33,7 @@ module.exports = {
connect,
checkBlockchainStatus,
getTxHashesByAddress,
_balance
_balance,
}
const SWEEP_QUEUE = new PQueue({
@ -55,93 +54,122 @@ const pify = _function => {
const logInfuraCall = call => {
if (!_.includes('infura', web3.currentProvider.host)) return
_.isNil(infuraCalls[call]) ? infuraCalls[call] = 1 : infuraCalls[call]++
logger.info(`Calling web3 method ${call} via Infura. Current count for this session: ${JSON.stringify(infuraCalls)}`)
_.isNil(infuraCalls[call]) ? (infuraCalls[call] = 1) : infuraCalls[call]++
logger.info(
`Calling web3 method ${call} via Infura. Current count for this session: ${JSON.stringify(infuraCalls)}`,
)
}
function connect (url) {
function connect(url) {
web3.setProvider(new web3.providers.HttpProvider(url))
}
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
function privateKey (account) {
function privateKey(account) {
return defaultWallet(account).getPrivateKey()
}
function isStrictAddress (cryptoCode, toAddress, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => util.isValidChecksumAddress(toAddress))
function isStrictAddress(cryptoCode, toAddress) {
return checkCryptoCode(cryptoCode).then(() =>
util.isValidChecksumAddress(toAddress),
)
}
function getTxHashesByAddress (cryptoCode, address) {
throw new Error(`Transactions hash retrieval is not implemented for this coin!`)
function getTxHashesByAddress() {
throw new Error(
`Transactions hash retrieval is not implemented for this coin!`,
)
}
function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
return SEND_QUEUE.add(() =>
(isErc20Token ? generateErc20Tx : generateTx)(toAddress, defaultWallet(account), cryptoAtoms, false, cryptoCode)
(isErc20Token ? generateErc20Tx : generateTx)(
toAddress,
defaultWallet(account),
cryptoAtoms,
false,
cryptoCode,
)
.then(pify(web3.eth.sendSignedTransaction))
.then(txid => {
return pify(web3.eth.getTransaction)(txid)
.then(tx => {
if (!tx) return { txid }
return pify(web3.eth.getTransaction)(txid).then(tx => {
if (!tx) return { txid }
const fee = new BN(tx.gas).times(new BN(tx.gasPrice)).decimalPlaces(0)
const fee = new BN(tx.gas).times(new BN(tx.gasPrice)).decimalPlaces(0)
return { txid, fee }
})
})
return { txid, fee }
})
}),
)
}
function checkCryptoCode (cryptoCode) {
function checkCryptoCode(cryptoCode) {
if (cryptoCode === 'ETH' || coins.utils.isErc20Token(cryptoCode)) {
return Promise.resolve(cryptoCode)
}
return Promise.reject(new Error('cryptoCode must be ETH'))
}
function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(code => confirmedBalance(defaultAddress(account), code))
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code =>
confirmedBalance(defaultAddress(account), code),
)
}
const pendingBalance = (address, cryptoCode) => {
const promises = [_balance(true, address, cryptoCode), _balance(false, address, cryptoCode)]
return Promise.all(promises).then(([pending, confirmed]) => BN(pending).minus(confirmed))
const promises = [
_balance(true, address, cryptoCode),
_balance(false, address, cryptoCode),
]
return Promise.all(promises).then(([pending, confirmed]) =>
BN(pending).minus(confirmed),
)
}
const confirmedBalance = (address, cryptoCode) => _balance(false, address, cryptoCode)
const confirmedBalance = (address, cryptoCode) =>
_balance(false, address, cryptoCode)
function _balance (includePending, address, cryptoCode) {
function _balance(includePending, address, cryptoCode) {
if (coins.utils.isErc20Token(cryptoCode)) {
const contract = new web3.eth.Contract(ABI.ERC20, coins.utils.getErc20Token(cryptoCode).contractAddress)
return contract.methods.balanceOf(address.toLowerCase()).call((_, balance) => {
return contract.methods.decimals().call((_, decimals) => BN(balance).div(10 ** decimals))
})
const contract = new web3.eth.Contract(
ABI.ERC20,
coins.utils.getErc20Token(cryptoCode).contractAddress,
)
return contract.methods
.balanceOf(address.toLowerCase())
.call((_, balance) => {
return contract.methods
.decimals()
.call((_, decimals) => BN(balance).div(10 ** decimals))
})
}
const block = includePending ? 'pending' : undefined
return pify(web3.eth.getBalance)(address.toLowerCase(), block)
/* NOTE: Convert bn.js bignum to bignumber.js bignum */
.then(balance => balance ? BN(balance) : BN(0))
return (
pify(web3.eth.getBalance)(address.toLowerCase(), block)
/* NOTE: Convert bn.js bignum to bignumber.js bignum */
.then(balance => (balance ? BN(balance) : BN(0)))
)
}
function generateErc20Tx (_toAddress, wallet, amount, includesFee, cryptoCode) {
function generateErc20Tx(_toAddress, wallet, amount, includesFee, cryptoCode) {
const fromAddress = '0x' + wallet.getAddress().toString('hex')
const toAddress = coins.utils.getErc20Token(cryptoCode).contractAddress
const contract = new web3.eth.Contract(ABI.ERC20, toAddress)
const contractData = contract.methods.transfer(_toAddress.toLowerCase(), hex(amount))
const contractData = contract.methods.transfer(
_toAddress.toLowerCase(),
hex(amount),
)
const txTemplate = {
from: fromAddress,
to: toAddress,
value: hex(BN(0)),
data: contractData.encodeABI()
data: contractData.encodeABI(),
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
@ -149,24 +177,22 @@ function generateErc20Tx (_toAddress, wallet, amount, includesFee, cryptoCode) {
const promises = [
pify(contractData.estimateGas)(txTemplate),
pify(web3.eth.getTransactionCount)(fromAddress),
pify(web3.eth.getBlock)('pending')
pify(web3.eth.getBlock)('pending'),
]
return Promise.all(promises)
.then(([gas, txCount, { baseFeePerGas }]) => [
BN(gas),
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
BN(baseFeePerGas)
BN(baseFeePerGas),
])
.then(([gas, txCount, baseFeePerGas]) => {
lastUsedNonces[fromAddress] = txCount
const maxPriorityFeePerGas = new BN(web3.utils.toWei('1.0', 'gwei')) // web3 default value
const maxFeePerGas = new BN(2).times(baseFeePerGas).plus(maxPriorityFeePerGas)
if (includesFee && (toSend.isNegative() || toSend.isZero())) {
throw new Error(`Trying to send a nil or negative amount (Transaction ID: ${txId} | Value provided: ${toSend.toNumber()}). This is probably caused due to the estimated fee being higher than the address' balance.`)
}
const maxFeePerGas = new BN(2)
.times(baseFeePerGas)
.plus(maxPriorityFeePerGas)
const rawTx = {
chainId: 1,
@ -177,7 +203,7 @@ function generateErc20Tx (_toAddress, wallet, amount, includesFee, cryptoCode) {
to: toAddress,
from: fromAddress,
value: hex(BN(0)),
data: contractData.encodeABI()
data: contractData.encodeABI(),
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
@ -189,7 +215,7 @@ function generateErc20Tx (_toAddress, wallet, amount, includesFee, cryptoCode) {
})
}
function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode, txId) {
function generateTx(_toAddress, wallet, amount, includesFee) {
const fromAddress = '0x' + wallet.getAddress().toString('hex')
const toAddress = _toAddress.toLowerCase()
@ -197,7 +223,7 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode, txId)
const txTemplate = {
from: fromAddress,
to: toAddress,
value: amount.toString()
value: amount.toString(),
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
@ -206,7 +232,7 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode, txId)
pify(web3.eth.estimateGas)(txTemplate),
pify(web3.eth.getGasPrice)(),
pify(web3.eth.getTransactionCount)(fromAddress),
pify(web3.eth.getBlock)('pending')
pify(web3.eth.getBlock)('pending'),
]
return Promise.all(promises)
@ -214,9 +240,9 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode, txId)
BN(gas),
BN(gasPrice),
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
BN(baseFeePerGas)
BN(baseFeePerGas),
])
.then(([gas, gasPrice, txCount, baseFeePerGas]) => {
.then(([gas, , txCount, baseFeePerGas]) => {
lastUsedNonces[fromAddress] = txCount
const maxPriorityFeePerGas = new BN(web3.utils.toWei('1.0', 'gwei')) // web3 default value
@ -234,7 +260,7 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode, txId)
gasLimit: hex(gas),
to: toAddress,
from: fromAddress,
value: hex(toSend)
value: hex(toSend),
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
@ -246,85 +272,97 @@ function generateTx (_toAddress, wallet, amount, includesFee, cryptoCode, txId)
})
}
function defaultWallet (account) {
function defaultWallet(account) {
return defaultHdNode(account).deriveChild(0).getWallet()
}
function defaultAddress (account) {
function defaultAddress(account) {
return defaultWallet(account).getChecksumAddressString()
}
function sweep (account, txId, cryptoCode, hdIndex, settings, operatorId) {
function sweep(account, txId, cryptoCode, hdIndex) {
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
const fromAddress = wallet.getChecksumAddressString()
return SWEEP_QUEUE.add(() => confirmedBalance(fromAddress, cryptoCode)
.then(r => {
return SWEEP_QUEUE.add(() =>
confirmedBalance(fromAddress, cryptoCode).then(r => {
if (r.eq(0)) return
return generateTx(defaultAddress(account), wallet, r, true, cryptoCode, txId)
.then(signedTx => pify(web3.eth.sendSignedTransaction)(signedTx))
})
return generateTx(
defaultAddress(account),
wallet,
r,
true,
cryptoCode,
txId,
).then(signedTx => pify(web3.eth.sendSignedTransaction)(signedTx))
}),
)
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info) {
const childNode = paymentHdNode(account).deriveChild(info.hdIndex)
return Promise.resolve(childNode.getWallet().getChecksumAddressString())
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(code => Promise.all([confirmedBalance(toAddress, code), code]))
.then(([confirmed, code]) => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, code)
.then(pending => {
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'published' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return pendingBalance(toAddress, code).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'published' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function paymentHdNode (account) {
function paymentHdNode(account) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derivePath(paymentPrefixPath)
}
function defaultHdNode (account) {
function defaultHdNode(account) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derivePath(defaultPrefixPath)
}
function newFunding (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(code => {
const fundingAddress = defaultAddress(account)
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code => {
const fundingAddress = defaultAddress(account)
const promises = [
pendingBalance(fundingAddress, code),
confirmedBalance(fundingAddress, code)
]
const promises = [
pendingBalance(fundingAddress, code),
confirmedBalance(fundingAddress, code),
]
return Promise.all(promises)
.then(([fundingPendingBalance, fundingConfirmedBalance]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
})
return Promise.all(promises).then(
([fundingPendingBalance, fundingConfirmedBalance]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
})
}
function checkBlockchainStatus (cryptoCode) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => connect(`http://localhost:${coins.utils.getCryptoCurrency(cryptoCode).defaultPort}`))
.then(() =>
connect(
`http://localhost:${coins.utils.getCryptoCurrency(cryptoCode).defaultPort}`,
),
)
.then(() => web3.eth.syncing)
.then(res => res === false ? 'ready' : 'syncing')
.then(res => (res === false ? 'ready' : 'syncing'))
}

View file

@ -8,7 +8,7 @@ const defaultPort = cryptoRec.defaultPort
const NAME = 'geth'
function run (account) {
function run() {
base.connect(`http://localhost:${defaultPort}`)
}

View file

@ -6,11 +6,13 @@ const { BALANCE_FETCH_SPEED_MULTIPLIER } = require('../../../constants')
const NAME = 'infura'
function run (account) {
if (!account.endpoint) throw new Error('Need to configure API endpoint for Infura')
function run(account) {
if (!account.endpoint)
throw new Error('Need to configure API endpoint for Infura')
const endpoint = _.startsWith('https://')(account.endpoint)
? account.endpoint : `https://${account.endpoint}`
? account.endpoint
: `https://${account.endpoint}`
base.connect(endpoint)
}
@ -18,24 +20,35 @@ function run (account) {
const txsCache = new NodeCache({
stdTTL: T.hour / 1000,
checkperiod: T.minute / 1000,
deleteOnExpire: true
deleteOnExpire: true,
})
function shouldGetStatus (tx) {
function shouldGetStatus(tx) {
const timePassedSinceTx = Date.now() - new Date(tx.created)
const timePassedSinceReq = Date.now() - new Date(txsCache.get(tx.id).lastReqTime)
const timePassedSinceReq =
Date.now() - new Date(txsCache.get(tx.id).lastReqTime)
if (timePassedSinceTx < 3 * T.minutes) return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 10 * T.seconds
if (timePassedSinceTx < 5 * T.minutes) return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 20 * T.seconds
if (timePassedSinceTx < 30 * T.minutes) return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.minute
if (timePassedSinceTx < 1 * T.hour) return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 2 * T.minute
if (timePassedSinceTx < 3 * T.hours) return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 5 * T.minute
if (timePassedSinceTx < 1 * T.day) return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.hour
if (timePassedSinceTx < 3 * T.minutes)
return (
_.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 10 * T.seconds
)
if (timePassedSinceTx < 5 * T.minutes)
return (
_.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 20 * T.seconds
)
if (timePassedSinceTx < 30 * T.minutes)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.minute
if (timePassedSinceTx < 1 * T.hour)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 2 * T.minute
if (timePassedSinceTx < 3 * T.hours)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > 5 * T.minute
if (timePassedSinceTx < 1 * T.day)
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.hour
return _.isNil(txsCache.get(tx.id).res) || timePassedSinceReq > T.hour
}
// Override geth's getStatus function to allow for different polling timing
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested, settings, operatorId) {
if (_.isNil(txsCache.get(tx.id))) {
txsCache.set(tx.id, { lastReqTime: Date.now() })
}
@ -45,7 +58,8 @@ function getStatus (account, tx, requested, settings, operatorId) {
return Promise.resolve(txsCache.get(tx.id).res)
}
return base.getStatus(account, tx, requested, settings, operatorId)
return base
.getStatus(account, tx, requested, settings, operatorId)
.then(res => {
if (res.status === 'confirmed') {
txsCache.del(tx.id) // Transaction reached final status, can trim it from the caching obj
@ -57,4 +71,9 @@ function getStatus (account, tx, requested, settings, operatorId) {
})
}
module.exports = _.merge(base, { NAME, run, getStatus, fetchSpeed: BALANCE_FETCH_SPEED_MULTIPLIER.SLOW })
module.exports = _.merge(base, {
NAME,
run,
getStatus,
fetchSpeed: BALANCE_FETCH_SPEED_MULTIPLIER.SLOW,
})

View file

@ -11,11 +11,11 @@ const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch (method, params) {
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
@ -25,107 +25,114 @@ function errorHandle (e) {
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'LTC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'LTC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance (cryptoCode) {
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance (cryptoCode) {
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// 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, settings, operatorId) {
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.then(() => fetch('sendtoaddress', [toAddress, coins]))
.then((txId) => fetch('gettransaction', [txId]))
.then((res) => _.pick(['fee', 'txid'], res))
.then((pickedObj) => {
.then(txId => fetch('gettransaction', [txId]))
.then(res => _.pick(['fee', 'txid'], res))
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 1))
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 0))
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode)
.then(pending => {
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode })
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus (cryptoCode) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => !!res['initialblockdownload'] ? 'syncing' : 'ready')
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress (cryptoCode, address) {
function getTxHashesByAddress() {
throw new Error(`Transactions hash retrieval not implemented for this coin!`)
}
@ -136,5 +143,5 @@ module.exports = {
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress
getTxHashesByAddress,
}

View file

@ -14,118 +14,150 @@ const SUPPORTED_COINS = coinUtils.cryptoCurrencies()
let t0
const checkCryptoCode = (cryptoCode) => !_.includes(cryptoCode, SUPPORTED_COINS)
? Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
: Promise.resolve()
const checkCryptoCode = cryptoCode =>
!_.includes(cryptoCode, SUPPORTED_COINS)
? Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
: Promise.resolve()
function _balance (cryptoCode) {
function _balance(cryptoCode) {
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
const unitScale = cryptoRec.unitScale
return new BN(10).shiftedBy(unitScale).decimalPlaces(0)
}
function balance (account, cryptoCode, settings, operatorId) {
return Promise.resolve()
.then(() => _balance(cryptoCode))
function balance(account, cryptoCode) {
return Promise.resolve().then(() => _balance(cryptoCode))
}
function pendingBalance (account, cryptoCode) {
return balance(account, cryptoCode)
.then(b => b.times(1.1))
function pendingBalance(account, cryptoCode) {
return balance(account, cryptoCode).then(b => b.times(1.1))
}
function confirmedBalance (account, cryptoCode) {
function confirmedBalance(account, cryptoCode) {
return balance(account, cryptoCode)
}
// Note: This makes it easier to test insufficient funds errors
let sendCount = 100
function isInsufficient (cryptoAtoms, cryptoCode) {
function isInsufficient(cryptoAtoms, cryptoCode) {
const b = _balance(cryptoCode)
return cryptoAtoms.gt(b.div(1000).times(sendCount))
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
sendCount++
return new Promise((resolve, reject) => {
setTimeout(() => {
if (isInsufficient(cryptoAtoms, cryptoCode)) {
console.log('[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode, cryptoAtoms.toString())
console.log(
'[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode,
cryptoAtoms.toString(),
)
return reject(new E.InsufficientFundsError())
}
console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s',
cryptoCode, cryptoAtoms.toString(), toAddress)
console.log(
'[%s] DEBUG: Mock wallet sending %s cryptoAtoms to %s',
cryptoCode,
cryptoAtoms.toString(),
toAddress,
)
return resolve({ txid: '<txHash>', fee: new BN(0) })
}, 2000)
})
}
function sendCoinsBatch (account, txs, cryptoCode) {
function sendCoinsBatch(account, txs, cryptoCode) {
sendCount = sendCount + txs.length
return new Promise((resolve, reject) => {
setTimeout(() => {
const cryptoSum = _.reduce((acc, value) => acc.plus(value.crypto_atoms), BN(0), txs)
const cryptoSum = _.reduce(
(acc, value) => acc.plus(value.crypto_atoms),
BN(0),
txs,
)
if (isInsufficient(cryptoSum, cryptoCode)) {
console.log('[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode, cryptoSum.toString())
console.log(
'[%s] DEBUG: Mock wallet insufficient funds: %s',
cryptoCode,
cryptoSum.toString(),
)
return reject(new E.InsufficientFundsError())
}
console.log('[%s] DEBUG: Mock wallet sending %s cryptoAtoms in a batch',
cryptoCode, cryptoSum.toString())
console.log(
'[%s] DEBUG: Mock wallet sending %s cryptoAtoms in a batch',
cryptoCode,
cryptoSum.toString(),
)
return resolve({ txid: '<txHash>', fee: BN(0) })
}, 2000)
})
}
function newAddress () {
function newAddress() {
t0 = Date.now()
return Promise.resolve('<Fake address, don\'t send>')
return Promise.resolve("<Fake address, don't send>")
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
const promises = [
pendingBalance(account, cryptoCode),
confirmedBalance(account, cryptoCode),
newAddress(account, { cryptoCode })
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
return Promise.all(promises).then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
fundingAddress,
}),
)
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
const elapsed = Date.now() - t0
if (elapsed < PUBLISH_TIME) return Promise.resolve({ receivedCryptoAtoms: new BN(0), status: 'notSeen' })
if (elapsed < AUTHORIZE_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'published' })
if (elapsed < CONFIRM_TIME) return Promise.resolve({ receivedCryptoAtoms: requested, status: 'authorized' })
if (elapsed < PUBLISH_TIME)
return Promise.resolve({
receivedCryptoAtoms: new BN(0),
status: 'notSeen',
})
if (elapsed < AUTHORIZE_TIME)
return Promise.resolve({
receivedCryptoAtoms: requested,
status: 'published',
})
if (elapsed < CONFIRM_TIME)
return Promise.resolve({
receivedCryptoAtoms: requested,
status: 'authorized',
})
console.log('[%s] DEBUG: Mock wallet has confirmed transaction [%s]', cryptoCode, toAddress.slice(0, 5))
console.log(
'[%s] DEBUG: Mock wallet has confirmed transaction [%s]',
cryptoCode,
toAddress.slice(0, 5),
)
return Promise.resolve({ status: 'confirmed' })
}
function getTxHashesByAddress (cryptoCode, address) {
return new Promise((resolve, reject) => {
function getTxHashesByAddress() {
return new Promise(resolve => {
setTimeout(() => {
return resolve([]) // TODO: should return something other than empty list?
}, 100)
})
}
function checkBlockchainStatus (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => Promise.resolve('ready'))
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
@ -137,5 +169,5 @@ module.exports = {
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress
getTxHashesByAddress,
}

View file

@ -13,59 +13,61 @@ const BLOCKCHAIN_DIR = process.env.BLOCKCHAIN_DIR
const cryptoRec = utils.getCryptoCurrency(COINS.XMR)
const configPath = utils.configPath(cryptoRec, BLOCKCHAIN_DIR)
const walletDir = path.resolve(utils.cryptoDir(cryptoRec, BLOCKCHAIN_DIR), 'wallets')
const walletDir = path.resolve(
utils.cryptoDir(cryptoRec, BLOCKCHAIN_DIR),
'wallets',
)
const DIGEST_QUEUE = new PQueue({
concurrency: 1,
interval: 150,
})
function createDigestRequest (account = {}, method, params = []) {
return DIGEST_QUEUE.add(() => jsonRpc.fetchDigest(account, method, params)
.then(res => {
function createDigestRequest(account = {}, method, params = []) {
return DIGEST_QUEUE.add(() =>
jsonRpc.fetchDigest(account, method, params).then(res => {
const r = JSON.parse(res)
if (r.error) throw r.error
return r.result
})
}),
)
}
function rpcConfig () {
function rpcConfig() {
try {
const config = jsonRpc.parseConf(configPath)
return {
username: config['rpc-login'].split(':')[0],
password: config['rpc-login'].split(':')[1],
port: cryptoRec.walletPort || cryptoRec.defaultPort
port: cryptoRec.walletPort || cryptoRec.defaultPort,
}
} catch (err) {
logger.error('Wallet is currently not installed!')
logger.error(`Wallet is currently not installed! ${err}`)
return {
username: '',
password: '',
port: cryptoRec.walletPort || cryptoRec.defaultPort
port: cryptoRec.walletPort || cryptoRec.defaultPort,
}
}
}
function fetch (method, params) {
function fetch(method, params) {
return createDigestRequest(rpcConfig(), method, params)
}
function handleError (error, method) {
switch(error.code) {
case -13:
{
if (
fs.existsSync(path.resolve(walletDir, 'Wallet')) &&
fs.existsSync(path.resolve(walletDir, 'Wallet.keys'))
) {
logger.debug('Found wallet! Opening wallet...')
return openWallet()
}
logger.debug('Couldn\'t find wallet! Creating...')
return createWallet()
function handleError(error, method) {
switch (error.code) {
case -13: {
if (
fs.existsSync(path.resolve(walletDir, 'Wallet')) &&
fs.existsSync(path.resolve(walletDir, 'Wallet.keys'))
) {
logger.debug('Found wallet! Opening wallet...')
return openWallet()
}
logger.debug("Couldn't find wallet! Creating...")
return createWallet()
}
case -21:
throw new Error('Wallet already exists!')
case -22:
@ -83,56 +85,63 @@ function handleError (error, method) {
_.join(' ', [
`json-rpc::${method} error:`,
JSON.stringify(_.get('message', error, '')),
JSON.stringify(_.get('response.data.error', error, ''))
])
JSON.stringify(_.get('response.data.error', error, '')),
]),
)
}
}
function openWallet () {
return fetch('open_wallet', { filename: 'Wallet' })
.catch(() => openWalletWithPassword())
function openWallet() {
return fetch('open_wallet', { filename: 'Wallet' }).catch(() =>
openWalletWithPassword(),
)
}
function openWalletWithPassword () {
return fetch('open_wallet', { filename: 'Wallet', password: rpcConfig().password })
function openWalletWithPassword() {
return fetch('open_wallet', {
filename: 'Wallet',
password: rpcConfig().password,
})
}
function createWallet () {
function createWallet() {
return fetch('create_wallet', { filename: 'Wallet', language: 'English' })
.then(() => new Promise(() => setTimeout(() => openWallet(), 3000)))
.then(() => fetch('auto_refresh'))
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'XMR') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'XMR')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function refreshWallet () {
return fetch('refresh')
.catch(err => handleError(err, 'refreshWallet'))
function refreshWallet() {
return fetch('refresh').catch(err => handleError(err, 'refreshWallet'))
}
function accountBalance (cryptoCode) {
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => fetch('get_balance', { account_index: 0, address_indices: [0] }))
.then(() =>
fetch('get_balance', { account_index: 0, address_indices: [0] }),
)
.then(res => {
return BN(res.unlocked_balance).decimalPlaces(0)
})
.catch(err => handleError(err, 'accountBalance'))
}
function balance (account, cryptoCode, settings, operatorId) {
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => fetch('transfer_split', {
.then(() =>
fetch('transfer_split', {
destinations: [{ amount: cryptoAtoms, address: toAddress }],
account_index: 0,
subaddr_indices: [],
@ -142,103 +151,143 @@ function sendCoins (account, tx, settings, operatorId, feeMultiplier) {
unlock_time: 0,
get_tx_hex: false,
new_algorithm: false,
get_tx_metadata: false
}))
get_tx_metadata: false,
}),
)
.then(res => ({
fee: BN(res.fee_list[0]).abs(),
txid: res.tx_hash_list[0]
txid: res.tx_hash_list[0],
}))
.catch(err => handleError(err, 'sendCoins'))
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('create_address', { account_index: 0 }))
.then(res => res.address)
.catch(err => handleError(err, 'newAddress'))
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => fetch('get_address_index', { address: toAddress }))
.then(addressRes => fetch('get_transfers', { in: true, pool: true, account_index: addressRes.index.major, subaddr_indices: [addressRes.index.minor] }))
.then(addressRes =>
fetch('get_transfers', {
in: true,
pool: true,
account_index: addressRes.index.major,
subaddr_indices: [addressRes.index.minor],
}),
)
.then(transferRes => {
const confirmedToAddress = _.filter(it => it.address === toAddress, transferRes.in ?? [])
const pendingToAddress = _.filter(it => it.address === toAddress, transferRes.pool ?? [])
const confirmed = _.reduce((acc, value) => acc.plus(value.amount), BN(0), confirmedToAddress)
const pending = _.reduce((acc, value) => acc.plus(value.amount), BN(0), pendingToAddress)
const confirmedToAddress = _.filter(
it => it.address === toAddress,
transferRes.in ?? [],
)
const pendingToAddress = _.filter(
it => it.address === toAddress,
transferRes.pool ?? [],
)
const confirmed = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
confirmedToAddress,
)
const pending = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
pendingToAddress,
)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
.catch(err => handleError(err, 'getStatus'))
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => Promise.all([
fetch('get_balance', { account_index: 0, address_indices: [0] }),
fetch('create_address', { account_index: 0 }),
fetch('get_transfers', { pool: true, account_index: 0 })
]))
.then(() =>
Promise.all([
fetch('get_balance', { account_index: 0, address_indices: [0] }),
fetch('create_address', { account_index: 0 }),
fetch('get_transfers', { pool: true, account_index: 0 }),
]),
)
.then(([balanceRes, addressRes, transferRes]) => {
const memPoolBalance = _.reduce((acc, value) => acc.plus(value.amount), BN(0), transferRes.pool)
const memPoolBalance = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
transferRes.pool,
)
return {
fundingPendingBalance: BN(balanceRes.balance).minus(balanceRes.unlocked_balance).plus(memPoolBalance),
fundingPendingBalance: BN(balanceRes.balance)
.minus(balanceRes.unlocked_balance)
.plus(memPoolBalance),
fundingConfirmedBalance: BN(balanceRes.unlocked_balance),
fundingAddress: addressRes.address
fundingAddress: addressRes.address,
}
})
.catch(err => handleError(err, 'newFunding'))
}
function cryptoNetwork (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => {
switch(parseInt(rpcConfig().port, 10)) {
case 18082:
return 'main'
case 28082:
return 'test'
case 38083:
return 'stage'
default:
return ''
}
})
}
function checkBlockchainStatus (cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
try {
const config = jsonRpc.parseConf(configPath)
// Daemon uses a different connection of the wallet
const rpcConfig = {
username: config['rpc-login'].split(':')[0],
password: config['rpc-login'].split(':')[1],
port: cryptoRec.defaultPort
}
return jsonRpc.fetchDigest(rpcConfig, 'get_info')
.then(res => !!res.synchronized ? 'ready' : 'syncing')
} catch (err) {
throw new Error('XMR daemon is currently not installed')
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => {
switch (parseInt(rpcConfig().port, 10)) {
case 18082:
return 'main'
case 28082:
return 'test'
case 38083:
return 'stage'
default:
return ''
}
})
}
function getTxHashesByAddress (cryptoCode, address) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => {
try {
const config = jsonRpc.parseConf(configPath)
// Daemon uses a different connection of the wallet
const rpcConfig = {
username: config['rpc-login'].split(':')[0],
password: config['rpc-login'].split(':')[1],
port: cryptoRec.defaultPort,
}
return jsonRpc
.fetchDigest(rpcConfig, 'get_info')
.then(res => (res.synchronized ? 'ready' : 'syncing'))
} catch (err) {
throw new Error(`XMR daemon is currently not installed. ${err}`)
}
})
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() => fetch('get_address_index', { address: address }))
.then(addressRes => fetch('get_transfers', { in: true, pool: true, pending: true, account_index: addressRes.index.major, subaddr_indices: [addressRes.index.minor] }))
.then(addressRes =>
fetch('get_transfers', {
in: true,
pool: true,
pending: true,
account_index: addressRes.index.major,
subaddr_indices: [addressRes.index.minor],
}),
)
.then(_.map(({ txid }) => txid))
}
@ -250,5 +299,5 @@ module.exports = {
newFunding,
cryptoNetwork,
checkBlockchainStatus,
getTxHashesByAddress
getTxHashesByAddress,
}

View file

@ -1,98 +0,0 @@
const https = require('https')
const BN = require('../../../bn')
const E = require('../../../error')
const _ = require('lodash/fp')
const SUPPORTED_COINS = ['BTC']
const axios = require('axios').create({
// TODO: get rejectUnauthorized true to work
baseURL: `${process.env.WALLET_URL}/api`,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
})
const checkCryptoCode = (cryptoCode) => !_.includes(cryptoCode, SUPPORTED_COINS)
? Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
: Promise.resolve()
function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(() => {
return axios.post('/balance', {
cryptoCode,
config: settings.config,
operatorId
})
})
.then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
return new BN(data.balance)
})
}
function sendCoins (account, tx, settings, operatorId) {
const { cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => {
return axios.post('/sendCoins', {
tx,
config: settings.config,
operatorId
})
})
.then(({ data }) => {
if (data.error && data.error.errorCode === 'sc-001') throw new E.InsufficientFundsError()
else if (data.error) throw new Error(JSON.stringify(data.error))
const fee = new BN(data.fee).decimalPlaces(0)
const txid = data.txid
return { txid, fee }
})
}
function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode)
.then(() => axios.post('/newAddress', {
info,
tx,
config: settings.config,
operatorId
}))
.then(({ data }) => {
if(data.error) throw new Error(JSON.stringify(data.error))
return data.newAddress
})
}
function getStatus (account, tx, requested, settings, operatorId) {
return checkCryptoCode(tx.cryptoCode)
.then(() => axios.get(`/balance/${tx.toAddress}?cryptoCode=${tx.cryptoCode}`))
.then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
const confirmed = new BN(data.confirmedBalance)
const pending = new BN(data.pendingBalance)
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
throw new E.NotImplementedError()
}
function sweep (account, txId, cryptoCode, hdIndex, settings, operatorId) {
throw new E.NotImplementedError()
}
module.exports = {
balance,
sendCoins,
newAddress,
newFunding,
getStatus,
sweep,
supportsHd: true,
}

View file

@ -14,46 +14,54 @@ const SWEEP_QUEUE = new PQueue({
interval: 250,
})
function checkCryptoCode (cryptoCode) {
function checkCryptoCode(cryptoCode) {
if (cryptoCode === 'TRX' || coins.utils.isTrc20Token(cryptoCode)) {
return Promise.resolve(cryptoCode)
}
return Promise.reject(new Error('cryptoCode must be TRX'))
}
function defaultWallet (account) {
function defaultWallet(account) {
const mnemonic = account.mnemonic
if (!mnemonic) throw new Error('No mnemonic seed!')
return TronWeb.fromMnemonic(mnemonic.replace(/[\r\n]/gm, ' ').trim(), `${DEFAULT_PREFIX_PATH}/0`)
return TronWeb.fromMnemonic(
mnemonic.replace(/[\r\n]/gm, ' ').trim(),
`${DEFAULT_PREFIX_PATH}/0`,
)
}
function paymentWallet (account, index) {
function paymentWallet(account, index) {
const mnemonic = account.mnemonic
if (!mnemonic) throw new Error('No mnemonic seed!')
return TronWeb.fromMnemonic(mnemonic.replace(/[\r\n]/gm, ' ').trim(), `${PAYMENT_PREFIX_PATH}/${index}`)
return TronWeb.fromMnemonic(
mnemonic.replace(/[\r\n]/gm, ' ').trim(),
`${PAYMENT_PREFIX_PATH}/${index}`,
)
}
function newAddress (account, info, tx, settings, operatorId) {
function newAddress(account, info) {
const wallet = paymentWallet(account, info.hdIndex)
return Promise.resolve(wallet.address)
}
function defaultAddress (account) {
function defaultAddress(account) {
return defaultWallet(account).address
}
function balance (account, cryptoCode, settings, operatorId) {
return checkCryptoCode(cryptoCode)
.then(code => confirmedBalance(defaultAddress(account), code))
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code =>
confirmedBalance(defaultAddress(account), code),
)
}
const confirmedBalance = (address, cryptoCode) => _balance(address, cryptoCode)
const _balance = async (address, cryptoCode) => {
if (coins.utils.isTrc20Token(cryptoCode)) {
const contractAddress = coins.utils.getTrc20Token(cryptoCode).contractAddress
const contractAddress =
coins.utils.getTrc20Token(cryptoCode).contractAddress
const { abi } = await tronWeb.trx.getContract(contractAddress)
const contract = tronWeb.contract(abi.entrys, contractAddress)
@ -70,7 +78,12 @@ const sendCoins = async (account, tx) => {
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
const rawTx = await txFunction(toAddress, defaultWallet(account), cryptoAtoms.toString(), cryptoCode)
const rawTx = await txFunction(
toAddress,
defaultWallet(account),
cryptoAtoms.toString(),
cryptoCode,
)
let response = null
@ -97,16 +110,26 @@ const generateTrc20Tx = async (toAddress, wallet, amount, cryptoCode) => {
const functionSelector = 'transfer(address,uint256)'
const parameters = [
{ type: 'address', value: tronWeb.address.toHex(toAddress) },
{ type: 'uint256', value: amount }
{ type: 'uint256', value: amount },
]
const tx = await tronWeb.transactionBuilder.triggerSmartContract(contractAddress, functionSelector, {}, parameters, wallet.address)
const tx = await tronWeb.transactionBuilder.triggerSmartContract(
contractAddress,
functionSelector,
{},
parameters,
wallet.address,
)
return tronWeb.trx.sign(tx.transaction, wallet.privateKey.slice(2))
}
const generateTx = async (toAddress, wallet, amount) => {
const transaction = await tronWeb.transactionBuilder.sendTrx(toAddress, amount, wallet.address)
const transaction = await tronWeb.transactionBuilder.sendTrx(
toAddress,
amount,
wallet.address,
)
const privateKey = wallet.privateKey
@ -114,21 +137,19 @@ const generateTx = async (toAddress, wallet, amount) => {
return tronWeb.trx.sign(transaction, privateKey.slice(2))
}
function newFunding (account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(code => {
const fundingAddress = defaultAddress(account)
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(code => {
const fundingAddress = defaultAddress(account)
return confirmedBalance(fundingAddress, code)
.then((balance) => ({
fundingPendingBalance: BN(0),
fundingConfirmedBalance: balance,
fundingAddress
}))
})
return confirmedBalance(fundingAddress, code).then(balance => ({
fundingPendingBalance: BN(0),
fundingConfirmedBalance: balance,
fundingAddress,
}))
})
}
function sweep (account, txId, cryptoCode, hdIndex) {
function sweep(account, txId, cryptoCode, hdIndex) {
const wallet = paymentWallet(account, hdIndex)
const fromAddress = wallet.address
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
@ -138,7 +159,12 @@ function sweep (account, txId, cryptoCode, hdIndex) {
return SWEEP_QUEUE.add(async () => {
const r = await confirmedBalance(fromAddress, cryptoCode)
if (r.eq(0)) return
const signedTx = await txFunction(defaultAddress(account), wallet, r.toString(), cryptoCode)
const signedTx = await txFunction(
defaultAddress(account),
wallet,
r.toString(),
cryptoCode,
)
let response = null
try {
response = await tronWeb.trx.sendRawTransaction(signedTx)
@ -157,24 +183,28 @@ function connect(account) {
const apiKey = account.apiKey
tronWeb = new TronWeb({
fullHost: endpoint,
headers: { "TRON-PRO-API-KEY": apiKey },
privateKey: '01'
headers: { 'TRON-PRO-API-KEY': apiKey },
privateKey: '01',
})
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(code => confirmedBalance(toAddress, code))
.then((confirmed) => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gt(0)) return { receivedCryptoAtoms: confirmed, status: 'insufficientFunds' }
.then(confirmed => {
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gt(0))
return { receivedCryptoAtoms: confirmed, status: 'insufficientFunds' }
return { receivedCryptoAtoms: 0, status: 'notSeen' }
})
}
function getTxHashesByAddress (cryptoCode, address) {
throw new Error(`Transactions hash retrieval is not implemented for this coin!`)
function getTxHashesByAddress() {
throw new Error(
`Transactions hash retrieval is not implemented for this coin!`,
)
}
module.exports = {

View file

@ -3,7 +3,7 @@ const base = require('../tron/base')
const NAME = 'trongrid'
function run (account) {
function run(account) {
const endpoint = 'https://api.trongrid.io'
base.connect({ ...account, endpoint })

View file

@ -1,5 +1,5 @@
const _ = require('lodash/fp')
const pRetry = require('p-retry')
const pRetry = require('p-retry')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
@ -12,11 +12,11 @@ const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch (method, params) {
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle (e) {
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
@ -26,134 +26,152 @@ function errorHandle (e) {
}
}
function checkCryptoCode (cryptoCode) {
if (cryptoCode !== 'ZEC') return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'ZEC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance (cryptoCode) {
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance (cryptoCode) {
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ unconfirmed_balance: balance }) => new BN(balance).shiftedBy(unitScale).decimalPlaces(0))
.then(({ unconfirmed_balance: balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
// 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, settings, operatorId) {
function balance(account, cryptoCode) {
return accountBalance(cryptoCode)
}
function sendCoins (account, tx, settings, operatorId) {
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
const checkSendStatus = function (opid) {
return new Promise((resolve, reject) => {
fetch('z_getoperationstatus', [[opid]])
.then(res => {
const status = _.get('status', res[0])
switch (status) {
case 'success':
resolve(res[0])
break
case 'failed':
throw new pRetry.AbortError(res[0].error)
case 'executing':
reject(new Error('operation still executing'))
break
}
})
fetch('z_getoperationstatus', [[opid]]).then(res => {
const status = _.get('status', res[0])
switch (status) {
case 'success':
resolve(res[0])
break
case 'failed':
throw new pRetry.AbortError(res[0].error)
case 'executing':
reject(new Error('operation still executing'))
break
}
})
})
}
const checker = opid => pRetry(() => checkSendStatus(opid), { retries: 20, minTimeout: 300, factor: 1.05 })
const checker = opid =>
pRetry(() => checkSendStatus(opid), {
retries: 20,
minTimeout: 300,
factor: 1.05,
})
return checkCryptoCode(cryptoCode)
.then(() => fetch('z_sendmany', ['ANY_TADDR', [{ address: toAddress, amount: coins }], null, null, 'NoPrivacy']))
.then(() =>
fetch('z_sendmany', [
'ANY_TADDR',
[{ address: toAddress, amount: coins }],
null,
null,
'NoPrivacy',
]),
)
.then(checker)
.then((res) => {
.then(res => {
return {
fee: _.get('params.fee', res),
txid: _.get('result.txid', res)
txid: _.get('result.txid', res),
}
})
.then((pickedObj) => {
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
function newAddress (account, info, tx, settings, operatorId) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode).then(() => fetch('getnewaddress'))
}
function addressBalance (address, confs) {
return fetch('getreceivedbyaddress', [address, confs])
.then(r => new BN(r).shiftedBy(unitScale).decimalPlaces(0))
function addressBalance(address, confs) {
return fetch('getreceivedbyaddress', [address, confs]).then(r =>
new BN(r).shiftedBy(unitScale).decimalPlaces(0),
)
}
function confirmedBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 1))
function confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance (address, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => addressBalance(address, 0))
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
function getStatus (account, tx, requested, settings, operatorId) {
function getStatus(account, tx, requested) {
const { toAddress, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => confirmedBalance(toAddress, cryptoCode))
.then(confirmed => {
if (confirmed.gte(requested)) return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
if (confirmed.gte(requested))
return { receivedCryptoAtoms: confirmed, status: 'confirmed' }
return pendingBalance(toAddress, cryptoCode)
.then(pending => {
if (pending.gte(requested)) return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0)) return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
return pendingBalance(toAddress, cryptoCode).then(pending => {
if (pending.gte(requested))
return { receivedCryptoAtoms: pending, status: 'authorized' }
if (pending.gt(0))
return { receivedCryptoAtoms: pending, status: 'insufficientFunds' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
})
}
function newFunding (account, cryptoCode, settings, operatorId) {
function newFunding(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode })
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress
}))
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus (cryptoCode) {
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => !!res['initial_block_download_complete'] ? 'ready' : 'syncing')
.then(res => (res['initial_block_download_complete'] ? 'ready' : 'syncing'))
}
function getTxHashesByAddress (cryptoCode, address) {
checkCryptoCode(cryptoCode)
.then(() => fetch('getaddresstxids', [address]))
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode).then(() => fetch('getaddresstxids', [address]))
}
module.exports = {
@ -163,5 +181,5 @@ module.exports = {
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress
getTxHashesByAddress,
}