-
Notifications
You must be signed in to change notification settings - Fork 68
fix: zksyncos withdrawals and finalization #296
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
base: zksyncos-alpha
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,19 @@ | ||
| import { createEthersClient, createEthersSdk } from "@dutterbutter/zksync-sdk/ethers"; | ||
| import { estimateGas } from "@wagmi/core"; | ||
| import { AbiCoder } from "ethers"; | ||
| import { encodeFunctionData } from "viem"; | ||
| import { encodeFunctionData, type Address } from "viem"; | ||
|
|
||
| import { wagmiConfig } from "@/data/wagmi"; | ||
|
|
||
| import type { Token, TokenAmount } from "@/types"; | ||
| import type { BigNumberish, ethers } from "ethers"; | ||
| import type { Provider } from "zksync-ethers"; | ||
| import type { Address } from "zksync-ethers/build/types"; | ||
|
|
||
| export type FeeEstimationParams = { | ||
| type: "transfer" | "withdrawal"; | ||
| from: string; | ||
| to: string; | ||
| tokenAddress: string; | ||
| from: Address; | ||
| to: Address; | ||
| tokenAddress: Address; | ||
| isNativeToken: boolean | null; | ||
| assetId?: string | null; | ||
| amount: string; | ||
|
|
@@ -25,6 +25,8 @@ export default ( | |
| tokens: Ref<{ [tokenSymbol: string]: Token } | undefined>, | ||
| balances: Ref<TokenAmount[]> | ||
| ) => { | ||
| const { getL1VoidSigner } = useZkSyncWalletStore(); | ||
|
|
||
| let params: FeeEstimationParams | undefined; | ||
|
|
||
| const gasLimit = ref<bigint | undefined>(); | ||
|
|
@@ -115,15 +117,15 @@ export default ( | |
|
|
||
| const [price, limit] = await Promise.all([ | ||
| retry(() => provider.getGasPrice()), | ||
| retry(() => { | ||
| retry(async () => { | ||
| const isCustomBridgeToken = !!token?.l2BridgeAddress; | ||
| if (isCustomBridgeToken) { | ||
| return getCustomGasLimit({ | ||
| from: params!.from, | ||
| to: params!.to, | ||
| token: params!.tokenAddress, | ||
| amount: tokenBalance, | ||
| bridgeAddress: token?.l2BridgeAddress, | ||
| bridgeAddress: token?.l2BridgeAddress as Address, | ||
| }); | ||
| } else if (params!.isNativeToken && params!.assetId) { | ||
| const assetData = AbiCoder.defaultAbiCoder().encode( | ||
|
|
@@ -152,13 +154,24 @@ export default ( | |
| args: [params!.assetId, assetData], | ||
| }), | ||
| }); | ||
| } else { | ||
| return provider[params!.type === "transfer" ? "estimateGasTransfer" : "estimateGasWithdraw"]({ | ||
| } else if (params!.type === "transfer") { | ||
| return provider.estimateGasTransfer({ | ||
| from: params!.from, | ||
| to: params!.to, | ||
| token: params!.tokenAddress, | ||
| amount: tokenBalance, | ||
| }); | ||
| } else { | ||
| const signer = await getL1VoidSigner(true); | ||
| const client = createEthersClient({ l1: signer.provider, l2: signer.providerL2, signer }); | ||
| const sdk = createEthersSdk(client); | ||
|
|
||
| const quote = await sdk.withdrawals.quote({ | ||
| to: params!.to, | ||
| token: params!.tokenAddress, | ||
| amount: 1n, // TODO: estimation fails if we pass actual user balance | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would you pass user balance here and not the provided amount the user wants to withdrawal?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it worked on previous protocol version, we were basically estimating max possible fee by passing max possible amount. This would allow to not have to recalculate on amount value change (slightly better UX, less waiting). But not it fails because it needs value to be (balance - total fee). |
||
| }); | ||
| return quote.fees.gasLimit; | ||
| } | ||
| }), | ||
| ]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,44 +1,22 @@ | ||
| import { useMemoize } from "@vueuse/core"; | ||
| import { Wallet, typechain } from "zksync-ethers"; | ||
| import IL1Nullifier from "zksync-ethers/abi/IL1Nullifier.json"; | ||
|
|
||
| import { L1_BRIDGE_ABI } from "@/data/abis/l1BridgeAbi"; | ||
| import { customBridgeTokens } from "@/data/customBridgeTokens"; | ||
| import { createEthersClient, createEthersSdk, createFinalizationServices } from "@dutterbutter/zksync-sdk/ethers"; | ||
|
|
||
| import { useSentryLogger } from "../useSentryLogger"; | ||
|
|
||
| import type { Hash } from "@/types"; | ||
| import type { Address } from "viem"; | ||
| import type { FinalizeWithdrawalParams } from "zksync-ethers/build/types"; | ||
| import type { Hash } from "viem"; | ||
|
|
||
| export default (transactionInfo: ComputedRef<TransactionInfo>) => { | ||
| const status = ref<"not-started" | "processing" | "waiting-for-signature" | "sending" | "done">("not-started"); | ||
| const error = ref<Error | undefined>(); | ||
| const transactionHash = ref<Hash | undefined>(); | ||
| const onboardStore = useOnboardStore(); | ||
| const providerStore = useZkSyncProviderStore(); | ||
| const walletStore = useZkSyncWalletStore(); | ||
| const tokensStore = useZkSyncTokensStore(); | ||
| const { isCorrectNetworkSet } = storeToRefs(onboardStore); | ||
| const { ethToken } = storeToRefs(tokensStore); | ||
| const { captureException } = useSentryLogger(); | ||
|
|
||
| const retrieveBridgeAddresses = useMemoize(() => | ||
| providerStore.requestProvider().then((provider) => provider.getDefaultBridgeAddresses()) | ||
| ); | ||
| const retrieveL1NullifierAddress = useMemoize(async () => { | ||
| const providerL1 = await walletStore.getL1VoidSigner(); | ||
| return await typechain.IL1AssetRouter__factory.connect( | ||
| ( | ||
| await retrieveBridgeAddresses() | ||
| ).sharedL1, | ||
| providerL1 | ||
| ).L1_NULLIFIER(); | ||
| }); | ||
|
|
||
| const gasLimit = ref<bigint | undefined>(); | ||
| const gasPrice = ref<bigint | undefined>(); | ||
| const finalizeWithdrawalParams = ref<FinalizeWithdrawalParams | undefined>(); | ||
|
|
||
| const totalFee = computed(() => { | ||
| if (!gasLimit.value || !gasPrice.value) return undefined; | ||
|
|
@@ -48,101 +26,32 @@ export default (transactionInfo: ComputedRef<TransactionInfo>) => { | |
| return ethToken.value; | ||
| }); | ||
|
|
||
| const getFinalizationParams = async () => { | ||
| const provider = await providerStore.requestProvider(); | ||
| const wallet = new Wallet( | ||
| // random private key cause we don't care about actual signer | ||
| // finalizeWithdrawalParams method only exists on Wallet class | ||
| "0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110", | ||
| provider | ||
| ); | ||
| return await wallet.getFinalizeWithdrawalParams(transactionInfo.value.transactionHash); | ||
| }; | ||
|
|
||
| const getTransactionParams = async () => { | ||
| finalizeWithdrawalParams.value = await getFinalizationParams(); | ||
| const provider = await providerStore.requestProvider(); | ||
| const chainId = BigInt(await provider.getNetwork().then((n) => n.chainId)); | ||
| const p = finalizeWithdrawalParams.value!; | ||
|
|
||
| // Check if this is a custom bridge withdrawal | ||
| // First check if the token already has the bridge address stored | ||
| let l1BridgeAddress = transactionInfo.value.token.l1BridgeAddress; | ||
|
|
||
| // If not, look it up from the custom bridge tokens configuration | ||
| if (!l1BridgeAddress) { | ||
| const { eraNetwork } = storeToRefs(providerStore); | ||
|
|
||
| const customBridgeToken = customBridgeTokens.find( | ||
| (token) => | ||
| token.l2Address.toLowerCase() === transactionInfo.value.token.address.toLowerCase() && | ||
| token.chainId === eraNetwork.value.l1Network?.id | ||
| ); | ||
|
|
||
| l1BridgeAddress = customBridgeToken?.l1BridgeAddress; | ||
| } | ||
|
|
||
| const isCustomBridge = !!l1BridgeAddress; | ||
|
|
||
| if (isCustomBridge) { | ||
| // Use custom bridge finalization | ||
| return { | ||
| address: l1BridgeAddress as Address, | ||
| abi: L1_BRIDGE_ABI, | ||
| account: onboardStore.account.address!, | ||
| functionName: "finalizeWithdrawal", | ||
| args: [ | ||
| BigInt(p.l1BatchNumber ?? 0n), | ||
| BigInt(p.l2MessageIndex), | ||
| Number(p.l2TxNumberInBlock) as number, | ||
| p.message as Hash, | ||
| p.proof as Hash[], | ||
| ], | ||
| } as const; | ||
| } else { | ||
| // Use standard bridge finalization through L1Nullifier | ||
| const finalizeDepositParams = { | ||
| chainId: BigInt(chainId), | ||
| l2BatchNumber: BigInt(p.l1BatchNumber ?? 0n), | ||
| l2MessageIndex: BigInt(p.l2MessageIndex), | ||
| l2Sender: p.sender as Address, | ||
| l2TxNumberInBatch: Number(p.l2TxNumberInBlock), | ||
| message: p.message as Hash, | ||
| merkleProof: p.proof as Hash[], | ||
| }; | ||
|
|
||
| return { | ||
| address: (await retrieveL1NullifierAddress()) as Hash, | ||
| abi: IL1Nullifier, | ||
| account: onboardStore.account.address!, | ||
| functionName: "finalizeDeposit", | ||
| args: [finalizeDepositParams], | ||
| } as const; | ||
| } | ||
| }; | ||
|
|
||
| const { | ||
| inProgress: estimationInProgress, | ||
| error: estimationError, | ||
| execute: estimateFee, | ||
| } = usePromise( | ||
| async () => { | ||
| const l2TxHash = transactionInfo.value!.transactionHash as Hash; | ||
| tokensStore.requestTokens(); | ||
| const publicClient = onboardStore.getPublicClient(); | ||
|
|
||
| const transactionParams = await getTransactionParams(); | ||
| const [price, limit] = await Promise.all([ | ||
| const [price, estimate] = await Promise.all([ | ||
| retry(async () => BigInt((await publicClient.getGasPrice()).toString())), | ||
| retry(async () => { | ||
| return BigInt((await publicClient.estimateContractGas(transactionParams as any)).toString()); | ||
| const signer = await walletStore.getL1VoidSigner(true); | ||
| const client = createEthersClient({ l1: signer.provider, l2: signer.providerL2, signer }); | ||
| const svc = createFinalizationServices(client); | ||
| const { params } = await svc.fetchFinalizeDepositParams(l2TxHash); | ||
|
|
||
| return svc.estimateFinalization(params); | ||
| }), | ||
| ]); | ||
|
|
||
| gasPrice.value = price; | ||
| gasLimit.value = limit; | ||
| gasLimit.value = estimate.gasLimit; | ||
|
|
||
| return { | ||
| transactionParams, | ||
| gasPrice: gasPrice.value, | ||
| gasLimit: gasLimit.value, | ||
| }; | ||
|
|
@@ -158,14 +67,15 @@ export default (transactionInfo: ComputedRef<TransactionInfo>) => { | |
| if (!isCorrectNetworkSet.value) { | ||
| await onboardStore.setCorrectNetwork(); | ||
| } | ||
| const wallet = await onboardStore.getWallet(); | ||
| const { transactionParams, gasLimit, gasPrice } = (await estimateFee())!; | ||
| status.value = "waiting-for-signature"; | ||
| transactionHash.value = await wallet.writeContract({ | ||
| ...(transactionParams as any), | ||
| gasPrice: BigInt(gasPrice.toString()), | ||
| gas: BigInt(gasLimit.toString()), | ||
| }); | ||
| const signer = (await walletStore.getL1Signer())!; | ||
| const client = createEthersClient({ l1: signer.provider, l2: signer.providerL2, signer }); | ||
| const sdk = createEthersSdk(client); | ||
| const transaction = await sdk.withdrawals.finalize(transactionInfo.value!.transactionHash as Hash); | ||
| if (!transaction.receipt) { | ||
| throw new Error("Finalization transaction failed"); | ||
| } | ||
|
Comment on lines
+74
to
+77
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest using the try-variant ( |
||
| transactionHash.value = transaction.receipt?.hash as Hash; | ||
|
|
||
| status.value = "sending"; | ||
| const receipt = await retry(() => | ||
|
|
@@ -186,6 +96,8 @@ export default (transactionInfo: ComputedRef<TransactionInfo>) => { | |
| status.value = "done"; | ||
| return receipt; | ||
| } catch (err) { | ||
| // eslint-disable-next-line no-console | ||
| console.error(err); | ||
| error.value = formatError(err as Error); | ||
| status.value = "not-started"; | ||
| captureException({ | ||
|
|
||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need price? For zksync os chains we are 1559 complaint which does not use price? Is this just here for legacy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, legacy code. I really didn't have time to rewrite everything properly.