forked from hexxagon-io/chain-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
legacy.js
202 lines (173 loc) · 6.24 KB
/
legacy.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const glob = require("glob-promise");
const path = require("path");
const { Buffer } = require("buffer");
const { Hash } = require("@keplr-wallet/crypto");
const { AccAddress } = require("@hexxagon/feather.js");
const fs = require("fs").promises;
const axelar = require("./axelar.js");
(async () => {
// create build directory
fs.mkdir("./build", { recursive: true });
// convert chains to json
const chains = {};
const chainsOutPath = "./build/chains.json";
const chainFiles = await glob("./chains/*/*.js");
const tokens = [];
chainFiles.forEach((file) => {
const [_, folder, network, fileName] = file.split("/");
if (typeof chains[network] === "undefined") {
chains[network] = {};
}
const fullPath = `./${file}`;
const chainData = require(fullPath);
if (network !== "localterra" && !isValidUrl(chainData.lcd)) {
console.log(`${chainData.chainID}: Invalid LCD URL: ${chainData.lcd}`);
return;
}
tokens.push(
...(chainData.tokens ?? []).map((t) => ({
...t,
chainID: chainData.chainID,
network,
}))
);
delete chainData["tokens"];
chains[network][chainData.chainID] = chainData;
});
// convert coins to json
const coinsOutPath = "./build/coins.json";
const coinsOut = {};
const ibcDenomMapOutPath = "./build/ibc_denoms.json";
const ibcDenomMapOut = {};
tokens.forEach((token) => {
const { network, chainID, ...coinData } = token;
if (typeof coinsOut[network] === "undefined") {
coinsOut[network] = {};
}
if (typeof ibcDenomMapOut[network] === "undefined") {
ibcDenomMapOut[network] = {};
}
coinsOut[network][coinData.token] = { ...coinData, chains: [chainID] };
// add IBC denom on Terra
// chain is disabled
if (!chains[network][chainID]) {
console.log(`${chainID} used by ${coinData.token} is disabled.`);
return;
}
const isICS = AccAddress.validate(coinData.token);
if (!isICS && chains[network][chainID].ibc) {
const channel = chains[network][chainID].ibc.fromTerra;
const ibcDenomOnTerra = calculateIBCDenom(channel, coinData.token);
const nonHashedDenom = `transfer/${channel}/${coinData.token}`;
ibcDenomMapOut[network][ibcDenomOnTerra] = {
token: coinData.token,
chainID: Object.values(chains[network]).find(
({ prefix }) => prefix === "terra"
)?.chainID,
};
// add IBC denom on other chains
Object.values(chains[network]).forEach(({ chainID: chainID2 }) => {
if (!chains[network][chainID2].ibc || chainID === chainID2) return;
const channel = chains[network][chainID2].ibc.toTerra;
const ibcDenomOnOther = calculateIBCDenom(channel, nonHashedDenom);
ibcDenomMapOut[network][ibcDenomOnOther] = {
token: coinData.token,
chainID: chainID2,
};
});
} else if (isICS && chains[network][chainID]?.ibc?.ics) {
const channel = chains[network][chainID].ibc.ics.fromTerra;
const denom = `cw20:${coinData.token}`;
const ibcDenomOnTerra = calculateIBCDenom(channel, denom);
const nonHashedDenom = `transfer/${channel}/${denom}`;
ibcDenomMapOut[network][ibcDenomOnTerra] = {
token: coinData.token,
chainID: Object.values(chains[network]).find(
({ prefix }) => prefix === "terra"
).chainID,
// to send it back on the original chain
icsChannel: channel,
};
// add IBC denom on other chains
if (!coinData.isAxelar) {
Object.values(chains[network]).forEach(({ chainID: chainID2 }) => {
if (!chains[network][chainID2].ibc || chainID === chainID2) return;
const channel = chains[network][chainID2].ibc.toTerra;
const ibcDenomOnOther = calculateIBCDenom(channel, nonHashedDenom);
ibcDenomMapOut[network][ibcDenomOnOther] = {
token: coinData.token,
chainID: chainID2,
};
});
}
} else if (chains[network][chainID].prefix === "terra") {
// add IBC denom on other chains
if (!isICS) {
Object.values(chains[network]).forEach(({ chainID: chainID2 }) => {
if (!chains[network][chainID2].ibc || chainID === chainID2) return;
const ibcDenomOnOther = calculateIBCDenom(
chains[network][chainID2].ibc.toTerra,
coinData.token
);
ibcDenomMapOut[network][ibcDenomOnOther] = {
token: coinData.token,
chainID: chainID2,
};
});
} else {
Object.values(chains[network]).forEach(({ chainID: chainID2 }) => {
if (
!chains[network][chainID2].ibc?.icsFromTerra ||
chainID === chainID2
)
return;
const denom = `cw20:${coinData.token}`;
const channel = chains[network][chainID2].ibc.icsFromTerra.toTerra;
const ibcDenomOnOther = calculateIBCDenom(channel, denom);
ibcDenomMapOut[network][ibcDenomOnOther] = {
token: coinData.token,
chainID: chainID2,
icsChannel: channel,
};
});
}
}
});
// Format the JSON with indentions before writing.
const jsonList = JSON.stringify(chains, null, 2);
await fs.writeFile(chainsOutPath, jsonList);
const coinsList = JSON.stringify(coinsOut, null, 2);
await fs.writeFile(coinsOutPath, coinsList);
const ibcList = JSON.stringify(ibcDenomMapOut, null, 2);
await fs.writeFile(ibcDenomMapOutPath, ibcList);
const currenciesList = require("./currencies.js");
await fs.writeFile(
"./build/currencies.json",
JSON.stringify(currenciesList, null, 2)
);
// copy images inside ./build
const images = await glob("./img/*/*.{png,svg}");
await Promise.all(
images.map(async (file) => {
await fs.mkdir(`./build/${path.dirname(file).replace("./", "")}`, {
recursive: true,
});
await fs.copyFile(file, `./build/${file.replace("./", "")}`);
})
);
})();
function calculateIBCDenom(channel, denom) {
return (
"ibc/" +
Buffer.from(Hash.sha256(Buffer.from(`transfer/${channel}/${denom}`)))
.toString("hex")
.toUpperCase()
);
}
function isValidUrl(url) {
try {
return new URL(url).protocol === "https:";
} catch (e) {
return false;
}
}