Skip to content

Commit

Permalink
Merge pull request #47 from ChainSafe/41-at-web3zksyncts-the-method-e…
Browse files Browse the repository at this point in the history
…stimategasl1tol2-needs-the-sent-data-to-be-formatted-similar-to-the-formatting-done-at-estimategas

Fix an issue with estimateGasL1ToL2
  • Loading branch information
Muhammad-Altabba authored Sep 2, 2024
2 parents e61a442 + 5494af3 commit 2e7d928
Showing 1 changed file with 24 additions and 49 deletions.
73 changes: 24 additions & 49 deletions src/web3zksync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,10 @@ export class Web3ZkSync extends Web3.Web3 {
transaction: Partial<TransactionRequest>,
returnFormat: web3Types.DataFormat = DEFAULT_RETURN_FORMAT,
): Promise<web3Types.Numbers> {
return this._rpc.estimateGasL1ToL2(transaction, returnFormat);
return this._rpc.estimateGasL1ToL2(
this.prepareTransaction(transaction as Eip712TxData, returnFormat) as TransactionRequest,
returnFormat,
);
}

/**
Expand Down Expand Up @@ -459,35 +462,7 @@ export class Web3ZkSync extends Web3.Web3 {
}
return this.contractAddresses().mainContract!;
}
// /**
// * Returns `tx` as a normalized JSON-RPC transaction request, which has all values `hexlified` and any numeric
// * values converted to Quantity values.
// * @param tx The transaction request that should be normalized.
// */
// override getRpcTransaction(tx: TransactionRequest): JsonRpcTransactionRequest {
// const result: any = super.getRpcTransaction(tx);
// if (!tx.customData) {
// return result;
// }
// result.type = ethers.toBeHex(EIP712_TX_TYPE);
// result.eip712Meta = {
// gasPerPubdata: ethers.toBeHex(tx.customData.gasPerPubdata ?? 0),
// } as any;
// if (tx.customData.factoryDeps) {
// result.eip712Meta.factoryDeps = tx.customData.factoryDeps.map((dep: ethers.BytesLike) =>
// // TODO (SMA-1605): we arraify instead of hexlifying because server expects Vec<u8>.
// // We should change deserialization there.
// Array.from(ethers.getBytes(dep)),
// );
// }
// if (tx.customData.paymasterParams) {
// result.eip712Meta.paymasterParams = {
// paymaster: ethers.hexlify(tx.customData.paymasterParams.paymaster),
// paymasterInput: Array.from(ethers.getBytes(tx.customData.paymasterParams.paymasterInput)),
// };
// }
// return result;
// }

async getTokenBalance(token: Address, walletAddress: Address): Promise<bigint> {
const erc20 = new this.eth.Contract(IERC20ABI, token);

Expand All @@ -501,14 +476,24 @@ export class Web3ZkSync extends Web3.Web3 {
return customData;
}

prepareTransaction(transaction: Eip712TxData) {
if (!transaction.customData) {
return transaction;
// /**
// * Returns `tx` as a normalized JSON-RPC transaction request, which has all values `hexlified` and any numeric
// * values converted to Quantity values.
// * @param tx The transaction request that should be normalized.
// */
prepareTransaction(
transaction: Eip712TxData,
returnFormat: web3Types.DataFormat = DEFAULT_RETURN_FORMAT,
): web3Types.FormatType<Eip712TxData, typeof returnFormat> {
const tx = format(transactionSchema, transaction, returnFormat);

if (!tx.customData) {
return tx;
}
const { customData, ...tx } = transaction as Eip712TxData;
const { customData } = transaction;
tx.eip712Meta = {
gasPerPubdata: web3Utils.toHex(customData?.gasPerPubdata ?? 0),
} as any;
};
if (customData?.factoryDeps) {
const factoryDeps = customData.factoryDeps.map(dep =>
// TODO (SMA-1605): we arraify instead of hexlifying because server expects Vec<u8>.
Expand All @@ -532,16 +517,8 @@ export class Web3ZkSync extends Web3.Web3 {
async estimateGas(transaction: Transaction) {
// if `to` is not set, when `eth_estimateGas` was called, the connected node returns the error: "Failed to serialize transaction: toAddressIsNull".
// for this pass the zero address as the `to` parameter, in-case if `to` was not provided if it is a contract deployment transaction.
const tx = format(
transactionSchema,
{ ...transaction, to: transaction.to ?? ZERO_ADDRESS } as Transaction,
ETH_DATA_FORMAT,
);

const dataToSend = this.prepareTransaction({
...tx,
customData: (transaction as Eip712TxData).customData,
} as Eip712TxData);
const tx = { ...transaction, to: transaction.to ?? ZERO_ADDRESS };
const dataToSend = this.prepareTransaction(tx as Eip712TxData, ETH_DATA_FORMAT);
const gas = await this.requestManager.send({
method: 'eth_estimateGas',
params: [dataToSend],
Expand Down Expand Up @@ -586,8 +563,7 @@ export class Web3ZkSync extends Web3.Web3 {
}
formatted.gasLimit = formatted.gasLimit ?? (await this.estimateGas(formatted));
if (formatted.type === 0n) {
formatted.gasPrice =
formatted.gasPrice ?? (await getGasPrice(this, DEFAULT_RETURN_FORMAT));
formatted.gasPrice = formatted.gasPrice ?? (await getGasPrice(this, DEFAULT_RETURN_FORMAT));
return formatted;
}
if (formatted.type === 2n && formatted.gasPrice) {
Expand All @@ -603,8 +579,7 @@ export class Web3ZkSync extends Web3.Web3 {
const gasFees = await this.eth.calculateFeeData();
if (gasFees.maxFeePerGas && gasFees.maxPriorityFeePerGas) {
if (formatted.type !== BigInt(EIP712_TX_TYPE)) {
formatted.maxFeePerGas =
formatted.maxFeePerGas ?? web3Utils.toBigInt(gasFees.maxFeePerGas);
formatted.maxFeePerGas = formatted.maxFeePerGas ?? web3Utils.toBigInt(gasFees.maxFeePerGas);
formatted.maxPriorityFeePerGas =
formatted.maxPriorityFeePerGas ??
(web3Utils.toBigInt(formatted.maxFeePerGas) >
Expand Down

0 comments on commit 2e7d928

Please sign in to comment.