Sub-Adapters 8
Preview and test each sub adapter.
Ethereum (eth)
Bitcoin (btc)
Polkadot (dot)
Litecoin (ltc)
Bitcoin Cash (bch)
Dogecoin (doge)
Monero (xmr)
Cardano (cardano)
Adapter Code
Check the entire code written for the Adapter.
Source code
Showing TS source.
1export const name = 'CoinMetrics - Issuance';
2export const version = '0.2.6';
3export const license = 'MIT';
4export const description = 'Calculate issuance rate and total USD issuance, using CoinMetrics APIs';
5export const changeLog = 'Upgrade to CoinMetrics API v4';
6
7interface Asset {
8 id: string;
9 name: string;
10 icon: string;
11 useSupply?: boolean;
12 issuanceDescription?: string;
13 website?: string;
14}
15
16const assets: Asset[] = [
17 {
18 id: 'eth',
19 name: 'Ethereum',
20 icon: 'QmedJLPy6R7x3dDEy2cfMd8gXbZm9e3vxvgBLXp3YZEHCy',
21 issuanceDescription: 'ETH is issued to miners and validators.',
22 website: 'https://ethereum.org',
23 },
24 {
25 id: 'btc',
26 name: 'Bitcoin',
27 icon: 'QmerwCGnYE4DE9oe4a6hppgKRz2vvMdx1AuwvgFLgrdeh7',
28 issuanceDescription: 'BTC is issued to miners.',
29 useSupply: true,
30 },
31 {
32 id: 'dot',
33 name: 'Polkadot',
34 icon: 'Qmc2A56wLVhqcxeKC3ueje88d9YhEBWgiNXAz32to8yu5g',
35 useSupply: true,
36 issuanceDescription: 'DOT is issued to stakers and the Polkadot treasury',
37 website: 'https://polkadot.network',
38 },
39 {
40 id: 'ltc',
41 name: 'Litecoin',
42 icon: 'QmQfWmmsfGDsVfgkjMwneB9MRgg8SEumgM7ZaGSP98ZsgW',
43 useSupply: true,
44 issuanceDescription: 'LTC is issued to miners.',
45 website: 'https://litecoin.org',
46 },
47 {
48 id: 'bch',
49 name: 'Bitcoin Cash',
50 icon: 'QmT1RaaEdJiJnyGMWLpHxamk3mhjrqoodAkjpD2wCxHeyS',
51 useSupply: true,
52 issuanceDescription: 'BCH is issued to miners.',
53 },
54 {
55 id: 'doge',
56 name: 'Dogecoin',
57 icon: 'QmRpogZjZEKLX33245zT6QSiDENMb26fS922Ds6pp3VzE3',
58 useSupply: true,
59 issuanceDescription: 'DOGE is issued to miners.',
60 },
61 {
62 id: 'xmr',
63 name: 'Monero',
64 icon: 'QmWVPtKiQYo2up8ewEzGWLWRZHRNcQLn5U38NFBGm7X6cd',
65 useSupply: true,
66 issuanceDescription: 'XMR is issued to miners.',
67 },
68 // Missing data
69 // {
70 // id: 'xtz',
71 // name: 'Tezos',
72 // icon: 'QmS1MvZJqD3CAxnos5t64HXtMsTh2SqXoWWndkL6dHVduW',
73 // },
74]
75
76function daysAgo(days: number) {
77 const startDate = new Date(Date.now() - (86400 * 1000 * days))
78 const month = (startDate.getMonth() + 1).toString().padStart(2, '0')
79 const day = startDate.getDate().toString().padStart(2, '0')
80 const startDateString = `${startDate.getFullYear()}-${month}-${day}`
81 return startDateString
82}
83
84export function setup(sdk: Context) {
85 const getCoinMetricsIssuanceData = (id: string) => async () => {
86 const startDateString = daysAgo(7)
87 const { data } = await sdk.http.get(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=IssTotUSD&assets=${id}&start_time=${startDateString}`);
88
89 const sevenDayMA = data.reduce(
90 (total: number, value: any) => total + parseFloat(value.IssTotUSD),
91 0
92 ) / data.length;
93
94 return sevenDayMA;
95 };
96
97 const getCoinMetricsInflationRate = (id: string) => async () => {
98 const startDateString = daysAgo(7)
99 const { data } = await sdk.http.get(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=IssContPctAnn&assets=${id}&start_time=${startDateString}`);
100
101 const sevenDayMA =
102 data.reduce(
103 (total: number, value: any) => total + parseFloat(value.IssContPctAnn),
104 0
105 ) / data.length;
106
107 return sevenDayMA / 100;
108 }
109
110 const getCoinMetricsInflationRateFromSupply = (id: string) => async () => {
111 const startDateString = daysAgo(8) // 7 days, inclusive
112 const { data } = await sdk.http.get(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=SplyCur&assets=${id}&start_time=${startDateString}`);
113
114 const rate = ((data[data.length - 1].SplyCur / data[0].SplyCur) - 1) * (365 / (data.length - 1));
115
116 return rate;
117 }
118
119 for (const { id, name, icon, useSupply, issuanceDescription, website } of assets) {
120 sdk.register({
121 id,
122 queries: {
123 issuance7DayAvgUSD: getCoinMetricsIssuanceData(id),
124 issuanceRateCurrent: useSupply ? getCoinMetricsInflationRateFromSupply(id) : getCoinMetricsInflationRate(id),
125 },
126 metadata: {
127 icon: icon && sdk.ipfs.getDataURILoader(icon, 'image/svg+xml'),
128 category: 'l1',
129 name,
130 issuanceDescription,
131 website: website || null,
132 },
133 });
134 }
135
136 const getCardanoIssuanceData = async () => {
137 const startDateString = daysAgo(8);
138 const { data } = await sdk.http.get(`https://community-api.coinmetrics.io/v4/timeseries/asset-metrics?page_size=10000&metrics=SplyCur,PriceUSD&assets=ada&start_time=${startDateString}`);
139
140 let issuedADAInUSD = 0;
141 for (let i = 1; i < data.length; i += 1) {
142 issuedADAInUSD += (data[i].SplyCur - data[i - 1].SplyCur) * data[i].PriceUSD;
143 }
144 issuedADAInUSD /= data.length - 1;
145 return issuedADAInUSD
146 };
147
148 sdk.register({
149 id: 'cardano',
150 queries: {
151 issuance7DayAvgUSD: getCardanoIssuanceData,
152 issuanceRateCurrent: getCoinMetricsInflationRateFromSupply('ada'),
153 },
154 metadata: {
155 icon: sdk.ipfs.getDataURILoader('QmbyTYm2BzcwgbRXoXXjihYT2mrwVPejFgKaJJXy6SHBRT', 'image/svg+xml'),
156 category: 'l1',
157 name: 'Cardano',
158 issuanceDescription: 'ADA is issued to validators, delegators and Cardano treasury.',
159 website: 'https://cardano.org',
160 },
161 });
162}
163
It's something off?
Report it to the discussion board on Discord, we will take care of it.