Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thal0x committed Oct 5, 2023
1 parent d4b2110 commit beeaf76
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 219 deletions.
3 changes: 1 addition & 2 deletions src/components/AssetInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { FC, Fragment, useMemo, useState } from "react";

import { AssetWithMetadata, useAssets } from "@/context/assets";
import { Chain } from "@/context/chains";
import { useBalancesByChain } from "@/cosmos";
import Toast from "@/elements/Toast";
import { useAccount } from "@/hooks/useAccount";
import { getFee } from "@/utils/utils";
import { getFee, useBalancesByChain } from "@/utils/utils";

import AssetSelect from "./AssetSelect";
import ChainSelect from "./ChainSelect";
Expand Down
2 changes: 1 addition & 1 deletion src/components/SwapWidget/useSwapWidget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { useNetwork, useSwitchNetwork } from "wagmi";

import { AssetWithMetadata, useAssets } from "@/context/assets";
import { Chain, useChains } from "@/context/chains";
import { useBalancesByChain } from "@/cosmos";
import { useAccount } from "@/hooks/useAccount";
import { useRoute } from "@/solve";
import { useBalancesByChain } from "@/utils/utils";

export const LAST_SOURCE_CHAIN_KEY = "IBC_DOT_FUN_LAST_SOURCE_CHAIN";

Expand Down
61 changes: 0 additions & 61 deletions src/components/TransactionDialog/TransactionDialogContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,7 @@ const TransactionDialogContent: FC<Props> = ({
}
}, [warningOpen]);

// const chainRecord = getChainByID(route.sourceAssetChainID);

// const chain = useChain(chainRecord?.chain_name);

const { getWalletRepo } = useManager();
// const walletClient = chain.chainWallet?.client;

const [txStatuses, setTxStatuses] = useState<RouteTransaction[]>(() =>
Array.from({ length: transactionCount }, () => {
Expand Down Expand Up @@ -238,31 +233,6 @@ const TransactionDialogContent: FC<Props> = ({
...txStatuses.slice(1),
]);

// await executeRoute(
// skipRouter,
// route,
// userAddresses,
// addressList,
// ({ txHash, explorerLink }, i) => {
// setTxStatuses((statuses) => {
// const newStatuses = [...statuses];
// newStatuses[i] = {
// status: "SUCCESS",
// explorerLink,
// txHash,
// };
// if (i < statuses.length - 1) {
// newStatuses[i + 1] = {
// status: "PENDING",
// explorerLink: null,
// txHash: null,
// };
// }
// return newStatuses;
// });
// },
// );

await skipRouter.executeRoute({
route,
userAddresses,
Expand Down Expand Up @@ -313,37 +283,6 @@ const TransactionDialogContent: FC<Props> = ({
});
},
});

// await executeRoute(
// skipRouter,
// walletClient,
// route,

// // (error: any) => {
// // console.error(error);
// // setTxError(error.message);
// // setIsError(true);
// // setTxStatuses((statuses) => {
// // const newStatuses = [...statuses];
// // return newStatuses.map((status) => {
// // if (status.status === "PENDING") {
// // return {
// // status: "INIT",
// // explorerLink: null,
// // txHash: null,
// // };
// // }
// // return status;
// // });
// // });
// // }
// );
// toast(
// "Transaction Successful",
// "Your transaction was successful",
// "success",
// );
// setTxComplete(true);
} catch (err: unknown) {
console.error(err);
if (err instanceof Error) {
Expand Down
110 changes: 0 additions & 110 deletions src/cosmos/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
import { SkipRouter } from "@skip-router/core";
import { useQuery } from "@tanstack/react-query";
import { erc20ABI, PublicClient, usePublicClient } from "wagmi";

import { multicall3ABI } from "@/constants/abis";
import { Chain } from "@/context/chains";
import { useSkipClient } from "@/solve";
import { getStargateClientForChainID } from "@/utils/utils";

export function chainNameToChainlistURL(chainName: string) {
const idToNameMap: Record<string, string> = {
kichain: "ki-chain",
Expand All @@ -24,104 +15,3 @@ export function chainNameToChainlistURL(chainName: string) {

return `https://raw.githubusercontent.com/cosmostation/chainlist/main/chain/${name}`;
}

export async function getBalancesByChain(address: string, chainID: string) {
const client = await getStargateClientForChainID(chainID);

const balances = await client.getAllBalances(address);

return balances.reduce(
(acc, balance) => {
return {
...acc,
[balance.denom]: balance.amount,
};
},
{} as Record<string, string>,
);
}

export function useBalancesByChain(
address?: string,
chain?: Chain,
enabled: boolean = true,
) {
const publicClient = usePublicClient({
chainId: chain?.chainType === "evm" ? parseInt(chain.chainID) : undefined,
});

const skipRouter = useSkipClient();

return useQuery({
queryKey: ["balances-by-chain", address, chain?.chainID],
queryFn: async () => {
if (!chain || !address) {
return {};
}

if (chain.chainType === "evm") {
return getEvmChainBalances(
skipRouter,
publicClient,
address,
chain.chainID,
);
}

return getBalancesByChain(address, chain.chainID);
},
refetchInterval: false,
refetchOnMount: false,
refetchOnWindowFocus: false,
refetchOnReconnect: false,
keepPreviousData: true,
enabled: !!chain && !!address && enabled,
});
}

async function getEvmChainBalances(
skipClient: SkipRouter,
publicClient: PublicClient,
address: string,
chainID: string,
) {
const assets = await skipClient.assets({
chainID,
includeEvmAssets: true,
});

const chainAssets = assets[chainID];

const balances = await publicClient.multicall({
multicallAddress: "0xcA11bde05977b3631167028862bE2a173976CA11",
contracts: chainAssets.map((asset) => {
if (!asset.tokenContract) {
return {
address: "0xcA11bde05977b3631167028862bE2a173976CA11",
abi: multicall3ABI,
functionName: "getEthBalance",
args: [address as `0x${string}`],
};
}

return {
address: asset.tokenContract as `0x${string}`,
abi: erc20ABI,
functionName: "balanceOf",
args: [address as `0x${string}`],
};
}),
});

console.log(balances);

return chainAssets.reduce(
(acc, asset, index) => {
return {
...acc,
[asset.denom]: balances[index].result?.toString() || "0",
};
},
{} as Record<string, string>,
);
}
2 changes: 1 addition & 1 deletion src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { useManager } from "@cosmos-kit/react";

import { SwapWidget } from "@/components/SwapWidget";
import { WalletModalProvider } from "@/components/WalletModal";
import { getBalancesByChain } from "@/cosmos";
import { useInterval } from "@/utils/hooks";
import { queryClient } from "@/utils/query";
import { getBalancesByChain } from "@/utils/utils";

export default function Home() {
const { walletRepos } = useManager();
Expand Down
44 changes: 0 additions & 44 deletions src/solve/execute-route.ts

This file was deleted.

Loading

0 comments on commit beeaf76

Please sign in to comment.