Skip to content

Instantly share code, notes, and snippets.

@howardpen9
Last active July 27, 2021 02:21
Show Gist options
  • Save howardpen9/c2c85ad65c3b4ac8889880a4ce14ed4b to your computer and use it in GitHub Desktop.
Save howardpen9/c2c85ad65c3b4ac8889880a4ce14ed4b to your computer and use it in GitHub Desktop.
Defillama API Fetch
function pullJSON() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("TVL_Protocol");
var url = "https://api.llama.fi/protocols"; // Paste your JSON URL here
var response = UrlFetchApp.fetch(url); // get feed
var dataAll = JSON.parse(response.getContentText()); //
var dataSet = dataAll;
const dt = new Date()
.toJSON();
///.toLocaleString('en-us', { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', hour12: true, timeZone: 'Asia/Taipei' });
//.replace(/(\d+)\/(\d+)\/(\d+)/, '$3-$1-$2');
var rows = [],
data;
for (i = 0; i < dataSet.length; i++) {
data = dataSet[i];
rows.push([
dt,
data.id,
data.name,
data.address,
data.symbol,
data.url,
data.description,
data.logo,
data.audits,
data.audit_note,
data.gecko_id,
data.cmcId,
data.category,
data.module,
data.twitter,
data.slug,
data.chains[0],
data.change_1h,
data.change_1d,
data.change_7d,
data.tvl,
data.chainTvls.Ethereum,
data.chainTvls.Optimism,
data.chainTvls.Polygon,
data.chainTvls.Fantom,
data.chainTvls.Solana,
data.chainTvls.Terra,
data.chainTvls.Binance,
data.chainTvls.Heco,
data.chainTvls.Avalanche,
data.chainTvls.Algorand,
data.chainTvls.xDai,
data.chainTvls.Kava,
data.chainTvls.NEO,
data.chainTvls.Osmosis,
data.chainTvls.Sifchain,
data.chainTvls.RSK,
data.chainTvls.Zilliqa,
data.chainTvls.OKExChain,
data.chainTvls.Secret,
data.chainTvls.Osmosis,
]); //your JSON entities here
}
sheet.getRange(2, 1, rows.length, rows[0].length).setValues(rows);
//dataRange.setValues(rows);
// Copy and Paste to historical Sheet
var sheets = SpreadsheetApp.openById("1AEMzKDa_P-8KPGwwY_SCIlLE--RPkpEZYNhCmIrqp2E").getSheetByName("TVL_Historical");
data = sheet.getRange(2, 1, rows.length, rows[0].length).getValues();
sheets.getRange( sheets.getLastRow()+1 , 1, data.length, data[0].length ).setValues(data)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment