Skip to content

Commit

Permalink
refactor chain info reader for improved readability and structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vuonghuuhung committed Nov 28, 2024
1 parent 0b9a9a4 commit 7b27600
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions packages/common/src/chain-infos/chain-info-reader.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { fetchRetry } from "../helpers";
import path from "path";
import {
CHAIN_REGISTRY_BACKEND_ENDPOINTS,
CHAIN_REGISTRY_GITHUB_API_ENDPOINTS,
CHAIN_REGISTRY_GITHUB_RAWCONTENT_ENDPOINTS
} from "../constants";
import { ChainInfoReader, ChainInfoReaderFromGitRawOptions, CustomChainInfo } from "./types";
import path from "path";
import { fetchRetry } from "../helpers";
import {
ChainInfoReader,
ChainInfoReaderFromGitRawOptions,
CustomChainInfo
} from "./types";

export class ChainInfoReaderFromBackend implements ChainInfoReader {
constructor(private readonly baseUrl?: string, private readonly dex?: string) {}
constructor(
private readonly baseUrl?: string,
private readonly dex?: string
) {}

async readChainInfos() {
const chains = (await (
await fetchRetry(
this.baseUrl ?? CHAIN_REGISTRY_BACKEND_ENDPOINTS.BASE_URL +
path.join(
CHAIN_REGISTRY_BACKEND_ENDPOINTS.BASE_ENDPOINT,
CHAIN_REGISTRY_BACKEND_ENDPOINTS.CHAIN_INFOS
) + "?dex=" + this.dex
)
).json()) as CustomChainInfo[];
const { BASE_ENDPOINT, BASE_URL, CHAIN_INFOS } =
CHAIN_REGISTRY_BACKEND_ENDPOINTS;
const url =
(this.baseUrl ?? BASE_URL) +
path.join(BASE_ENDPOINT, CHAIN_INFOS) +
"?dex=" +
this.dex;
const chains = (await (await fetchRetry(url)).json()) as CustomChainInfo[];
return chains;
}
}
Expand All @@ -28,7 +34,9 @@ export class ChainInfoReaderFromOraiCommon implements ChainInfoReader {
constructor(private readonly sourceUrl: string) {}

async readChainInfos() {
const chains = (await (await fetchRetry(this.sourceUrl)).json()) as CustomChainInfo[];
const chains = (await (
await fetchRetry(this.sourceUrl)
).json()) as CustomChainInfo[];
return chains;
}
}
Expand All @@ -55,12 +63,16 @@ export class ChainInfoReaderFromGit implements ChainInfoReader {
).json();

const responses = (
await Promise.allSettled(response.map((chain) => fetchRetry(chain.download_url)))
await Promise.allSettled(
response.map((chain) => fetchRetry(chain.download_url))
)
)
.filter((chain) => chain.status === "fulfilled")
.map((chain) => chain.value);

const chains: CustomChainInfo[] = await Promise.all(responses.map((data) => data.json()));
const chains: CustomChainInfo[] = await Promise.all(
responses.map((data) => data.json())
);
return chains;
}
}
Expand All @@ -77,7 +89,8 @@ export class ChainInfoReaderFromGitRaw implements ChainInfoReader {
}
) {
if (!this.options.baseUrl)
this.options.baseUrl = CHAIN_REGISTRY_GITHUB_RAWCONTENT_ENDPOINTS.BASE_URL;
this.options.baseUrl =
CHAIN_REGISTRY_GITHUB_RAWCONTENT_ENDPOINTS.BASE_URL;
this.generateUrls();
}

Expand Down

0 comments on commit 7b27600

Please sign in to comment.