Move types around and co-locate context types

This commit is contained in:
abefernan 2023-07-03 08:16:51 +02:00
parent 0343295ab3
commit 599d0844df
4 changed files with 77 additions and 59 deletions

View File

@ -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;
};

View File

@ -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);

View File

@ -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<RegistryChain> => {
const chainGhUrl = registryGhUrl + chainGhName + "/chain.json";
const { data: chain }: RegistryChainResponse = await axios.get(chainGhUrl);
return chain;
};
export const getAssetsFromRegistry = async (
chainGhName: string,
): Promise<readonly RegistryAsset[]> => {
const assetsGhUrl = registryGhUrl + chainGhName + "/assetlist.json";
const {
data: { assets },
}: RegistryAssetsResponse = await axios.get(assetsGhUrl);
return assets;
};

View File

@ -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;
}