我正在编写一个脚本,它将取消我在GDAX上的所有订单。根据取消订单的文件,我需要向/delete发送一个删除请求。但我想,在我做这件事之前,我需要先做在消息上签名。
当我使用fetch提交请求时,我得到以下响应:{ message:‘’}
下面是我正在处理的代码示例,当然是替换了机密内容:
var crypto = require('crypto');
var fetch = require('fetch');
const coinbaseSecret = 'abc...';
const coinbaseAPIKey = 'abc...';
const coinbasePassword = 'abc...';
const coinbaseRestAPIURL = "https://api-public.sandbox.gdax.com";
function start(){
getTime(function(time){
cancelAll(time, function(){
console.log('done');
});
});
}
function getTime(callback){
fetch.fetchUrl(coinbaseRestAPIURL + '/time', null, function(error, meta, body){
var response = JSON.parse(body.toString());
console.log('response', response);
var timeStamp = response.epoch;
callback(timeStamp);
});
}
function cancelAll(timeStamp, callback) {
// Refer to https://docs.gdax.com/#cancel-an-order
var signature = getSignature('DELETE', '/delete', "");
console.log('signature', signature);
var headers = {
'Content-Type': 'application/json',
'CB-ACCESS-KEY': coinbaseAPIKey,
'CB-ACCESS-SIGN': signature,
'CB-ACCESS-TIMESTAMP': timeStamp, //Date.now() / 1000,
'CB-ACCESS-PASSPHRASE': coinbasePassword
};
console.log('headers', headers);
fetch.fetchUrl(coinbaseRestAPIURL + '/delete', {
method: 'DELETE',
headers: headers
}, function(error, meta, body){
var response = JSON.parse(body.toString());
console.log('response', response);
callback();
})
}
function getSignature(method, requestPath, body) {
// Refer to https://docs.gdax.com/#signing-a-message
const secret = coinbaseSecret;
const timestamp = Date.now() / 1000;
const what = timestamp + method + requestPath + body;
const key = Buffer(secret, 'base64');
const hmac = crypto.createHmac('sha256', key);
const signature = hmac.update(what).digest('base64');
return signature;
}
start();
发布于 2018-01-03 12:59:38
转到Gdax-Node Github repo,看看它们的代码和示例。
1)通过使用api详细信息配置authenticatedClient来创建它,2)然后使用authedClient对象和calncelAllOrders方法:
authedClient.cancelAllOrders({product_id: 'BTC-USD'}, callback);您可以用一个函数来包装它,调用'x‘的次数(它在文档中声明),或者如果您愿意的话,可以冷冰冰地想一些更花哨的东西。
注意:-确保您拉下github回购,不要直接从npm安装,因为有一些错误和问题已经解决了git回购,但没有被推到npm。
...so在下载gdax包时使用npm install coinbase/gdax-node。
希望这能帮点忙..。
https://stackoverflow.com/questions/46480959
复制相似问题