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

@ -1,51 +1,53 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { utils: coinUtils } = require('@lamassu/coins')
const kraken = require('../exchange/kraken')
const bitstamp = require('../exchange/bitstamp')
const itbit = require('../exchange/itbit')
const binanceus = require('../exchange/binanceus')
const cex = require('../exchange/cex')
const bitpay = require('../ticker/bitpay')
const binance = require('../exchange/binance')
const bitfinex = require('../exchange/bitfinex')
const logger = require('../../logger')
const { BTC, BCH, DASH, ETH, LTC, ZEC, USDT, TRX, USDT_TRON, LN, USDC } = COINS
const ALL = {
cex: cex,
binanceus: binanceus,
kraken: kraken,
bitstamp: bitstamp,
itbit: itbit,
bitpay: bitpay,
binance: binance,
bitfinex: bitfinex
}
function buildMarket (fiatCode, cryptoCode, serviceName) {
if (!_.includes(cryptoCode, ALL[serviceName].CRYPTO)) {
throw new Error('Unsupported crypto: ' + cryptoCode)
}
if (_.isNil(fiatCode)) throw new Error('Market pair building failed: Missing fiat code')
return cryptoCode + '/' + fiatCode
}
function verifyFiatSupport (fiatCode, serviceName) {
const fiat = ALL[serviceName].FIAT
return fiat === 'ALL_CURRENCIES' ? true : _.includes(fiatCode, fiat)
}
function isConfigValid (config, fields) {
const values = _.map(it => _.get(it)(config))(fields)
return _.every(it => it || it === 0)(values)
}
function defaultFiatMarket (serviceName) {
return ALL[serviceName].DEFAULT_FIAT_MARKET
}
module.exports = { buildMarket, ALL, verifyFiatSupport, isConfigValid, defaultFiatMarket }
const _ = require('lodash/fp')
const kraken = require('../exchange/kraken')
const bitstamp = require('../exchange/bitstamp')
const itbit = require('../exchange/itbit')
const binanceus = require('../exchange/binanceus')
const cex = require('../exchange/cex')
const bitpay = require('../ticker/bitpay')
const binance = require('../exchange/binance')
const bitfinex = require('../exchange/bitfinex')
const ALL = {
cex: cex,
binanceus: binanceus,
kraken: kraken,
bitstamp: bitstamp,
itbit: itbit,
bitpay: bitpay,
binance: binance,
bitfinex: bitfinex,
}
function buildMarket(fiatCode, cryptoCode, serviceName) {
if (!_.includes(cryptoCode, ALL[serviceName].CRYPTO)) {
throw new Error('Unsupported crypto: ' + cryptoCode)
}
if (_.isNil(fiatCode))
throw new Error('Market pair building failed: Missing fiat code')
return cryptoCode + '/' + fiatCode
}
function verifyFiatSupport(fiatCode, serviceName) {
const fiat = ALL[serviceName].FIAT
return fiat === 'ALL_CURRENCIES' ? true : _.includes(fiatCode, fiat)
}
function isConfigValid(config, fields) {
const values = _.map(it => _.get(it)(config))(fields)
return _.every(it => it || it === 0)(values)
}
function defaultFiatMarket(serviceName) {
return ALL[serviceName].DEFAULT_FIAT_MARKET
}
module.exports = {
buildMarket,
ALL,
verifyFiatSupport,
isConfigValid,
defaultFiatMarket,
}

View file

