From 9f98f7575e8592c1bd6cc20bc5e5a6ac735211b8 Mon Sep 17 00:00:00 2001 From: petarTxFusion Date: Thu, 26 Sep 2024 17:07:49 +0200 Subject: [PATCH] feat: utilize estimated `gasPerPubData` --- src/signer.ts | 9 +++++++++ src/smart-account-utils.ts | 2 +- src/wallet.ts | 14 +++++++++----- tests/integration/provider.test.ts | 3 +++ tests/integration/smart-account.test.ts | 4 ++-- tests/integration/wallet.test.ts | 2 ++ tests/unit/smart-account.test.ts | 7 +++---- tests/utils.ts | 6 +++--- 8 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/signer.ts b/src/signer.ts index 089f134..1575128 100644 --- a/src/signer.ts +++ b/src/signer.ts @@ -707,6 +707,15 @@ export class Signer extends AdapterL2(ethers.JsonRpcSigner) { tx.maxFeePerGas ??= fee.maxFeePerGas; tx.maxPriorityFeePerGas ??= fee.maxPriorityFeePerGas; } + if ( + tx.type === null || + tx.type === undefined || + tx.type === EIP712_TX_TYPE || + tx.customData + ){ + tx.customData ??= {}; + tx.customData.gasPerPubdata = fee.gasPerPubdataLimit + } } return tx; } diff --git a/src/smart-account-utils.ts b/src/smart-account-utils.ts index c141527..dfe52fc 100644 --- a/src/smart-account-utils.ts +++ b/src/smart-account-utils.ts @@ -194,7 +194,6 @@ export const populateTransactionECDSA: TransactionBuilder = async ( populatedTx.value = populatedTx.value ? BigInt(populatedTx.value) : 0n; populatedTx.data ??= '0x'; populatedTx.customData = tx.customData ?? {}; - populatedTx.customData.gasPerPubdata ??= DEFAULT_GAS_PER_PUBDATA_LIMIT; populatedTx.customData.factoryDeps ??= []; populatedTx.from ??= new ethers.Wallet(secret).address; @@ -226,6 +225,7 @@ export const populateTransactionECDSA: TransactionBuilder = async ( }); populatedTx.gasLimit ??= fee.gasLimit; + populatedTx.customData.gasPerPubdata ??= fee.gasPerPubdataLimit; if (!populatedTx.gasPrice) { populatedTx.maxFeePerGas ??= fee.maxFeePerGas; populatedTx.maxPriorityFeePerGas ??= fee.maxPriorityFeePerGas; diff --git a/src/wallet.ts b/src/wallet.ts index 08f147b..0cdfbfb 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -12,15 +12,15 @@ import { } from 'ethers'; import { Address, - BalancesMap, + BalancesMap, Fee, FinalizeWithdrawalParams, FullDepositFee, PaymasterParams, PriorityOpResponse, TransactionLike, TransactionRequest, - TransactionResponse, -} from './types'; + TransactionResponse +} from "./types"; import {AdapterL1, AdapterL2} from './adapters'; import { IBridgehub, @@ -1438,12 +1438,14 @@ export class Wallet extends AdapterL2(AdapterL1(ethers.Wallet)) { 'Provide combination of maxFeePerGas and maxPriorityFeePerGas or provide gasPrice. Not both!' ); } + let fee: Fee; if ( !populated.gasLimit || + (!tx.customData || !tx.customData.gasPerPubdata) || (!populated.gasPrice && (!populated.maxFeePerGas || !populated.maxPriorityFeePerGas)) ) { - const fee = await this.provider.estimateFee(populated); + fee = await this.provider.estimateFee(populated); populated.gasLimit ??= fee.gasLimit; if (!populated.gasPrice && populated.type === 0) { populated.gasPrice = fee.maxFeePerGas; @@ -1458,10 +1460,12 @@ export class Wallet extends AdapterL2(AdapterL1(ethers.Wallet)) { tx.type === EIP712_TX_TYPE || tx.customData ) { + tx.customData ??= {}; + tx.customData.gasPerPubdata ??= fee!.gasPerPubdataLimit; populated.type = EIP712_TX_TYPE; populated.value ??= 0; populated.data ??= '0x'; - populated.customData = this._fillCustomData(tx.customData ?? {}); + populated.customData = this._fillCustomData(tx.customData); populated.nonce = populated.nonce ?? (await this.getNonce()); populated.chainId = populated.chainId ?? (await this.provider.getNetwork()).chainId; diff --git a/tests/integration/provider.test.ts b/tests/integration/provider.test.ts index 2ec2b63..441edee 100644 --- a/tests/integration/provider.test.ts +++ b/tests/integration/provider.test.ts @@ -399,6 +399,7 @@ describe('Provider', () => { if (IS_ETH_BASED) { it('should return an ETH withdraw transaction', async () => { const tx = { + type: 113, from: ADDRESS1, value: 7_000_000_000n, to: utils.L2_BASE_TOKEN_ADDRESS, @@ -419,6 +420,7 @@ describe('Provider', () => { value: 7_000_000_000n, to: utils.L2_BASE_TOKEN_ADDRESS, data: '0x51cff8d900000000000000000000000036615cf349d7f6344891b1e7ca7c72883f5dc049', + type: 113, customData: { paymasterParams: { paymaster: '0xa222f0c183AFA73a8Bc1AFb48D34C88c9Bf7A174', @@ -630,6 +632,7 @@ describe('Provider', () => { if (IS_ETH_BASED) { it('should return an ETH transfer transaction', async () => { const tx = { + type: 113, from: ADDRESS1, to: ADDRESS2, value: 7_000_000_000, diff --git a/tests/integration/smart-account.test.ts b/tests/integration/smart-account.test.ts index 1e038f2..664cad6 100644 --- a/tests/integration/smart-account.test.ts +++ b/tests/integration/smart-account.test.ts @@ -156,7 +156,7 @@ describe('SmartAccount', async () => { to: ADDRESS2, value: 7_000_000_000, }); - expect(result).to.be.deepEqualExcluding(tx, ['gasLimit', 'chainId']); + expect(result).to.be.deepEqualExcluding(tx, ['gasLimit', 'chainId', 'customData']); expect(BigInt(result.gasLimit!) > 0n).to.be.true; }).timeout(25_000); @@ -177,7 +177,7 @@ describe('SmartAccount', async () => { to: ADDRESS2, value: 7_000_000, }); - expect(result).to.be.deepEqualExcluding(tx, ['gasLimit', 'chainId']); + expect(result).to.be.deepEqualExcluding(tx, ['gasLimit', 'chainId', 'customData']); expect(BigInt(result.gasLimit!) > 0n).to.be.true; }); }); diff --git a/tests/integration/wallet.test.ts b/tests/integration/wallet.test.ts index 42a67fe..5ac9e01 100644 --- a/tests/integration/wallet.test.ts +++ b/tests/integration/wallet.test.ts @@ -236,6 +236,7 @@ describe('Wallet', () => { 'gasLimit', 'chainId', 'chainId', + 'customData', ]); expect(BigInt(result.gasLimit!) > 0n).to.be.true; }).timeout(25_000); @@ -263,6 +264,7 @@ describe('Wallet', () => { 'maxFeePerGas', 'maxPriorityFeePerGas', 'chainId', + 'customData', ]); expect(BigInt(result.gasLimit!) > 0n).to.be.true; expect(BigInt(result.maxFeePerGas!) > 0n).to.be.true; diff --git a/tests/unit/smart-account.test.ts b/tests/unit/smart-account.test.ts index 644ddeb..02484e4 100644 --- a/tests/unit/smart-account.test.ts +++ b/tests/unit/smart-account.test.ts @@ -148,7 +148,7 @@ describe('populateTransaction()', () => { PRIVATE_KEY1, provider ); - expect(result).to.be.deepEqualExcluding(tx, ['nonce']); + expect(result).to.be.deepEqualExcluding(tx, ['nonce', 'customData']); }); it('should populate tx using gasPrice as fee model', async () => { @@ -177,7 +177,7 @@ describe('populateTransaction()', () => { PRIVATE_KEY1, provider ); - expect(result).to.be.deepEqualExcluding(tx, ['nonce']); + expect(result).to.be.deepEqualExcluding(tx, ['nonce', 'customData']); }); it('should populate `tx.maxFeePerGas`', async () => { @@ -192,7 +192,6 @@ describe('populateTransaction()', () => { maxPriorityFeePerGas: 100_000_000n, gasLimit: 156_726n, customData: { - gasPerPubdata: 50_000, factoryDeps: [], }, }; @@ -207,7 +206,7 @@ describe('populateTransaction()', () => { PRIVATE_KEY1, provider ); - expect(result).to.be.deepEqualExcluding(tx, ['nonce']); + expect(result).to.be.deepEqualExcluding(tx, ['nonce', 'customData']); }); it('should throw an error when provider is not set', async () => { diff --git a/tests/utils.ts b/tests/utils.ts index 7944dfa..52bfe28 100644 --- a/tests/utils.ts +++ b/tests/utils.ts @@ -9,9 +9,9 @@ export const PRIVATE_KEY2 = export const DAI_L1 = '0x70a0F165d6f8054d0d0CF8dFd4DD2005f0AF6B55'; export const APPROVAL_TOKEN = '0x841c43Fa5d8fFfdB9efE3358906f7578d8700Dd4'; // Crown token export const PAYMASTER = '0xa222f0c183AFA73a8Bc1AFb48D34C88c9Bf7A174'; // Crown token paymaster - -export const IS_ETH_BASED = false; -export const L2_CHAIN_URL = IS_ETH_BASED +export const IS_ETH_BASED = ['true', '1', 'yes'].includes( + process.env.IS_ETH_CHAIN ?? 'true' +);export const L2_CHAIN_URL = IS_ETH_BASED ? 'http://127.0.0.1:15100' : 'http://127.0.0.1:15200'; export const L1_CHAIN_URL = 'http://127.0.0.1:15045';