Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buy order #2

Open
mfizz1 opened this issue Feb 13, 2018 · 1 comment
Open

Buy order #2

mfizz1 opened this issue Feb 13, 2018 · 1 comment

Comments

@mfizz1
Copy link

mfizz1 commented Feb 13, 2018

I know its not directly related but I am struggling to use your methods to place a buy order I have replaced body with

var body = JSON.stringify({ price: '1.0', size: '1.0', side: 'buy', product_id: 'BTC-USD' })

and method with POST and requestURL with /orders keeps coming back with error 400 invalid signature

@obixmt
Copy link

obixmt commented Feb 22, 2018

I just spent 4 days on this same issue before finally succeeding, I feel your pain. I believe the issue is in how the function UrlFetchApp.fetch() options are bundled. Here is some of my code, specifically java uses 'payload' not 'body'! Put your info in the myAuth() function first.

I spent FOREVER looking for a piece of code like this, hope it helps!

function myAuth(){
  var vAuth = {
    API_key:'<your_key>',
    API_secret:'<your_secret>',
    API_passphrase:'<your_passphase>',
    API_baseurl:'https://api.gdax.com'};
  
  return vAuth;
}


function postOrder(vOrder) {
  var vMethod = 'POST';
  var vPath = '/orders';

  var vBody = JSON.stringify({
    'price': vOrder.price, 
    'size': vOrder.size, 
    'side': vOrder.side, 
    'type': vOrder.type, 
    'product_id': vOrder.product_id});
  
  var vParams = getAuth(vMethod, vPath, vBody);
  
  var vResp = UrlFetchApp.fetch(vParams.url, vParams.options);
  if (vResp.getResponseCode() == 200) {
    var json = JSON.parse(vResp.getContentText());
    Logger.log(json);
  }
}


function getAuth(vMethod, vPath, vBody){
  var vAuth = myAuth(); // Function returns array containing authentication info.

  var vTimeStamp = Math.floor(new Date().getTime()/1000).toString();

  var vMsg = vTimeStamp + vMethod + vPath + vBody;
  var vSecret = CryptoJS.enc.Base64.parse(vAuth.API_secret);  // decode secret
  var vHash64 = CryptoJS.HmacSHA256(vMsg, vSecret);  // create hash
  var vSignature = vHash64.toString(CryptoJS.enc.Base64);  // encode signature
 
// This is where the problem usually is!
  var vParams = {
    'method': vMethod,
    'headers': {
      'CB-ACCESS-TIMESTAMP': vTimeStamp, 
      'CB-ACCESS-SIGN': vSignature, 
      'CB-ACCESS-PASSPHRASE': vAuth.API_passphrase,
      'CB-ACCESS-KEY': vAuth.API_key},
    'payload': vBody,
    'contentType': 'application/json'};

  return { 'url': vAuth.API_baseurl + vPath, 'options': vParams};
}

// Call the function however you like...
postOrder({'price':'100.0', 'size': '0.1', 'side': 'buy', 'type': 'limit', 'product_id': 'LTC-USD' });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants