Skip to content

Instantly share code, notes, and snippets.

@yaronvel
Created January 28, 2021 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaronvel/9b4eec836257c1d04baab1aa45b93529 to your computer and use it in GitHub Desktop.
Save yaronvel/9b4eec836257c1d04baab1aa45b93529 to your computer and use it in GitHub Desktop.
const express = require("express");
const crypto = require("crypto");
const axios = require("axios");
const secret =
const apiKey =
const phrase =
async function queryApi(path) {
let timestamp = Date.now() / 1000;
let method = "GET";
// create the prehash string by concatenating required parts
let what = timestamp + method + path;
// decode the base64 secret
let key = Buffer.from(secret, "base64");
// create a sha256 hmac with the secret
let hmac = crypto.createHmac("sha256", key);
// sign the require message with the hmac
// and finally base64 encode the result
let signature = hmac.update(what).digest("base64");
let headers = {
"CB-ACCESS-KEY": apiKey,
"CB-ACCESS-SIGN": signature,
"CB-ACCESS-TIMESTAMP": timestamp,
"CB-ACCESS-PASSPHRASE": phrase,
"Content-Type": "application/json"
};
const res = await axios.get("https://api.pro.coinbase.com" + path, {
headers
});
//console.log(res.data[0][0]);
//console.log(res.data.timestamp);
//console.log(JSON.parse(res.data).timestamp);
return res;
}
function findMax(aa) {
let maxtime = 0
for(let i = 0 ; i < aa.length ; i++) {
if(maxtime < Number(aa[0][0])) maxtime = aa[0][0]
}
return maxtime
}
async function test() {
const oraceTimestamp = (await queryApi("/oracle")).data.timestamp
const candleTimestamp = findMax((await queryApi("/products/ETH-USD/candles")).data)//[0][0]
console.log("orace - candle = ", Number(oraceTimestamp) - Number(candleTimestamp))
console.log("orace - real = ", -Number(Date.now()/1000) + Number(oraceTimestamp))
setTimeout(test, 1000);
}
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment