forked from dfinity/portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxdr-price.js
35 lines (29 loc) · 923 Bytes
/
xdr-price.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const fetch = require("node-fetch-retry");
let cache;
/** @type {import('@docusaurus/types').PluginModule} */
const xdrPricePlugin = async function (context, options) {
return {
name: "xdr-price",
async loadContent() {
if (!cache) {
const icpPrice = await fetch(
"https://api.coinbase.com/v2/prices/ICP-USD/buy"
)
.then((res) => res.json())
.then((res) => +res.data.amount);
const icpXdrPrice = await fetch(
"https://ic-api.internetcomputer.org/api/v3/icp-xdr-conversion-rates"
)
.then((res) => res.json())
.then((res) => res.icp_xdr_conversion_rates[0][1] / 10000);
cache = icpPrice / icpXdrPrice;
}
return cache;
},
async contentLoaded({ content, actions }) {
const { setGlobalData } = actions;
setGlobalData(content);
},
};
};
module.exports = xdrPricePlugin;