From 93e3af89eef216a74651bc712113d4a79648acd8 Mon Sep 17 00:00:00 2001
From: RedBeardEth <90423049+RedBeardEth@users.noreply.github.com>
Date: Fri, 19 Jan 2024 17:12:55 +1100
Subject: [PATCH] fix bridge
---
.../[id]/(list)/AttributesDropdown.tsx | 4 +
.../src/app/staking/StakingContainer.tsx | 76 ++++---------------
apps/nextjs/src/hooks/useBridgeContract.ts | 39 +++++-----
apps/nextjs/src/hooks/useTokenContract.ts | 19 ++---
apps/nextjs/src/hooks/useTransferToL1.ts | 11 ++-
apps/nextjs/src/hooks/useTransferToL2.ts | 20 +++--
6 files changed, 71 insertions(+), 98 deletions(-)
diff --git a/apps/nextjs/src/app/collection/[id]/(list)/AttributesDropdown.tsx b/apps/nextjs/src/app/collection/[id]/(list)/AttributesDropdown.tsx
index 358bef52..b8233c1d 100644
--- a/apps/nextjs/src/app/collection/[id]/(list)/AttributesDropdown.tsx
+++ b/apps/nextjs/src/app/collection/[id]/(list)/AttributesDropdown.tsx
@@ -41,6 +41,10 @@ export const AttributesDropdown = ({
);
const finalAttributes = attributesFetched?.items ?? attributes;
+ if (!finalAttributes.length) {
+ return null;
+ }
+
return (
{
- refetchCarrackApprovedData(), refetchGalleonApprovedData();
+ refetchGalleonApprovedData();
}, [isSuccess]);
- const isApproved =
- shipType === "galleon" ? isGalleonApprovedData : isCarrackApprovedData;
+ const isApproved = isGalleonApprovedData;
const onApproveClick = async () => {
- const approvalFunction =
- shipType === "galleon"
- ? approveGalleon({
- address: realmsAddress as `0x${string}`,
- abi: ERC721,
- functionName: "setApprovalForAll",
- args: [galleonAddress, true],
- })
- : approveCarrack({
- address: realmsAddress as `0x${string}`,
- abi: ERC721,
- functionName: "setApprovalForAll",
- args: [carrackAddress, true],
- });
-
if (!isApproved) {
- await approvalFunction;
+ await approveGalleon({
+ address: realmsAddress as `0x${string}`,
+ abi: ERC721,
+ functionName: "setApprovalForAll",
+ args: [galleonAddress, true],
+ });
}
};
const onButtonClick = async () => {
if (!unstake) {
- const boardingFunction =
- shipType === "galleon"
- ? boardGalleon({
- address: galleonAddress,
- abi: GalleonStaking,
- functionName: "boardShip",
- args: [selectedRealms.map(BigInt)],
- })
- : boardCarrack({
- address: carrackAddress,
- abi: CarrackStaking,
- functionName: "boardShip",
- args: [selectedRealms.map(BigInt)],
- });
- await boardingFunction;
+ await boardGalleon({
+ address: galleonAddress,
+ abi: GalleonStaking,
+ functionName: "boardShip",
+ args: [selectedRealms.map(BigInt)],
+ });
} else {
const exitFunction =
shipType === "galleon"
@@ -461,7 +430,6 @@ const StakingModal = ({
isGalleonApproveLoading ||
isExitGalleonPending ||
isExitCarrackPending ||
- isBoardCarrackPending ||
isBoardGalleonPending;
const onSelectRealms = (realms: any) => {
@@ -514,9 +482,7 @@ const StakingModal = ({
) : (
The Galleon
-
- Rewards: 49x $LORDS per epoch (a bonus of 12% over Carrack).
-
+
Rewards: 49x $LORDS per epoch.
Board The Galleon
- The Galleon
-
- Rewards: 43.75x $LORDS per epoch.
-
-
- Claim: After each epoch (Realm must be staked for full epoch).
-
-
)}
diff --git a/apps/nextjs/src/hooks/useBridgeContract.ts b/apps/nextjs/src/hooks/useBridgeContract.ts
index f711e2f1..93a412a2 100644
--- a/apps/nextjs/src/hooks/useBridgeContract.ts
+++ b/apps/nextjs/src/hooks/useBridgeContract.ts
@@ -1,14 +1,13 @@
"use client";
import { useEffect, useMemo, useState } from "react";
-import { StarknetBridgeLords as L1_BRIDGE_ABI } from "@/abi/L1/StarknetBridgeLords";
import { NETWORK_NAME } from "@/constants/env";
import { ChainType, tokens } from "@/constants/tokens";
import { useContractWrite as useL2ContractWrite } from "@starknet-react/core";
import { parseEther } from "viem";
import {
useAccount as useL1Account,
- useContractWrite as useL1ContractWrite,
+ useWriteContract as useL1ContractWrite,
useWaitForTransactionReceipt,
} from "wagmi";
@@ -20,37 +19,30 @@ export const useBridgeContract = () => {
const l2BridgeAddress =
tokens.L2.LORDS.bridgeAddress?.[ChainType.L2[NETWORK_NAME]];
- /* const {
- writeAsync: deposit,
+ const {
+ writeContractAsync: deposit,
data: depositData,
error: depositError,
- } = useL1ContractWrite({
- address: l1BridgeAddress as `0x${string}`,
- abi: L1_BRIDGE_ABI,
- functionName: "deposit",
- });
+ } = useL1ContractWrite();
const {
data: depositReceipt,
isLoading,
status: depositTxStatus,
isSuccess: depositIsSuccess,
isError: depostTxError,
- } = useWaitForTransaction({
- hash: depositData?.hash,
+ } = useWaitForTransactionReceipt({
+ hash: depositData,
});
- const { writeAsync: withdraw, error: withdrawError } = useL1ContractWrite({
- address: l1BridgeAddress as `0x${string}`,
- abi: L1_BRIDGE_ABI,
- functionName: "withdraw",
- });
+ const { writeContractAsync: withdraw, error: withdrawError } =
+ useL1ContractWrite();
const {
data: withdrawReceipt,
isSuccess: withdrawIsSuccess,
isError: withdrawTxError,
- } = useWaitForTransaction({
- hash: depositData?.hash,
- });*/
+ } = useWaitForTransactionReceipt({
+ hash: depositData,
+ });
const [amount, setAmount] = useState
();
@@ -82,5 +74,14 @@ export const useBridgeContract = () => {
calls,
initiateWithdraw,
withdrawHash,
+ withdraw,
+ withdrawIsSuccess,
+ withdrawReceipt,
+ withdrawError,
+ deposit,
+ depositIsSuccess,
+ depositError,
+ depositTxStatus,
+ depositReceipt,
};
};
diff --git a/apps/nextjs/src/hooks/useTokenContract.ts b/apps/nextjs/src/hooks/useTokenContract.ts
index a15cc098..4a3a31c4 100644
--- a/apps/nextjs/src/hooks/useTokenContract.ts
+++ b/apps/nextjs/src/hooks/useTokenContract.ts
@@ -6,10 +6,9 @@ import { ChainType, tokens } from "@/constants/tokens";
import { isEth } from "@/utils/utils";
import { useAccount as useL2Account } from "@starknet-react/core";
import {
- useBalance,
- useContractRead,
useAccount as useL1Account,
- useContractWrite as useL1ContractWrite,
+ useWriteContract as useL1ContractWrite,
+ useReadContract,
} from "wagmi";
export const useTokenContractAPI = (
@@ -35,15 +34,12 @@ export const useTokenContractAPI = (
};
const {
- writeAsync: approve,
+ writeContractAsync: approve,
data: approveHash,
- isLoading: approveWriteLoading,
- } = useL1ContractWrite({
- ...l1ERC20Contract,
- functionName: "approve",
- });
+ isPending: approveWriteLoading,
+ } = useL1ContractWrite();
- const { data: allowance /*, isError, isLoading */ } = useContractRead({
+ const { data: allowance /*, isError, isLoading */ } = useReadContract({
...l1ERC20Contract,
functionName: "allowance",
args: [
@@ -52,9 +48,10 @@ export const useTokenContractAPI = (
? l1Token.bridgeAddress[ChainType.L1[NETWORK_NAME]]
: spender) as `0x${string}`,
],
- enabled: !!(l1Account && spender),
+ //enabled: !!(l1Account && spender),
});
return {
+ l1ERC20Contract,
approve,
approveWriteLoading,
approveHash,
diff --git a/apps/nextjs/src/hooks/useTransferToL1.ts b/apps/nextjs/src/hooks/useTransferToL1.ts
index bfdb161d..b8ab3975 100644
--- a/apps/nextjs/src/hooks/useTransferToL1.ts
+++ b/apps/nextjs/src/hooks/useTransferToL1.ts
@@ -1,7 +1,10 @@
"use client";
import { useCallback, useEffect, useState } from "react";
+import { StarknetBridgeLords as L1_BRIDGE_ABI } from "@/abi/L1/StarknetBridgeLords";
import { useTransferLog } from "@/app/providers/TransferLogProvider";
+import { NETWORK_NAME } from "@/constants/env";
+import { ChainType, tokens } from "@/constants/tokens";
import {
ActionType,
CompleteTransferToL1Steps,
@@ -104,6 +107,9 @@ export const useTransferToL1 = () => {
);
};
+const l1BridgeAddress =
+ tokens.L1.LORDS.bridgeAddress?.[ChainType.L1[NETWORK_NAME]];
+
export const useCompleteTransferToL1 = () => {
const { withdraw, withdrawIsSuccess, withdrawReceipt, withdrawError } =
useBridgeContract();
@@ -156,7 +162,10 @@ export const useCompleteTransferToL1 = () => {
),
);
console.log("Calling withdraw", withdrawalEvents.amount);
- const { hash } = await withdraw({
+ const hash = await withdraw({
+ address: l1BridgeAddress as `0x${string}`,
+ abi: L1_BRIDGE_ABI,
+ functionName: "withdraw",
args: [withdrawalEvents[0].amount, l1Account!],
});
onTransactionHash(withdrawError, hash);
diff --git a/apps/nextjs/src/hooks/useTransferToL2.ts b/apps/nextjs/src/hooks/useTransferToL2.ts
index fa4af6ce..e2e1f415 100644
--- a/apps/nextjs/src/hooks/useTransferToL2.ts
+++ b/apps/nextjs/src/hooks/useTransferToL2.ts
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useState } from "react";
+import { StarknetBridgeLords as L1_BRIDGE_ABI } from "@/abi/L1/StarknetBridgeLords";
import { useTransferLog } from "@/app/providers/TransferLogProvider";
import { NETWORK_NAME } from "@/constants/env";
import { ChainType, tokens } from "@/constants/tokens";
@@ -29,20 +30,20 @@ export const stepOf = (step: any, steps: any) => {
};
export const useTransferToL2 = () => {
+ const l2BridgeAddress =
+ tokens.L2.LORDS.bridgeAddress?.[ChainType.L2[NETWORK_NAME]];
const [amount, setAmount] = useState("");
//onst [trackInitiated, trackSuccess, trackError, trackReject] = useTransferToL2Tracking();
const {
deposit,
depositIsSuccess,
- error: depositError,
+ depositError,
depositTxStatus,
depositReceipt,
} = useBridgeContract();
- const { allowance, approve, approveHash } = useTokenContractAPI(
- "LORDS",
- true,
- );
+ const { allowance, approve, approveHash, l1ERC20Contract } =
+ useTokenContractAPI("LORDS", true);
const {
data,
isError,
@@ -123,9 +124,12 @@ export const useTransferToL2 = () => {
stepOf(TransferStep.CONFIRM_TX, TransferToL2Steps),
),
);
- const { hash } = await deposit({
+ const hash = await deposit({
+ address: l1BridgeAddress,
+ abi: L1_BRIDGE_ABI,
+ functionName: "deposit",
args: [parseUnits(amount, 18), BigInt(l2Account || "0x"), BigInt(1)],
- value: BigInt(1),
+ value: parseEther("0.000000000001"),
});
onTransactionHash(depositError, hash, amount);
};
@@ -162,6 +166,8 @@ export const useTransferToL2 = () => {
{ amount, l1BridgeAddress },
);
await approve({
+ ...l1ERC20Contract,
+ functionName: "approve",
args: [l1BridgeAddress, parseEther(amount)],
});
}