Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
starknetdev committed Oct 4, 2023
1 parent c70d594 commit 8d39bb7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 35 deletions.
71 changes: 38 additions & 33 deletions ui/src/app/lib/burner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import { useAccount, useConnectors } from "@starknet-react/core";
import { ArcadeConnector } from "./arcade";
import { useContracts } from "../hooks/useContracts";
import { BurnerStorage } from "../types";
import { mainnet_addr } from "./constants";
import { getRPCUrl, getArcadeClassHash } from "./constants";

export const PREFUND_AMOUNT = "0x38D7EA4C68000"; // 0.001ETH

const provider = new Provider({ rpc: { nodeUrl: mainnet_addr }, sequencer: { baseUrl: mainnet_addr } });
const rpc_addr = getRPCUrl();
const provider = new Provider({
rpc: { nodeUrl: rpc_addr! },
sequencer: { baseUrl: rpc_addr! },
});

export const useBurner = () => {
const { refresh } = useConnectors();
Expand All @@ -31,7 +35,8 @@ export const useBurner = () => {
const [isSettingPermissions, setIsSettingPermissions] = useState(false);
const [isToppingUp, setIsToppingUp] = useState(false);
const [isWithdrawing, setIsWithdrawing] = useState(false);
const { gameContract, lordsContract } = useContracts();
const { gameContract, lordsContract, ethContract } = useContracts();
const arcadeClassHash = getArcadeClassHash();

// init
useEffect(() => {
Expand Down Expand Up @@ -121,6 +126,30 @@ export const useBurner = () => {
[walletAccount]
);

const prefundAccount = async (address: string, account: AccountInterface) => {
try {
const { transaction_hash } = await account.execute({
contractAddress: ethContract?.address ?? "",
entrypoint: "transfer",
calldata: CallData.compile([address, PREFUND_AMOUNT, "0x0"]),
});

const result = await account.waitForTransaction(transaction_hash, {
retryInterval: 1000,
successStates: [TransactionFinalityStatus.ACCEPTED_ON_L2],
});

if (!result) {
throw new Error("Transaction did not complete successfully.");
}

return result;
} catch (error) {
console.error(error);
throw error;
}
};

const create = useCallback(async () => {
setIsDeploying(true);
const privateKey = stark.randomAddress();
Expand All @@ -137,7 +166,7 @@ export const useBurner = () => {

const address = hash.calculateContractAddressFromHash(
publicKey,
process.env.NEXT_PUBLIC_GOERLI_ARCADE_ACCOUNT_CLASS_HASH!,
arcadeClassHash!,
constructorAACalldata,
0
);
Expand All @@ -155,7 +184,7 @@ export const useBurner = () => {
transaction_hash: deployTx,
contract_address: accountAAFinalAdress,
} = await burner.deployAccount({
classHash: process.env.NEXT_PUBLIC_GOERLI_ARCADE_ACCOUNT_CLASS_HASH!,
classHash: arcadeClassHash!,
constructorCalldata: constructorAACalldata,
contractAddress: address,
addressSalt: publicKey,
Expand Down Expand Up @@ -216,7 +245,7 @@ export const useBurner = () => {
entrypoint: "update_whitelisted_calls",
calldata: [
"1",
process.env.NEXT_PUBLIC_ETH_CONTRACT_ADDRESS!,
ethContract?.address ?? "",
selector.getSelectorFromName("transfer"),
"1",
],
Expand All @@ -236,7 +265,7 @@ export const useBurner = () => {
try {
setIsToppingUp(true);
const { transaction_hash } = await account.execute({
contractAddress: process.env.NEXT_PUBLIC_ETH_CONTRACT_ADDRESS!,
contractAddress: ethContract?.address ?? "",
entrypoint: "transfer",
calldata: CallData.compile([address, PREFUND_AMOUNT, "0x0"]),
});
Expand Down Expand Up @@ -276,7 +305,7 @@ export const useBurner = () => {
);

const call = {
contractAddress: process.env.NEXT_PUBLIC_ETH_CONTRACT_ADDRESS!,
contractAddress: ethContract?.address ?? "",
entrypoint: "transfer",
calldata: CallData.compile([
masterAccountAddress,
Expand All @@ -295,7 +324,7 @@ export const useBurner = () => {
BigInt(balance) - estimatedFee * (BigInt(11) / BigInt(10));

const { transaction_hash } = await account.execute({
contractAddress: process.env.NEXT_PUBLIC_ETH_CONTRACT_ADDRESS!,
contractAddress: ethContract?.address ?? "",
entrypoint: "transfer",
calldata: CallData.compile([
masterAccountAddress,
Expand Down Expand Up @@ -406,27 +435,3 @@ export const useBurner = () => {
listConnectors,
};
};

const prefundAccount = async (address: string, account: AccountInterface) => {
try {
const { transaction_hash } = await account.execute({
contractAddress: process.env.NEXT_PUBLIC_ETH_CONTRACT_ADDRESS!,
entrypoint: "transfer",
calldata: CallData.compile([address, PREFUND_AMOUNT, "0x0"]),
});

const result = await account.waitForTransaction(transaction_hash, {
retryInterval: 1000,
successStates: [TransactionFinalityStatus.ACCEPTED_ON_L2],
});

if (!result) {
throw new Error("Transaction did not complete successfully.");
}

return result;
} catch (error) {
console.error(error);
throw error;
}
};
4 changes: 2 additions & 2 deletions ui/src/app/lib/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export const braavosConnector = new InjectedConnector({

function argentWebWalletUrl() {
switch (process.env.NEXT_PUBLIC_NETWORK) {
case "dev":
case "goerli":
return "https://web.hydrogen.argent47.net";
case "production":
case "mainnet":
return "https://web.argent.xyz/";
default:
return "https://web.hydrogen.argent47.net";
Expand Down
9 changes: 9 additions & 0 deletions ui/src/app/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ export function getContracts() {
}
}

export function getArcadeClassHash() {
switch (process.env.NEXT_PUBLIC_NETWORK) {
case "goerli":
return process.env.NEXT_PUBLIC_GOERLI_ARCADE_CLASS_HASH;
case "mainnet":
return process.env.NEXT_PUBLIC_MAINNET_ARCADE_CLASS_HASH;
}
}

export function getAppUrl() {
switch (process.env.NEXT_PUBLIC_NETWORK) {
case "goerli":
Expand Down

0 comments on commit 8d39bb7

Please sign in to comment.