array – Can my javascript code for pulling data from an API and working with it in Goole Sheets be more efficient?

I’m trying to build out some DeFi position tracking pages in Google Sheets. I want to pull from the Zapper API (or other APIs) to track trades, positions, gains, losses, fees…etc. This is my first crack at tracking a position on SushiSwap.

I tried to make re-usable functions and organize the code so it’s easy to understand/work with.

I’d love some insight on better ways to write the functions and/or organize the code. I think this could be a big project and I want to make sure I’m using a good system to write code and organize things. Basically, am I on the right track? Where could I do better?

// function to grab the API data and parse it.
// returns an array
function pullAndParseAPISushi (walletAddress, networkName){
  var apiKEY = "96e0cc51-a62e-42ca-acee-910ea7d2a241";  // API key from Zapper
  var url = "https://api.zapper.fi/v1/staked-balance/masterchef?addresses%5B%5D="+ 
walletAddress + "&network=" + networkName + "&api_key=" + apiKEY;  // assembles the API URL with the wallet address and network name
  var response = UrlFetchApp.fetch(url); // pulls data from the API
  var theparsedJSONdata = JSON.parse(response); // parses the JSON response from the API

  return theparsedJSONdata  // returns the parsed array
}


// access level 2 of the json data - it gets at the array of values after the wallet address 
/** this is the key line that breaks open the data.  Need this for next level data */
/** first level is the wallet address, second level is the big array, third level is the reward tokens...etc. */
function createTheArray(parsedDataFromJson, walletAddress) { 

  var levelTwo = parsedDataFromJson(walletAddress)(0) // 'breaks through' the first array

  var lastArrayReturn = ();  /** creates and loads an array with all the various pairs */
        for (const key in levelTwo){
           //console.log(key + " -> " + levelTwo(key))
           var tempArray = new Array(0)    // not sure why new array has argument for 0
           tempArray.push(key,levelTwo(key))  // stores each pair in a sigle array
           lastArrayReturn.push(tempArray) // loads smaller arrays into larger, single array; 2D array
    }

  // lastArrayReturn is an array of arrays for level 2 data (i.e. the data after the wallet address in the JSON object)
  console.log(lastArrayReturn)

  return lastArrayReturn
}


// transposes a given array 
function transposeArray(theArray){
  var result = new Array(theArray(0).length);
  for(var i=0; i < result.length; i++){
      result(i) = new Array(theArray.length);
      for(var j = 0; j < result(i).length; j++){
        result(i)(j) = theArray(j)(i)
      }
  }
  console.log("the transposed array is: " + result)
  return result
}


// putting the array from the api parse into the Sushi Data Pull sheet
function placeSushiData (anArray) {

    theSushiArray = transposeArray(anArray)  // call a function to transpose the array
    let ss = SpreadsheetApp.getActiveSpreadsheet();           // get active spreadsheet
    let targetSheet = ss.getSheetByName('Sushi Data Pull');   // the tab where the data is going
    let targetRange = targetSheet.getRange(1,4, theSushiArray.length, theSushiArray(0).length);  // set the targe range of cells
    let targetDateRange = targetSheet.getRange(2,3,1,1);  // range for the timestamp; 2nd row, 3rd column

    targetRange.setValues(theSushiArray); // sets cells in the target range to the values in the array
    targetDateRange.setValue(new Date()); // puts time stamp in the date column
}


// fuction to run the data pull and placement
function runTheProgram(){
    var walletAddress = "0x00000000000000000000000000";
    var networkName = "polygon";
    var dataArray = pullAndParseAPISushi(walletAddress, networkName)
    var adjustedArray = createTheArray(dataArray, walletAddress)
    placeSushiData(adjustedArray)
}