Created
December 16, 2021 09:23
-
-
Save rawinng/6000c3e6e1d2dbe210bcb63b46643ff5 to your computer and use it in GitHub Desktop.
Sample connection to FTX exchange.
This file contains 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
var hmacSHA256 = require("crypto-js/hmac-sha256"); | |
var hex = require("crypto-js/enc-hex"); | |
var ftxConf = require("../conf/ftx"); | |
var axios = require("axios"); | |
const signRequest = (method, url, body = null) => { | |
const ts = new Date().getTime(); | |
var payload = `${ts}${method}${url}`; | |
if (body !== null) { | |
payload += `${body}`; | |
} | |
const signature = hex.stringify(hmacSHA256(payload, ftxConf.apiSecret)); | |
return { | |
signature: signature, | |
ts: ts, | |
}; | |
}; | |
const requestFtx = (method, url, body = null) => { | |
const signatureResult = signRequest(method, url, body); | |
const headers = { | |
"FTX-KEY": ftxConf.apiKey, | |
"FTX-TS": signatureResult.ts, | |
"FTX-SIGN": signatureResult.signature, | |
}; | |
const req = { | |
baseURL: ftxConf.url, | |
method: method, | |
url: url, | |
headers: headers, | |
}; | |
if (body !== null) { | |
req = { ...req, data: body }; | |
} | |
return axios.request(req); | |
}; | |
// sample call | |
requestFtx("GET", "/api/wallet/saved_addresses").then( | |
(resp) => { | |
console.log(resp.statusText); | |
console.log(JSON.stringify(resp.data)); | |
}, | |
(err) => { | |
console.log(err.stack); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment