Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions composables/zksync/useFee.ts
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;
Expand All @@ -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>();
Expand Down Expand Up @@ -115,15 +117,15 @@ export default (

const [price, limit] = await Promise.all([

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?

Copy link
Member Author

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.

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(
Expand Down Expand Up @@ -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

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
}),
]);
Expand Down
130 changes: 21 additions & 109 deletions composables/zksync/useWithdrawalFinalization.ts
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;
Expand All @@ -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,
};
Expand All @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using the try-variant (tryFinalize) here but more a preference.

transactionHash.value = transaction.receipt?.hash as Hash;

status.value = "sending";
const receipt = await retry(() =>
Expand All @@ -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({
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
"dependencies": {
"@ankr.com/ankr.js": "^0.5.0",
"@chenfengyuan/vue-qrcode": "^2.0.0",
"@dutterbutter/zksync-sdk": "^0.0.11-alpha",
"@dutterbutter/zksync-sdk": "^0.0.12-alpha",
"@lottiefiles/dotlottie-vue": "^0.6.0",
"@sentry/vue": "^9.0.0",
"@vitejs/plugin-vue": "^3.2.0",
Expand Down
Loading
Loading