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

Fix/token balance #1177

Merged
merged 2 commits into from
Dec 17, 2024
Merged
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
11 changes: 11 additions & 0 deletions packages/profile/src/components/inventory/token/helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const formatBalance = (balance: string) => {
// Catch prefix until number
let prefix = "";
for (const char of balance) {
if (!isNaN(parseInt(char))) {
break;
}
prefix += char;
}
return `${prefix}${parseFloat(balance.replace(prefix, "")).toLocaleString()}`;
};
21 changes: 3 additions & 18 deletions packages/profile/src/components/inventory/token/send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { useForm } from "react-hook-form";
import { Link, useNavigate, useParams } from "react-router-dom";
import { Call, constants, uint256 } from "starknet";
import { z } from "zod";
import { formatBalance } from "./helper";

export function SendToken() {
const { address: tokenAddress } = useParams<{ address: string }>();
Expand Down Expand Up @@ -112,21 +113,6 @@ export function SendToken() {
{ enabled: t && ["ETH", "STRK"].includes(t.meta.symbol) && !!amount },
);

const countervalueFormatted = useMemo(() => {
if (!countervalue) return undefined;
// Catch prefix until number
let prefix = "";
for (const char of countervalue.formatted) {
if (!isNaN(parseInt(char))) {
break;
}
prefix += char;
}
return `${prefix}${parseFloat(
countervalue.formatted.replace(prefix, ""),
).toLocaleString()}`;
}, [countervalue]);

if (!t) {
return null;
}
Expand Down Expand Up @@ -186,8 +172,7 @@ export function SendToken() {
)
}
>
{parseFloat(t.balance.formatted).toLocaleString()}{" "}
{t.meta.symbol}
{formatBalance(t.balance.formatted)} {t.meta.symbol}
</div>
</div>
</div>
Expand All @@ -202,7 +187,7 @@ export function SendToken() {
/>
{countervalue && (
<span className="absolute right-4 top-3.5 text-sm text-muted-foreground">
{countervalueFormatted}
{formatBalance(countervalue.formatted)}
</span>
)}
</div>
Expand Down
22 changes: 5 additions & 17 deletions packages/profile/src/components/inventory/token/token.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { useToken } from "@/hooks/token";
import { TokenPair } from "@cartridge/utils/api/cartridge";
import { useMemo } from "react";
import { compare } from "compare-versions";
import { formatBalance } from "./helper";

export function Token() {
const { address } = useParams<{ address: string }>();
Expand Down Expand Up @@ -106,21 +107,6 @@ function ERC20() {
{ enabled: t && ["ETH", "STRK"].includes(t.meta.symbol) },
);

const countervalueFormatted = useMemo(() => {
if (!countervalue) return undefined;
// Catch prefix until number
let prefix = "";
for (const char of countervalue.formatted) {
if (!isNaN(parseInt(char))) {
break;
}
prefix += char;
}
return `${prefix}${parseFloat(
countervalue.formatted.replace(prefix, ""),
).toLocaleString()}`;
}, [countervalue]);

const compatibility = useMemo(() => {
if (!version) return false;
return compare(version, "0.4.0", ">=");
Expand All @@ -145,10 +131,12 @@ function ERC20() {
t.balance === undefined ? (
<Skeleton className="h-[20px] w-[120px] rounded" />
) : (
parseFloat(t.balance.formatted).toLocaleString()
formatBalance(t.balance.formatted)
)
} ${t.meta.symbol}`}
description={countervalueFormatted && `$${countervalueFormatted} (USD)`}
description={
countervalue && `${formatBalance(countervalue.formatted)} (USD)`
}
icon={
<img
className="w-8 h-8"
Expand Down
26 changes: 7 additions & 19 deletions packages/profile/src/components/inventory/token/tokens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import { Balance, ERC20Metadata, useCountervalue } from "@cartridge/utils";
import { formatEther } from "viem";
import { useTokens } from "@/hooks/token";
import { TokenPair } from "@cartridge/utils/api/cartridge";
import { useMemo, useState } from "react";
import { useState } from "react";
import { formatBalance } from "./helper";

export function Tokens() {
// const { isVisible } = useConnection();
Expand Down Expand Up @@ -66,21 +67,6 @@ function TokenCardContent({
pair: `${token.meta.symbol}_USDC` as TokenPair,
});

const countervalueFormatted = useMemo(() => {
if (!countervalue) return undefined;
// Catch prefix until number
let prefix = "";
for (const char of countervalue.formatted) {
if (!isNaN(parseInt(char))) {
break;
}
prefix += char;
}
return `${prefix}${parseFloat(
countervalue.formatted.replace(prefix, ""),
).toLocaleString()}`;
}, [countervalue]);

return (
<CardContent
className={cn(
Expand All @@ -99,12 +85,14 @@ function TokenCardContent({

<div className="bg-secondary flex flex-1 gap-x-1.5 items-center justify-between p-3 text-medium">
<div className="flex items-center gap-2">
<p>{parseFloat(token.balance.formatted).toLocaleString()}</p>
<p>{formatBalance(token.balance.formatted)}</p>
<span className="text-muted-foreground">{token.meta.symbol}</span>
</div>

{countervalueFormatted && (
<span className="text-muted-foreground">{countervalueFormatted}</span>
{countervalue && (
<span className="text-muted-foreground">
{formatBalance(countervalue.formatted)}
</span>
)}
</div>
</CardContent>
Expand Down
Loading