v12.0.0 - initial commit

This commit is contained in:
padreug 2025-12-31 19:04:13 +01:00
commit e2c49ea43c
1145 changed files with 97211 additions and 0 deletions

View file

@ -0,0 +1,163 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const BN = require('../../../bn')
const E = require('../../../error')
const { utils: coinUtils } = require('@lamassu/coins')
const cryptoRec = coinUtils.getCryptoCurrency('BCH')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'BCH')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.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) {
return accountBalance(cryptoCode)
}
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 => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
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 confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
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' }
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) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main',
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
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(_.map(({ hash }) => hash))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,252 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const { getSatBEstimateFee } = require('../../../blockexplorers/mempool.space')
const BN = require('../../../bn')
const E = require('../../../error')
const logger = require('../../../logger')
const { utils: coinUtils } = require('@lamassu/coins')
const cryptoRec = coinUtils.getCryptoCurrency('BTC')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
const SUPPORTS_BATCHING = true
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -5:
return logger.error(`${err}`)
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'BTC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalances'))
.then(({ mine }) =>
new BN(mine.trusted).shiftedBy(unitScale).decimalPlaces(0),
)
.catch(errorHandle)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getbalances'))
.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) {
return accountBalance(cryptoCode)
}
function estimateFee() {
return getSatBEstimateFee()
.then(result => BN(result))
.catch(err => {
logger.error('failure estimating fes', err)
})
}
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)
})
}
function sendCoins(account, tx, settings, operatorId, feeMultiplier) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const coins = cryptoAtoms.shiftedBy(-unitScale).toFixed(8)
return checkCryptoCode(cryptoCode)
.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 => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
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 => ({
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}))
.catch(errorHandle)
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => fetch('getnewaddress'))
.catch(errorHandle)
}
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 pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
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' }
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) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
.catch(errorHandle)
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main',
)
}
function fetchRBF(txId) {
return fetch('getmempoolentry', [txId])
.then(res => {
return [txId, res['bip125-replaceable']]
})
.catch(err => {
errorHandle(err)
return [txId, true]
})
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
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(_.map(({ hash }) => hash))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
fetchRBF,
sendCoinsBatch,
checkBlockchainStatus,
getTxHashesByAddress,
fetch,
SUPPORTS_BATCHING,
}

View file

@ -0,0 +1,191 @@
const _ = require('lodash/fp')
const { BitGoAPI } = require('@bitgo/sdk-api')
const { toLegacyAddress, toCashAddress } = require('bchaddrjs')
const BN = require('../../../bn')
const E = require('../../../error')
const pjson = require('../../../../package.json')
const userAgent = 'Lamassu-Server/' + pjson.version
const NAME = 'BitGo'
const BITGO_MODULES = {
BCH: require('@bitgo/sdk-coin-bch'),
BTC: require('@bitgo/sdk-coin-btc'),
DASH: require('@bitgo/sdk-coin-dash'),
LTC: require('@bitgo/sdk-coin-ltc'),
ZEC: require('@bitgo/sdk-coin-zec'),
}
const SUPPORTED_COINS = _.keys(BITGO_MODULES)
const BCH_CODES = ['BCH', 'TBCH']
const getWallet = (account, cryptoCode) => {
const accessToken = account.token.trim()
const env = account.environment === 'test' ? 'test' : 'prod'
const walletId = account[`${cryptoCode}WalletId`]
const bitgo = new BitGoAPI({ accessToken, env, userAgent })
BITGO_MODULES[cryptoCode].register(bitgo)
cryptoCode = cryptoCode.toLowerCase()
const coin = env === 'test' ? `t${cryptoCode}` : cryptoCode
return bitgo.coin(coin).wallets().get({ id: walletId })
}
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve()
}
function getLegacyAddress(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
return toLegacyAddress(address)
}
function getCashAddress(address, cryptoCode) {
if (!BCH_CODES.includes(cryptoCode)) return address
return toCashAddress(address)
}
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) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => {
const params = {
address: getLegacyAddress(toAddress, cryptoCode),
amount: cryptoAtoms.toNumber(),
walletPassphrase: account[`${cryptoCode}WalletPassphrase`],
enforceMinConfirmsForChange: false,
}
return wallet.send(params)
})
.then(result => {
let fee = parseFloat(result.transfer.feeString)
let txid = result.transfer.txid
return { txid: txid, fee: new BN(fee).decimalPlaces(0) }
})
.catch(err => {
if (err.message === 'insufficient funds')
throw new E.InsufficientFundsError()
throw err
})
}
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => getWallet(account, cryptoCode))
.then(wallet => new BN(wallet._wallet.spendableBalanceString))
}
function newAddress(account, info) {
return checkCryptoCode(info.cryptoCode)
.then(() => getWallet(account, info.cryptoCode))
.then(wallet => {
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))
}
return getCashAddress(address, info.cryptoCode)
})
})
}
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(({ transfers }) => {
const filterConfirmed = _.filter(
it => it.state === 'confirmed' && 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))
const toBn = _.map(it => new BN(it.valueString))
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' }
return { receivedCryptoAtoms: pending, status: 'notSeen' }
})
}
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) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
NAME,
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
}

