2

我无法弄清楚 Kraken API 输入的去向。我使用 https://api.kraken.com/0/private/AddOrder作为起点。输入包括:pair = XBTUSD、type = buy、ordertype = limit 等。我是 API 新手,我意识到这不是一种典型的方法,但我非常感谢任何指导。

我已经成功地将 API 用于检索帐户余额等其他事情。只是不确定输入适合的位置。我无法从这里的文档中弄清楚https://www.kraken.com/help/api。我想使用一个特定的购买订单作为学习示例。即买入0.003 BTC,配对XBTUSD,限5000 USD...

相关的代码 -

function buyKraken () {  

  var path = "/0/private/AddOrder";   
  var nonce = new Date () * 1000;  
  var postdata = "nonce=" + nonce;  
  var signature = getKrakenSignature (path, postdata, nonce);  
  var url = 'https://api.kraken.com' + path;  

var options = {  
    method: 'post',  
    headers: {  
      'API-Key': "###########",  
      'API-Sign': signature  
    },      
    payload: postdata  
  };  
  var response = UrlFetchApp.fetch (url, options);  
  Logger.log(response);  
  ;
}
4

2 回答 2

1

对于可能来寻找答案的新人,我使用了一个不同的小功能:

    function getKrakenSignature (path, postdata, krakenSecretKey, nonce) {
        var sha256obj = new jsSHA ("SHA-256", "BYTES");
        sha256obj.update (nonce + postdata);
        var hash_digest = sha256obj.getHash ("BYTES");

        var sha512obj = new jsSHA ("SHA-512", "BYTES");
        sha512obj.setHMACKey (krakenSecretKey, "B64");
        sha512obj.update (path);
        sha512obj.update (hash_digest);
        return sha512obj.getHMAC ("B64");
     }

我使用了这个存储库中的 jsSHA 函数:https ://github.com/Caligatio/jsSHA 您只需在 Google 脚本中创建一个新文件,然后复制/粘贴该文件的原始文件: https://raw.githubusercontent .com/Caligatio/jsSHA/master/src/sha.js

于 2020-02-14T17:46:15.797 回答
0

请参阅 Github 的这个kraken api项目 并转到示例部分。

你需要的部分是这个,它是所有代码的一部分,请不要复制和粘贴

function QueryPrivate($method, array $request = array())
    {
        if(!isset($request['nonce'])) {
            // generate a 64 bit nonce using a timestamp at microsecond resolution
            // string functions are used to avoid problems on 32 bit systems
            $nonce = explode(' ', microtime());
            $request['nonce'] = $nonce[1] . str_pad(substr($nonce[0], 2, 6), 6, '0');
        }
        // build the POST data string
        $postdata = http_build_query($request, '', '&');
        // set API key and sign the message
        $path = '/' . $this->version . '/private/' . $method;
        $sign = hash_hmac('sha512', $path . hash('sha256', $request['nonce'] . $postdata, true), base64_decode($this->secret), true);
        $headers = array(
            'API-Key: ' . $this->key, // "your key", 
            'API-Sign: ' . base64_encode($sign)//your signature
        );
        // make request
        curl_setopt($this->curl, CURLOPT_URL, $this->url . $path);
        curl_setopt($this->curl, CURLOPT_POSTFIELDS, $postdata);
        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $headers);
        $result = curl_exec($this->curl);
        if($result===false)
            throw new KrakenAPIException('CURL error: ' . curl_error($this->curl));
        // decode results
        $result = json_decode($result, true);
        if(!is_array($result))
            throw new KrakenAPIException('JSON decode error');
        return $result;
    }

// Add a standard order: buy €300 worth of BTC at market at 2013-08-12T09:27:22+0000 
$res = QueryPrivate('AddOrder', array(
    'pair' => 'XBTCZEUR', 
    'type' => 'buy', 
    'ordertype' => 'market', 
    'oflags' => 'viqc',
    'volume' => '300', 
    'starttm' => '1376299642' 
));
print_r($res);
于 2018-04-06T11:01:32.123 回答