Skip to content

Commit

Permalink
wallet-amount
Browse files Browse the repository at this point in the history
  • Loading branch information
Bosun-Josh121 committed Nov 3, 2024
1 parent 4e0edee commit e5cfb8e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
17 changes: 16 additions & 1 deletion components/UI/profileCard/profileCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { getTweetLink } from "@utils/browserService";
import { hexToDecimal } from "@utils/feltService";
import { TEXT_TYPE } from "@constants/typography";
import Typography from "../typography/typography";
import { calculateTotalBalance } from '../../../services/argentPortfolioService';

type ProfileCardProps = {
rankingData: RankingData;
Expand All @@ -36,6 +37,7 @@ const ProfileCard: FunctionComponent<ProfileCardProps> = ({
isOwner,
}) => {
const [userXp, setUserXp] = useState<number>();
const [totalBalance, setTotalBalance] = useState<number | null>(null);
const sinceDate = useCreationDate(identity);
const formattedAddress = (
identity.owner.startsWith("0x") ? identity.owner : `0x${identity.owner}`
Expand All @@ -48,6 +50,19 @@ const ProfileCard: FunctionComponent<ProfileCardProps> = ({
return rank;
}, []);

useEffect(() => {
const fetchTotalBalance = async () => {
try {
const balance = await calculateTotalBalance(formattedAddress, "USD");
setTotalBalance(balance);
} catch (err) {
console.error("Error fetching total balance:", err);
}
};

fetchTotalBalance();
}, [formattedAddress]);

const computeData = useCallback(() => {
if (
rankingData &&
Expand Down Expand Up @@ -108,7 +123,7 @@ const ProfileCard: FunctionComponent<ProfileCardProps> = ({
type={TEXT_TYPE.BODY_SMALL}
className={`${styles.wallet_amount} font-extrabold`}
>
$2,338.34
{totalBalance !== null ? `$${totalBalance.toFixed(2)}` : "Loading..."}
</Typography>
<EyeIcon />
</div>
Expand Down
29 changes: 29 additions & 0 deletions services/argentPortfolioService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,32 @@ export const calculateTokenPrice = async (
throw err;
}
};

export const calculateTotalBalance = async (
walletAddress: string,
currency: "USD" | "EUR" | "GBP" = "USD"
): Promise<number> => {
try {
const tokens = await fetchUserTokens(walletAddress); // Fetch all tokens in wallet
let totalBalance = 0;

for (const token of tokens) {
try {
// Calculate the price of each token and add it to the total balance
const tokenValue = await calculateTokenPrice(
token.tokenAddress,
token.tokenBalance,
currency
);
totalBalance += tokenValue;
} catch (err) {
console.error(`Error calculating price for token ${token.tokenAddress}:`, err);
}
}

return totalBalance;
} catch (err) {
console.error("Error calculating total balance:", err);
throw err;
}
};

0 comments on commit e5cfb8e

Please sign in to comment.