View file

@ -0,0 +1,157 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../../../bn')
const E = require('../../../error')
const cryptoRec = coinUtils.getCryptoCurrency('DASH')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'DASH')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.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) {
return accountBalance(cryptoCode)
}
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 => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
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 confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
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' }
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) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
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(_.map(({ hash }) => hash))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,436 @@
const _ = require('lodash/fp')
const axios = require('axios')
const { utils: coinUtils } = require('@lamassu/coins')
const NAME = 'LN'
const SUPPORTED_COINS = ['LN']
const BN = require('../../../bn')
function request(graphqlQuery, token, endpoint) {
const headers = {
'content-type': 'application/json',
'X-API-KEY': token,
}
return axios({
method: 'post',
url: endpoint,
headers: headers,
data: graphqlQuery,
})
.then(r => {
if (r.error) throw r.error
return r.data
})
.catch(err => {
throw new Error(err)
})
}
function checkCryptoCode(cryptoCode) {
if (!SUPPORTED_COINS.includes(cryptoCode)) {
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
}
return Promise.resolve()
}
function getTransactionsByAddress(token, endpoint, walletId, address) {
const accountInfo = {
operationName: 'me',
query: `query me($walletId: WalletId!, , $address: OnChainAddress!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
transactionsByAddress (address: $address) {
edges {
node {
direction
settlementAmount
status
}
}
}
}
}
}
}`,
variables: { walletId, address },
}
return request(accountInfo, token, endpoint)
.then(r => {
return r.data.me.defaultAccount.walletById.transactionsByAddress
})
.catch(err => {
throw new Error(err)
})
}
function getGaloyWallet(token, endpoint, walletId) {
const accountInfo = {
operationName: 'me',
query: `query me($walletId: WalletId!) {
me {
defaultAccount {
walletById(walletId: $walletId) {
id
walletCurrency
balance
}
}
}
}`,
variables: { walletId },
}
return request(accountInfo, token, endpoint)
.then(r => {
return r.data.me.defaultAccount.walletById
})
.catch(err => {
throw new Error(err)
})
}
function isLnInvoice(address) {
return address.toLowerCase().startsWith('lnbc')
}
function isLnurl(address) {
return address.toLowerCase().startsWith('lnurl')
}
function sendFundsOnChain(walletId, address, cryptoAtoms, token, endpoint) {
const sendOnChain = {
operationName: 'onChainPaymentSend',
query: `mutation onChainPaymentSend($input: OnChainPaymentSendInput!) {
onChainPaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
variables: { input: { address, amount: cryptoAtoms.toString(), walletId } },
}
return request(sendOnChain, token, endpoint).then(result => {
return result.data.onChainPaymentSend
})
}
function sendFundsLNURL(walletId, lnurl, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
operationName: 'lnurlPaymentSend',
query: `mutation lnurlPaymentSend($input: LnurlPaymentSendInput!) {
lnurlPaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
variables: {
input: {
lnurl: `${lnurl}`,
walletId: `${walletId}`,
amount: `${cryptoAtoms}`,
},
},
}
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnurlPaymentSend,
)
}
function sendFundsLN(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendLnNoAmount = {
operationName: 'lnNoAmountInvoicePaymentSend',
query: `mutation lnNoAmountInvoicePaymentSend($input: LnNoAmountInvoicePaymentInput!) {
lnNoAmountInvoicePaymentSend(input: $input) {
errors {
message
path
}
status
}
}`,
variables: {
input: {
paymentRequest: invoice,
walletId,
amount: cryptoAtoms.toString(),
},
},
}
return request(sendLnNoAmount, token, endpoint).then(
result => result.data.lnNoAmountInvoicePaymentSend,
)
}
function sendProbeRequest(walletId, invoice, cryptoAtoms, token, endpoint) {
const sendProbeNoAmount = {
operationName: 'lnNoAmountInvoiceFeeProbe',
query: `mutation lnNoAmountInvoiceFeeProbe($input: LnNoAmountInvoiceFeeProbeInput!) {
lnNoAmountInvoiceFeeProbe(input: $input) {
amount
errors {
message
path
}
}
}`,
variables: {
input: {
paymentRequest: invoice,
walletId,
amount: cryptoAtoms.toString(),
},
},
}
return request(sendProbeNoAmount, token, endpoint).then(
result => result.data.lnNoAmountInvoiceFeeProbe,
)
}
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,
)
}
if (isLnurl(toAddress)) {
return sendFundsLNURL(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
}
return sendFundsOnChain(
account.walletId,
toAddress,
cryptoAtoms,
account.apiSecret,
account.endpoint,
)
})
.then(result => {
switch (result.status) {
case 'ALREADY_PAID':
throw new Error('Transaction already exists!')
case 'FAILURE':
throw new Error('Transaction failed!', JSON.stringify(result.errors))
case 'SUCCESS':
return '<galoy transaction>'
case 'PENDING':
return '<galoy transaction>'
default:
throw new Error(
`Transaction failed: ${_.head(result.errors).message}`,
)
}
})
}
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 Promise.all(promises).then(results =>
_.zipObject(probeHardLimits, results),
)
}
function newOnChainAddress(walletId, token, endpoint) {
const createOnChainAddress = {
operationName: 'onChainAddressCreate',
query: `mutation onChainAddressCreate($input: OnChainAddressCreateInput!) {
onChainAddressCreate(input: $input) {
address
errors {
message
path
}
}
}`,
variables: { input: { walletId } },
}
return request(createOnChainAddress, token, endpoint).then(result => {
return result.data.onChainAddressCreate.address
})
}
function newInvoice(walletId, cryptoAtoms, token, endpoint) {
const createInvoice = {
operationName: 'lnInvoiceCreate',
query: `mutation lnInvoiceCreate($input: LnInvoiceCreateInput!) {
lnInvoiceCreate(input: $input) {
errors {
message
path
}
invoice {
paymentRequest
}
}
}`,
variables: { input: { walletId, amount: cryptoAtoms.toString() } },
}
return request(createInvoice, token, endpoint).then(result => {
return result.data.lnInvoiceCreate.invoice.paymentRequest
})
}
function balance(account, cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
return new BN(wallet.balance || 0)
})
}
function newAddress(account, info, tx) {
const { cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode).then(() =>
newInvoice(
account.walletId,
cryptoAtoms,
account.apiSecret,
account.endpoint,
),
)
}
function getInvoiceStatus(token, endpoint, address) {
const query = {
operationName: 'lnInvoicePaymentStatus',
query: `query lnInvoicePaymentStatus($input: LnInvoicePaymentStatusInput!) {
lnInvoicePaymentStatus(input: $input) {
status
}
}`,
variables: { input: { paymentRequest: address } },
}
return request(query, token, endpoint)
.then(r => {
return r?.data?.lnInvoicePaymentStatus?.status
})
.catch(err => {
throw new Error(err)
})
}
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) },
)
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) {
// Regular BTC address
return checkCryptoCode(cryptoCode)
.then(() =>
getGaloyWallet(account.apiSecret, account.endpoint, account.walletId),
)
.then(wallet => {
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,
}
})
}
function cryptoNetwork(account, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() =>
account.environment === 'test' ? 'test' : 'main',
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => Promise.resolve('ready'))
}
module.exports = {
NAME,
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
probeLN,
}

