Skip to content

Commit

Permalink
add reset to calls on top up dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Oct 11, 2023
1 parent 370e632 commit d2497cc
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 104 deletions.
62 changes: 32 additions & 30 deletions ui/src/app/components/start/Spawn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,39 @@ export const Spawn = ({
</div>
</>
) : (
<form
onSubmit={async (e) => {
if (formData) {
await handleSubmit(e);
}
}}
>
<div className="flex flex-col gap-2">
<Button
type="submit"
size={"xl"}
disabled={
!formFilled ||
!account ||
isWrongNetwork ||
loading ||
estimatingFee ||
!checkEnoughLords
<>
<form
onSubmit={async (e) => {
if (formData) {
await handleSubmit(e);
}
>
{checkEnoughLords
? formFilled
? "Start Game!!"
: "Fill details"
: "Not enough Lords"}
</Button>
{!checkEnoughLords && (
<Button onClick={mintLords}>Mint Lords</Button>
)}
</div>
</form>
}}
>
<div className="flex flex-col gap-2">
<Button
type="submit"
size={"xl"}
disabled={
!formFilled ||
!account ||
isWrongNetwork ||
loading ||
estimatingFee ||
!checkEnoughLords
}
>
{checkEnoughLords
? formFilled
? "Start Game!!"
: "Fill details"
: "Not enough Lords"}
</Button>
</div>
</form>
{!checkEnoughLords && (
<Button onClick={mintLords}>Mint Lords</Button>
)}
</>
)}
</div>
<div className="absolute bottom-10 left-0 right-0 flex flex-col items-center gap-4 z-10 pb-8">
Expand Down
52 changes: 49 additions & 3 deletions ui/src/app/lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}
}
65 changes: 16 additions & 49 deletions ui/src/app/lib/utils/syscalls.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
import {
InvokeTransactionReceiptResponse,
Call,
Account,
AccountInterface,
provider,
} from "starknet";
import { GameData } from "@/app/components/GameData";
import {
Adventurer,
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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -170,6 +124,7 @@ export function syscalls({
showTopUpDialog,
setTopUpAccount,
setEstimatingFee,
resetCalls,
}: SyscallsProps) {
const gameData = new GameData();

Expand Down Expand Up @@ -359,6 +314,8 @@ export function syscalls({
console.log(e);
stopLoading(e, true);
}
} else {
resetCalls();
}
};

Expand Down Expand Up @@ -607,6 +564,8 @@ export function syscalls({
console.log(e);
stopLoading(e, true);
}
} else {
resetCalls();
}
};

Expand Down Expand Up @@ -840,6 +799,8 @@ export function syscalls({
console.log(e);
stopLoading(e, true);
}
} else {
resetCalls();
}
};

Expand Down Expand Up @@ -1012,6 +973,8 @@ export function syscalls({
console.log(e);
stopLoading(e, true);
}
} else {
resetCalls();
}
};

Expand Down Expand Up @@ -1173,6 +1136,8 @@ export function syscalls({
console.log(e);
stopLoading(e, true);
}
} else {
resetCalls();
}
};

Expand Down Expand Up @@ -1433,6 +1398,8 @@ export function syscalls({
console.log(e);
stopLoading(e, true);
}
} else {
resetCalls();
}
};

Expand Down
59 changes: 37 additions & 22 deletions ui/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -169,6 +171,7 @@ export default function Home() {
setTopUpAccount,
setEstimatingFee,
account,
resetCalls,
});

const playState = useMemo(
Expand Down Expand Up @@ -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();
}
};

Expand Down

1 comment on commit d2497cc

@vercel
Copy link

@vercel vercel bot commented on d2497cc Oct 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.