Skip to content

Commit

Permalink
fix: add explicit return types
Browse files Browse the repository at this point in the history
  • Loading branch information
marslavish committed Mar 18, 2024
1 parent facf128 commit 095175a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 30 deletions.
7 changes: 5 additions & 2 deletions packages/utils/src/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export const getDenomBySymbol = (
return getAssetByKeyValue(assets, 'symbol', symbol, chainName)?.base;
};

export const getExponentFromAsset = (asset: Asset) => {
export const getExponentFromAsset = (asset: Asset): number | undefined => {
return asset.denom_units.find(({ denom }) => denom === asset.display)
?.exponent;
};
Expand Down Expand Up @@ -169,7 +169,10 @@ export const getTokenNameByDenom = (
return asset?.name;
};

export const getChainNameByDenom = (assets: AssetList[], denom: Denom) => {
export const getChainNameByDenom = (
assets: AssetList[],
denom: Denom
): string | undefined => {
const isIbcDenom = denom.startsWith('ibc/');

if (isIbcDenom) {
Expand Down
43 changes: 35 additions & 8 deletions packages/utils/src/chains.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Chain } from '@chain-registry/types';
import { customFind } from './assets';

export const getGasPriceRangesFromChain = (chain: Chain) => {
export interface GasPriceRanges {
low: number;
average: number;
high: number;
}

export const getGasPriceRangesFromChain = (chain: Chain): GasPriceRanges => {
const feeToken = chain.fees?.fee_tokens?.[0];
return {
low: feeToken?.low_gas_price ?? 0.01,
Expand All @@ -10,31 +16,52 @@ export const getGasPriceRangesFromChain = (chain: Chain) => {
};
};

export const getChainByChainName = (chains: Chain[], chainName: string) => {
export const getChainByChainName = (
chains: Chain[],
chainName: string
): Chain | undefined => {
return customFind(chains, (chain) => chain.chain_name === chainName);
};

export const getChainByChainId = (chains: Chain[], chainId: string) => {
export const getChainByChainId = (
chains: Chain[],
chainId: string
): Chain | undefined => {
return customFind(chains, (chain) => chain.chain_id === chainId);
};

export const getChainNameByChainId = (chains: Chain[], chainId: string) => {
export const getChainNameByChainId = (
chains: Chain[],
chainId: string
): string | undefined => {
return getChainByChainId(chains, chainId)?.chain_name;
};

export const getChainIdByChainName = (chains: Chain[], chainName: string) => {
export const getChainIdByChainName = (
chains: Chain[],
chainName: string
): string | undefined => {
return getChainByChainName(chains, chainName)?.chain_id;
};

export const getChainGasPriceRanges = (chains: Chain[], chainName: string) => {
export const getChainGasPriceRanges = (
chains: Chain[],
chainName: string
): GasPriceRanges | undefined => {
const chain = getChainByChainName(chains, chainName);
return chain ? getGasPriceRangesFromChain(chain) : undefined;
};

export const getChainPrettyName = (chains: Chain[], chainName: string) => {
export const getChainPrettyName = (
chains: Chain[],
chainName: string
): string | undefined => {
return getChainByChainName(chains, chainName)?.pretty_name;
};

export const getChainBech32Prefix = (chains: Chain[], chainName: string) => {
export const getChainBech32Prefix = (
chains: Chain[],
chainName: string
): string | undefined => {
return getChainByChainName(chains, chainName)?.bech32_prefix;
};
3 changes: 1 addition & 2 deletions packages/utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
"declaration": true,
"declarationDir": "./types",
"emitDeclarationOnly": true,
"isolatedModules": true,
"strict": true
"isolatedModules": true
},
"include": ["src/**/*"],
"exclude": ["node_modules"]
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/types/assets.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Asset, AssetDenomUnit, AssetList } from '@chain-registry/types';
export type Denom = AssetDenomUnit['denom'];
export type Exponent = AssetDenomUnit['exponent'];
export declare const customFind: <T>(array: T[], filterFn: (item: T) => boolean) => T | undefined;
export declare const customFind: <T>(array: T[], filterFn: (item: T) => boolean) => T;
export declare const getAssetByDenom: (assets: AssetList[], denom: Denom, chainName?: string) => Asset | undefined;
export declare const getAssetBySymbol: (assets: AssetList[], symbol: string, chainName?: string) => Asset | undefined;
export declare const getDenomByCoinGeckoId: (assets: AssetList[], coinGeckoId: string, chainName?: string) => Denom | undefined;
Expand Down
11 changes: 4 additions & 7 deletions packages/utils/types/chains.d.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Chain } from '@chain-registry/types';
export declare const getGasPriceRangesFromChain: (chain: Chain) => {
export interface GasPriceRanges {
low: number;
average: number;
high: number;
};
}
export declare const getGasPriceRangesFromChain: (chain: Chain) => GasPriceRanges;
export declare const getChainByChainName: (chains: Chain[], chainName: string) => Chain | undefined;
export declare const getChainByChainId: (chains: Chain[], chainId: string) => Chain | undefined;
export declare const getChainNameByChainId: (chains: Chain[], chainId: string) => string | undefined;
export declare const getChainIdByChainName: (chains: Chain[], chainName: string) => string | undefined;
export declare const getChainGasPriceRanges: (chains: Chain[], chainName: string) => {
low: number;
average: number;
high: number;
} | undefined;
export declare const getChainGasPriceRanges: (chains: Chain[], chainName: string) => GasPriceRanges | undefined;
export declare const getChainPrettyName: (chains: Chain[], chainName: string) => string | undefined;
export declare const getChainBech32Prefix: (chains: Chain[], chainName: string) => string | undefined;
17 changes: 7 additions & 10 deletions packages/utils/types/ibc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export declare const getTransferChannel: (info: IBCInfo) => {
};
ordering: string;
version: string;
tags?: object | undefined;
} | undefined;
tags?: object;
};
export declare const getNonTransferChannel: (info: IBCInfo) => {
chain_1: {
channel_id: string;
Expand All @@ -28,8 +28,8 @@ export declare const getNonTransferChannel: (info: IBCInfo) => {
};
ordering: string;
version: string;
tags?: object | undefined;
} | undefined;
tags?: object;
};
export declare const getWasmChannel: (info: IBCInfo) => {
chain_1: {
channel_id: string;
Expand All @@ -41,8 +41,8 @@ export declare const getWasmChannel: (info: IBCInfo) => {
};
ordering: string;
version: string;
tags?: object | undefined;
} | undefined;
tags?: object;
};
export declare const getIbcAssetPath: (ibc: IBCInfo[], chain: string, counterparty: string, assets: AssetList[], base: string) => any;
export declare const getIbcDenomByBase: (ibc: IBCInfo[], chain: string, counterparty: string, assets: AssetList[], base: string) => string;
export declare const getIbcAssets: (chainName: string, ibc: IBCInfo[], assets: AssetList[]) => {
Expand All @@ -53,7 +53,4 @@ export declare const getCw20Assets: (chainName: string, ibc: IBCInfo[], assets:
chain_name: string;
assets: any;
}[];
export declare const getAssetLists: (chainName: string, ibc: IBCInfo[], assets: AssetList[]) => {
chain_name: string;
assets: any;
};
export declare const getAssetLists: (chainName: string, ibc: IBCInfo[], assets: AssetList[]) => any[];

0 comments on commit 095175a

Please sign in to comment.