Last active
May 12, 2022 02:25
-
-
Save pahud/c6d3363589cdb915de82ba9c6288cfe4 to your computer and use it in GitHub Desktop.
Apps Script to update STEPN prices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function updateData(){ | |
// var ss = SpreadsheetApp.getActiveSpreadsheet(), | |
var sheet = SpreadsheetApp.getActive().getSheetByName('GST') | |
let coins = [ | |
'green-satoshi-token-bsc', | |
'green-satoshi-token', | |
'binancecoin', | |
'solana', | |
]; | |
var prices = getCGSimplePrices(coins.join(','), 'usd') | |
console.log(prices) | |
let gst_bsc = getPrice(prices, 'green-satoshi-token-bsc', 'usd'); | |
let gst_sol = getPrice(prices, 'green-satoshi-token', 'usd'); | |
let bnb = getPrice(prices, 'binancecoin', 'usd'); | |
let sol = getPrice(prices, 'solana', 'usd'); | |
// request new value at every spreadsheets content change. | |
sheet.getRange("B1").setValue(gst_sol) | |
sheet.getRange("B2").setValue(gst_bsc) | |
sheet.getRange("B3").setValue(bnb) | |
sheet.getRange("B4").setValue(sol) | |
} | |
function getPrice(prices, id, cur) { | |
return prices[id][cur] | |
} | |
function getCGSimplePrices(ids, vsc){ | |
var fetch_url = "https://api.coingecko.com/api/v3/simple/price?ids="+ ids + "&vs_currencies=" + vsc | |
Logger.log(fetch_url) | |
options = {muteHttpExceptions: true}; | |
for (let i =0; i<20; i++) { | |
var response = UrlFetchApp.fetch(fetch_url, options); | |
console.log(response.getResponseCode()) | |
console.log('i='+i) | |
if (response.getResponseCode() == 200) { break } | |
Utilities.sleep(500*i) | |
} | |
var dataAll = Utilities.jsonParse(response.getContentText()); | |
return dataAll; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment