In my application, I use the async/await
logic for working with an API.
The problem is I can’t figure out why the check()
function works with no hassle:
const KEY = 'my key';
const SECRET = 'my secret';
function getSign(form) {
return crypto.createHmac('sha512', SECRET).update(form).digest('hex').toString();
}
async function check() {
let form = {};
let header = {'Content-Type':'application/x-www-form-urlencoded'};
header.KEY = KEY;
header.SIGN = await getSign(querystring.stringify(form));
// Checking the state (works)!
let info = await fetch(API_TRADE_URL + OPENORDERS_URL, { method:'POST', headers:header, form:form })
info = info.json();
return info;
}
… while the placeOrder()
function fails and the API sends a message there is a problem with KEY/SECRET (Invalid data
):
async function placeOrder ( currencyPair, rate, amount ) {
let form = { 'currencyPair':currencyPair, 'rate':rate, 'amount':amount };
let header = {};
header.KEY = KEY;
header.SIGN = getSign(querystring.stringify(form));
// Here I am placing the order (does not work!)
let orderRes = await fetch(API_TRADE_URL + BUY_URL, { method:'POST', headers:header, form:form } );
orderRes = orderRes.json();
return orderRes;
}
I was suspicious about the KEY/SECRET pair or the getSign()
function but the buy()
function (based on Node’s request
) provided as a demo on the API’s site (the function which I am trying to mimic) works as expected:
function buy(currencyPair, rate, amount, cp) {
let form = {'currencyPair':currencyPair,'rate':rate,'amount':amount};
let header = {};
header.KEY = KEY;
header.SIGN = getSign(querystring.stringify(form));
// Here I am placing the order (works!)
Request({method: 'POST', url: API_TRADE_URL + BUY_URL, headers: header, form:form }, function (res) { console.log(res) });
}
The important thing here is I don’t want to use the buy()
function because I need to work without callbacks here. I use the same data, the same logic (working in the check()
function mentioned above) but it fails in the case of the placeOrder
function.