Skip to content

Commit

Permalink
Merge pull request #112 from BibliothecaDAO/main
Browse files Browse the repository at this point in the history
Bridge simplification and fixes
  • Loading branch information
RedBeardEth authored Feb 5, 2024
2 parents 8fa22b8 + 49652e9 commit 8b202e2
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 371 deletions.
4 changes: 2 additions & 2 deletions apps/auth-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@auth/core": "^0.24.0"
"@auth/core": "^0.25.1"
},
"devDependencies": {
"@realms-world/eslint-config": "workspace:*",
Expand All @@ -20,7 +20,7 @@
"@realms-world/tsconfig": "workspace:*",
"eslint": "^8.56.0",
"nitropack": "^2.8.1",
"prettier": "^3.2.4",
"prettier": "^3.2.5",
"typescript": "^5.3.3"
},
"eslintConfig": {
Expand Down
24 changes: 12 additions & 12 deletions apps/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"@emotion/styled": "^11.11.0",
"@mdx-js/loader": "^3.0.0",
"@mdx-js/react": "^3.0.0",
"@mui/material": "^5.15.6",
"@mui/styles": "^5.15.6",
"@mui/material": "^5.15.7",
"@mui/styles": "^5.15.7",
"@next/mdx": "^14.1.0",
"@rainbow-me/rainbowkit": "2.0.0-beta.2",
"@realms-world/api": "workspace:*",
Expand All @@ -31,16 +31,16 @@
"@realms-world/styles": "workspace:*",
"@realms-world/ui": "workspace:*",
"@realms-world/utils": "workspace:*",
"@reservoir0x/reservoir-kit-ui": "^1.25.1",
"@reservoir0x/reservoir-kit-ui": "^1.25.2",
"@starknet-react/chains": "^0.1.6",
"@starknet-react/core": "^2.2.4",
"@starkware-industries/commons-js-enums": "^1.2.0",
"@starkware-industries/commons-js-utils": "^1.2.2",
"@svgr/webpack": "^8.1.0",
"@t3-oss/env-nextjs": "^0.8.0",
"@tanstack/react-query": "5.18.0",
"@tanstack/react-query-devtools": "5.18.0",
"@tanstack/react-query-next-experimental": "5.18.0",
"@tanstack/react-query": "5.18.1",
"@tanstack/react-query-devtools": "5.18.1",
"@tanstack/react-query-next-experimental": "5.18.1",
"@trpc/client": "next",
"@trpc/react-query": "next",
"@trpc/server": "next",
Expand All @@ -49,16 +49,16 @@
"embla-carousel-autoplay": "8.0.0-rc22",
"embla-carousel-react": "8.0.0-rc22",
"framer-motion": "^11.0.3",
"inngest": "^3.12.0",
"inngest": "^3.13.0",
"lodash": "4.17.21",
"lucide-react": "^0.319.0",
"lucide-react": "^0.321.0",
"next": "^14.1.0",
"react": "18.2.0",
"react-dom": "18.2.0",
"starknet": "^5.24.3",
"starknetkit": "^1.1.3",
"superjson": "2.2.1",
"viem": "^2.6.1",
"viem": "^2.7.3",
"wagmi": "2.5.5",
"zod": "^3.22.4"
},
Expand All @@ -68,12 +68,12 @@
"@realms-world/tailwind-config": "workspace:*",
"@realms-world/tsconfig": "workspace:*",
"@types/mdx": "^2.0.11",
"@types/node": "^20.11.13",
"@types/react": "^18.2.48",
"@types/node": "^20.11.16",
"@types/react": "^18.2.52",
"@types/react-dom": "^18.2.18",
"dotenv-cli": "^7.3.0",
"eslint": "^8.56.0",
"prettier": "^3.2.4",
"prettier": "^3.2.5",
"tailwindcss": "3.4.1",
"typescript": "^5.3.3"
},
Expand Down
38 changes: 28 additions & 10 deletions apps/nextjs/src/app/bridge/Transfer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { useState } from "react";
import Link from "next/link";
import { EthereumLoginButton } from "@/app/_components/wallet/EthereumLoginButton";
import { StarknetLoginButton } from "@/app/_components/wallet/StarknetLoginButton";
import { useTransferToL1 } from "@/hooks/useTransferToL1";
import { ActionType, TransferToL1Steps } from "@/constants/transferSteps";
import { useWriteInitiateWithdrawLords } from "@/hooks/bridge/useWriteInitiateWithdrawLords";
import { useTransfer } from "@/hooks/useTransfer";
//import { useTransferToL1 } from "@/hooks/useTransferToL1";
import { useTransferToL2 } from "@/hooks/useTransferToL2";
import EthereumLogo from "@/icons/ethereum.svg";
import LordsIcon from "@/icons/lords.svg";
Expand All @@ -19,20 +22,35 @@ import { useWalletsProviderContext } from "../providers/WalletsProvider";
import { TokenBalance } from "./TokenBalance";

