-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetch_cbdata.js
35 lines (27 loc) · 1.18 KB
/
fetch_cbdata.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
const crypto = require('crypto');
/* Grabs the latest data from the Coinbase Oracle (ABI encoded signed prices)
*
* Note: you need to run this with `node --experimental-fetch fetch_cbdata.js` or add a fetch(…) shim
* Supply your Coinbase API key, secret, and passphrase as environment variables (API_KEY, API_SECRET, API_PASSPHRASE)
*/
const { API_KEY, API_SECRET, API_PASSPHRASE } = process.env;
if (!API_KEY || !API_SECRET || !API_PASSPHRASE) {
console.log('error: missing one or more of API_KEY, API_SECRET, API_PASSPHRASE environment variables');
process.exit(1)
}
const API_URL = 'https://api.exchange.coinbase.com'
async function main() {
const timestamp = (new Date().getTime() / 1000).toString();
const message = timestamp + 'GET' + '/oracle';
const hmac = crypto.createHmac('sha256', Buffer.from(API_SECRET, 'base64')).update(message);
const signature = hmac.digest('base64')
const headers = {
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': API_KEY,
'CB-ACCESS-PASSPHRASE': API_PASSPHRASE
}
const res = await fetch(API_URL + '/oracle', { method: 'GET', headers });
process.stdout.write(await res.text());
}
main();