-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
82 lines (64 loc) · 2.89 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
70
71
72
73
74
75
76
77
78
79
80
81
82
const crypto = require('crypto')
const axios = require('axios')
require('dotenv').config()
const fromCoins = ['BTC', 'ETH', 'LTC']
const toCurrency = 'GBP'
const baseUrl = 'https://api.pro.coinbase.com'
const getSignature = (apiSecret, timestamp, method, requestPath, body) => {
// create the pre-hash string by concatenating required parts
const preHash = timestamp + method + requestPath + JSON.stringify(body)
// decode the base64 secret
const apiSecretBase64 = Buffer.from(apiSecret, 'base64')
// create a sha256 hmac with the secret
const hmac = crypto.createHmac('sha256', apiSecretBase64)
// sign the require message with the hmac and finally base64 encode the result
const signature = hmac.update(preHash).digest('base64')
return signature
}
const fetchData = async (requestPath, body, apiKey, apiSecret, apiPassphrase, timestamp) => {
const method = 'GET'
const signature = getSignature(apiSecret, timestamp, method, requestPath, body)
const response = await axios.get(`${baseUrl}${requestPath}`, {
headers: {
'CB-ACCESS-KEY': apiKey,
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-PASSPHRASE': apiPassphrase
},
data: body
})
return response.data
}
const zip = (arr1, arr2) => arr1.map((k, i) => [k, arr2[i]])
const fetchTickersByCoin = async (fromCoins, toCurrency, apiKey, apiSecret, apiPassphrase, timestamp) => {
const productIds = fromCoins.map(coin => `${coin}-${toCurrency}`)
const tickerRequestPaths = productIds.map(productId => `/products/${productId}/ticker`)
const tickers = await Promise.all(
tickerRequestPaths.map(path => fetchData(path, { }, apiKey, apiSecret, apiPassphrase, timestamp))
)
const entries = new Map(zip(fromCoins, tickers))
return Object.fromEntries(entries)
}
const getTotalValue = (fromCoins, accounts, tickersByCoin) => {
const conversionFromCoin = coin => {
const balance = accounts.find(account => coin == account.currency).balance
const price = tickersByCoin[coin].price
return balance * price
}
const conversions = fromCoins.map(conversionFromCoin)
return conversions.reduce((total, amount) => total + amount)
}
const main = async (fromCoins, toCurrency) => {
const apiKey = process.env.API_KEY
const apiSecret = process.env.API_SECRET
const apiPassphrase = process.env.API_PASSPHRASE
const timestamp = Date.now() / 1000
const [accounts, tickersByCoin] = await Promise.all([
fetchData('/accounts', { }, apiKey, apiSecret, apiPassphrase, timestamp),
fetchTickersByCoin(fromCoins, toCurrency, apiKey, apiSecret, apiPassphrase, timestamp)
])
const totalValue = getTotalValue(fromCoins, accounts, tickersByCoin)
console.log(`Total value (${toCurrency}): ${totalValue}`)
}
main(fromCoins, toCurrency)
.catch(console.log)