-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (54 loc) · 1.73 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const cors = require('cors-for-cloud-functions')
const Binance = require('binance-api-node')
const binance = Binance.default()
const { PublicClient: Coinbase } = require('gdax')
const coinbase = new Coinbase()
const ERR_NO_EXCHANGE = `An exchange is required: 'binance' || 'coinbase'.`
const fetchAssets = async (exchange) => {
let exchangeAssets = null
if (exchange === 'BINANCE') {
try {
const { symbols } = await binance.exchangeInfo()
exchangeAssets = symbols
} catch (err) {
if (err) return { err }
}
const data = exchangeAssets
.filter(item => item.status === 'TRADING')
.map(item => ({
base: item.baseAsset,
original: item.symbol,
pair: [item.quoteAsset, item.baseAsset].join('-'),
quote: item.quoteAsset
}))
return { data }
}
if (exchange === 'COINBASE') {
try {
exchangeAssets = await coinbase.getProducts()
} catch (err) {
return { err }
}
const data = exchangeAssets.map(item => ({
base: item.base_currency,
original: item.id,
pair: [item.quote_currency, item.base_currency].join('-'),
quote: item.quote_currency
}))
return { data }
} else return { err: `Exchange, ${exchange}, is not supported.` }
}
exports['api-assets'] = async (request, response) => {
const { req, res, isOptions } = cors(request, response)
if (isOptions) return res.status(204).send('')
const {
body,
query
} = req
let exchange = body.exchange || query.exchange
if (!exchange) return res.send({ err: ERR_NO_EXCHANGE })
exchange = exchange.toUpperCase()
const { err, data } = await fetchAssets(exchange)
if (err) return res.status(404).send({ err })
return res.status(200).send({ data })
}