Author

Topic: Need help with Poloniex API (buy/sell command errors) (Read 1084 times)

newbie
Activity: 88
Merit: 0
I could not answer for last few days.

It looks like in the latest version URL encoding happens twice.

Why don't you try the following
Code:

function poloniex(command,options) {

  // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2

  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys");

  var key = sheet.getRange("A1").getValue();
  var secret = sheet.getRange("A2").getValue();
  var nonce = 1495932972127042 + new Date().getTime();

  var payload = options || {};
  payload.nonce = nonce;
  payload.command = command;

  var uri = "https://poloniex.com/tradingApi";

  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret);

  var stringSignature = "";
  for (i = 0; i < signature.length; i++) {
    var byte = signature;
    if (byte < 0)
      byte += 256;
    var byteStr = byte.toString(16);
    if (byteStr.length == 1) byteStr = '0'+byteStr;
    stringSignature += byteStr;
   }

  var headers = {
    "key": key,
    "sign": stringSignature
  }

  var params = {
    "method": "post",
    "headers": headers,
    "payload": payload
  }

// To see the request that is send to Poloniex uncomment following lines.
// var req = getRequest(url, params)
// for(i in req) {
//   Logger.log(i + ": " + req[i]);
// }
 
  var response = UrlFetchApp.fetch(uri, params);

  var dataAll = JSON.parse(response.getContentText());
                process.stdout.write(params)

  return JSON.stringify(dataAll)

}

poloniex("buy", {
                  currencyPair: "USDT_ZEC",
                  amount: 100,
                  rate: 0.01
});

newbie
Activity: 62
Merit: 0
Can you please provide the value of variable payloadEncoded.

Also it will be helpful to put the following

Code:
var req = UrlFetchApp.getRequest(url, params)
before calling  UrlFetchApp.fetch(uri, params); and to provide the value of variable req


I dont know much about google script so I made some changes but it does not work. Can you edit the code directly and make some experiments of the spreadsheet? I can send you the spread sheet with the code embled so you can try.
newbie
Activity: 88
Merit: 0
Can you please provide the value of variable payloadEncoded.

Also it will be helpful to put the following

Code:
var req = UrlFetchApp.getRequest(url, params)
before calling  UrlFetchApp.fetch(uri, params); and to provide the value of variable req

newbie
Activity: 62
Merit: 0
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.

2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.

I tried to add those to the payload, but it even cannot run. Can you help me to get this run? I will be very happy to send you money just to say thank. (If you offer a service to do that, pls give me the price Smiley ).

Try the following (code changes in green):

Quote
// work in progress

// you need a poloniex API key and secret with trading option enabled
// you can test it with:
// = poloniex ("returnBalances","BTC")
// or
// = poloniex ("returnBalances")


function poloniex(command,options) {
      
  // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2
      
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys");
  
  var key = sheet.getRange("A1").getValue();
    
  var secret = sheet.getRange("A2").getValue();

  var nonce = 1495932972127042 + new Date().getTime();
    
 var payload = options || {};
  payload.nonce = nonce;
  payload.command = command;

  
  
  var payloadEncoded = Object.keys(payload).map(function(param) {
    return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]);
  }).join('&');
    
  var uri = "https://poloniex.com/tradingApi";

  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret);
    
  var stringSignature = "";
  
  for (i = 0; i < signature.length; i++) {
    var byte = signature;
    if (byte < 0)
      byte += 256;
    var byteStr = byte.toString(16);
    if (byteStr.length == 1) byteStr = '0'+byteStr;
    stringSignature += byteStr;
   }
    
  var headers = {
    "key": key,
    "sign": stringSignature
  }
  
  var params = {
    "method": "post",
    "headers": headers,
    "payload": payloadEncoded
  }
  
  var response = UrlFetchApp.fetch(uri, params);
  
  var dataAll = JSON.parse(response.getContentText());
  
 //Edited
  return JSON.stringify(dataAll) }
  //end edited

}

Note that you now have to call buy, sell, differently.

eg. you should now be able to call buy like this:

Code:
poloniex("buy", {
  currencyPair: "USDT_ZEC",
  amount: 100,
  rate: 0.01
});

The parameter names within the object {} correspond directly to the expected poloniex API POST parameters.

I haven't actually run it, but send me a PM if it works and you feel like tipping me.

When I run your code, it said: Limit Exceeded: URLFetch header size. (line 57, file "Code")
(Line 57: var response = UrlFetchApp.fetch(uri, params); )
Can you give me your email, I will share the sample spreadsheet with script embled so you can easily help me to fix it?
**I also want to process buy/sell command by placing a function in a cell inside the Spreadsheet (not a function in the script). Can I do that?
Honestly, I dont master in this so certainly I will really appreciate your help with an amount of btc. Thanks
legendary
Activity: 3038
Merit: 2166
Playgram - The Telegram Casino
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.

2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.

I tried to add those to the payload, but it even cannot run. Can you help me to get this run? I will be very happy to send you money just to say thank. (If you offer a service to do that, pls give me the price Smiley ).

Try the following (code changes in green):

Quote
// work in progress