View file

@ -0,0 +1,399 @@
'use strict'
const _ = require('lodash/fp')
const Web3 = require('web3')
const web3 = new Web3()
const hdkey = require('ethereumjs-wallet/hdkey')
const { FeeMarketEIP1559Transaction } = require('@ethereumjs/tx')
const { default: Common, Chain, Hardfork } = require('@ethereumjs/common')
const { default: PQueue } = require('p-queue')
const util = require('ethereumjs-util')
const coins = require('@lamassu/coins')
const _pify = require('pify')
const BN = require('../../../bn')
const ABI = require('../../tokens')
const logger = require('../../../logger')
const paymentPrefixPath = "m/44'/60'/0'/0'"
const defaultPrefixPath = "m/44'/60'/1'/0'"
let lastUsedNonces = {}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
sweep,
defaultAddress,
supportsHd: true,
newFunding,
privateKey,
isStrictAddress,
connect,
checkBlockchainStatus,
getTxHashesByAddress,
_balance,
}
const SWEEP_QUEUE = new PQueue({
concurrency: 3,
interval: 250,
})
const SEND_QUEUE = new PQueue({
concurrency: 1,
})
const delay = ms => new Promise(resolve => setTimeout(resolve, ms))
const QUEUE_EXECUTION_DELAY = 250
const nodeCalls = {}
const pify = _function => {
if (_.isString(_function.call)) logInfuraCall(_function.call)
return _pify(_function)
}
const logInfuraCall = call => {
_.isNil(nodeCalls[call]) ? (nodeCalls[call] = 1) : nodeCalls[call]++
logger.info(
`Calling web3 method ${call}. Current count for this session: ${JSON.stringify(nodeCalls)}`,
)
}
function connect(url) {
web3.setProvider(new web3.providers.HttpProvider(url))
}
const hex = bigNum => '0x' + bigNum.integerValue(BN.ROUND_DOWN).toString(16)
function privateKey(account) {
return defaultWallet(account).getPrivateKey()
}
function isStrictAddress(cryptoCode, toAddress) {
return checkCryptoCode(cryptoCode).then(() =>
util.isValidChecksumAddress(toAddress),
)
}
function getTxHashesByAddress() {
throw new Error(
`Transactions hash retrieval is not implemented for this coin!`,
)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const isErc20Token = coins.utils.isErc20Token(cryptoCode)
return SEND_QUEUE.add(() =>
delay(QUEUE_EXECUTION_DELAY)
.then(() => {
return (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) {
logger.warn(`sendCoins: Transaction ${txid} not found on chain`)
return { txid }
}
const fee = new BN(tx.gas).times(new BN(tx.gasPrice)).decimalPlaces(0)
return { txid, fee }
})
})
.catch(error => {
logger.error(`sendCoins: Error occurred - ${error.message}`, error)
throw error
}),
)
}
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) {
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 confirmedBalance = (address, cryptoCode) =>
_balance(false, 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 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)))
)
}
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 txTemplate = {
from: fromAddress,
to: toAddress,
value: hex(BN(0)),
data: contractData.encodeABI(),
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const promises = [
pify(contractData.estimateGas)(txTemplate),
pify(web3.eth.getTransactionCount)(fromAddress),
pify(web3.eth.getBlock)('pending'),
]
return Promise.all(promises)
.then(([gas, txCount, { baseFeePerGas }]) => [
BN(gas),
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
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)
logger.info(
`generateErc20Tx: Building transaction - nonce: ${txCount}, maxFeePerGas: ${maxFeePerGas.toString()}, gasLimit: ${gas.toString()}`,
)
const rawTx = {
chainId: 1,
nonce: txCount,
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
gasLimit: hex(gas),
to: toAddress,
from: fromAddress,
value: hex(BN(0)),
data: contractData.encodeABI(),
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
const privateKey = wallet.getPrivateKey()
const signedTx = tx.sign(privateKey)
return '0x' + signedTx.serialize().toString('hex')
})
.catch(error => {
logger.error(
`generateErc20Tx: Error during transaction generation - ${error.message}`,
error,
)
throw error
})
}
function generateTx(_toAddress, wallet, amount, includesFee) {
const fromAddress = '0x' + wallet.getAddress().toString('hex')
const toAddress = _toAddress.toLowerCase()
const txTemplate = {
from: fromAddress,
to: toAddress,
value: amount.toString(),
}
const common = new Common({ chain: Chain.Mainnet, hardfork: Hardfork.London })
const promises = [
pify(web3.eth.estimateGas)(txTemplate),
pify(web3.eth.getGasPrice)(),
pify(web3.eth.getTransactionCount)(fromAddress),
pify(web3.eth.getBlock)('pending'),
]
return Promise.all(promises)
.then(([gas, gasPrice, txCount, { baseFeePerGas }]) => [
BN(gas),
BN(gasPrice),
_.max([0, txCount, lastUsedNonces[fromAddress] + 1]),
BN(baseFeePerGas),
])
.then(([gas, , txCount, baseFeePerGas]) => {
lastUsedNonces[fromAddress] = txCount
const maxPriorityFeePerGas = new BN(web3.utils.toWei('1.0', 'gwei')) // web3 default value
const maxFeePerGas = baseFeePerGas.times(2).plus(maxPriorityFeePerGas)
const toSend = includesFee
? new BN(amount).minus(maxFeePerGas.times(gas))
: amount
const rawTx = {
chainId: 1,
nonce: txCount,
maxPriorityFeePerGas: web3.utils.toHex(maxPriorityFeePerGas),
maxFeePerGas: web3.utils.toHex(maxFeePerGas),
gasLimit: hex(gas),
to: toAddress,
from: fromAddress,
value: hex(toSend),
}
const tx = FeeMarketEIP1559Transaction.fromTxData(rawTx, { common })
const privateKey = wallet.getPrivateKey()
const signedTx = tx.sign(privateKey)
return '0x' + signedTx.serialize().toString('hex')
})
.catch(error => {
logger.error(
`generateTx: Error during transaction generation - ${error.message}`,
error,
)
throw error
})
}
function defaultWallet(account) {
return defaultHdNode(account).deriveChild(0).getWallet()
}
function defaultAddress(account) {
return defaultWallet(account).getChecksumAddressString()
}
function sweep(account, txId, cryptoCode, hdIndex) {
const wallet = paymentHdNode(account).deriveChild(hdIndex).getWallet()
const fromAddress = wallet.getChecksumAddressString()
return SWEEP_QUEUE.add(() =>
delay(QUEUE_EXECUTION_DELAY)
.then(() => 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))
}),
)
}
function newAddress(account, info) {
const childNode = paymentHdNode(account).deriveChild(info.hdIndex)
return Promise.resolve(childNode.getWallet().getChecksumAddressString())
}
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' }
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) {
const masterSeed = account.seed
if (!masterSeed) throw new Error('No master seed!')
const key = hdkey.fromMasterSeed(masterSeed)
return key.derivePath(paymentPrefixPath)
}
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) {
return checkCryptoCode(cryptoCode).then(code => {
const fundingAddress = defaultAddress(account)
const promises = [
pendingBalance(fundingAddress, code),
confirmedBalance(fundingAddress, code),
]
return Promise.all(promises).then(
([fundingPendingBalance, fundingConfirmedBalance]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
})
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() =>
connect(
`http://localhost:${coins.utils.getCryptoCurrency(cryptoCode).defaultPort}`,
),
)
.then(() => web3.eth.syncing)
.then(res => (res === false ? 'ready' : 'syncing'))
}

View file

@ -0,0 +1,15 @@
const _ = require('lodash/fp')
const base = require('./base')
const { utils: coinUtils } = require('@lamassu/coins')
const cryptoRec = coinUtils.getCryptoCurrency('ETH')
const defaultPort = cryptoRec.defaultPort
const NAME = 'geth'
function run() {
base.connect(`http://localhost:${defaultPort}`)
}
module.exports = _.merge(base, { NAME, run })

View file

@ -0,0 +1,79 @@
const _ = require('lodash/fp')
const NodeCache = require('node-cache')
const base = require('../geth/base')
const T = require('../../../time')
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')
const endpoint = _.startsWith('https://')(account.endpoint)
? account.endpoint
: `https://${account.endpoint}`
base.connect(endpoint)
}
const txsCache = new NodeCache({
stdTTL: T.hour / 1000,
checkperiod: T.minute / 1000,
deleteOnExpire: true,
})
function shouldGetStatus(tx) {
const timePassedSinceTx = Date.now() - new Date(tx.created)
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
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) {
if (_.isNil(txsCache.get(tx.id))) {
txsCache.set(tx.id, { lastReqTime: Date.now() })
}
// return last available response
if (!shouldGetStatus(tx)) {
return Promise.resolve(txsCache.get(tx.id).res)
}
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
} else {
txsCache.set(tx.id, { lastReqTime: Date.now(), res })
txsCache.ttl(tx.id, T.hour / 1000)
}
return res
})
}
module.exports = _.merge(base, {
NAME,
run,
getStatus,
fetchSpeed: BALANCE_FETCH_SPEED_MULTIPLIER.SLOW,
})

