From 07216e9fc6bbc24576707d2fb46575fd47ca5ab6 Mon Sep 17 00:00:00 2001 From: JunichiSugiura Date: Mon, 23 Dec 2024 11:50:41 +0900 Subject: [PATCH] Cleanup token util --- .../components/ConfirmTransaction.stories.tsx | 2 +- .../src/components/Funding/DepositEth.tsx | 2 +- packages/keychain/src/hooks/connection.ts | 18 ++- packages/keychain/src/utils/token.tsx | 137 ------------------ 4 files changed, 16 insertions(+), 143 deletions(-) delete mode 100644 packages/keychain/src/utils/token.tsx diff --git a/packages/keychain/src/components/ConfirmTransaction.stories.tsx b/packages/keychain/src/components/ConfirmTransaction.stories.tsx index ce45aa570..de19a3f25 100644 --- a/packages/keychain/src/components/ConfirmTransaction.stories.tsx +++ b/packages/keychain/src/components/ConfirmTransaction.stories.tsx @@ -1,7 +1,7 @@ import type { Meta, StoryObj } from "@storybook/react"; import { ConfirmTransaction } from "./ConfirmTransaction"; -import { ETH_CONTRACT_ADDRESS } from "../utils/token"; +import { ETH_CONTRACT_ADDRESS } from "@cartridge/utils"; const meta = { component: ConfirmTransaction, diff --git a/packages/keychain/src/components/Funding/DepositEth.tsx b/packages/keychain/src/components/Funding/DepositEth.tsx index fd1bbb57c..7cdd68dcf 100644 --- a/packages/keychain/src/components/Funding/DepositEth.tsx +++ b/packages/keychain/src/components/Funding/DepositEth.tsx @@ -27,13 +27,13 @@ import { } from "@cartridge/ui-next"; import { useConnection } from "@/hooks/connection"; import { useToast } from "@/hooks/toast"; -import { ETH_CONTRACT_ADDRESS } from "@/utils/token"; import { ErrorAlert } from "../ErrorAlert"; import { CopyAddress } from "@/components/CopyAddress"; import { parseEther } from "viem"; import { AmountSelection, DEFAULT_AMOUNT } from "./AmountSelection"; import { Balance } from "./Balance"; import { TokenPair, usePriceQuery } from "@cartridge/utils/api/cartridge"; +import { ETH_CONTRACT_ADDRESS } from "@cartridge/utils"; type DepositEthProps = { onComplete?: (deployHash?: string) => void; diff --git a/packages/keychain/src/hooks/connection.ts b/packages/keychain/src/hooks/connection.ts index 3e82ba71f..7a5224c6e 100644 --- a/packages/keychain/src/hooks/connection.ts +++ b/packages/keychain/src/hooks/connection.ts @@ -6,14 +6,13 @@ import { ConnectionCtx, OpenSettingsCtx, } from "@/utils/connection"; -import { getChainName, isIframe } from "@cartridge/utils"; -import { RpcProvider } from "starknet"; +import { ETH_CONTRACT_ADDRESS, getChainName, isIframe } from "@cartridge/utils"; +import { getChecksumAddress, RpcProvider } from "starknet"; import { Prefund, ResponseCodes, toSessionPolicies, } from "@cartridge/controller"; -import { mergeDefaultETHPrefund } from "@/utils/token"; import { setIsSignedUp } from "@/utils/cookie"; import { ConnectionContext, @@ -122,7 +121,18 @@ export function useConnectionValue() { ? JSON.parse(decodeURIComponent(prefundParam)) : []; setHasPrefundRequest(!!prefundParam); - setPrefunds(mergeDefaultETHPrefund(prefunds)); + setPrefunds( + prefunds.find( + (p) => + getChecksumAddress(p.address) === + getChecksumAddress(ETH_CONTRACT_ADDRESS), + ) + ? prefunds + : [ + { address: ETH_CONTRACT_ADDRESS, min: "100000000000000" }, + ...prefunds, + ], + ); // Handle theme and policies const policiesParam = urlParams.get("policies"); diff --git a/packages/keychain/src/utils/token.tsx b/packages/keychain/src/utils/token.tsx deleted file mode 100644 index 9594363bb..000000000 --- a/packages/keychain/src/utils/token.tsx +++ /dev/null @@ -1,137 +0,0 @@ -import { Prefund } from "@cartridge/controller"; -import { EthereumIcon } from "@cartridge/ui-next"; -import { formatAddress } from "@cartridge/utils"; -import { Image } from "@chakra-ui/react"; -import { formatEther } from "viem"; -import { uint256 } from "starknet"; -import Controller from "./controller"; - -export const ETH_CONTRACT_ADDRESS = - "0x49d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7"; - -export const ETH_MIN_PREFUND = "100000000000000"; - -export function isEther(t: TokenInfo | Prefund) { - return ( - formatAddress(t.address, { padding: true }) === - formatAddress(ETH_CONTRACT_ADDRESS, { padding: true }) - ); -} - -export function isFunded(t: TokenInfo) { - return t.min <= (t.balance ?? 0n); -} - -export function getBalanceStr(t: TokenInfo) { - if (typeof t.balance === "undefined") return "..."; - return isEther(t) ? formatEther(t.balance) : t.balance.toString(); -} - -export function getMinStr(t: TokenInfo) { - return isEther(t) ? formatEther(t.min) : t.min.toString(); -} - -export function mergeDefaultETHPrefund(prefunds: Prefund[]) { - return prefunds.find(isEther) - ? prefunds - : [{ address: ETH_CONTRACT_ADDRESS, min: ETH_MIN_PREFUND }, ...prefunds]; -} - -export async function fetchTokenInfo(prefunds: Prefund[]) { - const res = await fetch("https://mainnet-api.ekubo.org/tokens"); - const data: TokenInfoRaw[] = await res.json(); - const tokens = prefunds.map((t) => { - if (isEther(t)) { - return { - address: ETH_CONTRACT_ADDRESS, - min: BigInt(t.min), - name: "Ether", - symbol: "ETH", - decimals: 18, - logo: , - }; - } - - const info = data.find( - ({ l2_token_address }) => - formatAddress(l2_token_address, { padding: true }) === - formatAddress(t.address, { padding: true }), - ); - - if (!info) { - throw new Error(`Cannot find token info for: ${t.address}`); - } - - return { - address: t.address, - min: BigInt(t.min), - name: info.name, - symbol: info.symbol, - decimals: info.decimals, - logo: ( - {`${info.name} - ), - }; - }); - return tokens; -} - -export async function updateBalance( - tokens: TokenInfo[], - controller: Controller, -) { - const res = await Promise.allSettled( - tokens.map(async (t) => { - try { - const balance = await controller.callContract({ - contractAddress: t.address, - entrypoint: "balanceOf", - calldata: [controller.address], - }); - - return { - ...t, - balance: uint256.uint256ToBN({ - low: balance[0], - high: balance[1], - }), - error: undefined, - }; - } catch { - return { - ...t, - error: new Error("Failed to update balance"), - }; - } - }), - ); - - return res - .filter((res) => res.status === "fulfilled") - .map((res) => (res as PromiseFulfilledResult).value); -} - -export type TokenInfoRaw = { - name: string; - symbol: string; - decimals: number; - l2_token_address: string; - sort_order: string; - total_supply: number; - logo_url: string; -}; - -export type TokenInfo = { - name: string; - symbol: string; - decimals: number; - address: string; - logo: React.ReactNode; - min: bigint; - balance?: bigint; - error?: Error; -};