Skip to content

Commit

Permalink
fix bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeardEth committed Jan 19, 2024
1 parent a780782 commit 93e3af8
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export const AttributesDropdown = ({
);
const finalAttributes = attributesFetched?.items ?? attributes;

if (!finalAttributes.length) {
return null;
}

return (
<div
className={` ${"hidden"} w-screen overscroll-y-none p-3 sm:block sm:w-24 sm:flex-none sm:overscroll-auto lg:w-72`}
Expand Down
76 changes: 16 additions & 60 deletions apps/nextjs/src/app/staking/StakingContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,6 @@ const StakingModal = ({

const { writeContractAsync: boardGalleon, isPending: isBoardGalleonPending } =
useWriteContract();
const { writeContractAsync: boardCarrack, isPending: isBoardCarrackPending } =
useWriteContract();
const { writeContractAsync: exitGalleon, isPending: isExitGalleonPending } =
useWriteContract();
const { writeContractAsync: exitCarrack, isPending: isExitCarrackPending } =
Expand All @@ -372,15 +370,6 @@ const StakingModal = ({
isPending: isGalleonApproveLoading,
data: approveGalleonData,
} = useWriteContract();
const { writeContractAsync: approveCarrack, data: approveCarrackData } =
useWriteContract();
const { data: isCarrackApprovedData, refetch: refetchCarrackApprovedData } =
useReadContract({
address: realmsAddress as `0x${string}`,
abi: ERC721,
functionName: "isApprovedForAll",
args: [address, carrackAddress],
});
const { data: isGalleonApprovedData, refetch: refetchGalleonApprovedData } =
useReadContract({
address: realmsAddress as `0x${string}`,
Expand All @@ -391,53 +380,33 @@ const StakingModal = ({

const { data: approvedTransactionData, isSuccess } =
useWaitForTransactionReceipt({
hash: approveCarrackData ?? approveGalleonData,
hash: approveGalleonData,
});

useEffect(() => {
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"
Expand All @@ -461,7 +430,6 @@ const StakingModal = ({
isGalleonApproveLoading ||
isExitGalleonPending ||
isExitCarrackPending ||
isBoardCarrackPending ||
isBoardGalleonPending;

const onSelectRealms = (realms: any) => {
Expand Down Expand Up @@ -514,9 +482,7 @@ const StakingModal = ({
) : (
<div className="flex flex-col self-center">
<h5>The Galleon</h5>
<div className="pb-2 text-lg">
Rewards: 49x $LORDS per epoch (a bonus of 12% over Carrack).
</div>
<div className="pb-2 text-lg">Rewards: 49x $LORDS per epoch.</div>
<Alert
message={
"Lords earnt after epoch 35 are locked until the DAO approves the migration to Starknet."
Expand All @@ -530,16 +496,6 @@ const StakingModal = ({
>
Board The Galleon
</Button>
<h5>The Galleon</h5>
<div className="pb-2 text-lg">
Rewards: 43.75x $LORDS per epoch.
</div>
<div className="pb-2 text-lg">
Claim: After each epoch (Realm must be staked for full epoch).
</div>
<Button onClick={() => setShipType("carrack")} variant={"outline"}>
Board The Carrack
</Button>
</div>
)}
</DialogContent>
Expand Down
39 changes: 20 additions & 19 deletions apps/nextjs/src/hooks/useBridgeContract.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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<string | null>();

Expand Down Expand Up @@ -82,5 +74,14 @@ export const useBridgeContract = () => {
calls,
initiateWithdraw,
withdrawHash,
withdraw,
withdrawIsSuccess,
withdrawReceipt,
withdrawError,
deposit,
depositIsSuccess,
depositError,
depositTxStatus,
depositReceipt,
};
};
19 changes: 8 additions & 11 deletions apps/nextjs/src/hooks/useTokenContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand All @@ -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: [
Expand All @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion apps/nextjs/src/hooks/useTransferToL1.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 13 additions & 7 deletions apps/nextjs/src/hooks/useTransferToL2.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -162,6 +166,8 @@ export const useTransferToL2 = () => {
{ amount, l1BridgeAddress },
);
await approve({
...l1ERC20Contract,
functionName: "approve",
args: [l1BridgeAddress, parseEther(amount)],
});
}
Expand Down

0 comments on commit 93e3af8

Please sign in to comment.