Skip to content

Commit 66bbfab

Browse files
authored
Merge pull request #2 from leapwallet/chore/fetch-from-cosmos-chain-registry
chore: fetch from cosmos chain registry
2 parents b948561 + f48757d commit 66bbfab

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

src/index.js

+44-2
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,52 @@ async function fetchFromPingPub(){
7070
}
7171
}
7272

73+
async function fetchFromCosmosChainRegistry() {
74+
const res = await fetch(
75+
'https://api.github.com/repos/cosmos/chain-registry/git/trees/master?recursive=1'
76+
);
77+
const data = await res.json();
78+
const COSMOS_CHAIN_REGISTRY_BASE_URL = data.tree.filter(
79+
(folder) => folder.path === '_IBC'
80+
)[0]?.url;
81+
82+
if (!COSMOS_CHAIN_REGISTRY_BASE_URL) {
83+
return fetchFromPingPub();
84+
}
85+
86+
const response = await fetch(COSMOS_CHAIN_REGISTRY_BASE_URL);
87+
const ibcData = await response.json();
88+
const hrefs = ibcData.tree
89+
.filter((file) => file.path)
90+
.map((file) => file.path);
91+
92+
const ibcSupport = {};
93+
94+
hrefs.forEach((href) => {
95+
const [src, dest] = href.slice(0, -5).split('-');
96+
if (ibcSupport[src]) {
97+
ibcSupport[src][dest] = true;
98+
} else {
99+
ibcSupport[src] = { [dest]: true };
100+
}
101+
if (ibcSupport[dest]) {
102+
ibcSupport[dest][src] = true;
103+
} else {
104+
ibcSupport[dest] = { [src]: true };
105+
}
106+
});
107+
108+
return {
109+
baseUrl:
110+
'https://raw.githubusercontent.com/cosmos/chain-registry/master/_IBC',
111+
hrefs,
112+
ibcSupport,
113+
};
114+
}
73115

74116
const main = async () => {
75117
try {
76-
const { baseUrl, ibcSupport, hrefs } = await fetchFromPingPub()
118+
const { baseUrl, ibcSupport, hrefs } = await fetchFromCosmosChainRegistry()
77119
const allData = Object.entries(ibcSupport).reduce((acc, [key, value]) => {
78120
return {
79121
...acc,
@@ -108,4 +150,4 @@ const main = async () => {
108150
}
109151
};
110152

111-
main();
153+
main();

0 commit comments

Comments
 (0)