Skip to content

Commit d985f39

Browse files
committed
feat(colo17): fetch prices automatically
1 parent 095a81f commit d985f39

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

packages/colo17/priceFetch.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const CoinGecko = {
2+
keyRange: 'priceKey',
3+
priceApi: 'https://pro-api.coingecko.com/api/v3/simple/price'
4+
};
5+
6+
/**
7+
* ack: https://www.coingecko.com/en/api/documentation
8+
*/
9+
function FetchPrice(token='agoric', vs='usd') {
10+
console.warn('AMBIENT: UrlFetchApp');
11+
const { fetch } = UrlFetchApp;
12+
console.warn('AMBIENT: SpreadsheetApp');
13+
const doc = SpreadsheetApp.getActive();
14+
const apiKey = doc.getRangeByName(CoinGecko.keyRange).getValue();
15+
16+
const url = `${CoinGecko.priceApi}?ids=${token}&vs_currencies=${vs}&x_cg_pro_api_key=${apiKey}`;
17+
18+
const resp = fetch(url,
19+
{ headers: {accept: 'application/json'}});
20+
const data = JSON.parse(resp.getContentText());
21+
// console.log(data);
22+
const price = data[token].usd;
23+
return price;
24+
}

0 commit comments

Comments
 (0)