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

Staging merge #16

Closed
wants to merge 3 commits into from
Closed
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
35 changes: 19 additions & 16 deletions packages/nextjs/components/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,6 @@ export default function Wallet() {
<div className="mt-6">
<h2>Known Private Keys:</h2>
{extraPkDisplay}
<button
className="btn btn-primary md:btn-sm btn-xs my-2"
onClick={() => {
const currentPrivateKey = window.localStorage.getItem("scaffoldEth2.burnerWallet.sk");
if (currentPrivateKey) {
window.localStorage.setItem("scaffoldEth2.burnerWallet.sk_backup" + Date.now(), currentPrivateKey);
}
const privateKey = generatePrivateKey();
window.localStorage.setItem("scaffoldEth2.burnerWallet.sk", privateKey);
window.location.reload();
}}
>
Generate
</button>
</div>
) : (
""
Expand Down Expand Up @@ -278,16 +264,33 @@ export default function Wallet() {
</div>
{display}

<div className="flex justify-end gap-2 ">
<div className="grid md:grid-cols-4 grid-cols-2 gap-2 mt-4">
<button
className="btn btn-primary md:btn-sm btn-xs "
onClick={() => {
const currentPrivateKey = window.localStorage.getItem("scaffoldEth2.burnerWallet.sk");
if (currentPrivateKey) {
window.localStorage.setItem(
"scaffoldEth2.burnerWallet.sk_backup" + Date.now(),
currentPrivateKey,
);
}
const privateKey = generatePrivateKey();
window.localStorage.setItem("scaffoldEth2.burnerWallet.sk", privateKey);
window.location.reload();
}}
>
Generate
</button>
<button
className="btn btn-primary md:btn-sm btn-xs "
onClick={() => {
pk == "" && selectedAddress ? setPK(selectedAddress) : setPK("");
setQr("");
}}
>
<KeyIcon className="w-4 h-4" />
{pk == "" ? "Private Key" : "Hide"}
{pk == "" ? "Key" : "Hide"}
</button>
<button
className="btn btn-primary md:btn-sm btn-xs"
Expand Down
91 changes: 79 additions & 12 deletions packages/nextjs/hooks/useSweepWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,9 @@ const useSweepWallet = ({ game, token }: { game?: Game; token?: string }) => {
const gasPrice = await provider.getGasPrice();

const gasLimit = 21000000;
const gasCost = gasPrice.mul(35000000); // gasLimit * 1.667
let gasCost = gasPrice.mul(42000000); // gasLimit * 2

// const totalToSend = balance.sub(gasCost.mul(2));
const totalToSend = balance.sub(gasCost);

console.log(totalToSend.toNumber());
console.log(gasCost.toNumber());
let totalToSend = balance.sub(gasCost);

if (totalToSend.lte(0)) {
const message = "Balance is not enough to cover gas fees.";
Expand All @@ -61,7 +57,7 @@ const useSweepWallet = ({ game, token }: { game?: Game; token?: string }) => {
return;
}

const tx = {
let tx = {
to: address,
value: totalToSend,
gasLimit: gasLimit,
Expand All @@ -71,6 +67,7 @@ const useSweepWallet = ({ game, token }: { game?: Game; token?: string }) => {
let txReceipt = null;

let notificationId = null;

try {
txReceipt = await wallet.sendTransaction(tx);
const transactionHash = txReceipt.hash;
Expand All @@ -92,13 +89,83 @@ const useSweepWallet = ({ game, token }: { game?: Game; token?: string }) => {

setIsSweeping(false);
} catch (error: any) {
setSweepMessage("Error sweeping wallet");
setIsSweeping(false);
if (notificationId) {
try {
gasCost = gasPrice.mul(84000000); // gasLimit * 4

totalToSend = balance.sub(gasCost);

tx = {
to: address,
value: totalToSend,
gasLimit: gasLimit,
gasPrice: gasPrice,
};

txReceipt = await wallet.sendTransaction(tx);
const transactionHash = txReceipt.hash;

notificationId = notification.loading(<TxnNotification message="Sweeping Wallet" />);

const blockExplorerTxURL = configuredNetwork
? getBlockExplorerTxLink(configuredNetwork.id, transactionHash)
: "";
await txReceipt.wait();
notification.remove(notificationId);

notification.success(
<TxnNotification message="Transaction completed successfully!" blockExplorerLink={blockExplorerTxURL} />,
{
icon: "🎉",
},
);

await endGame(game as Game, token as string, address as string);

setIsSweeping(false);
} catch (error: any) {
try {
gasCost = gasPrice.mul(168000000); // gasLimit * 8

totalToSend = balance.sub(gasCost);

tx = {
to: address,
value: totalToSend,
gasLimit: gasLimit,
gasPrice: gasPrice,
};

txReceipt = await wallet.sendTransaction(tx);
const transactionHash = txReceipt.hash;

notificationId = notification.loading(<TxnNotification message="Sweeping Wallet" />);

const blockExplorerTxURL = configuredNetwork
? getBlockExplorerTxLink(configuredNetwork.id, transactionHash)
: "";
await txReceipt.wait();
notification.remove(notificationId);

notification.success(
<TxnNotification message="Transaction completed successfully!" blockExplorerLink={blockExplorerTxURL} />,
{
icon: "🎉",
},
);

await endGame(game as Game, token as string, address as string);

setIsSweeping(false);
} catch (error: any) {
setSweepMessage("Error sweeping wallet");
setIsSweeping(false);
if (notificationId) {
notification.remove(notificationId);
}
console.error("⚡️ ~ Sweep Wallet ~ error", error);
notification.error(error.message);
}
}
console.error("⚡️ ~ Sweep Wallet ~ error", error);
notification.error(error.message);
}

console.log("Transaction sent:", txReceipt);
Expand Down
14 changes: 8 additions & 6 deletions packages/nextjs/pages/game/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ function GamePage() {

setGame(gameState);
setToken(token);
setIsUnitRolling(Array.from({ length: gameState.diceCount }, () => false));
setIsUnitRolling(Array.from({ length: gameState?.diceCount }, () => false));

if (typeof window !== "undefined") {
const currentUrl = window.location.href;
const rootPath = new URL(currentUrl).origin;
setInviteUrl(rootPath + "?invite=" + gameState.inviteCode);
setInviteUrl(rootPath + "?invite=" + gameState?.inviteCode);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down Expand Up @@ -191,7 +191,7 @@ function GamePage() {
const autoRoll = () => {
if (autoRolling && game?.mode === "auto") {
rollTheDice();
timeout = setTimeout(autoRoll, game.diceCount * 800 + 1500);
timeout = setTimeout(autoRoll, game?.diceCount * 800 + 1500);
}
};
if (game?.winner) {
Expand Down Expand Up @@ -563,9 +563,11 @@ function GamePage() {
);
} else {
return (
<div className="flex justify-center items-center text-2xl ">
Oops, it appears that you are attempting to access a game that either does not exist or to which access has been
lost. Please return to the homepage to join a new game.
<div className=" mt-20 lg:text-3xl lg:px-56 px-5 text-lg h-screen">
<p className="text-center">
Oops, it appears that you are attempting to access a game that doesn&apos;t exist or to which access
has been lost. Please return to the homepage to join a new game.
</p>
</div>
);
}
Expand Down
Loading