From d9f37a42b49b24fba1fbc3e2a56ccaa2890ddda9 Mon Sep 17 00:00:00 2001 From: Starknet Dev Date: Wed, 9 Oct 2024 19:41:21 +0100 Subject: [PATCH 1/3] - add beasts fetcher - add withdraw button for eth, lords, beasts, GTs and bloberts - add balances of these in the profile dialog - add test withdrawal for 1 beast --- ui/src/app/api/getBeasts.ts | 53 ++++++++++++++++ .../app/components/profile/ProfileDialog.tsx | 61 ++++++++++++++++--- ui/src/app/lib/utils/syscalls.ts | 48 +++++++++------ ui/src/app/page.tsx | 23 +++++-- 4 files changed, 157 insertions(+), 28 deletions(-) create mode 100644 ui/src/app/api/getBeasts.ts diff --git a/ui/src/app/api/getBeasts.ts b/ui/src/app/api/getBeasts.ts new file mode 100644 index 000000000..efd1641ff --- /dev/null +++ b/ui/src/app/api/getBeasts.ts @@ -0,0 +1,53 @@ +import { Network } from "@/app/hooks/useUIStore"; +import { networkConfig } from "@/app/lib/networkConfig"; +import { indexAddress } from "@/app/lib/utils"; + +export const getBeasts = async ( + owner: string, + beastsAddress: string, + network: Network +): Promise => { + const recursiveFetch: any = async ( + beasts: any[], + nextPageKey: string | null + ) => { + let url = `${ + networkConfig[network!].blastUrl + }/builder/getWalletNFTs?contractAddress=${beastsAddress}&walletAddress=${indexAddress( + owner + ).toLowerCase()}&pageSize=100`; + + if (nextPageKey) { + url += `&pageKey=${nextPageKey}`; + } + + try { + const response = await fetch(url, { + method: "GET", + headers: { + "Content-Type": "application/json", + }, + }); + + const data = await response.json(); + beasts = beasts.concat( + data?.nfts?.map((beast: any) => { + const tokenId = JSON.parse(beast.tokenId); + return Number(tokenId); + }) + ); + + if (data.nextPageKey) { + return recursiveFetch(beasts, data.nextPageKey); + } + } catch (ex) { + console.log("error fetching beasts", ex); + } + + return beasts; + }; + + let beastsData = await recursiveFetch([], null); + + return beastsData; +}; diff --git a/ui/src/app/components/profile/ProfileDialog.tsx b/ui/src/app/components/profile/ProfileDialog.tsx index 8625c3c14..ff9149c78 100644 --- a/ui/src/app/components/profile/ProfileDialog.tsx +++ b/ui/src/app/components/profile/ProfileDialog.tsx @@ -1,5 +1,5 @@ import { Button } from "@/app/components/buttons/Button"; -import { CartridgeIcon } from "@/app/components/icons/Icons"; +import { CartridgeIcon, GiBruteIcon } from "@/app/components/icons/Icons"; import useAdventurerStore from "@/app/hooks/useAdventurerStore"; import useNetworkAccount from "@/app/hooks/useNetworkAccount"; import { useQueriesStore } from "@/app/hooks/useQueryStore"; @@ -9,6 +9,9 @@ import { copyToClipboard, displayAddress, padAddress } from "@/app/lib/utils"; import { NullAdventurer } from "@/app/types"; import CartridgeConnector from "@cartridge/connector"; import { useConnect, useDisconnect } from "@starknet-react/core"; +import Image from "next/image"; +import Eth from "public/icons/eth.svg"; +import Lords from "public/icons/lords.svg"; import { useState } from "react"; import { MdClose } from "react-icons/md"; import { AccountInterface } from "starknet"; @@ -18,12 +21,16 @@ interface ProfileDialogprops { adminAccountAddress: string, account: AccountInterface, ethBalance: bigint, - lordsBalance: bigint + lordsBalance: bigint, + beasts: number[] ) => Promise; ethBalance: bigint; lordsBalance: bigint; ethContractAddress: string; lordsContractAddress: string; + goldenTokens: number[]; + beasts: number[]; + blobertsData: any; } export const ProfileDialog = ({ @@ -32,6 +39,9 @@ export const ProfileDialog = ({ lordsBalance, ethContractAddress, lordsContractAddress, + goldenTokens, + beasts, + blobertsData, }: ProfileDialogprops) => { const { setShowProfile, setNetwork } = useUIStore(); const { disconnect } = useDisconnect(); @@ -57,6 +67,8 @@ export const ProfileDialog = ({ setTimeout(() => setCopiedDelegate(false), 2000); }; + const tournamentEnded = process.env.NEXT_PUBLIC_TOURNAMENT_ENDED === "true"; + return (
-
+
{checkCartridgeConnector(connector) && ( )} @@ -90,10 +102,44 @@ export const ProfileDialog = ({ {displayAddress(address!)} )} +
+ + +

{(Number(ethBalance) / 10 ** 18).toFixed(4).toString()}

+
+ + +

{(Number(lordsBalance) / 10 ** 18).toFixed(2).toString()}

+
+ + golden-tokens +

{goldenTokens?.length}

+
+ + +

{beasts.length}

+
+ {tournamentEnded && ( + + bloberts +

{blobertsData?.tokens.length}

+
+ )} +
{checkCartridgeConnector(connector) && ( -
+

Withdraw

@@ -121,7 +167,7 @@ export const ProfileDialog = ({ (connector as unknown as CartridgeConnector).openMenu() } > - Change Settings + Set Delegate