-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.js
52 lines (50 loc) · 1.93 KB
/
helpers.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// function getTokenUrl (token) {
// return `${token.rest}/cosmwasm/wasm/v1/contract/${token.contract}/smart/eyJ0b2tlbl9pbmZvIjoge319`
// }
// async function getTokenData (endpoint) {
// try {
// let data = await fetch(getTokenUrl(endpoint.token));
// const json = await data.json();
// return json.data;
// } catch (err) {
// throw Error(`could not fetch data: ${err}`)
// }
// }
module.exports = {
calculatePrice: (rate, price) => (rate * price),
calculateMarketCap: (price, supply) => (price * (supply / 1_000_000)),
getGravediggerUrl: (gravedigger) => (
`${gravedigger.rest}/cosmwasm/wasm/v1/contract/${gravedigger.contract}/smart/eyJzdGF0ZSI6e319`
),
getBondedAssetUrl: (id) => (
`https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`
),
getBondedAssetData: async function getBondedAssetData (endpoint) {
try {
let data = await fetch(this.getBondedAssetUrl(endpoint.assetID));
const json = await data.json();
return json[endpoint.assetID].usd;
} catch (err) {
throw Error(`could not fetch data: ${err}`);
}
},
getGravediggerData: async function getGravediggerData (endpoint) {
let out;
let data = await fetch(this.getGravediggerUrl(endpoint.gravedigger));
try {
const json = await data.json();
out = json.data;
} catch (err) {
throw err;
}
return out;
},
processData: function processData (results, token) {
const out = Object.assign({}, token);
out.price = this.calculatePrice(results.gravedigger.exchange_rate, results.bondedAssetPrice);
out.total_supply = results.gravedigger.total_usteak;
out.circulating_supply = results.gravedigger.total_usteak;
out.marketcap = this.calculateMarketCap(out.price, out.total_supply);
return out;
}
}