View file

@ -0,0 +1,147 @@
const _ = require('lodash/fp')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../../../bn')
const E = require('../../../error')
const cryptoRec = coinUtils.getCryptoCurrency('LTC')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'LTC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.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) {
return accountBalance(cryptoCode)
}
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 => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
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 confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
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' }
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) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initialblockdownload'] ? 'syncing' : 'ready'))
}
function getTxHashesByAddress() {
throw new Error(`Transactions hash retrieval not implemented for this coin!`)
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,175 @@
const _ = require('lodash/fp')
const BN = require('../../../bn')
const E = require('../../../error')
const { utils: coinUtils } = require('@lamassu/coins')
const NAME = 'FakeWallet'
const SECONDS = 1000
const PUBLISH_TIME = 3 * SECONDS
const AUTHORIZE_TIME = PUBLISH_TIME + 5 * SECONDS
const CONFIRM_TIME = AUTHORIZE_TIME + 10 * SECONDS
const SUPPORTED_COINS = coinUtils.cryptoCurrencies()
const SUPPORTS_BATCHING = true
let t0
const checkCryptoCode = cryptoCode =>
!_.includes(cryptoCode, SUPPORTED_COINS)
? Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
: Promise.resolve()
function _balance(cryptoCode) {
const cryptoRec = coinUtils.getCryptoCurrency(cryptoCode)
const unitScale = cryptoRec.unitScale
return new BN(10).shiftedBy(unitScale).decimalPlaces(0)
}
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 confirmedBalance(account, cryptoCode) {
return balance(account, cryptoCode)
}
// Note: This makes it easier to test insufficient funds errors
let sendCount = 100
function isInsufficient(cryptoAtoms, cryptoCode) {
const b = _balance(cryptoCode)
return cryptoAtoms.gt(b.div(1000).times(sendCount))
}
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(),
)
return reject(new E.InsufficientFundsError())
}
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) {
sendCount = sendCount + txs.length
return new Promise((resolve, reject) => {
setTimeout(() => {
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(),
)
return reject(new E.InsufficientFundsError())
}
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() {
t0 = Date.now()
return Promise.resolve("<Fake address, don't send>")
}
function newFunding(account, cryptoCode) {
const promises = [
pendingBalance(account, cryptoCode),
confirmedBalance(account, cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises).then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function getStatus(account, 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',
})
console.log(
'[%s] DEBUG: Mock wallet has confirmed transaction [%s]',
cryptoCode,
toAddress.slice(0, 5),
)
return Promise.resolve({ status: 'confirmed' })
}
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'))
}
module.exports = {
NAME,
SUPPORTS_BATCHING,
balance,
sendCoinsBatch,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,303 @@
const fs = require('fs')
const path = require('path')
const _ = require('lodash/fp')
const { COINS, utils } = require('@lamassu/coins')
const { default: PQueue } = require('p-queue')
const BN = require('../../../bn')
const E = require('../../../error')
const logger = require('../../../logger')
const jsonRpc = require('../../common/json-rpc')
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 DIGEST_QUEUE = new PQueue({
concurrency: 1,
interval: 150,
})
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() {
try {
const config = jsonRpc.parseConf(configPath)
return {
username: config['rpc-login'].split(':')[0],
password: config['rpc-login'].split(':')[1],
port: cryptoRec.walletPort || cryptoRec.defaultPort,
}
} catch (err) {
logger.error(`Wallet is currently not installed! ${err}`)
return {
username: '',
password: '',
port: cryptoRec.walletPort || cryptoRec.defaultPort,
}
}
}
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()
}
case -21:
throw new Error('Wallet already exists!')
case -22:
try {
return openWalletWithPassword()
} catch {
throw new Error('Invalid wallet password!')
}
case -17:
throw new E.InsufficientFundsError()
case -37:
throw new E.InsufficientFundsError()
default:
throw new Error(
_.join(' ', [
`json-rpc::${method} error:`,
JSON.stringify(_.get('message', error, '')),
JSON.stringify(_.get('response.data.error', error, '')),
]),
)
}
}
function openWallet() {
return fetch('open_wallet', { filename: 'Wallet' }).catch(() =>
openWalletWithPassword(),
)
}
function openWalletWithPassword() {
return fetch('open_wallet', {
filename: 'Wallet',
password: rpcConfig().password,
})
}
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))
return Promise.resolve()
}
function refreshWallet() {
return fetch('refresh').catch(err => handleError(err, 'refreshWallet'))
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.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) {
return accountBalance(cryptoCode)
}
function sendCoins(account, tx) {
const { toAddress, cryptoAtoms, cryptoCode } = tx
return checkCryptoCode(cryptoCode)
.then(() => refreshWallet())
.then(() =>
fetch('transfer_split', {
destinations: [{ amount: cryptoAtoms, address: toAddress }],
account_index: 0,
subaddr_indices: [],
priority: 0,
mixin: 6,
ring_size: 7,
unlock_time: 0,
get_tx_hex: false,
new_algorithm: false,
get_tx_metadata: false,
}),
)
.then(res => ({
fee: BN(res.fee_list[0]).abs(),
txid: res.tx_hash_list[0],
}))
.catch(err => handleError(err, 'sendCoins'))
}
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) {
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(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,
)
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) {
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(([balanceRes, addressRes, transferRes]) => {
const memPoolBalance = _.reduce(
(acc, value) => acc.plus(value.amount),
BN(0),
transferRes.pool,
)
return {
fundingPendingBalance: BN(balanceRes.balance)
.minus(balanceRes.unlocked_balance)
.plus(memPoolBalance),
fundingConfirmedBalance: BN(balanceRes.unlocked_balance),
fundingAddress: addressRes.address,
}
})
.catch(err => handleError(err, 'newFunding'))
}
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 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(_.map(({ txid }) => txid))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
cryptoNetwork,
checkBlockchainStatus,
getTxHashesByAddress,
}

