From d985f39ceae5a8016409cd98b7a448e7e3605b8a Mon Sep 17 00:00:00 2001 From: Dan Connolly Date: Fri, 17 Feb 2023 11:43:53 -0600 Subject: [PATCH] feat(colo17): fetch prices automatically --- packages/colo17/priceFetch.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 packages/colo17/priceFetch.js diff --git a/packages/colo17/priceFetch.js b/packages/colo17/priceFetch.js new file mode 100644 index 0000000..777d429 --- /dev/null +++ b/packages/colo17/priceFetch.js @@ -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; +}