|
| 1 | +TITLE: Chain Utility Functions Types |
| 2 | +DESCRIPTION: The types for chain utility functions |
| 3 | +SOURCE: v1, v2 |
| 4 | + |
| 5 | +LANGUAGE: javascript |
| 6 | +CODE: |
| 7 | +``` |
| 8 | +/************************ |
| 9 | + * Chain utils |
| 10 | + ************************/ |
| 11 | +import { |
| 12 | + getGasPriceRangesFromChain, |
| 13 | + getChainByChainName, |
| 14 | + getChainByChainId, |
| 15 | + getChainNameByChainId, |
| 16 | + getChainIdByChainName, |
| 17 | + getChainGasPriceRanges, |
| 18 | + getChainPrettyName, |
| 19 | + getChainBech32Prefix |
| 20 | +} from '@chain-registry/utils'; |
| 21 | + |
| 22 | +// import from chain-registry or your own Chain[] |
| 23 | +import { chains } from 'chain-registry'; |
| 24 | + |
| 25 | +// Type Definitions |
| 26 | +import { Chain } from '@chain-registry/types'; |
| 27 | +export interface GasPriceRanges { |
| 28 | + low: number; |
| 29 | + average: number; |
| 30 | + high: number; |
| 31 | +} |
| 32 | +export declare const getGasPriceRangesFromChain: (chain: Chain) => GasPriceRanges; |
| 33 | +export declare const getChainByChainName: (chains: Chain[], chainName: string) => Chain | undefined; |
| 34 | +export declare const getChainByChainId: (chains: Chain[], chainId: string) => Chain | undefined; |
| 35 | +export declare const getChainNameByChainId: (chains: Chain[], chainId: string) => string | undefined; |
| 36 | +export declare const getChainIdByChainName: (chains: Chain[], chainName: string) => string | undefined; |
| 37 | +export declare const getChainGasPriceRanges: (chains: Chain[], chainName: string) => GasPriceRanges | undefined; |
| 38 | +export declare const getChainPrettyName: (chains: Chain[], chainName: string) => string | undefined; |
| 39 | +export declare const getChainBech32Prefix: (chains: Chain[], chainName: string) => string | undefined; |
| 40 | + |
| 41 | + |
| 42 | +/************************ |
| 43 | + * Asset utils |
| 44 | + ************************/ |
| 45 | +import { |
| 46 | + getAssetByDenom, |
| 47 | + getAssetBySymbol, |
| 48 | + getChainLogo, |
| 49 | + getChainNameByDenom, |
| 50 | + getChainNameByStakingDenom, |
| 51 | + getCoinGeckoIdByDenom, |
| 52 | + getDenomByCoinGeckoId, |
| 53 | + getDenomBySymbol, |
| 54 | + getExponentByDenom, |
| 55 | + getExponentBySymbol, |
| 56 | + getNativeAssetByChainName, |
| 57 | + getSymbolByDenom, |
| 58 | + getAssetLogoByDenom, |
| 59 | + getAssetNameByDenom |
| 60 | +} from '@chain-registry/utils'; |
| 61 | + |
| 62 | +// import from chain-registry or your own AssetList[] |
| 63 | +import { assets } from 'chain-registry'; |
| 64 | + |
| 65 | +// Type Definitions |
| 66 | +import { Asset, AssetList, Chain, DenomUnit } from '@chain-registry/types'; |
| 67 | +export type Denom = DenomUnit['denom']; |
| 68 | +export type Exponent = DenomUnit['exponent']; |
| 69 | +export declare const getAssetByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => Asset | undefined; |
| 70 | +export declare const getAssetBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Asset | undefined; |
| 71 | +export declare const getDenomByCoinGeckoId: (assets: AssetList[], coinGeckoId: string, chainName?: string) => Denom | undefined; |
| 72 | +export declare const getDenomsByCoinGeckoId: (assets: AssetList[], coinGeckoId: string, chainName?: string) => Denom[]; |
| 73 | +type GetCoinGeckoIdByDenomOptions = { |
| 74 | + chainName?: string; |
| 75 | + allowTestnet?: boolean; |
| 76 | + customAssetFilter?: (asset: Asset) => boolean; |
| 77 | + excludedChainNames?: string[]; |
| 78 | +}; |
| 79 | +export declare const getCoinGeckoIdByDenom: (assets: AssetList[], denom: Denom, { chainName, allowTestnet, customAssetFilter, excludedChainNames }?: GetCoinGeckoIdByDenomOptions) => string | undefined; |
| 80 | +export declare const getSymbolByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined; |
| 81 | +export declare const getDenomBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Denom | undefined; |
| 82 | +export declare const getExponentFromAsset: (asset: Asset) => number | undefined; |
| 83 | +export declare const getExponentByDenomFromAsset: (asset: Asset, denom: string) => number | undefined; |
| 84 | +export declare const getExponentByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => Exponent | undefined; |
| 85 | +export declare const getExponentBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Exponent | undefined; |
| 86 | +export declare const getNativeAssetByChainName: (assets: AssetList[], chainName: string) => Asset | undefined; |
| 87 | +export declare const getAssetLogoByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined; |
| 88 | +export declare const getChainLogo: (assets: AssetList[], chainName: string) => string | undefined; |
| 89 | +export declare const getAssetNameByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => string | undefined; |
| 90 | +export declare const getChainNameByDenom: (assets: AssetList[], denom: Denom) => string | undefined; |
| 91 | +export declare const getChainByStakingDenom: (chains: Chain[], denom: Denom) => Chain | undefined; |
| 92 | +export declare const getChainNameByStakingDenom: (chains: Chain[], denom: Denom) => string | undefined; |
| 93 | + |
| 94 | + |
| 95 | +/************************ |
| 96 | + * Calculation utils |
| 97 | + ************************/ |
| 98 | +import { |
| 99 | + mapCoinGeckoPricesToDenoms, |
| 100 | + convertBaseUnitToDollarValue, |
| 101 | + convertDollarValueToBaseUnit, |
| 102 | + convertBaseUnitToDisplayUnit, |
| 103 | + convertDisplayUnitToBaseUnit, |
| 104 | + roundDown |
| 105 | +} from '@chain-registry/utils'; |
| 106 | + |
| 107 | +// Type Definitions |
| 108 | +import { AssetList } from '@chain-registry/types'; |
| 109 | +import { Denom } from './assets'; |
| 110 | +export interface CoinGeckoUSDPrice { |
| 111 | + usd: number; |
| 112 | +} |
| 113 | +export interface DenomPriceMap { |
| 114 | + [key: Denom]: number; |
| 115 | +} |
| 116 | +export declare const mapCoinGeckoPricesToDenoms: (assets: AssetList[], prices: Record<string, CoinGeckoUSDPrice>) => DenomPriceMap; |
| 117 | +export declare const roundDown: (value: number | string) => string; |
| 118 | +export declare const convertBaseUnitToDollarValue: (assets: AssetList[], prices: DenomPriceMap, symbol: string, amount: string | number, chainName?: string) => string; |
| 119 | +export declare const convertBaseUnitToDollarValueByDenom: (assets: AssetList[], prices: DenomPriceMap, denom: string, amount: string | number, chainName?: string) => string; |
| 120 | +export declare const convertDollarValueToBaseUnit: (assets: AssetList[], prices: DenomPriceMap, symbol: string, value: string | number, chainName?: string) => string; |
| 121 | +export declare const convertBaseUnitToDisplayUnit: (assets: AssetList[], symbol: string, amount: string | number, chainName?: string) => string; |
| 122 | +export declare const convertBaseUnitToDisplayUnitByDenom: (assets: AssetList[], denom: string, amount: string | number, chainName?: string) => string; |
| 123 | +export declare const convertDisplayUnitToBaseUnit: (assets: AssetList[], symbol: string, amount: string | number, chainName?: string) => string; |
| 124 | + |
| 125 | +---------------------------------------- |
| 126 | + |
| 127 | +TITLE: Installing Chain Registry V1 Packages |
| 128 | +DESCRIPTION: Shows how to install chain registry packages for v1 with the correct version suffix. All v1 packages should be installed with "@1" postfix to ensure compatibility. |
| 129 | +SOURCE: v1 |
| 130 | + |
| 131 | +LANGUAGE: terminal |
| 132 | +CODE: |
| 133 | +``` |
| 134 | +# Install core chain registry packages for v1 (latest legacy versions) |
| 135 | +npm install chain-registry@"<2.0.0" |
| 136 | +npm install @chain-registry/client@"<2.0.0" |
| 137 | +npm install @chain-registry/utils@"<2.0.0" |
| 138 | +npm install @chain-registry/types@"<2.0.0" |
| 139 | + |
| 140 | +# Install additional v1 packages as needed |
| 141 | +npm install @chain-registry/assets@"<2.0.0" |
| 142 | +npm install @chain-registry/keplr@"<2.0.0" |
| 143 | +npm install @chain-registry/cosmostation@"<2.0.0" |
| 144 | +npm install @chain-registry/osmosis@"<2.0.0" |
| 145 | +npm install @chain-registry/juno@"<2.0.0" |
| 146 | +``` |
| 147 | + |
| 148 | +---------------------------------------- |
| 149 | + |
| 150 | +TITLE: Chain Registry V1 Data Structure (snake_case) |
| 151 | +DESCRIPTION: Example showing v1 chain registry data structure with snake_case property naming convention. All chain objects, asset lists, and other data use snake_case keys like chain_name, chain_id, bech32_prefix, etc. |
| 152 | +SOURCE: v1 |
| 153 | + |
| 154 | +LANGUAGE: typescript |
| 155 | +CODE: |
| 156 | +``` |
| 157 | +import { chains, assets } from 'chain-registry'; |
| 158 | + |
| 159 | +// V1 uses snake_case for all property names |
| 160 | +const osmosisChain = { |
| 161 | + chain_name: "osmosis", |
| 162 | + chain_id: "osmosis-1", |
| 163 | + bech32_prefix: "osmo", |
| 164 | + pretty_name: "Osmosis", |
| 165 | + network_type: "mainnet", |
| 166 | + daemon_name: "osmosisd", |
| 167 | + node_home: "$HOME/.osmosisd", |
| 168 | + key_algos: ["secp256k1"], |
| 169 | + slip44: 118, |
| 170 | + fees: { |
| 171 | + fee_tokens: [ |
| 172 | + { |
| 173 | + denom: "uosmo", |
| 174 | + low_gas_price: 0.0025, |
| 175 | + average_gas_price: 0.025, |
| 176 | + high_gas_price: 0.04 |
| 177 | + } |
| 178 | + ] |
| 179 | + } |
| 180 | +}; |
| 181 | + |
| 182 | +const osmosisAssets = { |
| 183 | + chain_name: "osmosis", |
| 184 | + assets: [ |
| 185 | + { |
| 186 | + description: "The native token of Osmosis", |
| 187 | + denom_units: [ |
| 188 | + { denom: "uosmo", exponent: 0 }, |
| 189 | + { denom: "osmo", exponent: 6 } |
| 190 | + ], |
| 191 | + base: "uosmo", |
| 192 | + name: "Osmosis", |
| 193 | + display: "osmo", |
| 194 | + symbol: "OSMO", |
| 195 | + logo_URIs: { |
| 196 | + png: "https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/images/osmo.png" |
| 197 | + }, |
| 198 | + coingecko_id: "osmosis" |
| 199 | + } |
| 200 | + ] |
| 201 | +}; |
| 202 | +``` |
| 203 | + |
| 204 | +---------------------------------------- |
| 205 | + |
| 206 | +TITLE: Chain Registry V2 Data Structure (camelCase) |
| 207 | +DESCRIPTION: Example showing v2 chain registry data structure with camelCase property naming convention. All chain objects, asset lists, and other data use camelCase keys like chainName, chainId, bech32Prefix, etc. |
| 208 | +SOURCE: v2 |
| 209 | + |
| 210 | +LANGUAGE: typescript |
| 211 | +CODE: |
| 212 | +``` |
| 213 | +import { chains, assets } from 'chain-registry'; |
| 214 | + |
| 215 | +// V2 uses camelCase for all property names |
| 216 | +const osmosisChain = { |
| 217 | + chainName: "osmosis", |
| 218 | + chainId: "osmosis-1", |
| 219 | + bech32Prefix: "osmo", |
| 220 | + prettyName: "Osmosis", |
| 221 | + networkType: "mainnet", |
| 222 | + daemonName: "osmosisd", |
| 223 | + nodeHome: "$HOME/.osmosisd", |
| 224 | + keyAlgos: ["secp256k1"], |
| 225 | + slip44: 118, |
| 226 | + fees: { |
| 227 | + feeTokens: [ |
| 228 | + { |
| 229 | + denom: "uosmo", |
| 230 | + lowGasPrice: 0.0025, |
| 231 | + averageGasPrice: 0.025, |
| 232 | + highGasPrice: 0.04 |
| 233 | + } |
| 234 | + ] |
| 235 | + } |
| 236 | +}; |
| 237 | + |
| 238 | +const osmosisAssets = { |
| 239 | + chainName: "osmosis", |
| 240 | + assets: [ |
| 241 | + { |
| 242 | + description: "The native token of Osmosis", |
| 243 | + denomUnits: [ |
| 244 | + { denom: "uosmo", exponent: 0 }, |
| 245 | + { denom: "osmo", exponent: 6 } |
| 246 | + ], |
| 247 | + base: "uosmo", |
| 248 | + name: "Osmosis", |
| 249 | + display: "osmo", |
| 250 | + symbol: "OSMO", |
| 251 | + logoURIs: { |
| 252 | + png: "https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/images/osmo.png" |
| 253 | + }, |
| 254 | + coingeckoId: "osmosis" |
| 255 | + } |
| 256 | + ] |
| 257 | +}; |
| 258 | +``` |
0 commit comments