@ -7,17 +7,19 @@ const request = require('request-promise')
const { utils: coinUtils } = require('@lamassu/coins')
const logger = require('../../logger')
const { isRemoteNode, isRemoteWallet } = require('../../environment-helper')
const { isRemoteWallet } = require('../../environment-helper')
const { isEnvironmentValid } = require('../../blockchain/install')
const BLOCKCHAIN_DIR = process.env.BLOCKCHAIN_DIR
module.exports = {
fetch, fetchDigest, parseConf, rpcConfig
fetch,
fetchDigest,
parseConf,
rpcConfig,
}
function fetch (account = {}, method, params) {
function fetch(account = {}, method, params) {
params = _.defaultTo([], params)
return Promise.resolve(true)
@ -25,18 +27,22 @@ function fetch (account = {}, method, params) {
const data = {
method,
params,
id: uuid.v4()
id: uuid.v4(),
}
if (_.isNil(account.port)) throw new Error('port attribute required for jsonRpc')
if (_.isNil(account.port))
throw new Error('port attribute required for jsonRpc')
const url = _.defaultTo(`http://${account.host}:${account.port}`, account.url)
const url = _.defaultTo(
`http://${account.host}:${account.port}`,
account.url,
)
return axios({
method: 'post',
auth: {username: account.username, password: account.password},
auth: { username: account.username, password: account.password },
url,
data
data,
})
})
.then(r => {
@ -44,17 +50,19 @@ function fetch (account = {}, method, params) {
return r.data.result
})
.catch(err => {
throw new Error(JSON.stringify({
responseMessage: _.get('message', err),
message: _.get('response.data.error.message', err),
code: _.get('response.data.error.code', err)
}))
throw new Error(
JSON.stringify({
responseMessage: _.get('message', err),
message: _.get('response.data.error.message', err),
code: _.get('response.data.error.code', err),
}),
)
})
}
function generateDigestOptions (account = {}, method, params) {
function generateDigestOptions(account = {}, method, params) {
const headers = {
'Content-Type': 'application/json'
'Content-Type': 'application/json',
}
const dataString = `{"jsonrpc":"2.0","id":"${uuid.v4()}","method":"${method}","params":${JSON.stringify(params)}}`
@ -68,31 +76,30 @@ function generateDigestOptions (account = {}, method, params) {
auth: {
user: account.username,
pass: account.password,
sendImmediately: false
}
sendImmediately: false,
},
}
return options
}
function fetchDigest(account = {}, method, params = []) {
return Promise.resolve(true)
.then(() => {
if (_.isNil(account.port))
throw new Error('port attribute required for jsonRpc')
return Promise.resolve(true).then(() => {
if (_.isNil(account.port))
throw new Error('port attribute required for jsonRpc')
const options = generateDigestOptions(account, method, params)
return request(options)
})
const options = generateDigestOptions(account, method, params)
return request(options)
})
}
function split (str) {
function split(str) {
const i = str.indexOf('=')
if (i === -1) return []
return [str.slice(0, i), str.slice(i + 1)]
}
function parseConf (confPath) {
function parseConf(confPath) {
const conf = fs.readFileSync(confPath)
const lines = conf.toString().split('\n')
@ -109,34 +116,36 @@ function parseConf (confPath) {
return res
}
function rpcConfig (cryptoRec) {
function rpcConfig(cryptoRec) {
try {
if (isRemoteWallet(cryptoRec) && isEnvironmentValid(cryptoRec)) {
return {
username: process.env[`${cryptoRec.cryptoCode}_NODE_USER`],
password: process.env[`${cryptoRec.cryptoCode}_NODE_PASSWORD`],
host: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_HOST`],
port: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_PORT`]
port: process.env[`${cryptoRec.cryptoCode}_NODE_RPC_PORT`],
}
}
const configPath = coinUtils.configPath(cryptoRec, BLOCKCHAIN_DIR)
const config = parseConf(configPath)
return {
username: config.rpcuser,
password: config.rpcpassword,
host: 'localhost',
port: config.rpcport || cryptoRec.defaultPort
port: config.rpcport || cryptoRec.defaultPort,
}
} catch (err) {
if (!isEnvironmentValid(cryptoRec)) {
logger.error('Environment is not correctly setup for remote wallet usage!')
logger.error(
`Environment is not correctly setup for remote wallet usage!, ${err}`,
)
} else {
logger.error('Wallet is currently not installed!')
}
return {
port: cryptoRec.defaultPort
port: cryptoRec.defaultPort,
}
}
}

View file

@ -2,5 +2,5 @@ module.exports = {
PENDING: 'PENDING',
RETRY: 'RETRY',
APPROVED: 'APPROVED',
REJECTED: 'REJECTED'
}
REJECTED: 'REJECTED',
}

View file

@ -1,6 +1,6 @@
const uuid = require('uuid')
const {APPROVED} = require('../consts')
const { APPROVED } = require('../consts')
const CODE = 'mock-compliance'
@ -8,18 +8,19 @@ const createLink = (settings, userId, level) => {
return `this is a mock external link, ${userId}, ${level}`
}
const getApplicantStatus = (account, userId) => {
const getApplicantStatus = account => {
return Promise.resolve({
service: CODE,
status: {
level: account.applicantLevel, answer: APPROVED
}
level: account.applicantLevel,
answer: APPROVED,
},
})
}
const createApplicant = () => {
return Promise.resolve({
id: uuid.v4()
id: uuid.v4(),
})
}
@ -27,5 +28,5 @@ module.exports = {
CODE,
createApplicant,
getApplicantStatus,
createLink
createLink,
}

View file

@ -4,7 +4,7 @@ const _ = require('lodash/fp')
const FormData = require('form-data')
const axiosConfig = {
baseURL: 'https://api.sumsub.com'
baseURL: 'https://api.sumsub.com',
}
const getSigBuilder = (apiToken, secretKey) => config => {
@ -25,10 +25,13 @@ const getSigBuilder = (apiToken, secretKey) => config => {
return config
}
const request = ((account, config) => {
const request = (account, config) => {
const instance = axios.create(axiosConfig)
instance.interceptors.request.use(getSigBuilder(account.apiToken, account.secretKey), Promise.reject)
instance.interceptors.request.use(
getSigBuilder(account.apiToken, account.secretKey),
Promise.reject,
)
return instance(config)
})
}
module.exports = request

View file

@ -2,7 +2,9 @@ const request = require('./request')
const createApplicant = (account, userId, level) => {
if (!userId || !level) {
return Promise.reject(`Missing required fields: userId: ${userId}, level: ${level}`)
return Promise.reject(
`Missing required fields: userId: ${userId}, level: ${level}`,
)
}
const config = {
@ -10,12 +12,12 @@ const createApplicant = (account, userId, level) => {
url: `/resources/applicants?levelName=${level}`,
data: {
externalUserId: userId,
sourceKey: 'lamassu'
sourceKey: 'lamassu',
},
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
Accept: 'application/json',
},
}
return request(account, config)
@ -23,7 +25,9 @@ const createApplicant = (account, userId, level) => {
const createLink = (account, userId, level) => {
if (!userId || !level) {
return Promise.reject(`Missing required fields: userId: ${userId}, level: ${level}`)
return Promise.reject(
`Missing required fields: userId: ${userId}, level: ${level}`,
)
}
const config = {
@ -31,8 +35,8 @@ const createLink = (account, userId, level) => {
url: `/resources/sdkIntegrations/levels/${level}/websdkLink?ttlInSecs=${600}&externalUserId=${userId}`,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
Accept: 'application/json',
},
}
return request(account, config)
@ -47,9 +51,9 @@ const getApplicantByExternalId = (account, id) => {
method: 'GET',
url: `/resources/applicants/-;externalUserId=${id}/one`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
return request(account, config)
@ -64,9 +68,9 @@ const getApplicantStatus = (account, id) => {
method: 'GET',
url: `/resources/applicants/${id}/status`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
return request(account, config)
@ -81,9 +85,9 @@ const getApplicantById = (account, id) => {
method: 'GET',
url: `/resources/applicants/${id}/one`,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
Accept: 'application/json',
'Content-Type': 'application/json',
},
}
return request(account, config)
@ -94,5 +98,5 @@ module.exports = {
createApplicant,
getApplicantByExternalId,
getApplicantById,
getApplicantStatus
getApplicantStatus,
}

View file

@ -6,48 +6,53 @@ const { PENDING, RETRY, APPROVED, REJECTED } = require('../consts')
const CODE = 'sumsub'
const getApplicantByExternalId = (account, userId) => {
return sumsubApi.getApplicantByExternalId(account, userId)
.then(r => r.data)
return sumsubApi.getApplicantByExternalId(account, userId).then(r => r.data)
}
const createApplicant = (account, userId, level) => {
return sumsubApi.createApplicant(account, userId, level)
return sumsubApi
.createApplicant(account, userId, level)
.then(r => r.data)
.catch(err => {
if (err.response.status === 409) return getApplicantByExternalId(account, userId)
if (err.response.status === 409)
return getApplicantByExternalId(account, userId)
throw err
})
}
const createLink = (account, userId, level) => {
return sumsubApi.createLink(account, userId, level)
.then(r => r.data.url)
return sumsubApi.createLink(account, userId, level).then(r => r.data.url)
}
const getApplicantStatus = (account, userId) => {
return sumsubApi.getApplicantByExternalId(account, userId)
.then(r => {
const levelName = _.get('data.review.levelName', r)
const reviewStatus = _.get('data.review.reviewStatus', r)
const reviewAnswer = _.get('data.review.reviewResult.reviewAnswer', r)
const reviewRejectType = _.get('data.review.reviewResult.reviewRejectType', r)
return sumsubApi.getApplicantByExternalId(account, userId).then(r => {
const levelName = _.get('data.review.levelName', r)
const reviewStatus = _.get('data.review.reviewStatus', r)
const reviewAnswer = _.get('data.review.reviewResult.reviewAnswer', r)
const reviewRejectType = _.get(
'data.review.reviewResult.reviewRejectType',
r,
)
// if last review was from a different level, return the current level and RETRY
if (levelName !== account.applicantLevel) return { level: account.applicantLevel, answer: RETRY }
// if last review was from a different level, return the current level and RETRY
if (levelName !== account.applicantLevel)
return { level: account.applicantLevel, answer: RETRY }
let answer = PENDING
if (reviewStatus === 'init') answer = RETRY
if (reviewAnswer === 'GREEN' && reviewStatus === 'completed') answer = APPROVED
if (reviewAnswer === 'RED' && reviewRejectType === 'RETRY') answer = RETRY
if (reviewAnswer === 'RED' && reviewRejectType === 'FINAL') answer = REJECTED
let answer = PENDING
if (reviewStatus === 'init') answer = RETRY
if (reviewAnswer === 'GREEN' && reviewStatus === 'completed')
answer = APPROVED
if (reviewAnswer === 'RED' && reviewRejectType === 'RETRY') answer = RETRY
if (reviewAnswer === 'RED' && reviewRejectType === 'FINAL')
answer = REJECTED
return { level: levelName, answer }
})
return { level: levelName, answer }
})
}
module.exports = {
CODE,
createApplicant,
getApplicantStatus,
createLink
}
createLink,
}

View file

@ -2,29 +2,29 @@ const Mailgun = require('mailgun-js')
const NAME = 'Mailgun'
function sendMessage ({apiKey, domain, fromEmail, toEmail}, rec) {
const mailgun = Mailgun({apiKey, domain})
function sendMessage({ apiKey, domain, fromEmail, toEmail }, rec) {
const mailgun = Mailgun({ apiKey, domain })
const to = rec.email.toEmail ?? toEmail
const emailData = {
from: `Lamassu Server ${fromEmail}`,
to,
subject: rec.email.subject,
text: rec.email.body
text: rec.email.body,
}
return mailgun.messages().send(emailData)
}
function sendCustomerMessage ({apiKey, domain, fromEmail}, rec) {
const mailgun = Mailgun({apiKey, domain})
function sendCustomerMessage({ apiKey, domain, fromEmail }, rec) {
const mailgun = Mailgun({ apiKey, domain })
const to = rec.email.toEmail
const emailData = {
from: fromEmail,
to,
subject: rec.email.subject,
text: rec.email.body
text: rec.email.body,
}
return mailgun.messages().send(emailData)
@ -33,5 +33,5 @@ function sendCustomerMessage ({apiKey, domain, fromEmail}, rec) {
module.exports = {
NAME,
sendMessage,
sendCustomerMessage
sendCustomerMessage,
}

View file

@ -1,6 +1,6 @@
const NAME = 'mock-email'
function sendMessage (settings, rec) {
function sendMessage(settings, rec) {
console.log('sending email', rec)
}
@ -11,5 +11,5 @@ function sendCustomerMessage(settings, rec) {
module.exports = {
NAME,
sendMessage,
sendCustomerMessage
sendCustomerMessage,
}

View file

@ -10,12 +10,19 @@ const FIAT = ['EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const loadConfig = (account) => {
const loadConfig = account => {
const mapper = {
'privateKey': 'secret'
privateKey: 'secret',
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account)
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = { loadConfig, DEFAULT_FIAT_MARKET, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE }
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
}

View file

@ -10,12 +10,19 @@ const FIAT = ['USD']
const DEFAULT_FIAT_MARKET = 'USD'
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const loadConfig = (account) => {
const loadConfig = account => {
const mapper = {
'privateKey': 'secret'
privateKey: 'secret',
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account)
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = { loadConfig, DEFAULT_FIAT_MARKET, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE }
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
}

View file

@ -11,12 +11,20 @@ const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 8
const REQUIRED_CONFIG_FIELDS = ['key', 'secret']
const loadConfig = (account) => {
const loadConfig = account => {
const mapper = {
'key': 'apiKey',
key: 'apiKey',
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account)
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = { loadConfig, REQUIRED_CONFIG_FIELDS, DEFAULT_FIAT_MARKET, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
module.exports = {
loadConfig,
REQUIRED_CONFIG_FIELDS,
DEFAULT_FIAT_MARKET,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -1,23 +1,31 @@
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, ETH, LTC, BCH, USDT, LN, USDC } = COINS
const CRYPTO = [BTC, ETH, LTC, BCH, USDT, LN, USDC]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 8
const REQUIRED_CONFIG_FIELDS = ['key', 'secret', 'clientId', 'currencyMarket']
const loadConfig = (account) => {
const mapper = {
'key': 'apiKey',
'clientId': 'uid'
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account)
return { ...mapped, timeout: 3000 }
}
module.exports = { loadConfig, DEFAULT_FIAT_MARKET, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
const { COINS } = require('@lamassu/coins')
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, ETH, LTC, BCH, USDT, LN, USDC } = COINS
const CRYPTO = [BTC, ETH, LTC, BCH, USDT, LN, USDC]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 8
const REQUIRED_CONFIG_FIELDS = ['key', 'secret', 'clientId', 'currencyMarket']
const loadConfig = account => {
const mapper = {
key: 'apiKey',
clientId: 'uid',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -1,92 +1,129 @@
const { utils: coinUtils } = require('@lamassu/coins')
const _ = require('lodash/fp')
const ccxt = require('ccxt')
const mem = require('mem')
const { buildMarket, ALL, isConfigValid } = require('../common/ccxt')
const { ORDER_TYPES } = require('./consts')
const logger = require('../../logger')
const { currencies } = require('../../new-admin/config')
const T = require('../../time')
const DEFAULT_PRICE_PRECISION = 2
const DEFAULT_AMOUNT_PRECISION = 8
function trade (side, account, tradeEntry, exchangeName) {
const { cryptoAtoms, fiatCode, cryptoCode: _cryptoCode, tradeId } = tradeEntry
try {
const cryptoCode = coinUtils.getEquivalentCode(_cryptoCode)
const exchangeConfig = ALL[exchangeName]
if (!exchangeConfig) throw Error('Exchange configuration not found')
const { USER_REF, loadOptions, loadConfig = _.noop, REQUIRED_CONFIG_FIELDS, ORDER_TYPE, AMOUNT_PRECISION } = exchangeConfig
if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS)) throw Error('Invalid config')
const selectedFiatMarket = account.currencyMarket
const symbol = buildMarket(selectedFiatMarket, cryptoCode, exchangeName)
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
const amount = coinUtils.toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
const accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {}
const withCustomKey = USER_REF ? { [USER_REF]: tradeId } : {}
const options = _.assign(accountOptions, withCustomKey)
const exchange = new ccxt[exchangeName](loadConfig(account))
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
return exchange.createOrder(symbol, ORDER_TYPES.MARKET, side, amount, null, options)
}
return exchange.fetchOrderBook(symbol)
.then(orderBook => {
const price = calculatePrice(side, amount, orderBook).toFixed(DEFAULT_PRICE_PRECISION)
return exchange.createOrder(symbol, ORDER_TYPES.LIMIT, side, amount, price, options)
})
} catch (e) {
return Promise.reject(e)
}
}
function calculatePrice (side, amount, orderBook) {
const book = side === 'buy' ? 'asks' : 'bids'
let collected = 0.0
for (const entry of orderBook[book]) {
collected += parseFloat(entry[1])
if (collected >= amount) return parseFloat(entry[0])
}
throw new Error('Insufficient market depth')
}
function _getMarkets (exchangeName, availableCryptos) {
const prunedCryptos = _.compose(_.uniq, _.map(coinUtils.getEquivalentCode))(availableCryptos)
try {
const exchange = new ccxt[exchangeName]()
const cryptosToQuoteAgainst = ['USDT']
const currencyCodes = _.concat(_.map(it => it.code, currencies), cryptosToQuoteAgainst)
return exchange.fetchMarkets()
.then(_.filter(it => (it.type === 'spot' || it.spot)))
.then(res =>
_.reduce((acc, value) => {
if (_.includes(value.base, prunedCryptos) && _.includes(value.quote, currencyCodes)) {
if (value.quote === value.base) return acc
if (_.isNil(acc[value.quote])) {
return { ...acc, [value.quote]: [value.base] }
}
acc[value.quote].push(value.base)
}
return acc
}, {}, res)
)
} catch (e) {
logger.debug(`No CCXT exchange found for ${exchangeName}`)
}
}
const getMarkets = mem(_getMarkets, {
maxAge: T.week,
cacheKey: (exchangeName, availableCryptos) => exchangeName
})
module.exports = { trade, getMarkets }
const { utils: coinUtils } = require('@lamassu/coins')
const _ = require('lodash/fp')
const ccxt = require('ccxt')
const mem = require('mem')
const { buildMarket, ALL, isConfigValid } = require('../common/ccxt')
const { ORDER_TYPES } = require('./consts')
const logger = require('../../logger')
const { currencies } = require('../../new-admin/config')
const T = require('../../time')
const DEFAULT_PRICE_PRECISION = 2
const DEFAULT_AMOUNT_PRECISION = 8
function trade(side, account, tradeEntry, exchangeName) {
const { cryptoAtoms, cryptoCode: _cryptoCode, tradeId } = tradeEntry
try {
const cryptoCode = coinUtils.getEquivalentCode(_cryptoCode)
const exchangeConfig = ALL[exchangeName]
if (!exchangeConfig) throw Error('Exchange configuration not found')
const {
USER_REF,
loadOptions,
loadConfig = _.noop,
REQUIRED_CONFIG_FIELDS,
ORDER_TYPE,
AMOUNT_PRECISION,
} = exchangeConfig
if (!isConfigValid(account, REQUIRED_CONFIG_FIELDS))
throw Error('Invalid config')
const selectedFiatMarket = account.currencyMarket
const symbol = buildMarket(selectedFiatMarket, cryptoCode, exchangeName)
const precision = _.defaultTo(DEFAULT_AMOUNT_PRECISION, AMOUNT_PRECISION)
const amount = coinUtils.toUnit(cryptoAtoms, cryptoCode).toFixed(precision)
const accountOptions = _.isFunction(loadOptions) ? loadOptions(account) : {}
const withCustomKey = USER_REF ? { [USER_REF]: tradeId } : {}
const options = _.assign(accountOptions, withCustomKey)
const exchange = new ccxt[exchangeName](loadConfig(account))
if (ORDER_TYPE === ORDER_TYPES.MARKET) {
return exchange.createOrder(
symbol,
ORDER_TYPES.MARKET,
side,
amount,
null,
options,
)
}
return exchange.fetchOrderBook(symbol).then(orderBook => {
const price = calculatePrice(side, amount, orderBook).toFixed(
DEFAULT_PRICE_PRECISION,
)
return exchange.createOrder(
symbol,
ORDER_TYPES.LIMIT,
side,
amount,
price,
options,
)
})
} catch (e) {
return Promise.reject(e)
}
}
function calculatePrice(side, amount, orderBook) {
const book = side === 'buy' ? 'asks' : 'bids'
let collected = 0.0
for (const entry of orderBook[book]) {
collected += parseFloat(entry[1])
if (collected >= amount) return parseFloat(entry[0])
}
throw new Error('Insufficient market depth')
}
function _getMarkets(exchangeName, availableCryptos) {
const prunedCryptos = _.compose(
_.uniq,
_.map(coinUtils.getEquivalentCode),
)(availableCryptos)
try {
const exchange = new ccxt[exchangeName]()
const cryptosToQuoteAgainst = ['USDT']
const currencyCodes = _.concat(
_.map(it => it.code, currencies),
cryptosToQuoteAgainst,
)
return exchange
.fetchMarkets()
.then(_.filter(it => it.type === 'spot' || it.spot))
.then(res =>
_.reduce(
(acc, value) => {
if (
_.includes(value.base, prunedCryptos) &&
_.includes(value.quote, currencyCodes)
) {
if (value.quote === value.base) return acc
if (_.isNil(acc[value.quote])) {
return { ...acc, [value.quote]: [value.base] }
}
acc[value.quote].push(value.base)
}
return acc
},
{},
res,
),
)
} catch (e) {
logger.debug(`No CCXT exchange found for ${exchangeName}. ${e}`)
}
}
const getMarkets = mem(_getMarkets, {
maxAge: T.week,
cacheKey: exchangeName => exchangeName,
})
module.exports = { trade, getMarkets }

View file

@ -10,12 +10,19 @@ const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const loadConfig = (account) => {
const loadConfig = account => {
const mapper = {
'privateKey': 'secret'
privateKey: 'secret',
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account)
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return { ...mapped, timeout: 3000 }
}
module.exports = { loadConfig, DEFAULT_FIAT_MARKET, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE }
module.exports = {
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
}

View file

@ -1,7 +1,6 @@
const ORDER_TYPES = {
MARKET: 'market',
LIMIT: 'limit'
}
module.exports = { ORDER_TYPES }
const ORDER_TYPES = {
MARKET: 'market',
LIMIT: 'limit',
}
module.exports = { ORDER_TYPES }

View file

@ -1,25 +1,42 @@
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const { COINS } = require('@lamassu/coins')
const ORDER_TYPE = ORDER_TYPES.LIMIT
const { BTC, ETH, USDT, LN } = COINS
const CRYPTO = [BTC, ETH, USDT, LN]
const FIAT = ['USD']
const DEFAULT_FIAT_MARKET = 'USD'
const AMOUNT_PRECISION = 4
const REQUIRED_CONFIG_FIELDS = ['clientKey', 'clientSecret', 'userId', 'walletId', 'currencyMarket']
const loadConfig = (account) => {
const mapper = {
'clientKey': 'apiKey',
'clientSecret': 'secret',
'userId': 'uid'
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(_.omit(['walletId'], account))
return { ...mapped, timeout: 3000 }
}
const loadOptions = ({ walletId }) => ({ walletId })
module.exports = { loadOptions, loadConfig, DEFAULT_FIAT_MARKET, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const { COINS } = require('@lamassu/coins')
const ORDER_TYPE = ORDER_TYPES.LIMIT
const { BTC, ETH, USDT, LN } = COINS
const CRYPTO = [BTC, ETH, USDT, LN]
const FIAT = ['USD']
const DEFAULT_FIAT_MARKET = 'USD'
const AMOUNT_PRECISION = 4
const REQUIRED_CONFIG_FIELDS = [
'clientKey',
'clientSecret',
'userId',
'walletId',
'currencyMarket',
]
const loadConfig = account => {
const mapper = {
clientKey: 'apiKey',
clientSecret: 'secret',
userId: 'uid',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(
_.omit(['walletId'], account),
)
return { ...mapped, timeout: 3000 }
}
const loadOptions = ({ walletId }) => ({ walletId })
module.exports = {
loadOptions,
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -1,30 +1,56 @@
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const { COINS } = require('@lamassu/coins')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, BCH, DASH, ETH, LTC, ZEC, XMR, USDT, TRX, USDT_TRON, LN, USDC } = COINS
const CRYPTO = [BTC, ETH, LTC, DASH, ZEC, BCH, XMR, USDT, TRX, USDT_TRON, LN, USDC]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 6
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const USER_REF = 'userref'
const loadConfig = (account) => {
const mapper = {
'privateKey': 'secret'
}
const mapped = _.mapKeys(key => mapper[key] ? mapper[key] : key)(account)
return {
...mapped,
timeout: 3000,
nonce: function () { return this.microseconds() }
}
}
const loadOptions = () => ({ expiretm: '+60' })
module.exports = { USER_REF, loadOptions, loadConfig, DEFAULT_FIAT_MARKET, REQUIRED_CONFIG_FIELDS, CRYPTO, FIAT, ORDER_TYPE, AMOUNT_PRECISION }
const _ = require('lodash/fp')
const { ORDER_TYPES } = require('./consts')
const { COINS } = require('@lamassu/coins')
const ORDER_TYPE = ORDER_TYPES.MARKET
const { BTC, BCH, DASH, ETH, LTC, ZEC, XMR, USDT, TRX, USDT_TRON, LN, USDC } =
COINS
const CRYPTO = [
BTC,
ETH,
LTC,
DASH,
ZEC,
BCH,
XMR,
USDT,
TRX,
USDT_TRON,
LN,
USDC,
]
const FIAT = ['USD', 'EUR']
const DEFAULT_FIAT_MARKET = 'EUR'
const AMOUNT_PRECISION = 6
const REQUIRED_CONFIG_FIELDS = ['apiKey', 'privateKey', 'currencyMarket']
const USER_REF = 'userref'
const loadConfig = account => {
const mapper = {
privateKey: 'secret',
}
const mapped = _.mapKeys(key => (mapper[key] ? mapper[key] : key))(account)
return {
...mapped,
timeout: 3000,
nonce: function () {
return this.microseconds()
},
}
}
const loadOptions = () => ({ expiretm: '+60' })
module.exports = {
USER_REF,
loadOptions,
loadConfig,
DEFAULT_FIAT_MARKET,
REQUIRED_CONFIG_FIELDS,
CRYPTO,
FIAT,
ORDER_TYPE,
AMOUNT_PRECISION,
}

View file

@ -1,14 +1,24 @@
module.exports = {
buy,
sell
sell,
}
function buy (cryptoAtoms, fiatCode, cryptoCode) {
console.log('[mock] buying %s %s for %s', cryptoAtoms.toString(), cryptoCode, fiatCode)
function buy(cryptoAtoms, fiatCode, cryptoCode) {
console.log(
'[mock] buying %s %s for %s',
cryptoAtoms.toString(),
cryptoCode,
fiatCode,
)
return Promise.resolve()
}
function sell (cryptoAtoms, fiatCode, cryptoCode) {
console.log('[mock] selling %s %s for %s', cryptoAtoms.toString(), cryptoCode, fiatCode)
function sell(cryptoAtoms, fiatCode, cryptoCode) {
console.log(
'[mock] selling %s %s for %s',
cryptoAtoms.toString(),
cryptoCode,
fiatCode,
)
return Promise.resolve()
}

View file

@ -1,46 +1,47 @@
const axios = require('axios')
const NAME = 'InforU'
function sendMessage (account, rec) {
const username = account.username
const apiKey = account.apiKey
const to = rec.sms.toNumber || account.toNumber
const text = rec.sms.body
const from = account.fromNumber
const url = 'https://capi.inforu.co.il/api/v2/SMS/SendSms'
const config = {
auth: {
username: username,
password: apiKey
},
maxBodyLength: Infinity,
headers:{
'Content-Type': 'application/json'
}
}
const data = {
Message: text,
Recipients: [{
Phone: to
}],
Settings: {
Sender: from
}
}
axios.post(url, data, config)
.catch(err => {
// console.log(err)
throw new Error(`inforu error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage
}
const axios = require('axios')
const NAME = 'InforU'
function sendMessage(account, rec) {
const username = account.username
const apiKey = account.apiKey
const to = rec.sms.toNumber || account.toNumber
const text = rec.sms.body
const from = account.fromNumber
const url = 'https://capi.inforu.co.il/api/v2/SMS/SendSms'
const config = {
auth: {
username: username,
password: apiKey,
},
maxBodyLength: Infinity,
headers: {
'Content-Type': 'application/json',
},
}
const data = {
Message: text,
Recipients: [
{
Phone: to,
},
],
Settings: {
Sender: from,
},
}
axios.post(url, data, config).catch(err => {
// console.log(err)
throw new Error(`inforu error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -2,7 +2,7 @@ const _ = require('lodash/fp')
const NAME = 'MockSMS'
function sendMessage (account, rec) {
function sendMessage(account, rec) {
console.log('Sending SMS: %j', rec)
return new Promise((resolve, reject) => {
if (_.endsWith('666', _.getOr(false, 'sms.toNumber', rec))) {
@ -15,5 +15,5 @@ function sendMessage (account, rec) {
module.exports = {
NAME,
sendMessage
sendMessage,
}

View file

@ -2,20 +2,19 @@ const Telnyx = require('telnyx')
const NAME = 'Telnyx'
function sendMessage (account, rec) {
function sendMessage(account, rec) {
const telnyx = Telnyx(account.apiKey)
const from = account.fromNumber
const text = rec.sms.body
const to = rec.sms.toNumber || account.toNumber
return telnyx.messages.create({ from, to, text })
.catch(err => {
throw new Error(`Telnyx error: ${err.message}`)
})
return telnyx.messages.create({ from, to, text }).catch(err => {
throw new Error(`Telnyx error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage
sendMessage,
}

View file

@ -3,10 +3,12 @@ const _ = require('lodash/fp')
const NAME = 'Twilio'
const BAD_NUMBER_CODES = [21201, 21202, 21211, 21214, 21216, 21217, 21219, 21408,
21610, 21612, 21614, 21608]
const BAD_NUMBER_CODES = [
21201, 21202, 21211, 21214, 21216, 21217, 21219, 21408, 21610, 21612, 21614,
21608,
]
function sendMessage (account, rec) {
function sendMessage(account, rec) {
return Promise.resolve()
.then(() => {
// to catch configuration errors like
@ -14,14 +16,16 @@ function sendMessage (account, rec) {
const client = twilio(account.accountSid, account.authToken)
const body = rec.sms.body
const _toNumber = rec.sms.toNumber || account.toNumber
const from = (_.startsWith('+')(account.fromNumber)
|| !_.isNumber(String(account.fromNumber).replace(/\s/g,'')))
? account.fromNumber : `+${account.fromNumber}`
const from =
_.startsWith('+')(account.fromNumber) ||
!_.isNumber(String(account.fromNumber).replace(/\s/g, ''))
? account.fromNumber
: `+${account.fromNumber}`
const opts = {
body: body,
to: _toNumber,
from
from,
}
return client.messages.create(opts)
@ -39,5 +43,5 @@ function sendMessage (account, rec) {
module.exports = {
NAME,
sendMessage
sendMessage,
}

View file

@ -3,10 +3,10 @@ const { SMS } = require('@vonage/sms')
const NAME = 'Vonage'
function sendMessage (account, rec) {
function sendMessage(account, rec) {
const credentials = new Auth({
apiKey: account.apiKey,
apiSecret: account.apiSecret
apiSecret: account.apiSecret,
})
const from = account.fromNumber
@ -14,10 +14,9 @@ function sendMessage (account, rec) {
const to = rec.sms.toNumber || account.toNumber
const smsClient = new SMS(credentials)
smsClient.send({ from, text, to })
.catch(err => {
throw new Error(`Vonage error: ${err.message}`)
})
smsClient.send({ from, text, to }).catch(err => {
throw new Error(`Vonage error: ${err.message}`)
})
}
module.exports = {

View file

@ -1,42 +1,41 @@
const axios = require('axios')
const NAME = 'Whatsapp'
function sendMessage (account, rec) {
const phoneId = account.phoneId
const token = account.apiKey
const to = rec.sms.toNumber || account.toNumber
const template = rec.sms.template
const url = `https://graph.facebook.com/v17.0/${phoneId}/messages`
const config = {
headers:{
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
}
}
const data = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
type: 'template',
to,
template: {
name: template,
language: { code: 'en_US' }
}
}
axios.post(url, data, config)
.catch(err => {
// console.log(err)
throw new Error(`Whatsapp error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage
}
const axios = require('axios')
const NAME = 'Whatsapp'
function sendMessage(account, rec) {
const phoneId = account.phoneId
const token = account.apiKey
const to = rec.sms.toNumber || account.toNumber
const template = rec.sms.template
const url = `https://graph.facebook.com/v17.0/${phoneId}/messages`
const config = {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
}
const data = {
messaging_product: 'whatsapp',
recipient_type: 'individual',
type: 'template',
to,
template: {
name: template,
language: { code: 'en_US' },
},
}
axios.post(url, data, config).catch(err => {
// console.log(err)
throw new Error(`Whatsapp error: ${err.message}`)
})
}
module.exports = {
NAME,
sendMessage,
}

View file

@ -7,16 +7,17 @@ const { BTC, BCH, LN } = COINS
const CRYPTO = [BTC, BCH, LN]
const FIAT = 'ALL_CURRENCIES'
function ticker (fiatCode, cryptoCode) {
return axios.get('https://bitpay.com/rates/' + cryptoCode + '/' + fiatCode)
function ticker(fiatCode, cryptoCode) {
return axios
.get('https://bitpay.com/rates/' + cryptoCode + '/' + fiatCode)
.then(r => {
const data = r.data.data
const price = new BN(data.rate.toString())
return {
rates: {
ask: price,
bid: price
}
bid: price,
},
}
})
}
@ -25,5 +26,5 @@ module.exports = {
ticker,
name: 'BitPay',
CRYPTO,
FIAT
FIAT,
}

View file

@ -1,7 +1,11 @@
const ccxt = require('ccxt')
const BN = require('../../bn')
const { buildMarket, verifyFiatSupport, defaultFiatMarket } = require('../common/ccxt')
const {
buildMarket,
verifyFiatSupport,
defaultFiatMarket,
} = require('../common/ccxt')
const { getRate } = require('../../../lib/forex')
const RETRIES = 2
@ -16,7 +20,7 @@ const sanityCheckRates = (ask, bid, tickerName) => {
}
}
function ticker (fiatCode, cryptoCode, tickerName) {
function ticker(fiatCode, cryptoCode, tickerName) {
if (!tickerObjects[tickerName]) {
tickerObjects[tickerName] = new ccxt[tickerName]({
timeout: 3000,
@ -30,37 +34,40 @@ function ticker (fiatCode, cryptoCode, tickerName) {
return getCurrencyRates(ticker, fiatCode, cryptoCode)
}
return getRate(RETRIES, tickerName, defaultFiatMarket(tickerName))
.then(({ fxRate }) => {
return getRate(RETRIES, tickerName, defaultFiatMarket(tickerName)).then(
({ fxRate }) => {
try {
return getCurrencyRates(ticker, defaultFiatMarket(tickerName), cryptoCode)
.then(res => ({
rates: {
ask: res.rates.ask.times(fxRate),
bid: res.rates.bid.times(fxRate)
}
}))
return getCurrencyRates(
ticker,
defaultFiatMarket(tickerName),
cryptoCode,
).then(res => ({
rates: {
ask: res.rates.ask.times(fxRate),
bid: res.rates.bid.times(fxRate),
},
}))
} catch (e) {
return Promise.reject(e)
}
})
},
)
}
function getCurrencyRates (ticker, fiatCode, cryptoCode) {
function getCurrencyRates(ticker, fiatCode, cryptoCode) {
try {
if (!ticker.has['fetchTicker']) {
throw new Error('Ticker not available')
}
const symbol = buildMarket(fiatCode, cryptoCode, ticker.id)
return ticker.fetchTicker(symbol)
.then(res => {
sanityCheckRates(res.ask, res.bid, cryptoCode)
return {
rates: {
ask: new BN(res.ask),
bid: new BN(res.bid)
}
}
return ticker.fetchTicker(symbol).then(res => {
sanityCheckRates(res.ask, res.bid, cryptoCode)
return {
rates: {
ask: new BN(res.ask),
bid: new BN(res.bid),
},
}
})
} catch (e) {
return Promise.reject(e)

View file

@ -1,12 +1,12 @@
const BN = require('../../bn')
function ticker (fiatCode, cryptoCode) {
function ticker() {
return Promise.resolve({
rates: {
ask: new BN(105),
bid: new BN(100)
}
bid: new BN(100),
},
})
}
module.exports = {ticker}
module.exports = { ticker }

View file

@ -3,26 +3,25 @@ const axios = require('axios').create({
// TODO: get rejectUnauthorized true to work
baseURL: `${process.env.TICKER_URL}/api/rates/`,
httpsAgent: new https.Agent({
rejectUnauthorized: false
})
rejectUnauthorized: false,
}),
})
const BN = require('../../../bn')
function ticker (account, fiatCode, cryptoCode) {
return axios.get(`${cryptoCode}/${fiatCode}`)
.then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
return {
rates: {
ask: BN(data.ask),
bid: BN(data.bid),
signature: data.signature
}
}
})
function ticker(account, fiatCode, cryptoCode) {
return axios.get(`${cryptoCode}/${fiatCode}`).then(({ data }) => {
if (data.error) throw new Error(JSON.stringify(data.error))
return {
rates: {
ask: BN(data.ask),
bid: BN(data.bid),
signature: data.signature,
},
}
})
}
module.exports = {
ticker
ticker,
}

View file

@ -1,282 +1,272 @@
[
{
"constant":true,
"inputs":[
],
"name":"name",
"outputs":[
"constant": true,
"inputs": [],
"name": "name",
"outputs": [
{
"name":"",
"type":"string"
"name": "",
"type": "string"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":false,
"inputs":[
"constant": false,
"inputs": [
{
"name":"_spender",
"type":"address"
"name": "_spender",
"type": "address"
},
{
"name":"_value",
"type":"uint256"
"name": "_value",
"type": "uint256"
}
],
"name":"approve",
"outputs":[
"name": "approve",
"outputs": [
{
"name":"success",
"type":"bool"
"name": "success",
"type": "bool"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":true,
"inputs":[
],
"name":"totalSupply",
"outputs":[
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [
{
"name":"",
"type":"uint256"
"name": "",
"type": "uint256"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":false,
"inputs":[
"constant": false,
"inputs": [
{
"name":"_from",
"type":"address"
"name": "_from",
"type": "address"
},
{
"name":"_to",
"type":"address"
"name": "_to",
"type": "address"
},
{
"name":"_value",
"type":"uint256"
"name": "_value",
"type": "uint256"
}
],
"name":"transferFrom",
"outputs":[
"name": "transferFrom",
"outputs": [
{
"name":"success",
"type":"bool"
"name": "success",
"type": "bool"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":true,
"inputs":[
],
"name":"decimals",
"outputs":[
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [
{
"name":"",
"type":"uint256"
"name": "",
"type": "uint256"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":true,
"inputs":[
],
"name":"version",
"outputs":[
"constant": true,
"inputs": [],
"name": "version",
"outputs": [
{
"name":"",
"type":"string"
"name": "",
"type": "string"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":true,
"inputs":[
"constant": true,
"inputs": [
{
"name":"_owner",
"type":"address"
"name": "_owner",
"type": "address"
}
],
"name":"balanceOf",
"outputs":[
"name": "balanceOf",
"outputs": [
{
"name":"balance",
"type":"uint256"
"name": "balance",
"type": "uint256"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":true,
"inputs":[
],
"name":"symbol",
"outputs":[
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [
{
"name":"",
"type":"string"
"name": "",
"type": "string"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":false,
"inputs":[
"constant": false,
"inputs": [
{
"name":"_to",
"type":"address"
"name": "_to",
"type": "address"
},
{
"name":"_value",
"type":"uint256"
"name": "_value",
"type": "uint256"
}
],
"name":"transfer",
"outputs":[
"name": "transfer",
"outputs": [
{
"name":"success",
"type":"bool"
"name": "success",
"type": "bool"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":false,
"inputs":[
"constant": false,
"inputs": [
{
"name":"_spender",
"type":"address"
"name": "_spender",
"type": "address"
},
{
"name":"_value",
"type":"uint256"
"name": "_value",
"type": "uint256"
},
{
"name":"_extraData",
"type":"bytes"
"name": "_extraData",
"type": "bytes"
}
],
"name":"approveAndCall",
"outputs":[
"name": "approveAndCall",
"outputs": [
{
"name":"success",
"type":"bool"
"name": "success",
"type": "bool"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"constant":true,
"inputs":[
"constant": true,
"inputs": [
{
"name":"_owner",
"type":"address"
"name": "_owner",
"type": "address"
},
{
"name":"_spender",
"type":"address"
"name": "_spender",
"type": "address"
}
],
"name":"allowance",
"outputs":[
"name": "allowance",
"outputs": [
{
"name":"remaining",
"type":"uint256"
"name": "remaining",
"type": "uint256"
}
],
"payable":false,
"type":"function"
"payable": false,
"type": "function"
},
{
"inputs":[
"inputs": [
{
"name":"_initialAmount",
"type":"uint256"
"name": "_initialAmount",
"type": "uint256"
},
{
"name":"_tokenName",
"type":"string"
"name": "_tokenName",
"type": "string"
},
{
"name":"_decimalUnits",
"type":"uint8"
"name": "_decimalUnits",
"type": "uint8"
},
{
"name":"_tokenSymbol",
"type":"string"
"name": "_tokenSymbol",
"type": "string"
}
],
"type":"constructor"
"type": "constructor"
},
{
"payable":false,
"type":"fallback"
"payable": false,
"type": "fallback"
},
{
"anonymous":false,
"inputs":[
"anonymous": false,
"inputs": [
{
"indexed":true,
"name":"_from",
"type":"address"
"indexed": true,
"name": "_from",
"type": "address"
},
{
"indexed":true,
"name":"_to",
"type":"address"
"indexed": true,
"name": "_to",
"type": "address"
},
{
"indexed":false,
"name":"_value",
"type":"uint256"
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name":"Transfer",
"type":"event"
"name": "Transfer",
"type": "event"
},
{
"anonymous":false,
"inputs":[
"anonymous": false,
"inputs": [
{
"indexed":true,
"name":"_owner",
"type":"address"
"indexed": true,
"name": "_owner",
"type": "address"
},
{
"indexed":true,
"name":"_spender",
"type":"address"
"indexed": true,
"name": "_spender",
"type": "address"
},
{
"indexed":false,
"name":"_value",
"type":"uint256"
"indexed": false,
"name": "_value",
"type": "uint256"
}
],
"name":"Approval",
"type":"event"
"name": "Approval",
"type": "event"
}
]
]

View file

@ -9,34 +9,34 @@ const HOLLISTIC_COINS = {
USDT: 'USDT',
USDT_TRON: 'USDT',
LTC: 'LTC',
TRX: 'TRX'
TRX: 'TRX',
}
const SINGLE_ASSET_COINS = {
ZEC: {
asset: 'ZEC',
blockchain: 'zcash'
blockchain: 'zcash',
},
BCH: {
asset: 'BCH',
blockchain: 'bitcoin_cash'
}
blockchain: 'bitcoin_cash',
},
}
const TYPE = {
TRANSACTION: 'transaction',
ADDRESS: 'address'
TRANSACTION: 'transaction',
ADDRESS: 'address',
}
const SUPPORTED_COINS = { ...HOLLISTIC_COINS, ...SINGLE_ASSET_COINS }
function rate (account, objectType, cryptoCode, objectId) {
function rate(account, objectType, cryptoCode, objectId) {
return isWalletScoringEnabled(account, cryptoCode).then(isEnabled => {
if (!isEnabled) return Promise.resolve(null)
const aml = new AML({
key: account.apiKey,
secret: account.apiSecret
secret: account.apiSecret,
})
const isHolistic = Object.keys(HOLLISTIC_COINS).includes(cryptoCode)
@ -44,38 +44,44 @@ function rate (account, objectType, cryptoCode, objectId) {
const requestBody = {
subject: {
asset: isHolistic ? 'holistic' : SINGLE_ASSET_COINS[cryptoCode].asset,
blockchain: isHolistic ? 'holistic' : SINGLE_ASSET_COINS[cryptoCode].blockchain,
blockchain: isHolistic
? 'holistic'
: SINGLE_ASSET_COINS[cryptoCode].blockchain,
type: objectType,
hash: objectId
hash: objectId,
},
type: objectType === TYPE.ADDRESS ? 'wallet_exposure' : 'source_of_funds'
type: objectType === TYPE.ADDRESS ? 'wallet_exposure' : 'source_of_funds',
}
const threshold = account.scoreThreshold
const endpoint = objectType === TYPE.ADDRESS ? '/v2/wallet/synchronous' : '/v2/analysis/synchronous'
const endpoint =
objectType === TYPE.ADDRESS
? '/v2/wallet/synchronous'
: '/v2/analysis/synchronous'
return aml.client
.post(endpoint, requestBody)
.then((res) => {
const resScore = res.data?.risk_score
return aml.client.post(endpoint, requestBody).then(res => {
const resScore = res.data?.risk_score
// elliptic returns 0-1 score, but we're accepting 0-100 config
// normalize score to 0-10 where 0 is the lowest risk
// elliptic score can be null and contains decimals
return {score: (resScore || 0) * 10, isValid: ((resScore || 0) * 100) < threshold}
})
// elliptic returns 0-1 score, but we're accepting 0-100 config
// normalize score to 0-10 where 0 is the lowest risk
// elliptic score can be null and contains decimals
return {
score: (resScore || 0) * 10,
isValid: (resScore || 0) * 100 < threshold,
}
})
})
}
function rateTransaction (account, cryptoCode, transactionId) {
function rateTransaction(account, cryptoCode, transactionId) {
return rate(account, TYPE.TRANSACTION, cryptoCode, transactionId)
}
function rateAddress (account, cryptoCode, address) {
function rateAddress(account, cryptoCode, address) {
return rate(account, TYPE.ADDRESS, cryptoCode, address)
}
function isWalletScoringEnabled (account, cryptoCode) {
function isWalletScoringEnabled(account, cryptoCode) {
const isAccountEnabled = !_.isNil(account) && account.enabled
if (!isAccountEnabled) return Promise.resolve(false)
@ -91,5 +97,5 @@ module.exports = {
NAME,
rateAddress,
rateTransaction,
isWalletScoringEnabled
isWalletScoringEnabled,
}

View file

@ -2,18 +2,22 @@ const NAME = 'FakeScoring'
const { WALLET_SCORE_THRESHOLD } = require('../../../constants')
function rateAddress (account, cryptoCode, address) {
return new Promise((resolve, _) => {
function rateAddress(account, cryptoCode, address) {
return new Promise(resolve => {
setTimeout(() => {
console.log('[WALLET-SCORING] DEBUG: Mock scoring rating wallet address %s', address)
return Promise.resolve(2)
.then(score => resolve({ address, score, isValid: score < WALLET_SCORE_THRESHOLD }))
console.log(
'[WALLET-SCORING] DEBUG: Mock scoring rating wallet address %s',
address,
)
return Promise.resolve(2).then(score =>
resolve({ address, score, isValid: score < WALLET_SCORE_THRESHOLD }),
)
}, 100)
})
}
function isWalletScoringEnabled (account, cryptoCode) {
return new Promise((resolve, _) => {
function isWalletScoringEnabled() {
return new Promise(resolve => {
setTimeout(() => {
return resolve(true)
}, 100)
@ -23,6 +27,6 @@ function isWalletScoringEnabled (account, cryptoCode) {
module.exports = {
NAME,
rateAddress,
rateTransaction:rateAddress,
isWalletScoringEnabled
rateTransaction: rateAddress,
isWalletScoringEnabled,
}

View file

@ -1,7 +1,6 @@
const axios = require('axios')
const _ = require('lodash/fp')
const NAME = 'Scorechain'
const SUPPORTED_COINS = {
BTC: 'BITCOIN',
@ -11,15 +10,15 @@ const SUPPORTED_COINS = {
LTC: 'LITECOIN',
DASH: 'DASH',
TRX: 'TRON',
USDT_TRON: 'TRON'
USDT_TRON: 'TRON',
}
const TYPE = {
TRANSACTION: 'TRANSACTION',
ADDRESS: 'ADDRESS'
ADDRESS: 'ADDRESS',
}
function rate (account, objectType, cryptoCode, objectId) {
function rate(account, objectType, cryptoCode, objectId) {
return isWalletScoringEnabled(account, cryptoCode).then(isEnabled => {
if (!isEnabled) return Promise.resolve(null)
@ -29,21 +28,25 @@ function rate (account, objectType, cryptoCode, objectId) {
objectType,
objectId,
blockchain: SUPPORTED_COINS[cryptoCode],
coin: "ALL"
coin: 'ALL',
}
const headers = {
'accept': 'application/json',
accept: 'application/json',
'X-API-KEY': account.apiKey,
'Content-Type': 'application/json'
'Content-Type': 'application/json',
}
return axios.post(`https://api.scorechain.com/v1/scoringAnalysis`, payload, {headers})
return axios
.post(`https://api.scorechain.com/v1/scoringAnalysis`, payload, {
headers,
})
.then(res => {
const resScore = res.data?.analysis?.assigned?.result?.score
if (!resScore) throw new Error('Failed to get score from Scorechain API')
if (!resScore)
throw new Error('Failed to get score from Scorechain API')
// normalize score to 0-10 where 0 is the lowest risk
return {score: (100 - resScore) / 10, isValid: resScore >= threshold}
return { score: (100 - resScore) / 10, isValid: resScore >= threshold }
})
.catch(err => {
throw err
@ -51,15 +54,15 @@ function rate (account, objectType, cryptoCode, objectId) {
})
}
function rateTransaction (account, cryptoCode, transactionId) {
function rateTransaction(account, cryptoCode, transactionId) {
return rate(account, TYPE.TRANSACTION, cryptoCode, transactionId)
}
function rateAddress (account, cryptoCode, address) {
function rateAddress(account, cryptoCode, address) {
return rate(account, TYPE.ADDRESS, cryptoCode, address)
}
function isWalletScoringEnabled (account, cryptoCode) {
function isWalletScoringEnabled(account, cryptoCode) {
const isAccountEnabled = !_.isNil(account) && account.enabled
if (!isAccountEnabled) return Promise.resolve(false)
@ -75,5 +78,5 @@ module.exports = {
NAME,
rateAddress,
rateTransaction,
isWalletScoringEnabled
isWalletScoringEnabled,
}

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,
}

View file

@ -5,44 +5,64 @@ const _ = require('lodash/fp')
const { fetchRBF } = require('../../wallet/bitcoind/bitcoind')
module.exports = { authorize }
function highConfidence (confidence, txref, txRBF) {
function highConfidence(confidence, txref, txRBF) {
if (txref.double_spend) return 0
if (txRBF) return 0
if (txref.confirmations > 0 || txref.confidence * 100 >= confidence) return txref.value
if (txref.confirmations > 0 || txref.confidence * 100 >= confidence)
return txref.value
return 0
}
function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvailable) {
return Promise.resolve()
.then(() => {
if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode)
function authorize(
account,
toAddress,
cryptoAtoms,
cryptoCode,
isBitcoindAvailable,
) {
return Promise.resolve().then(() => {
if (cryptoCode !== 'BTC')
throw new Error('Unsupported crypto: ' + cryptoCode)
const query = qs.stringify({
token: account.token,
includeConfidence: true
})
const query = qs.stringify({
token: account.token,
includeConfidence: true,
})
const confidence = account.confidenceFactor
const isRBFEnabled = account.rbf
const url = `https://api.blockcypher.com/v1/btc/main/addrs/${toAddress}?${query}`
const confidence = account.confidenceFactor
const isRBFEnabled = account.rbf
const url = `https://api.blockcypher.com/v1/btc/main/addrs/${toAddress}?${query}`
return axios.get(url)
.then(r => {
const data = r.data
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
const promises = _.map(unconfirmedTxref => fetchRBF(unconfirmedTxref.tx_hash), data.unconfirmed_txrefs)
return Promise.all(promises)
.then(values => {
const unconfirmedTxsRBF = _.fromPairs(values)
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref, unconfirmedTxsRBF[txref.tx_hash]), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
})
}
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return axios.get(url).then(r => {
const data = r.data
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
const promises = _.map(
unconfirmedTxref => fetchRBF(unconfirmedTxref.tx_hash),
data.unconfirmed_txrefs,
)
return Promise.all(promises).then(values => {
const unconfirmedTxsRBF = _.fromPairs(values)
const sumTxRefs = txrefs =>
_.sumBy(
txref =>
highConfidence(
confidence,
txref,
unconfirmedTxsRBF[txref.tx_hash],
),
txrefs,
)
const authorizedValue =
sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
})
}
const sumTxRefs = txrefs =>
_.sumBy(txref => highConfidence(confidence, txref), txrefs)
const authorizedValue =
sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
})
})
}

View file

@ -1,11 +1,11 @@
module.exports = {authorize}
module.exports = { authorize }
function authorize (account, toAddress, cryptoAtoms, cryptoCode) {
return Promise.resolve()
.then(() => {
if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode)
function authorize(account, toAddress, cryptoAtoms, cryptoCode) {
return Promise.resolve().then(() => {
if (cryptoCode !== 'BTC')
throw new Error('Unsupported crypto: ' + cryptoCode)
const isAuthorized = false
return isAuthorized
})
const isAuthorized = false
return isAuthorized
})
}