battagliaem

Poloniex GET/POST Google Script spreadsheet

Jun 9th, 2016
1,820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This is to use Google spreadsheet and Google Script with the Poloniex API both using public calls (GET) than private calls (POST) --> BTC donation: 122gvDXaLNLHyaYuMk9jh7fHXcRAffM6D5  ETH donation: 0x92e4A8627455c4069d6A963B793CE64013772Fb6
  2.  
  3. function updatePoloniex()
  4. {
  5.   // TODO: This function is using the GET method and it's for the public api so you won't need login.
  6.   // Choose the command you want from https://poloniex.com/support/api/, in my case is returnTicker
  7.   var response = UrlFetchApp.fetch("https://poloniex.com/public?command=returnTicker");
  8.   // TODO: set your sheet name here
  9.   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DATA");
  10.   // The following line will parse in the object 'json' the result of 'returnTicker' which has previously been put into the variable 'response'
  11.   var json = JSON.parse(response.getContentText());
  12.   // next, we ask to create a variable for each of the ticker we want to look for
  13.   var rate = json.BTC_MAID.last;
  14.   var rate2 = json.BTC_DAO.last;
  15.   var rate3 = json.BTC_LSK.last;
  16.   var rate4 = json.BTC_ETH.last;
  17.   var rate5 = json.ETH_DAO.last;
  18.   var rate6 = json.ETH_LSK.last;
  19.   var rate7 = json.USDT_ETH.last;
  20.  
  21.   // then we put the result of the parsing into the coordinates we desire on the sheet
  22.   // TODO: set column coordinates here in format (column, row); this is now set to A1
  23.   sheet.getRange(1, 1).setValue(rate);
  24.   sheet.getRange(2, 1).setValue(rate2);
  25.   sheet.getRange(3, 1).setValue(rate3);
  26.   sheet.getRange(4, 1).setValue(rate4);
  27.   sheet.getRange(5, 1).setValue(rate5);
  28.   sheet.getRange(6, 1).setValue(rate6);
  29.   sheet.getRange(7, 1).setValue(rate7);
  30. };
  31.  
  32.   // THIS IS THE PART THAT SHOWS YOU HOW TO DO PRIVATE CALLS with the POST METHOD and your Poloniex secret key
  33. function sendHttpPost() {
  34.  
  35.   // First you create a nonce, which is an incremental random number, to do so, we can use the current date time summed with a number
  36.   var nonce = 1465426902234426 + new Date().getTime();
  37.  
  38.   // First choose a command from here https://poloniex.com/support/api/ seeing if it require specific options
  39.   // Then, we set the variable 'p' as a string which combine the command & any related parameter (if any) & the nonce.
  40.   // in this case we specified the account=all parameter for the command=returnCompleteBalances as it gives us also loans and on orders balances
  41.   var p = "command=returnCompleteBalances&account=all&nonce="+nonce
  42.  
  43.   // Then, we sign this variable 'p' with our secret key (taken from the API of Poloniex) to obtain a new string 'signature'
  44.    var signature = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512,
  45.                                                 p,
  46.                                                 "HERE YOU PUT YOUR SECRET KEY");
  47.  
  48.   // Then, we convert the resulting string: from an array of byte (which is a standard output) to HEX
  49.   signature = signature.map(function(byte) {
  50.     return ('0' + (byte & 0xFF).toString(16)).slice(-2);
  51.   }).join('')
  52.  
  53.   // Then we create the variable 'headers' and we specify in the object "Key" the API key associated with the secret key (always taken from the API of Poloniex), and we pass the 'signature' output string to the object "Sign"
  54.   var headers = {
  55.     "Key" : "HERE YOU PUT YOUR API KEY",
  56.     "Sign" : signature
  57.   };
  58.  
  59.     // then we define 'options' as POST method, specifying the headers and the payload
  60.    var options = {
  61.      "method" : "POST",
  62.      "headers": headers,
  63.      "payload": p
  64.    };
  65.  
  66.  
  67.   // then we fetch the url passing the 'options' which make us call the command and sign it
  68.   var response2 = UrlFetchApp.fetch("https://poloniex.com/tradingApi", options);
  69.  
  70.   // we decide in which sheet of the current Google spreadsheet doc we want to dump the results
  71.    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DATA");
  72.  
  73.   // then we parse the fetched url in the var 'json2'
  74.    var json2 = JSON.parse(response2.getContentText());
  75.  
  76.   // asking different values that we pass into specific variables
  77.    var btcbalance = json2.BTC;
  78.    var maidbalance = json2.MAID;
  79.    var daobalance = json2.DAO;
  80.    var lskbalance = json2.LSK;
  81.    var ethbalance = json2.ETH;
  82.   Logger.log(btcbalance);
  83.  
  84.   // since each currency provide three balances onOrders, available, btcValue in a single string but we want them in single cell of the sheet we take them providing them a home :^)
  85.  
  86.     sheet.getRange(1, 3).setValue(btcbalance.onOrders);
  87.     sheet.getRange(1, 4).setValue(btcbalance.available);
  88.     sheet.getRange(1, 5).setValue(btcbalance.btcValue);
  89.  
  90.     sheet.getRange(2, 3).setValue(maidbalance.onOrders);
  91.     sheet.getRange(2, 4).setValue(maidbalance.available);
  92.     sheet.getRange(2, 5).setValue(maidbalance.btcValue);
  93.  
  94.     sheet.getRange(3, 3).setValue(daobalance.onOrders);
  95.     sheet.getRange(3, 4).setValue(daobalance.available);
  96.     sheet.getRange(3, 5).setValue(daobalance.btcValue);
  97.  
  98.     sheet.getRange(4, 3).setValue(lskbalance.onOrders);
  99.     sheet.getRange(4, 4).setValue(lskbalance.available);
  100.     sheet.getRange(4, 5).setValue(lskbalance.btcValue);
  101.  
  102.     sheet.getRange(5, 3).setValue(ethbalance.onOrders);
  103.     sheet.getRange(5, 4).setValue(ethbalance.available);
  104.     sheet.getRange(5, 5).setValue(ethbalance.btcValue);
  105.  
  106.  
  107.  
  108.    };
Add Comment
Please, Sign In to add comment