From 599d0844dff06dfd03adcc9ffcce76e9f2c0dfbe Mon Sep 17 00:00:00 2001 From: abefernan <44572727+abefernan@users.noreply.github.com> Date: Mon, 3 Jul 2023 08:16:51 +0200 Subject: [PATCH] Move types around and co-locate context types --- context/ChainsContext/types.tsx | 47 ++++++++++++ lib/displayHelpers.ts | 2 +- .../chainRegistry.ts | 72 ++++++++----------- types/index.ts | 15 ---- 4 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 context/ChainsContext/types.tsx rename components/chainSelect/chainregistry.ts => types/chainRegistry.ts (53%) diff --git a/context/ChainsContext/types.tsx b/context/ChainsContext/types.tsx new file mode 100644 index 0000000..6ceda93 --- /dev/null +++ b/context/ChainsContext/types.tsx @@ -0,0 +1,47 @@ +import { GithubChainRegistryItem, RegistryAsset } from "../../types/chainRegistry"; + +export interface ChainsContextType { + readonly state: State; + readonly dispatch: Dispatch; +} + +export interface State { + readonly chains: ChainItems; + readonly chain: ChainInfo; + readonly chainsError?: string | null; +} + +export type Dispatch = (action: Action) => void; + +export interface ChainItems { + readonly mainnets: readonly GithubChainRegistryItem[]; + readonly testnets: readonly GithubChainRegistryItem[]; +} + +export interface ChainInfo { + readonly registryName: string; + readonly chainId: string; + readonly chainDisplayName: string; + readonly nodeAddress: string; + readonly denom: string; + readonly displayDenom: string; + readonly displayDenomExponent: number; + readonly assets: readonly RegistryAsset[]; + readonly gasPrice: string; + readonly addressPrefix: string; + readonly explorerLink: string; +} + +export type Action = + | { + readonly type: "setChains"; + readonly payload: ChainItems; + } + | { + readonly type: "setChain"; + readonly payload: ChainInfo; + } + | { + readonly type: "setChainsError"; + readonly payload: string | null; + }; diff --git a/lib/displayHelpers.ts b/lib/displayHelpers.ts index 100bf61..095c274 100644 --- a/lib/displayHelpers.ts +++ b/lib/displayHelpers.ts @@ -2,7 +2,7 @@ import { Coin } from "@cosmjs/amino"; import { sha512 } from "@cosmjs/crypto"; import { fromBase64, fromBech32, toBase64, toBech32 } from "@cosmjs/encoding"; import { Decimal } from "@cosmjs/math"; -import { ChainInfo } from "../types"; +import { ChainInfo } from "../context/ChainsContext/types"; function capitalizeFirstLetter(str: string): string { return str.charAt(0).toUpperCase() + str.slice(1); diff --git a/components/chainSelect/chainregistry.ts b/types/chainRegistry.ts similarity index 53% rename from components/chainSelect/chainregistry.ts rename to types/chainRegistry.ts index a8f068d..f95acb4 100644 --- a/components/chainSelect/chainregistry.ts +++ b/types/chainRegistry.ts @@ -1,4 +1,19 @@ -import axios from "axios"; +export interface GithubChainRegistryItem { + name: string; + path: string; + sha: string; + size: number; + url: string; + html_url: string; + git_url: string; + download_url: string | null; + type: string; + _links: { + self: string; + git: string; + html: string; + }; +} export interface RegistryChainApisRpc { readonly address: string; @@ -36,57 +51,28 @@ export interface RegistryChain { readonly pretty_name: string; } -export interface RegistryChainResponse { - readonly data: RegistryChain; -} - /** * See https://github.com/cosmos/chain-registry/blob/1e9ecde770951cab90f0853a624411d79af90b83/provenance/assetlist.json#L8-L12 */ export interface RegistryAssetDenomUnit { - denom: string; - exponent: number; - aliases?: string[]; + readonly denom: string; + readonly exponent: number; + readonly aliases?: readonly string[]; } /** * See https://github.com/cosmos/chain-registry/blob/1e9ecde770951cab90f0853a624411d79af90b83/provenance/assetlist.json#L5-L28 */ export interface RegistryAsset { - description: string; - denom_units: RegistryAssetDenomUnit[]; - base: string; - name: string; - display: string; - symbol: string; - logo_URIs: { - png: string; - svg: string; + readonly denom_units: readonly RegistryAssetDenomUnit[]; + readonly base: string; + readonly display: string; + readonly name: string; + readonly symbol: string; + readonly description?: string; + readonly logo_URIs?: { + readonly png: string; + readonly svg: string; }; - coingecko_id: string; + readonly coingecko_id?: string; } - -export interface RegistryAssetsResponse { - readonly data: { readonly assets: readonly RegistryAsset[] }; -} - -const registryGhUrl = "https://cdn.jsdelivr.net/gh/cosmos/chain-registry@master/"; - -export const getChainFromRegistry = async (chainGhName: string): Promise => { - const chainGhUrl = registryGhUrl + chainGhName + "/chain.json"; - - const { data: chain }: RegistryChainResponse = await axios.get(chainGhUrl); - return chain; -}; - -export const getAssetsFromRegistry = async ( - chainGhName: string, -): Promise => { - const assetsGhUrl = registryGhUrl + chainGhName + "/assetlist.json"; - - const { - data: { assets }, - }: RegistryAssetsResponse = await axios.get(assetsGhUrl); - - return assets; -}; diff --git a/types/index.ts b/types/index.ts index b01fb4d..e7aaa2b 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1,6 +1,5 @@ import { StdFee } from "@cosmjs/amino"; import { EncodeObject } from "@cosmjs/proto-signing"; -import { RegistryAsset } from "../components/chainSelect/chainregistry"; declare global { interface Window { @@ -46,17 +45,3 @@ export interface WalletAccount { isNanoLedger?: boolean; name?: string; } - -export interface ChainInfo { - nodeAddress?: string; - denom?: string; - displayDenom?: string; - displayDenomExponent?: number; - assets?: readonly RegistryAsset[]; - gasPrice?: string; - chainId?: string; - chainDisplayName?: string; - registryName?: string; - addressPrefix?: string; - explorerLink?: string; -}