Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor example/asset list final #178

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions examples/asset-list/components/asset-list/AssetsOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { DropdownTransferModal } from './DropdownTransferModal';
import { RowTransferModal } from './RowTransferModal';

import { PrettyAsset, Transfer, TransferInfo } from './types';
import { getChainNameByDenom, getNativeAssetByChainName } from '@chain-registry/utils';
import { assets as assetsInRegistry } from '@/utils/local-chain-registry'

interface AssetsOverviewProps {
isLoading?: boolean;
Expand All @@ -40,7 +42,7 @@ const AssetsOverview = ({
isLoading: isLoadingTotalAssets,
refetch,
} = useTotalAssets(selectedChainName);
const { getChainName, getNativeDenom, isNativeAsset } =
const { isNativeAsset } =
useChainUtils(selectedChainName);

const modalControl = useDisclosure();
Expand All @@ -57,6 +59,8 @@ const AssetsOverview = ({
[ibcAssets]
);

const currentAssetLists = assetsInRegistry.filter(a => a.chain_name === selectedChainName)

const assetsToShow = useMemo(() => {
const returnAssets: SingleChainProps['list'] = assets.map((asset) => ({
imgSrc: asset.logoUrl ?? '',
Expand All @@ -69,8 +73,8 @@ const AssetsOverview = ({
showDeposit: !isNativeAsset(asset),
showWithdraw: !isNativeAsset(asset),
onDeposit: () => {
const sourceChainName = getChainName(asset.denom);
const sourceChainNativeDenom = getNativeDenom(sourceChainName);
const sourceChainName = getChainNameByDenom(currentAssetLists, asset.denom) || '';
const sourceChainNativeDenom = getNativeAssetByChainName(assetsInRegistry, sourceChainName)?.base || '';
flushSync(() => {
setRowTransferInfo({
sourceChainName,
Expand All @@ -88,7 +92,7 @@ const AssetsOverview = ({
rowModalControl.open();
},
onWithdraw: () => {
const destChainName = getChainName(asset.denom);
const destChainName = getChainNameByDenom(currentAssetLists, asset.denom) || '';

flushSync(() => {
setRowTransferInfo({
Expand All @@ -106,15 +110,13 @@ const AssetsOverview = ({
return returnAssets;
}, [
assets,
getChainName,
getNativeDenom,
isNativeAsset,
rowModalControl,
selectedChainName,
]);

const onWithdrawAsset = () => {
const destChainName = getChainName(ibcAssets[0].denom);
const destChainName = getChainNameByDenom(currentAssetLists, ibcAssets[0].denom) || '';
setTransferInfo({
sourceChainName: selectedChainName,
type: Transfer.Withdraw,
Expand All @@ -125,8 +127,8 @@ const AssetsOverview = ({
};

const onDepositAsset = () => {
const sourceChainName = getChainName(ibcAssets[0].denom);
const sourceChainAssetDenom = getNativeDenom(sourceChainName);
const sourceChainName = getChainNameByDenom(currentAssetLists, ibcAssets[0].denom) || ''
const sourceChainAssetDenom = getNativeAssetByChainName(assetsInRegistry, sourceChainName)?.base || '';
setTransferInfo({
sourceChainName,
type: Transfer.Deposit,
Expand Down
66 changes: 27 additions & 39 deletions examples/asset-list/components/asset-list/DropdownTransferModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { ibc } from 'osmo-query';
import { StdFee, coins } from '@cosmjs/amino';
import { ChainName } from 'cosmos-kit';
import { KeplrWalletName } from '@/config';
import { useDisclosure, useChainUtils, useTx, useBalance } from '@/hooks';
import { truncDecimals } from '@/utils';
import { useDisclosure, useTx, useBalance } from '@/hooks';

import {
PrettyAsset,
Expand All @@ -20,6 +19,8 @@ import {
Transfer,
Unpacked,
} from './types';
import { convertBaseUnitToDisplayUnitByDenom, convertDisplayUnitToBaseUnit, getChainNameByDenom, getIbcAssetPath, getNativeAssetByChainName } from '@chain-registry/utils';
import { assets as assetsInRegistry, ibc as IBCs } from '@/utils/local-chain-registry'

const { transfer } = ibc.applications.transfer.v1.MessageComposer.withTypeUrl;

Expand Down Expand Up @@ -60,17 +61,10 @@ const OverviewTransferWrapper = (
setInputValue,
} = props;

const {
convRawToDispAmount,
symbolToDenom,
getExponentByDenom,
getIbcInfo,
getChainName,
getNativeDenom,
} = useChainUtils(selectedChainName);

const { transferInfo, setTransferInfo } = transferInfoState;

const currentAssetLists = assetsInRegistry.filter(a => a.chain_name === selectedChainName)

const {
type: transferType,
token: transferToken,
Expand All @@ -96,24 +90,19 @@ const OverviewTransferWrapper = (
const { tx } = useTx(sourceChainName);

const availableAmount = useMemo((): number => {
if (!isDeposit) {
return transferToken.priceDisplayAmount ?? 0;
}

if (isLoadingBalance) {
return 0;
}

return new BigNumber(
convRawToDispAmount(transferToken.symbol, balance?.amount || ZERO_AMOUNT)
).toNumber();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isDeposit, isLoadingBalance, transferToken]);
if (isDeposit && balance) {
return new BigNumber(
convertBaseUnitToDisplayUnitByDenom(assetsInRegistry, balance.denom, balance.amount || ZERO_AMOUNT, sourceChainName)
).toNumber();
}

const dollarValue = new BigNumber(inputValue)
.multipliedBy(prices[symbolToDenom(transferToken.symbol)])
.decimalPlaces(2)
.toString();
return transferToken.available ?? 0;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isDeposit, isLoadingBalance, transferToken, balance]);

useEffect(() => {
if (!modalControl.isOpen) return;
Expand All @@ -129,17 +118,17 @@ const OverviewTransferWrapper = (
};

const handleTransferSubmit = async () => {
if (!sourceAddress || !destAddress) return;
if (!sourceAddress || !destAddress || !transferToken.denom) return;
setIsLoading(true);

const transferAmount = new BigNumber(inputValue)
.shiftedBy(getExponentByDenom(symbolToDenom(transferToken.symbol)))
.toString();
const transferAmount = convertDisplayUnitToBaseUnit(assetsInRegistry, transferToken.symbol, inputValue, selectedChainName)

const ibcPath = getIbcAssetPath(IBCs, selectedChainName, destChainName, assetsInRegistry, transferToken.denom)

const { sourcePort, sourceChannel } = getIbcInfo(
sourceChainName,
destChainName
);
const [{
channel_id: sourceChannel,
port_id: sourcePort,
}] = ibcPath

const fee: StdFee = {
amount: coins('1000', transferToken.denom ?? ''),
Expand Down Expand Up @@ -188,14 +177,12 @@ const OverviewTransferWrapper = (
return asset.symbol !== transferToken.symbol;
})
.map((asset) => ({
available: new BigNumber(asset.amount).toNumber(),
available: new BigNumber(convertBaseUnitToDisplayUnitByDenom(assetsInRegistry, asset.denom, asset.amount, selectedChainName)).toNumber(),
symbol: asset.symbol,
name: asset.prettyChainName,
denom: asset.denom,
imgSrc: asset.logoUrl ?? '',
priceDisplayAmount: new BigNumber(
truncDecimals(asset.dollarValue, 2)
).toNumber(),
priceDisplayAmount: prices[asset.denom] || 0
}));
}, [assets, isDeposit, transferToken]);

Expand All @@ -209,12 +196,12 @@ const OverviewTransferWrapper = (
if (!prev) return;

if (transferType === Transfer.Withdraw) {
const destChainName = getChainName(assetOption.denom ?? '');
const destChainName = getChainNameByDenom(currentAssetLists, assetOption.denom || '') || ''
return { ...prev, destChainName, token: assetOption };
}

const sourceChainName = getChainName(assetOption.denom ?? '');
const sourceChainAssetDenom = getNativeDenom(sourceChainName);
const sourceChainName = getChainNameByDenom(assetsInRegistry, assetOption.denom || '') || ''
const sourceChainAssetDenom = getNativeAssetByChainName(assetsInRegistry, sourceChainName)?.base || ''
return {
...prev,
sourceChainName,
Expand Down Expand Up @@ -271,6 +258,7 @@ export const DropdownTransferModal = (props: OverviewTransferWrapperProps) => {
isOpen={modalControl.isOpen}
title="Deposit"
onClose={() => closeModal()}
closeOnClickaway={false}
>
{transferInfoState ? (
<OverviewTransferWrapper
Expand Down
92 changes: 20 additions & 72 deletions examples/asset-list/components/asset-list/RowTransferModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { useChainWallet, useManager } from '@cosmos-kit/react';
import BigNumber from 'bignumber.js';
import { ChainName } from 'cosmos-kit';
import { coins, StdFee } from '@cosmjs/amino';
import { useDisclosure, useChainUtils, useBalance, useTx } from '@/hooks';
import { useDisclosure, useBalance, useTx } from '@/hooks';
import { KeplrWalletName } from '@/config';
import { ibc } from 'osmo-query';

import { PriceHash, TransferInfo, Transfer } from './types';

import { assets as assetsInRegistry, ibc as IBCs } from '@/utils/local-chain-registry'
import { convertBaseUnitToDisplayUnitByDenom, convertDisplayUnitToBaseUnit, getIbcAssetPath } from '@chain-registry/utils';
const { transfer } = ibc.applications.transfer.v1.MessageComposer.withTypeUrl;

interface IProps {
Expand Down Expand Up @@ -40,9 +41,6 @@ const TransferModalBody = (
setInputValue,
} = props;

const { getIbcInfo, symbolToDenom, getExponentByDenom, convRawToDispAmount } =
useChainUtils(selectedChainName);

const {
type: transferType,
token: transferToken,
Expand Down Expand Up @@ -73,19 +71,17 @@ const TransferModalBody = (
const { tx } = useTx(sourceChainName);

const availableAmount = useMemo(() => {
if (!isDeposit) return transferToken.priceDisplayAmount ?? 0;
if (isLoading) return 0;

return new BigNumber(
convRawToDispAmount(transferToken.symbol, balance?.amount || '0')
).toNumber();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isDeposit, isLoading, transferToken]);
if(isDeposit && balance) {
return BigNumber(
convertBaseUnitToDisplayUnitByDenom(assetsInRegistry, balance.denom, balance.amount, sourceChainName)
).toNumber()
}

const dollarValue = new BigNumber(inputValue)
.multipliedBy(prices[symbolToDenom(transferToken.symbol)])
.decimalPlaces(2)
.toNumber();
return transferToken.available || 0
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isDeposit, isLoading, transferToken, balance]);

useEffect(() => {
if (!modalControl.isOpen) return;
Expand All @@ -95,54 +91,6 @@ const TransferModalBody = (
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [modalControl.isOpen]);

const handleClick = async () => {
if (!sourceAddress || !destAddress) return;
setIsLoading(true);

const transferAmount = new BigNumber(inputValue)
.shiftedBy(getExponentByDenom(symbolToDenom(transferToken.symbol)))
.toString();

const { sourcePort, sourceChannel } = getIbcInfo(
sourceChainName,
destChainName
);

const fee: StdFee = {
amount: coins('1000', transferToken.denom ?? ''),
gas: '250000',
};

const token = {
denom: transferToken.denom ?? '',
amount: transferAmount,
};

const stamp = Date.now();
const timeoutInNanos = (stamp + 1.2e6) * 1e6;

const msg = transfer({
sourcePort,
sourceChannel,
sender: sourceAddress,
receiver: destAddress,
token,
// @ts-ignore
timeoutHeight: undefined,
timeoutTimestamp: BigInt(timeoutInNanos),
});

await tx([msg], {
fee,
onSuccess: () => {
updateData();
modalControl.close();
},
});

setIsLoading(false);
};

const sourceChain = useMemo(() => {
return {
symbol: sourceChainInfo.chain_name.toUpperCase(),
Expand All @@ -162,17 +110,17 @@ const TransferModalBody = (
}, [destChainInfo, destAddress, getChainLogo, destChainName]);

const handleSubmitTransfer = async () => {
if (!sourceAddress || !destAddress) return;
if (!sourceAddress || !destAddress || !transferToken.denom) return;
setIsLoading(true);

const transferAmount = new BigNumber(inputValue)
.shiftedBy(getExponentByDenom(symbolToDenom(transferToken.symbol)))
.toString();
const transferAmount = convertDisplayUnitToBaseUnit(assetsInRegistry, transferToken.symbol, inputValue, selectedChainName)

const ibcPath = getIbcAssetPath(IBCs, selectedChainName, destChainName, assetsInRegistry, transferToken.denom)

const { sourcePort, sourceChannel } = getIbcInfo(
sourceChainName,
destChainName
);
const [{
channel_id: sourceChannel,
port_id: sourcePort,
}] = ibcPath

const fee: StdFee = {
amount: coins('1000', transferToken.denom ?? ''),
Expand Down Expand Up @@ -226,7 +174,7 @@ const TransferModalBody = (
isNaN(Number(inputValue))
}
available={availableAmount}
priceDisplayAmount={dollarValue}
priceDisplayAmount={prices[balance?.denom || '']}
timeEstimateLabel="20 seconds"
amount={inputValue}
onChange={(value) => {
Expand Down
Loading
Loading