Skip to content

Commit

Permalink
feat(colo17): fetch prices automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
dckc committed Feb 17, 2023
1 parent 92a71b4 commit 5fc3464
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/colo17/priceFetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const CoinGecko = {
keyRange: 'priceKey',
priceApi: 'https://pro-api.coingecko.com/api/v3/simple/price'
};

/**
* ack: https://www.coingecko.com/en/api/documentation
*/
function FetchPrice(token='agoric', vs='usd') {
console.warn('AMBIENT: UrlFetchApp');
const { fetch } = UrlFetchApp;
console.warn('AMBIENT: SpreadsheetApp');
const doc = SpreadsheetApp.getActive();
const apiKey = doc.getRangeByName(CoinGecko.keyRange).getValue();

const url = `${CoinGecko.priceApi}?ids=${token}&vs_currencies=${vs}&x_cg_pro_api_key=${apiKey}`;

const resp = fetch(url,
{ headers: {accept: 'application/json'}});
const data = JSON.parse(resp.getContentText());
// console.log(data);
const price = data[token].usd;
return price;
}

0 comments on commit 5fc3464

Please sign in to comment.