Skip to content

Commit

Permalink
remove no used function
Browse files Browse the repository at this point in the history
  • Loading branch information
cmaaawscodepipelinedeploy committed May 5, 2024
1 parent 108a156 commit 1d856e6
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 599 deletions.
130 changes: 2 additions & 128 deletions examples/asset-list/hooks/useChainUtils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { useManager } from '@cosmos-kit/react';
import { useMemo } from 'react';
import { Asset, AssetList } from '@chain-registry/types';
import { asset_lists as ibcAssetLists } from '@chain-registry/assets';
import { assets as chainAssets, ibc } from 'chain-registry';
import { CoinDenom, CoinSymbol, Exponent, PriceHash } from '../utils/types';
import BigNumber from 'bignumber.js';
import { Coin } from '@cosmjs/amino';
import { assets as chainAssets } from 'chain-registry';
import { PrettyAsset } from '../components';
import { ChainName } from 'cosmos-kit';

export const useChainUtils = (chainName: string) => {
const { getChainRecord } = useManager();

const filterAssets = (assetList: AssetList[]): Asset[] => {
return (
Expand All @@ -20,7 +14,7 @@ export const useChainUtils = (chainName: string) => {
);
};

const { nativeAssets, ibcAssets } = useMemo(() => {
const { nativeAssets } = useMemo(() => {
// @ts-ignore
const nativeAssets = filterAssets(chainAssets);
// @ts-ignore
Expand All @@ -30,131 +24,11 @@ export const useChainUtils = (chainName: string) => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [chainName]);

const allAssets = [...nativeAssets, ...ibcAssets];

const getIbcAssetsLength = () => {
return ibcAssets.length;
};

const getAssetByDenom = (denom: CoinDenom): Asset => {
return allAssets.find((asset) => asset.base === denom) as Asset;
};

const denomToSymbol = (denom: CoinDenom): CoinSymbol => {
const asset = getAssetByDenom(denom);
const symbol = asset?.symbol;
if (!symbol) {
return denom;
}
return symbol;
};

const symbolToDenom = (symbol: CoinSymbol): CoinDenom => {
const asset = allAssets.find((asset) => asset.symbol === symbol);
const base = asset?.base;
if (!base) {
return symbol;
}
return base;
};

const getExponentByDenom = (denom: CoinDenom): Exponent => {
const asset = getAssetByDenom(denom);
const unit = asset.denom_units.find(({ denom }) => denom === asset.display);
return unit?.exponent || 0;
};

const convRawToDispAmount = (symbol: string, amount: string | number) => {
const denom = symbolToDenom(symbol);
return new BigNumber(amount)
.shiftedBy(-getExponentByDenom(denom))
.toString();
};

const calcCoinDollarValue = (prices: PriceHash, coin: Coin) => {
const { denom, amount } = coin;
return new BigNumber(amount)
.shiftedBy(-getExponentByDenom(denom))
.multipliedBy(prices[denom])
.toString();
};

const getChainName = (ibcDenom: CoinDenom) => {
if (nativeAssets.find((asset) => asset.base === ibcDenom)) {
return chainName;
}
const asset = ibcAssets.find((asset) => asset.base === ibcDenom);
const ibcChainName = asset?.traces?.[0].counterparty.chain_name;
if (!ibcChainName) throw Error('chainName not found: ' + ibcDenom);
return ibcChainName;
};

const getPrettyChainName = (ibcDenom: CoinDenom) => {
const chainName = getChainName(ibcDenom);
try {
const chainRecord = getChainRecord(chainName);
// @ts-ignore
return chainRecord.chain.pretty_name;
} catch (e) {
return 'CHAIN_INFO_NOT_FOUND'
}
};

const isNativeAsset = ({ denom }: PrettyAsset) => {
return !!nativeAssets.find((asset) => asset.base === denom);
};

const getNativeDenom = (chainName: ChainName) => {
const chainRecord = getChainRecord(chainName);
const denom = chainRecord.assetList?.assets[0].base;
if (!denom) throw Error('denom not found');
return denom;
};

const getIbcInfo = (fromChainName: string, toChainName: string) => {
let flipped = false;

let ibcInfo = ibc.find(
(i) =>
i.chain_1.chain_name === fromChainName &&
i.chain_2.chain_name === toChainName
);

if (!ibcInfo) {
ibcInfo = ibc.find(
(i) =>
i.chain_1.chain_name === toChainName &&
i.chain_2.chain_name === fromChainName
);
flipped = true;
}

if (!ibcInfo) {
throw new Error('cannot find IBC info');
}

const key = flipped ? 'chain_2' : 'chain_1';
const sourcePort = ibcInfo.channels[0][key].port_id;
const sourceChannel = ibcInfo.channels[0][key].channel_id;

return { sourcePort, sourceChannel };
};

return {
allAssets,
nativeAssets,
ibcAssets,
getAssetByDenom,
denomToSymbol,
symbolToDenom,
convRawToDispAmount,
calcCoinDollarValue,
getIbcAssetsLength,
getChainName,
getPrettyChainName,
isNativeAsset,
getNativeDenom,
getIbcInfo,
getExponentByDenom,
};
};
8 changes: 0 additions & 8 deletions examples/asset-list/utils/assets.ts

This file was deleted.

96 changes: 0 additions & 96 deletions examples/asset-list/utils/base.ts

This file was deleted.

3 changes: 0 additions & 3 deletions examples/asset-list/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
export * from './pool';
export * from './base';
export * from './assets';
export * from './format';
Loading

0 comments on commit 1d856e6

Please sign in to comment.