Skip to content

Commit f69c26d

Browse files
committed
https and h2o supply
1 parent 2d95d18 commit f69c26d

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

‎app/routes/coingecko/v1/totalsupply.mjs‎

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
11
const RPC_URL = "https://hydration-rpc.n.dwellir.com";
22

3-
// Token contract addresses mapping
3+
// Token contract addresses mapping (ERC20 tokens)
44
const TOKEN_CONTRACTS = {
55
hollar: "0x531a654d1696ED52e7275A8cede955E82620f99a",
66
gigadot: "0x34d5ffb83d14d82f87aaf2f13be895a3c814c2ad",
77
gigaeth: "0x8a598fe3e3a471ce865332e330d303502a0e2f52",
8-
// Add more tokens here as needed
98
};
109

1110
// Token decimals mapping
1211
const TOKEN_DECIMALS = {
1312
hollar: 18,
1413
gigadot: 18,
1514
gigaeth: 18,
16-
// Add more tokens here as needed
15+
h2o: 12,
16+
};
17+
18+
// Substrate tokens: pre-encoded storage keys for tokens.totalIssuance
19+
const SUBSTRATE_STORAGE_KEYS = {
20+
h2o: "0x99971b5749ac43e0235e41b0d378691857c875e4cff74148e4628f264b974c805153cb1f00942ff401000000",
1721
};
1822

1923
// ERC20 totalSupply function signature
2024
const TOTAL_SUPPLY_SIGNATURE = "0x18160ddd";
2125

26+
async function callSubstrateTotalIssuance(storageKey) {
27+
const response = await fetch(RPC_URL, {
28+
method: "POST",
29+
headers: { "Content-Type": "application/json" },
30+
body: JSON.stringify({
31+
jsonrpc: "2.0",
32+
method: "state_getStorage",
33+
params: [storageKey],
34+
id: 1,
35+
}),
36+
});
37+
38+
const data = await response.json();
39+
40+
if (data.error) {
41+
throw new Error(data.error.message || "RPC call failed");
42+
}
43+
44+
// Decode little-endian hex u128
45+
const hex = data.result.slice(2); // strip 0x
46+
const bytes = hex.match(/.{2}/g).reverse().join("");
47+
return BigInt("0x" + bytes);
48+
}
49+
2250
async function callTotalSupply(contractAddress) {
2351
const response = await fetch(RPC_URL, {
2452
method: "POST",
@@ -81,22 +109,22 @@ export default async (fastify, opts) => {
81109
handler: async (request, reply) => {
82110
const token = request.params.token.toLowerCase();
83111

84-
// Check if token exists in our mapping
85-
const contractAddress = TOKEN_CONTRACTS[token];
86-
if (!contractAddress) {
112+
const decimals = TOKEN_DECIMALS[token];
113+
if (!decimals) {
87114
return reply.code(404).send({
88115
error: `Token '${token}' not found`,
89116
});
90117
}
91118

92-
const decimals = TOKEN_DECIMALS[token];
93-
94119
try {
95-
// Call the contract via JSON-RPC
96-
const hexValue = await callTotalSupply(contractAddress);
97-
98-
// Convert hex to BigInt
99-
const rawSupply = BigInt(hexValue);
120+
let rawSupply;
121+
const storageKey = SUBSTRATE_STORAGE_KEYS[token];
122+
if (storageKey) {
123+
rawSupply = await callSubstrateTotalIssuance(storageKey);
124+
} else {
125+
const contractAddress = TOKEN_CONTRACTS[token];
126+
rawSupply = BigInt(await callTotalSupply(contractAddress));
127+
}
100128

101129
// Convert to human-readable format by dividing by 10^decimals
102130
const divisor = BigInt(10 ** decimals);

‎docker-stack.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,11 @@ services:
176176
- "traefik.http.routers.grafana.rule=Host(`grafana.nice.hydration.cloud`)"
177177
- "traefik.http.routers.grafana.entrypoints=websecure"
178178
- "traefik.http.routers.grafana.tls.certresolver=myresolver"
179+
- "traefik.http.routers.grafana.tls.domains[0].main=grafana.nice.hydration.cloud"
179180
- "traefik.http.services.grafana.loadbalancer.server.port=3000"
181+
- "traefik.http.routers.grafana-http.rule=Host(`grafana.nice.hydration.cloud`)"
182+
- "traefik.http.routers.grafana-http.entrypoints=web"
183+
- "traefik.http.routers.grafana-http.middlewares=https-redirect"
180184

181185
networks:
182186
hydration-net:

0 commit comments

Comments
 (0)