Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Add CoinMarketCap, Coinbase & Coinbase Pro #33

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish-reload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Publish & reload atomicagent
on:
create:
tags:
- v*
- 'v*'

jobs:
publish:
Expand Down
11 changes: 8 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"agendash": "^1.0.0",
"axios": "^0.19.2",
"bignumber.js": "^9.0.0",
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",
"commander": "^2.20.3",
"compression": "^1.7.4",
Expand Down
20 changes: 20 additions & 0 deletions sample.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ maxJobRetry = 5
jobRetryDelay = "5 seconds"
# jobReporter = "/Users/user/slack.js"

[rate]
updateInterval = "5 minutes"

[rate.pairs.BTC-ETH]
provider = "coinbase-pro"
type = "pair"
flip = true

[rate.pairs.ETH-BTC]
provider = "coinbase-pro"
type = "pair"

[rate.pairs.DAI-BTC]
provider = "coinmarketcap"
type = "pair"

[rateProvider]
[rateProvider.coinmarketcap]
apiKey = ""

[assets]
[assets.BTC]
network = "bitcoin_regtest"
Expand Down
2 changes: 1 addition & 1 deletion src/worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ module.exports.start = async () => {
})

await agenda.start()
await agenda.every('5 minutes', 'update-market-data')
await agenda.every(config.rate.updateInterval, 'update-market-data')
}

module.exports.stop = () => {
Expand Down
56 changes: 36 additions & 20 deletions src/worker/jobs/update-market-data.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,50 @@
const axios = require('axios')
const Sentry = require('@sentry/node')
const Bluebird = require('bluebird')
const BN = require('bignumber.js')
const Market = require('../../models/Market')
const debug = require('debug')('liquality:agent:worker')

const Market = require('../../models/Market')
const config = require('../../config')
const rateProviders = require('../rate')

module.exports = agenda => async job => {
debug('Updating market data')

const markets = await Market.find({ status: 'ACTIVE' }).exec()
const currencies = Array.from(new Set([].concat(...markets.map(market => [market.from, market.to]))))
const MAP = {}

await Promise.all(currencies.map(currency => {
return axios(`https://api.coinbase.com/v2/prices/${currency}-USD/spot`)
.then(res => {
MAP[currency] = res.data.data.amount
})
}))
await Bluebird.map(markets, async market => {
const pair = `${market.from}-${market.to}`

try {
const pairConfig = config.rate.pairs[pair]
const { provider, type, base, flip } = pairConfig
let { from, to } = market

await Promise.all(markets.map(market => {
const from = BN(MAP[market.from])
const to = BN(MAP[market.to])
if (flip) {
from = market.to
to = market.from
}

let rate = from.div(to) // Market rate
rate = rate.times(BN(1).minus(BN(market.spread))) // Remove spread
rate = rate.dp(8)
const providerFn = rateProviders[provider][type]

market.rate = rate
let rate = await providerFn(from, to, base)

debug(`${market.from}_${market.to}`, market.rate)
if (flip) {
rate = BN(1).div(rate)
}

market.rate = BN(rate).times(BN(1).minus(BN(market.spread))).dp(8).toNumber()

debug(pair, market.rate)

return market.save()
} catch (e) {
Sentry.withScope(scope => {
scope.setTag('pair', pair)
Sentry.captureException(e)
})

return market.save()
}))
debug(e)
}
}, { concurrency: 1 })
}
21 changes: 21 additions & 0 deletions src/worker/rate/coinbase-pro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const axios = require('axios')
const Bluebird = require('bluebird')
const BN = require('bignumber.js')

module.exports.pair = async (from, to) => {
const { data } = await axios(`https://api.pro.coinbase.com/products/${from}-${to}/ticker`)

return BN(data.price)
}

module.exports.convert = async (from, to, base) => {
const MAP = {}

await Bluebird.map([from, to], async currency => {
const { data } = await axios(`https://api.coinbase.com/v2/prices/${currency}-${base}/spot`)

MAP[currency] = BN(data.data.amount)
}, { concurrency: 1 })

return MAP[from].div(MAP[to])
}
21 changes: 21 additions & 0 deletions src/worker/rate/coinbase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const axios = require('axios')
const Bluebird = require('bluebird')
const BN = require('bignumber.js')

module.exports.pair = async (from, to) => {
const { data } = await axios(`https://api.coinbase.com/v2/prices/${from}-${to}/spot`)

return BN(data.data.amount)
}

module.exports.convert = async (from, to, base) => {
const MAP = {}

await Bluebird.map([from, to], async currency => {
const { data } = await axios(`https://api.coinbase.com/v2/prices/${currency}-${base}/spot`)

MAP[currency] = BN(data.data.amount)
}, { concurrency: 1 })

return MAP[from].div(MAP[to])
}
24 changes: 24 additions & 0 deletions src/worker/rate/coinmarketcap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const _ = require('lodash')
const axios = require('axios')
const BN = require('bignumber.js')

const config = require('../../config')

module.exports.pair = async (from, to) => {
const apiKey = _.get(config, 'rateProvider.coinmarketcap.apiKey')
if (!apiKey) throw new Error('Coinmarketcap requires rateProvider.coinmarketcap.apiKey')

const { data } = await axios({
url: `https://pro-api.coinmarketcap.com/v1/tools/price-conversion`,
params: {
amount: 1,
symbol: from,
convert: to
},
headers: {
'X-CMC_PRO_API_KEY': apiKey
}
})

return BN(data.data.quote[to].price).dp(8)
}
5 changes: 5 additions & 0 deletions src/worker/rate/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
'coinbase-pro': require('./coinbase-pro'),
coinbase: require('./coinbase'),
coinmarketcap: require('./coinmarketcap')
}