-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
165 lines (136 loc) · 4.75 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
const axios = require("axios");
const express = require("express");
const { Decimal } = require("@cosmjs/math");
const { QueryClient, setupAuthExtension } = require("@cosmjs/stargate");
const { Tendermint34Client } = require("@cosmjs/tendermint-rpc");
const {
ContinuousVestingAccount,
DelayedVestingAccount,
PeriodicVestingAccount,
} = require("cosmjs-types/cosmos/vesting/v1beta1/vesting");
require("dotenv").config();
const denom = process.env.DENOM || "ujuno";
const interval = process.env.INTERVAL || 7200000;
const vestingAccounts = process.env.VESTING_ACCOUNTS
? process.env.VESTING_ACCOUNTS.split(",")
: [];
const app = express();
const port = process.env.PORT || 3000;
async function makeClientWithAuth(rpcUrl) {
const tmClient = await Tendermint34Client.connect(rpcUrl);
return [QueryClient.withExtensions(tmClient, setupAuthExtension), tmClient];
}
// Declare variables
let totalSupply,
communityPool,
communityPoolMainDenomTotal,
circulatingSupply,
tmpCirculatingSupply,
apr,
bondedRatio,
totalStaked;
// Gets supply info from chain
async function updateData() {
try {
// Create Tendermint RPC Client
const [client, tmClient] = await makeClientWithAuth(
process.env.RPC_ENDPOINT
);
console.log("Updating supply info", new Date());
// Get total supply
totalSupply = await axios({
method: "get",
url: `${process.env.REST_API_ENDPOINT}/cosmos/bank/v1beta1/supply/${denom}`,
});
console.log("Total supply: ", totalSupply.data.amount.amount);
// Get community pool
communityPool = await axios({
method: "get",
url: `${process.env.REST_API_ENDPOINT}/cosmos/distribution/v1beta1/community_pool`,
});
// Get staking info
stakingInfo = await axios({
method: "get",
url: `${process.env.REST_API_ENDPOINT}/cosmos/staking/v1beta1/pool`,
});
totalStaked = stakingInfo.data.pool.bonded_tokens;
bondedRatio = totalStaked / totalSupply.data.amount.amount;
apr = 35 / bondedRatio;
console.log("APR: ", apr);
console.log("Total Staked: ", totalStaked);
console.log("Bonded ratio: ", bondedRatio);
// Loop through pool balances to find denom
for (let i in communityPool.data.pool) {
if (communityPool.data.pool[i].denom === denom) {
console.log("Community pool: ", communityPool.data.pool[i].amount);
communityPoolMainDenomTotal = communityPool.data.pool[i].amount;
// Subtract community pool from total supply
tmpCirculatingSupply =
totalSupply.data.amount.amount - communityPool.data.pool[i].amount;
}
}
// Iterate through vesting accounts and subtract vesting balance from total
for (let i = 0; i < vestingAccounts.length; i++) {
const account = await client.auth.account(vestingAccounts[i]);
let accountInfo = PeriodicVestingAccount.decode(account.value);
let originalVesting =
accountInfo.baseVestingAccount.originalVesting[0].amount;
let delegatedFree =
accountInfo.baseVestingAccount.delegatedFree.length > 0
? accountInfo.baseVestingAccount.delegatedFree[0].amount
: 0;
tmpCirculatingSupply -= originalVesting - delegatedFree;
}
circulatingSupply = tmpCirculatingSupply;
console.log("Circulating supply: ", circulatingSupply);
} catch (e) {
console.error(e);
}
}
// Get initial data
updateData();
// Update data on an interval (2 hours)
setInterval(updateData, interval);
app.get("/", async (req, res) => {
res.json({
apr,
bondedRatio,
circulatingSupply: Decimal.fromAtomics(circulatingSupply, 6).toString(),
communityPool: Decimal.fromAtomics(
communityPoolMainDenomTotal.split(".")[0],
6
).toString(),
denom: denom.substring(1).toUpperCase(),
totalStaked: Decimal.fromAtomics(totalStaked, 6).toString(),
totalSupply: Decimal.fromAtomics(
totalSupply.data.amount.amount,
6
).toString(),
});
});
app.get("/apr", async (req, res) => {
res.send(apr.toString());
});
app.get("/bonded-ratio", async (req, res) => {
res.send(bondedRatio.toString());
});
app.get("/circulating-supply", async (req, res) => {
res.send(Decimal.fromAtomics(circulatingSupply, 6).toString());
});
app.get("/total-staked", async (req, res) => {
res.send(Decimal.fromAtomics(totalStaked, 6).toString());
});
app.get("/total-supply", async (req, res) => {
res.send(Decimal.fromAtomics(totalSupply.data.amount.amount, 6).toString());
});
app.get("/community-pool", async (req, res) => {
res.send(
Decimal.fromAtomics(communityPoolMainDenomTotal.split(".")[0], 6).toString()
);
});
app.get("/denom", async (req, res) => {
res.send(denom.substring(1).toUpperCase());
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});