View file

@ -0,0 +1,221 @@
const TronWeb = require('tronweb')
const coins = require('@lamassu/coins')
const { default: PQueue } = require('p-queue')
const BN = require('../../../bn')
let tronWeb = null
const DEFAULT_PREFIX_PATH = "m/44'/195'/0'/0"
const PAYMENT_PREFIX_PATH = "m/44'/195'/1'/0"
const SWEEP_QUEUE = new PQueue({
concurrency: 3,
interval: 250,
})
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) {
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`,
)
}
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}`,
)
}
function newAddress(account, info) {
const wallet = paymentWallet(account, info.hdIndex)
return Promise.resolve(wallet.address)
}
function defaultAddress(account) {
return defaultWallet(account).address
}
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 { abi } = await tronWeb.trx.getContract(contractAddress)
const contract = tronWeb.contract(abi.entrys, contractAddress)
const balance = await contract.methods.balanceOf(address).call()
return BN(balance.toString())
}
const balance = await tronWeb.trx.getBalance(address)
return balance ? BN(balance) : BN(0)
}
const sendCoins = async (account, tx) => {
const { toAddress, cryptoAtoms, cryptoCode } = tx
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
const rawTx = await txFunction(
toAddress,
defaultWallet(account),
cryptoAtoms.toString(),
cryptoCode,
)
let response = null
try {
response = await tronWeb.trx.sendRawTransaction(rawTx)
if (!response.result) throw new Error(response.code)
} catch (err) {
// for some reason err here is just a string
throw new Error(err)
}
const transaction = response.transaction
const txid = transaction.txID
const transactionInfo = tronWeb.trx.getTransactionInfo(txid)
if (!transactionInfo || !transactionInfo.fee) return { txid }
const fee = new BN(transactionInfo.fee).decimalPlaces(0)
return { txid, fee }
}
const generateTrc20Tx = async (toAddress, wallet, amount, cryptoCode) => {
const contractAddress = coins.utils.getTrc20Token(cryptoCode).contractAddress
const functionSelector = 'transfer(address,uint256)'
const parameters = [
{ type: 'address', value: tronWeb.address.toHex(toAddress) },
{ type: 'uint256', value: amount },
]
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 privateKey = wallet.privateKey
// their api return a hex string starting with 0x but expects without it
return tronWeb.trx.sign(transaction, privateKey.slice(2))
}
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,
}))
})
}
function sweep(account, txId, cryptoCode, hdIndex) {
const wallet = paymentWallet(account, hdIndex)
const fromAddress = wallet.address
const isTrc20Token = coins.utils.isTrc20Token(cryptoCode)
const txFunction = isTrc20Token ? generateTrc20Tx : generateTx
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,
)
let response = null
try {
response = await tronWeb.trx.sendRawTransaction(signedTx)
if (!response.result) throw new Error(response.code)
} catch (err) {
// for some reason err here is just a string
throw new Error(err)
}
return response
})
}
function connect(account) {
if (tronWeb != null) return
const endpoint = account.endpoint
const apiKey = account.apiKey
tronWeb = new TronWeb({
fullHost: endpoint,
headers: { 'TRON-PRO-API-KEY': apiKey },
privateKey: '01',
})
}
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' }
return { receivedCryptoAtoms: 0, status: 'notSeen' }
})
}
function getTxHashesByAddress() {
throw new Error(
`Transactions hash retrieval is not implemented for this coin!`,
)
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
sweep,
defaultAddress,
supportsHd: true,
newFunding,
connect,
getTxHashesByAddress,
}

