diff --git a/ui/src/app/components/start/Spawn.tsx b/ui/src/app/components/start/Spawn.tsx index 73f6e7850..9478753f2 100644 --- a/ui/src/app/components/start/Spawn.tsx +++ b/ui/src/app/components/start/Spawn.tsx @@ -119,37 +119,39 @@ export const Spawn = ({ ) : ( -
{ - if (formData) { - await handleSubmit(e); - } - }} - > -
- - {!checkEnoughLords && ( - - )} -
-
+ }} + > +
+ +
+ + {!checkEnoughLords && ( + + )} + )}
diff --git a/ui/src/app/lib/utils/index.ts b/ui/src/app/lib/utils/index.ts index 65c5dd738..63cfe4e68 100644 --- a/ui/src/app/lib/utils/index.ts +++ b/ui/src/app/lib/utils/index.ts @@ -1,8 +1,8 @@ import { ClassValue, clsx } from "clsx"; import { twMerge } from "tailwind-merge"; import BN from "bn.js"; - -import { Adventurer, Item } from "../../types"; +import { Adventurer, Item, BurnerStorage } from "../../types"; +import Storage from "../storage"; import { GameData } from "../../components/GameData"; import { itemCharismaDiscount, @@ -11,7 +11,8 @@ import { potionBasePrice, } from "../constants"; import { z } from "zod"; -import { deathMessages } from "../constants"; +import { deathMessages, FEE_CHECK_BALANCE } from "../constants"; +import { Call, AccountInterface, Account } from "starknet"; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); @@ -358,3 +359,48 @@ export function getDeathMessageByRank(rank: number): string { return message || "Better luck next time - You can improve!"; } + +export async function checkArcadeBalance( + calls: Call[], + ethBalance: bigint, + showTopUpDialog: (...args: any[]) => any, + setTopUpAccount: (...args: any[]) => any, + setEstimatingFee: (...args: any[]) => any, + account?: AccountInterface +) { + console.log(calls); + console.log(ethBalance); + if (ethBalance < FEE_CHECK_BALANCE) { + const storage: BurnerStorage = Storage.get("burners"); + if (account && (account?.address ?? "0x0") in storage) { + try { + setEstimatingFee(true); + const newAccount = new Account( + account, + account?.address, + storage[account?.address]["privateKey"], + "1" + ); + const { suggestedMaxFee: estimatedFee } = await newAccount.estimateFee( + calls + ); + // Add 10% to fee for safety + const formattedFee = estimatedFee * (BigInt(11) / BigInt(10)); + setEstimatingFee(false); + if (ethBalance < formattedFee) { + showTopUpDialog(true); + setTopUpAccount(account?.address); + return true; + } else { + return false; + } + } catch (e) { + console.log(e); + setEstimatingFee(false); + return false; + } + } + } else { + return false; + } +} diff --git a/ui/src/app/lib/utils/syscalls.ts b/ui/src/app/lib/utils/syscalls.ts index c3b9a0905..ebec6612c 100644 --- a/ui/src/app/lib/utils/syscalls.ts +++ b/ui/src/app/lib/utils/syscalls.ts @@ -1,9 +1,7 @@ import { InvokeTransactionReceiptResponse, Call, - Account, AccountInterface, - provider, } from "starknet"; import { GameData } from "@/app/components/GameData"; import { @@ -11,14 +9,12 @@ import { FormData, NullAdventurer, UpgradeStats, - BurnerStorage, } from "@/app/types"; import { QueryKey } from "@/app/hooks/useQueryStore"; -import { getKeyFromValue, stringToFelt, getRandomNumber } from "."; +import { getKeyFromValue, stringToFelt } from "."; import { parseEvents } from "./parseEvents"; import { processNotifications } from "@/app/components/notifications/NotificationHandler"; -import Storage from "../storage"; -import { FEE_CHECK_BALANCE } from "../constants"; +import { checkArcadeBalance } from "."; export interface SyscallsProps { gameContract: any; @@ -47,49 +43,7 @@ export interface SyscallsProps { setTopUpAccount: (...args: any[]) => any; setEstimatingFee: (...args: any[]) => any; account?: AccountInterface; -} - -async function checkArcadeBalance( - calls: Call[], - ethBalance: bigint, - showTopUpDialog: (...args: any[]) => any, - setTopUpAccount: (...args: any[]) => any, - setEstimatingFee: (...args: any[]) => any, - account?: AccountInterface -) { - if (ethBalance < FEE_CHECK_BALANCE) { - const storage: BurnerStorage = Storage.get("burners"); - if (account && (account?.address ?? "0x0") in storage) { - try { - setEstimatingFee(true); - const newAccount = new Account( - account, - account?.address, - storage[account?.address]["privateKey"], - "1" - ); - const { suggestedMaxFee: estimatedFee } = await newAccount.estimateFee( - calls - ); - // Add 10% to fee for safety - const formattedFee = estimatedFee * (BigInt(11) / BigInt(10)); - setEstimatingFee(false); - if (ethBalance < formattedFee) { - showTopUpDialog(true); - setTopUpAccount(account?.address); - return true; - } else { - return false; - } - } catch (e) { - console.log(e); - setEstimatingFee(false); - return false; - } - } - } else { - return false; - } + resetCalls: (...args: any[]) => any; } function handleEquip( @@ -170,6 +124,7 @@ export function syscalls({ showTopUpDialog, setTopUpAccount, setEstimatingFee, + resetCalls, }: SyscallsProps) { const gameData = new GameData(); @@ -359,6 +314,8 @@ export function syscalls({ console.log(e); stopLoading(e, true); } + } else { + resetCalls(); } }; @@ -607,6 +564,8 @@ export function syscalls({ console.log(e); stopLoading(e, true); } + } else { + resetCalls(); } }; @@ -840,6 +799,8 @@ export function syscalls({ console.log(e); stopLoading(e, true); } + } else { + resetCalls(); } }; @@ -1012,6 +973,8 @@ export function syscalls({ console.log(e); stopLoading(e, true); } + } else { + resetCalls(); } }; @@ -1173,6 +1136,8 @@ export function syscalls({ console.log(e); stopLoading(e, true); } + } else { + resetCalls(); } }; @@ -1433,6 +1398,8 @@ export function syscalls({ console.log(e); stopLoading(e, true); } + } else { + resetCalls(); } }; diff --git a/ui/src/app/page.tsx b/ui/src/app/page.tsx index d9e3d4657..cf1075f62 100644 --- a/ui/src/app/page.tsx +++ b/ui/src/app/page.tsx @@ -57,6 +57,7 @@ import ScreenMenu from "./components/menu/ScreenMenu"; import { getArcadeConnectors } from "./lib/connectors"; import Header from "./components/navigation/Header"; import { Maintenance } from "./components/archived/Maintenance"; +import { checkArcadeBalance } from "./lib/utils"; const allMenuItems: Menu[] = [ { id: 1, label: "Start", screen: "start", disabled: false }, @@ -115,6 +116,7 @@ export default function Home() { const { gameContract, lordsContract, ethContract } = useContracts(); const { addTransaction } = useTransactionManager(); const addToCalls = useTransactionCartStore((state) => state.addToCalls); + const resetCalls = useTransactionCartStore((state) => state.resetCalls); const handleSubmitCalls = useTransactionCartStore( (state) => state.handleSubmitCalls ); @@ -169,6 +171,7 @@ export default function Home() { setTopUpAccount, setEstimatingFee, account, + resetCalls, }); const playState = useMemo( @@ -349,29 +352,41 @@ export default function Home() { // TEMPORARY FOR TESTING const mintLords = async () => { - try { - setIsMintingLords(true); - // Mint 250 LORDS - const mintLords: Call = { - contractAddress: lordsContract?.address ?? "", - entrypoint: "mint", - calldata: [address ?? "0x0", (250 * 10 ** 18).toString(), "0"], - }; - addToCalls(mintLords); - const tx = await handleSubmitCalls(account!, [...calls, mintLords]); - const result = await account?.waitForTransaction(tx?.transaction_hash, { - retryInterval: 2000, - }); - - if (!result) { - throw new Error("Lords Mint did not complete successfully."); + // Mint 250 LORDS + const mintLords: Call = { + contractAddress: lordsContract?.address ?? "", + entrypoint: "mint", + calldata: [address ?? "0x0", (250 * 10 ** 18).toString(), "0"], + }; + const balanceEmpty = await checkArcadeBalance( + [...calls, mintLords], + ethBalance.data?.value ?? BigInt(0), + showTopUpDialog, + setTopUpAccount, + setEstimatingFee, + account + ); + if (!balanceEmpty) { + try { + setIsMintingLords(true); + addToCalls(mintLords); + const tx = await handleSubmitCalls(account!, [...calls, mintLords]); + const result = await account?.waitForTransaction(tx?.transaction_hash, { + retryInterval: 2000, + }); + + if (!result) { + throw new Error("Lords Mint did not complete successfully."); + } + + setIsMintingLords(false); + lordsBalance.refetch(); + } catch (e) { + setIsMintingLords(false); + console.log(e); } - - setIsMintingLords(false); - lordsBalance.refetch(); - } catch (e) { - setIsMintingLords(false); - console.log(e); + } else { + resetCalls(); } };