export const Transfer = ({ action }: { action: string }) => {
const { address: l1Account } = useL1Account();
const { address: l2Account } = useAccount();
const { address: l1Address } = useL1Account();
const { address: l2Address } = useAccount();
// const [toastOpen, setToastOpen] = useState(false);
const [amount, setAmount] = useState("0");
const [amount, setAmount] = useState<string>("0");
const { balances, l2loading } = useWalletsProviderContext();
const { writeAsync: iniateWithdrawal } = useWriteInitiateWithdrawLords({
amount,
});
const { handleProgress, handleData, handleError } =
useTransfer(TransferToL1Steps);

const transferToL1 = useTransferToL1();
const transferToL2 = useTransferToL2();

const onTransferClick = async () => {
if (action == "withdraw") {
transferToL1(amount);
const withdrawHash = await iniateWithdrawal();
if (withdrawHash) {
handleData({
type: ActionType.TRANSFER_TO_L1,
sender: l2Address,
recipient: l1Address,
name: "Lords",
symbol: "LORDS",
amount: amount,
l2hash: withdrawHash?.transaction_hash,
});
}
} else {
transferToL2(amount);
transferToL2({ amount, l2Address });
}
};

Expand Down Expand Up @@ -137,9 +155,9 @@ export const Transfer = ({ action }: { action: string }) => {
: renderL2Network(action)}
</div>
<div className="flex gap-x-4 p-2">
{!l1Account && <EthereumLoginButton />}
{!l2Account && <StarknetLoginButton />}
{l1Account && l2Account && (
{!l1Address && <EthereumLoginButton />}
{!l2Address && <StarknetLoginButton />}
{l1Address && l2Address && (
<Button
className="mt-2 w-full"
onClick={() => onTransferClick()}
Expand Down
1 change: 0 additions & 1 deletion apps/nextjs/src/constants/env.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ChainType } from "@starkware-industries/commons-js-enums";
import { evaluate } from "@starkware-industries/commons-js-utils";

import { ChainId } from "@realms-world/constants";
Expand Down
111 changes: 25 additions & 86 deletions apps/nextjs/src/hooks/bridge/useWriteDepositLords.ts
Original file line number Diff line number Diff line change
@@ -1,92 +1,31 @@
"use client";

import type { L1WriteBaseParams } from "@/types/L1WriteBaseParams";
import type { Config } from "@wagmi/core";
import type { ContractFunctionArgs, WriteContractReturnType } from "viem";
import type { UseWriteContractReturnType } from "wagmi";
import type { WriteContractVariables } from "wagmi/query";
import { useCallback } 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 { useAccount as useL1Account, useWriteContract } from "wagmi";

import type { SupportedL1ChainId } from "@realms-world/constants/src/Chains";
import { SUPPORTED_L1_CHAIN_ID } from "@/constants/env";
import { tokens } from "@/constants/tokens";
import { useAccount as useL2Account } from "@starknet-react/core";
import { useWriteContract } from "wagmi";

const FUNCTION = "deposit";

export type WriteDepositLordsParameters<
config extends Config,
chainId extends SupportedL1ChainId,
> = L1WriteBaseParams<
typeof L1_BRIDGE_ABI,
typeof FUNCTION,
config,
chainId
> & {
args: {
amount: number;
l2Address: string;
};
} & { l2ChainId: number };

export type UseWriteDepositLordsReturnType<
config extends Config,
context = unknown,
> = Omit<
UseWriteContractReturnType<
WriteDepositLordsParameters<config, SupportedL1ChainId>
>,
"write" | "writeAsync"
> & {
writeDepositERC20Async: UseWriteContractReturnType<
config,
context
>["writeContractAsync"];
};

export function useWriteDepositLords<config extends Config, context = unknown>(
{
args,
l2ChainId,
...rest
}: {
args: {
amount: number;
l2Address: string;
};
l2ChainId: number;
rest: any;
},
options: any,
): Promise<WriteContractReturnType> {
const l1BridgeAddress =
tokens.L1.LORDS.bridgeAddress?.[ChainType.L1[NETWORK_NAME]];
const l2BridgeAddress =
tokens.L2.LORDS.bridgeAddress?.[ChainType.L2[NETWORK_NAME]];

const { error, writeContractAsync, ...writeReturn } = useWriteContract();
const { address: l1Address } = useL1Account();

const writeDepost = writeContractAsync(
{
chainId: 1,
address: tokens.L1.LORDS.bridgeAddress?.[
ChainType.L1[NETWORK_NAME]
] as `0x${string}`,
abi: L1_BRIDGE_ABI,
functionName: FUNCTION,
args: [args.amount, args.l2Address, BigInt(1)],
account: l1Address,
...rest,
} as unknown as WriteContractVariables<
typeof L1_BRIDGE_ABI,
typeof FUNCTION,
ContractFunctionArgs<typeof L1_BRIDGE_ABI, "nonpayable", typeof FUNCTION>,
config,
config["chains"][number]["id"]
>,
options,
export function useWriteDepositLords() {
const { writeContractAsync, ...writeReturn } = useWriteContract();

// if (!l2Address) throw new Error("Missing L2 Address");

const writeAsync = useCallback(
async ({ amount, l2Address }: { amount: bigint; l2Address: string }) => {
console.log(l2Address, amount);

return await writeContractAsync({
address: tokens.L1.LORDS.bridgeAddress?.[
SUPPORTED_L1_CHAIN_ID
] as `0x${string}`,
abi: L1_BRIDGE_ABI,
functionName: FUNCTION,
args: [amount, BigInt(l2Address), BigInt(1)],
});
},
[writeContractAsync],
);

return { writeDepost, ...writeReturn };
return { writeAsync, ...writeReturn };
}
93 changes: 28 additions & 65 deletions apps/nextjs/src/hooks/bridge/useWriteFinalizeWithdrawLords.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,38 @@
"use client";

import type { L1WriteBaseParams } from "@/types/L1WriteBaseParams";
import type { Config } from "@wagmi/core";
import type { ContractFunctionArgs, WriteContractReturnType } from "viem";
import type { WriteContractVariables } from "wagmi/query";
import { useCallback } from "react";
import { StarknetBridgeLords as L1_BRIDGE_ABI } from "@/abi/L1/StarknetBridgeLords";
import { SUPPORTED_L1_CHAIN_ID } from "@/constants/env";
import { tokens } from "@/constants/tokens";
import { useAccount as useL1Account, useWriteContract } from "wagmi";

import type { SupportedL1ChainId } from "@realms-world/constants/src/Chains";
import { useWriteContract } from "wagmi";

const FUNCTION = "withdraw";

export type WriteDepositLordsParameters<
config extends Config,
chainId extends SupportedL1ChainId,
> = L1WriteBaseParams<
typeof L1_BRIDGE_ABI,
typeof FUNCTION,
config,
chainId
> & {
args: {
amount: number;
l2Address: string;
};
} & { l2ChainId: number };

export function useWriteFinalizeWithdrawLords<
config extends Config,
context = unknown,
>(
{
args,

...rest
}: {
args: {
amount: number;
l2Address: string;
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
rest: any;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
options: any,
): Promise<WriteContractReturnType> {
const { writeContractAsync } = useWriteContract();
const { address: l1Address } = useL1Account();

const writeDepost = writeContractAsync(
{
chainId: 1,
address: tokens.L1.LORDS.bridgeAddress?.[
SUPPORTED_L1_CHAIN_ID
] as `0x${string}`,
abi: L1_BRIDGE_ABI,
functionName: FUNCTION,
args: [args.amount, args.l2Address, BigInt(1)],
account: l1Address,
...rest,
} as unknown as WriteContractVariables<
typeof L1_BRIDGE_ABI,
typeof FUNCTION,
ContractFunctionArgs<typeof L1_BRIDGE_ABI, "nonpayable", typeof FUNCTION>,
config,
config["chains"][number]["id"]
>,
options,
export function useWriteFinalizeWithdrawLords() {
const { writeContractAsync, ...writeReturn } = useWriteContract();

// if (!l2Address) throw new Error("Missing L2 Address");

const writeAsync = useCallback(
async ({
amount,
l1Address,
}: {
amount: bigint;
l1Address: `0x${string}`;
}) => {
console.log(l1Address, amount);

return await writeContractAsync({
address: tokens.L1.LORDS.bridgeAddress?.[
SUPPORTED_L1_CHAIN_ID
] as `0x${string}`,
abi: L1_BRIDGE_ABI,
functionName: FUNCTION,
args: [amount, l1Address],
});
},
[writeContractAsync],
);

return writeDepost;
return { writeAsync, ...writeReturn };
}
Loading

0 comments on commit 8b202e2

Please sign in to comment.