View file

@ -0,0 +1,12 @@
const _ = require('lodash/fp')
const base = require('../tron/base')
const NAME = 'trongrid'
function run(account) {
const endpoint = 'https://api.trongrid.io'
base.connect({ ...account, endpoint })
}
module.exports = _.merge(base, { NAME, run })

View file

@ -0,0 +1,185 @@
const _ = require('lodash/fp')
const pRetry = require('p-retry')
const jsonRpc = require('../../common/json-rpc')
const { utils: coinUtils } = require('@lamassu/coins')
const BN = require('../../../bn')
const E = require('../../../error')
const cryptoRec = coinUtils.getCryptoCurrency('ZEC')
const unitScale = cryptoRec.unitScale
const rpcConfig = jsonRpc.rpcConfig(cryptoRec)
function fetch(method, params) {
return jsonRpc.fetch(rpcConfig, method, params)
}
function errorHandle(e) {
const err = JSON.parse(e.message)
switch (err.code) {
case -6:
throw new E.InsufficientFundsError()
default:
throw e
}
}
function checkCryptoCode(cryptoCode) {
if (cryptoCode !== 'ZEC')
return Promise.reject(new Error('Unsupported crypto: ' + cryptoCode))
return Promise.resolve()
}
function accountBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.then(({ balance }) =>
new BN(balance).shiftedBy(unitScale).decimalPlaces(0),
)
}
function accountUnconfirmedBalance(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getwalletinfo'))
.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) {
return accountBalance(cryptoCode)
}
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
}
})
})
}
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(checker)
.then(res => {
return {
fee: _.get('params.fee', res),
txid: _.get('result.txid', res),
}
})
.then(pickedObj => {
return {
fee: new BN(pickedObj.fee).abs().shiftedBy(unitScale).decimalPlaces(0),
txid: pickedObj.txid,
}
})
.catch(errorHandle)
}
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 confirmedBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 1))
}
function pendingBalance(address, cryptoCode) {
return checkCryptoCode(cryptoCode).then(() => addressBalance(address, 0))
}
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' }
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) {
return checkCryptoCode(cryptoCode)
.then(() => {
const promises = [
accountUnconfirmedBalance(cryptoCode),
accountBalance(cryptoCode),
newAddress(account, { cryptoCode }),
]
return Promise.all(promises)
})
.then(
([fundingPendingBalance, fundingConfirmedBalance, fundingAddress]) => ({
fundingPendingBalance,
fundingConfirmedBalance,
fundingAddress,
}),
)
}
function checkBlockchainStatus(cryptoCode) {
return checkCryptoCode(cryptoCode)
.then(() => fetch('getblockchaininfo'))
.then(res => (res['initial_block_download_complete'] ? 'ready' : 'syncing'))
}
function getTxHashesByAddress(cryptoCode, address) {
checkCryptoCode(cryptoCode).then(() => fetch('getaddresstxids', [address]))
}
module.exports = {
balance,
sendCoins,
newAddress,
getStatus,
newFunding,
checkBlockchainStatus,
getTxHashesByAddress,
}