Created
April 14, 2022 18:25
-
-
Save ranfysvalle02/a6f2bf0bc54960b55acbcae2cf7dc281 to your computer and use it in GitHub Desktop.
Algotrading+Realm App Services
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports = async function(arg){ | |
const crypto = require('crypto'); | |
const axios = require('axios').default; | |
const createCBRequest = (method, path) => { | |
// current unix timestamp in seconds (note Date.now() returns milliseconds) | |
var cb_access_timestamp = Date.now() / 1000; | |
// obtained API key, secret, and passphrase from https://pro.coinbase.com/profile/api | |
var cb_access_passphrase = context.values.get('API_PASSPHRASE'); | |
var cb_api_key = context.values.get('API_KEY'); | |
var cb_api_secret = context.values.get('API_SECRET'); | |
var body = ''; // body is empty in this case | |
// create the prehash string by concatenating required parts | |
var message = cb_access_timestamp + method + path + body; | |
// decode the base64 secret | |
// NOTE: added .from() to address DeprecationWarning: Buffer() is deprecated | |
var key = Buffer.from(cb_api_secret, 'base64'); | |
// create a sha256 hmac with the secret | |
var hmac = crypto.createHmac('sha256', key); | |
// sign the require message with the hmac, and finally base64 encode the result | |
var cb_access_sign = hmac.update(message).digest('base64'); | |
const config = { | |
method, | |
url: `https://api.exchange.coinbase.com${path}`, | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': 0, | |
'User-Agent': 'mdb-algorealm', | |
'CB-ACCESS-KEY': cb_api_key, | |
'CB-ACCESS-PASSPHRASE': cb_access_passphrase, | |
'CB-ACCESS-SIGN': cb_access_sign, | |
'CB-ACCESS-TIMESTAMP': cb_access_timestamp | |
} | |
}; | |
return axios(config); | |
} | |
const response = await createCBRequest('GET', '/oracle'); | |
return {response: response.data}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment