Created
July 10, 2022 11:47
-
-
Save kangchihlun/44a0c6d8518e038ad06d39aa72951ff2 to your computer and use it in GitHub Desktop.
okex-V5 api request
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
const fetch = require('node-fetch') | |
const CryptoJS = require('crypto-js') | |
var accessKey = `` | |
var passphrase = `` | |
var secretKey = `` | |
const httpGet = (url, param,accessKey,passphrase,secretKey) => { | |
let paramKeys = Object.keys(param) | |
for (let index in paramKeys) { | |
if (index === 0) { | |
url += `?${paramKeys[index]}=${param[index]}` | |
} | |
else { | |
url += `&${paramKeys[index]}=${param[index]}` | |
} | |
} | |
const timestamp = new Date().toISOString() | |
const dirUrl = url.replace(/.*\/\/[^\/]*/, '') | |
let sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + 'GET' + dirUrl, secretKey)) | |
let options = { | |
method: 'get', | |
headers: { | |
'OK-ACCESS-KEY': accessKey, | |
'OK-ACCESS-SIGN': sign, | |
'OK-ACCESS-TIMESTAMP': timestamp, | |
'OK-ACCESS-PASSPHRASE': passphrase, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'x-simulated-trading': process.env.simulated | |
} | |
} | |
return fetch(url, options) | |
} | |
const httpPost = (url, param, accessKey, passphrase,secretKey) => { | |
const jsonValue = JSON.stringify(param) | |
const timestamp = new Date().toISOString() | |
const dirUrl = url.replace(/.*\/\/[^\/]*/, '') | |
let sign = CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(timestamp + 'POST' + dirUrl + jsonValue, secretKey)) | |
let options = { | |
method: 'post', | |
body: JSON.stringify(param), | |
headers: { | |
'OK-ACCESS-KEY': accessKey, | |
'OK-ACCESS-SIGN': sign, | |
'OK-ACCESS-TIMESTAMP': timestamp, | |
'OK-ACCESS-PASSPHRASE': passphrase, | |
'Accept': 'application/json', | |
'Content-Type': 'application/json', | |
'x-simulated-trading': process.env.simulated | |
} | |
} | |
return fetch(url, options) | |
} | |
// 取得帳戶餘額 | |
// 返回 | |
// { adjEq: '', | |
// details: [Array], | |
// imr: '', | |
// isoEq: '0', | |
// mgnRatio: '', | |
// mmr: '', | |
// notionalUsd: '', | |
// ordFroz: '', | |
// totalEq: '4819.54090876904', | |
// uTime: '1621500993660' } | |
const getWalletByCurrency = async (currency)=> { | |
return await httpGet(`${baseurl}/api/v5/account/balance?ccy=${currency}`, {}, accessKey, passphrase,secretKey) | |
} | |
const placeorder = async (ordst) => { | |
return await httpPost(`${baseurl}/api/v5/trade/order`,ordst, accessKey, passphrase,secretKey) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can I withdraw my USDT ERC20 to another wallet without paying gas fees if I use this script