Skip to content

Commit 4f01d33

Browse files
Move token utils and apply tokens context to profile
1 parent 9d8e1e4 commit 4f01d33

File tree

24 files changed

+352
-727
lines changed

24 files changed

+352
-727
lines changed

.github/workflows/test.yml

+1-1
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

+2-2
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

+2-2
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

+1-2
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

+7-3
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

+1-2
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

-112
This file was deleted.

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

+10-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { useToken } from "@/hooks/token";
21
import { Amount } from "@cartridge/ui-next";
3-
import { useCountervalue } from "@cartridge/utils";
2+
import { formatBalance, useCountervalue, useToken } from "@cartridge/utils";
43
import { TokenPair } from "@cartridge/utils/api/cartridge";
54
import { useCallback } from "react";
65
import { useParams } from "react-router-dom";
@@ -15,24 +14,26 @@ export function SendAmount({
1514
setError: (error: Error | undefined) => void;
1615
}) {
1716
const { address: tokenAddress } = useParams<{ address: string }>();
18-
const token = useToken({ tokenAddress: tokenAddress! });
17+
const { token } = useToken(tokenAddress!);
1918

2019
const handleMax = useCallback(
2120
(e: React.MouseEvent<HTMLDivElement | HTMLButtonElement>) => {
2221
e.preventDefault();
2322
if (!token) return;
24-
setAmount(parseFloat(token.balance.formatted.replace("~", "")));
23+
setAmount(
24+
parseFloat(formatBalance(token.balance ?? 0n).replace("~", "")),
25+
);
2526
},
2627
[token, setAmount],
2728
);
2829

2930
const { countervalue } = useCountervalue(
3031
{
3132
balance: amount?.toString() ?? "0",
32-
pair: `${token?.meta.symbol}_USDC` as TokenPair,
33+
pair: `${token.symbol}_USDC` as TokenPair,
3334
},
3435
{
35-
enabled: token && ["ETH", "STRK"].includes(token.meta.symbol) && !!amount,
36+
enabled: token && ["ETH", "STRK"].includes(token.symbol) && !!amount,
3637
},
3738
);
3839

@@ -52,9 +53,9 @@ export function SendAmount({
5253
<Amount
5354
amount={amount}
5455
conversion={countervalue?.formatted}
55-
balance={parseFloat(token.balance.formatted.replace("~", ""))}
56-
symbol={token.meta.symbol}
57-
decimals={token.meta.decimals ?? 18}
56+
balance={parseFloat(formatBalance(token.balance ?? 0n).replace("~", ""))}
57+
symbol={token.symbol}
58+
decimals={token.decimals ?? 18}
5859
setError={setError}
5960
onChange={handleChange}
6061
onMax={handleMax}

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useAccount } from "@/hooks/account";
22
import { useConnection } from "@/hooks/context";
3-
import { useToken } from "@/hooks/token";
43
import {
54
LayoutContainer,
65
LayoutContent,
@@ -18,14 +17,15 @@ import { useNavigate, useParams } from "react-router-dom";
1817
import { Call, uint256 } from "starknet";
1918
import { SendRecipient } from "@/components/modules/recipient";
2019
import { SendAmount } from "./amount";
20+
import { useToken } from "@cartridge/utils";
2121

2222
export function SendToken() {
2323
const { address: tokenAddress } = useParams<{ address: string }>();
2424
const { address } = useAccount();
2525
const { parent } = useConnection();
2626
const [validated, setValidated] = useState(false);
2727
const [warning, setWarning] = useState<string>();
28-
const token = useToken({ tokenAddress: tokenAddress! });
28+
const { token } = useToken(tokenAddress!);
2929
const navigate = useNavigate();
3030

3131
const [to, setTo] = useState("");
@@ -44,15 +44,13 @@ export function SendToken() {
4444

4545
const onSubmit = useCallback(
4646
async (to: string, amount: number) => {
47-
if (!token) return;
48-
4947
const formattedAmount = uint256.bnToUint256(
50-
BigInt(amount * 10 ** token.meta.decimals),
48+
BigInt(amount * 10 ** token.decimals),
5149
);
5250

5351
const calls: Call[] = [
5452
{
55-
contractAddress: token.meta.address,
53+
contractAddress: token.address,
5654
entrypoint: "transfer",
5755
calldata: [to, formattedAmount],
5856
},
@@ -70,13 +68,13 @@ export function SendToken() {
7068
return (
7169
<LayoutContainer>
7270
<LayoutHeader
73-
title={`Send ${token.meta.symbol}`}
71+
title={`Send ${token.symbol}`}
7472
description={<CopyAddress address={address} size="sm" />}
7573
icon={
7674
<div className="rounded-full size-11 bg-foreground-100 flex items-center justify-center">
7775
<img
7876
className="w-10 h-10"
79-
src={token.meta.logoUrl ?? "/public/placeholder.svg"}
77+
src={token.icon ?? "/public/placeholder.svg"}
8078
/>
8179
</div>
8280
}

0 commit comments

Comments
 (0)