// you need a poloniex API key and secret with trading option enabled
// you can test it with:
// = poloniex ("returnBalances","BTC")
// or
// = poloniex ("returnBalances")


function poloniex(command,options) {
     
  // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2
     
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys");
 
  var key = sheet.getRange("A1").getValue();
   
  var secret = sheet.getRange("A2").getValue();

  var nonce = 1495932972127042 + new Date().getTime();
     
  var payload = options || {};
  payload.nonce = nonce;
  payload.command = command;

 
 
  var payloadEncoded = Object.keys(payload).map(function(param) {
    return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]);
  }).join('&');
   
  var uri = "https://poloniex.com/tradingApi";

  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret);
   
  var stringSignature = "";
 
  for (i = 0; i < signature.length; i++) {
    var byte = signature;
    if (byte < 0)
      byte += 256;
    var byteStr = byte.toString(16);
    if (byteStr.length == 1) byteStr = '0'+byteStr;
    stringSignature += byteStr;
   }
   
  var headers = {
    "key": key,
    "sign": stringSignature
  }
 
  var params = {
    "method": "post",
    "headers": headers,
    "payload": payloadEncoded
  }
 
  var response = UrlFetchApp.fetch(uri, params);
 
  var dataAll = JSON.parse(response.getContentText());
 
  //Edited
  return JSON.stringify(dataAll) }
  //end edited

}

Note that you now have to call buy, sell, differently.

eg. you should now be able to call buy like this:

Code:
poloniex("buy", {
  currencyPair: "USDT_ZEC",
  amount: 100,
  rate: 0.01
});

The parameter names within the object {} correspond directly to the expected poloniex API POST parameters.

I haven't actually run it, but send me a PM if it works and you feel like tipping me.
newbie
Activity: 62
Merit: 0
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.

2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.

I tried to add those to the payload, but it even cannot run. Can you help me to get this run? I will be very happy to send you money just to say thank. (If you offer a service to do that, pls give me the price Smiley ).
newbie
Activity: 62
Merit: 0
Thank you all for your helps!
newbie
Activity: 9
Merit: 0
Very good answer. I have polonex.
legendary
Activity: 3038
Merit: 2166
Playgram - The Telegram Casino
1) Your function call poloniex("buy","USDT_ZEC",100,0.01) includes 4 arguments, your function poloniex(command,parameter,subparam) only accepts 3.

2) Your function poloniex doesn't actually do anything with the arguments you receive. You're only sending the command parameter (and the nonce), no currency pair, rate or total. You need to add those to the payload object.
newbie
Activity: 88
Merit: 0
Can you print the value of payloadEncoded variable?

I do not see that you pass field names  "currencyPair", "rate", and "amount" to your function.
 
newbie
Activity: 62
Merit: 0
Here is my code:

Quote
// work in progress

// you need a poloniex API key and secret with trading option enabled
// you can test it with:
// = poloniex ("returnBalances","BTC")
// or
// = poloniex ("returnBalances")


function poloniex(command,parameter,subparam) {
      
  // I assume that all the keys are in the "keys" spreadsheet. The key is in cell A1 and the secret in cell A2
      
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("keys");
  
  var key = sheet.getRange("A1").getValue();
    
  var secret = sheet.getRange("A2").getValue();

  var nonce = 1495932972127042 + new Date().getTime();
    
  var payload = {
    "nonce": nonce,
    "command": command
  }
  
  var payloadEncoded = Object.keys(payload).map(function(param) {
    return encodeURIComponent(param) + '=' + encodeURIComponent(payload[param]);
  }).join('&');
    
  var uri = "https://poloniex.com/tradingApi";

  var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, payloadEncoded, secret);
    
  var stringSignature = "";
  
  for (i = 0; i < signature.length; i++) {
    var byte = signature;
    if (byte < 0)
      byte += 256;
    var byteStr = byte.toString(16);
    if (byteStr.length == 1) byteStr = '0'+byteStr;
    stringSignature += byteStr;
   }
    
  var headers = {
    "key": key,
    "sign": stringSignature
  }
  
  var params = {
    "method": "post",
    "headers": headers,
    "payload": payloadEncoded
  }
  
  var response = UrlFetchApp.fetch(uri, params);
  
  var dataAll = JSON.parse(response.getContentText());
  
  //Edited
  if (parameter === undefined) {
    Logger.log(JSON.stringify(dataAll))
    return JSON.stringify(dataAll) }
  else if(parameter != undefined && subparam === undefined) {
    return dataAll[parameter] }
  else if (parameter != undefined && subparam != undefined) {
    return dataAll[parameter][subparam] }
  //end edited
}
newbie
Activity: 62
Merit: 0
Hi,
I am creating a trading tool with google spreadsheets and google apps scripts (very similar to Javascript). Everything seems to be ok (check balances, get addresses,...) but the buy/sell command. When I place an order by this command: = poloniex("buy","USDT_ZEC",100,0.01) and the api response: {"error":"Total must be at least 0.0001."}.
I tried to change the param to other values but always get the same results (even when I change the pair name to a wrong name like BTC_XXX, it answered the same error). PLease help me to address this. Thanks in advance!
Jump to: