Skip to content

Commit 97dd86d

Browse files
Move token utils and apply tokens context to profile
1 parent a8a0cb6 commit 97dd86d

File tree

21 files changed

+337
-697
lines changed

21 files changed

+337
-697
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
run_install: |
7070
- args: [--frozen-lockfile]
7171
- run: pnpm build
72-
- run: pnpm test:ci --coverage
72+
- run: pnpm test:ci -- --coverage
7373
- uses: codecov/codecov-action@v3
7474
with:
7575
token: ${{ secrets.CODECOV_TOKEN }}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
"utils": "pnpm --filter @cartridge/utils",
2626
"example:next": "pnpm --filter @cartridge/controller-example-next",
2727
"example:svelte": "pnpm --filter @cartridge/controller-example-svelte",
28-
"test": "pnpm keychain test",
29-
"test:ci": "pnpm keychain test:ci",
28+
"test": "turbo test",
29+
"test:ci": "turbo test:ci",
3030
"test:storybook": "pnpm turbo build:deps test:storybook",
3131
"test:storybook:update": "pnpm turbo build:deps test:storybook:update"
3232
},

packages/keychain/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"@types/react": "catalog:",
6565
"@types/react-dom": "catalog:",
6666
"@vitejs/plugin-react-swc": "catalog:",
67-
"@vitest/coverage-v8": "2.1.8",
67+
"@vitest/coverage-v8": "catalog:",
6868
"autoprefixer": "catalog:",
6969
"eslint": "catalog:",
7070
"http-server": "catalog:",
@@ -83,7 +83,7 @@
8383
"vite-plugin-node-polyfills": "^0.22.0",
8484
"vite-plugin-top-level-await": "catalog:",
8585
"vite-plugin-wasm": "catalog:",
86-
"vitest": "^2.1.8"
86+
"vitest": "catalog:"
8787
},
8888
"peerDependencies": {
8989
"@starknet-react/chains": "catalog:",

packages/keychain/src/components/Fees.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { useEffect, useState } from "react";
22
import { Spinner } from "@cartridge/ui-next";
3-
import { ERC20, useFeeToken } from "@cartridge/utils";
3+
import { ERC20, useFeeToken, convertTokenAmountToUSD } from "@cartridge/utils";
44
import { EstimateFee } from "starknet";
5-
import { convertTokenAmountToUSD } from "@/hooks/tokens";
65
import { ErrorAlert } from "./ErrorAlert";
76

87
export function Fees({

packages/keychain/src/components/funding/Balance.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ import {
66
CardTitle,
77
CoinsIcon,
88
} from "@cartridge/ui-next";
9-
import { useCreditBalance, useFeeToken } from "@cartridge/utils";
9+
import {
10+
useCreditBalance,
11+
useFeeToken,
12+
formatBalance,
13+
convertTokenAmountToUSD,
14+
} from "@cartridge/utils";
1015
import { useController } from "@/hooks/controller";
11-
import { formatBalance, convertTokenAmountToUSD } from "@/hooks/tokens";
1216

1317
export enum BalanceType {
1418
CREDITS = "credits",
@@ -52,7 +56,7 @@ export function Balance({ types }: BalanceProps) {
5256
<span className="text-foreground-400">{token.symbol}</span>
5357
</div>
5458

55-
{token && token.balance !== undefined && token.price ? (
59+
{token.balance !== undefined && token.price ? (
5660
<div className="text-foreground-400">
5761
{convertTokenAmountToUSD(token.balance, 18, token.price)}
5862
</div>

packages/keychain/src/components/funding/Deposit.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,13 @@ import {
2929
Separator,
3030
LayoutHeader,
3131
} from "@cartridge/ui-next";
32-
import { useFeeToken } from "@cartridge/utils";
32+
import { useFeeToken, convertUSDToTokenAmount } from "@cartridge/utils";
3333
import { useConnection } from "@/hooks/connection";
3434
import { ErrorAlert } from "../ErrorAlert";
3535
import { AmountSelection } from "./AmountSelection";
3636
import { Balance, BalanceType } from "./Balance";
3737
import { toast } from "sonner";
3838
import { DEFAULT_AMOUNT } from "./constants";
39-
import { convertUSDToTokenAmount } from "@/hooks/tokens";
4039

4140
type DepositProps = {
4241
onComplete?: (deployHash?: string) => void;

packages/keychain/src/hooks/tokens.tsx

Lines changed: 0 additions & 112 deletions
This file was deleted.

packages/profile/src/components/inventory/token/token.tsx

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,15 @@ import {
2525
isIframe,
2626
isPublicChain,
2727
StarkscanUrl,
28-
useCountervalue,
2928
useCreditBalance,
29+
useToken,
30+
formatBalance,
31+
convertTokenAmountToUSD,
3032
} from "@cartridge/utils";
31-
import { constants, getChecksumAddress } from "starknet";
32-
import { formatEther } from "viem";
33+
import { constants } from "starknet";
3334
import { useAccount } from "@/hooks/account";
34-
import { useBalance } from "@/hooks/token";
35-
import { TokenPair } from "@cartridge/utils/api/cartridge";
3635
import { useMemo } from "react";
3736
import { compare } from "compare-versions";
38-
import { formatBalance } from "./helper";
39-
import { erc20Metadata } from "@cartridge/presets";
4037

4138
export function Token() {
4239
const { address } = useParams<{ address: string }>();
@@ -98,49 +95,37 @@ function ERC20() {
9895
const navigate = useNavigate();
9996
const { address } = useParams<{ address: string }>();
10097
const { chainId, version } = useConnection();
101-
const balance = useBalance({ tokenAddress: address! });
102-
const { countervalue } = useCountervalue(
103-
{
104-
balance: formatEther(BigInt(balance?.raw ?? 0)),
105-
pair: `${balance?.meta.symbol}_USDC` as TokenPair,
106-
},
107-
{ enabled: balance && ["ETH", "STRK"].includes(balance.meta.symbol) },
108-
);
109-
const ekuboMeta = balance
110-
? erc20Metadata.find(
111-
(m) =>
112-
getChecksumAddress(m.l2_token_address) ===
113-
getChecksumAddress(balance.meta.contractAddress),
114-
)
115-
: undefined;
98+
const { token } = useToken(address!);
11699

117100
const compatibility = useMemo(() => {
118101
if (!version) return false;
119102
return compare(version, "0.5.6", ">=");
120103
}, [version]);
121104

122-
if (!balance) {
123-
return;
124-
}
125-
126105
return (
127106
<LayoutContainer>
128107
<LayoutHeader
129108
title={`${
130-
balance === undefined ? (
109+
token.balance === undefined ? (
131110
<Skeleton className="h-[20px] w-[120px] rounded" />
132111
) : (
133-
formatBalance(balance.amount.toString(), ["~"])
112+
formatBalance(token.balance)
134113
)
135-
} ${balance.meta.symbol}`}
114+
} ${token.symbol}`}
136115
description={
137-
countervalue && `${formatBalance(countervalue.formatted, ["~"])}`
116+
token.balance && token.price
117+
? convertTokenAmountToUSD(
118+
token.balance,
119+
token.decimals,
120+
token.price,
121+
)
122+
: undefined
138123
}
139124
icon={
140125
<div className="rounded-full size-11 bg-background-300 flex items-center justify-center">
141126
<img
142127
className="w-10 h-10"
143-
src={ekuboMeta?.logo_url ?? "/public/placeholder.svg"}
128+
src={token.icon ?? "/public/placeholder.svg"}
144129
/>
145130
</div>
146131
}
@@ -160,17 +145,17 @@ function ERC20() {
160145
<Link
161146
to={`${StarkscanUrl(
162147
chainId as constants.StarknetChainId,
163-
).contract(balance.meta.contractAddress)} `}
148+
).contract(token.address)} `}
164149
className="flex items-center gap-1 text-sm"
165150
target="_blank"
166151
>
167152
<div className="font-medium">
168-
{formatAddress(balance.meta.contractAddress, { size: "sm" })}
153+
{formatAddress(token.address, { size: "sm" })}
169154
</div>
170155
<ExternalIcon size="sm" />
171156
</Link>
172157
) : (
173-
<div>{formatAddress(balance.meta.contractAddress)}</div>
158+
<div>{formatAddress(token.address)}</div>
174159
)}
175160
</CardContent>
176161

0 commit comments

Comments
 (0)