Skip to content

Commit

Permalink
feat: utilize estimated gasPerPubData
Browse files Browse the repository at this point in the history
  • Loading branch information
petarTxFusion committed Sep 26, 2024
1 parent 456c73a commit 9f98f75
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 15 deletions.
9 changes: 9 additions & 0 deletions src/signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/smart-account-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
14 changes: 9 additions & 5 deletions src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/smart-account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
});
});
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ describe('Wallet', () => {
'gasLimit',
'chainId',
'chainId',
'customData',
]);
expect(BigInt(result.gasLimit!) > 0n).to.be.true;
}).timeout(25_000);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions tests/unit/smart-account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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 () => {
Expand All @@ -192,7 +192,6 @@ describe('populateTransaction()', () => {
maxPriorityFeePerGas: 100_000_000n,
gasLimit: 156_726n,
customData: {
gasPerPubdata: 50_000,
factoryDeps: [],
},
};
Expand All @@ -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 () => {
Expand Down
6 changes: 3 additions & 3 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit 9f98f75

Please sign in to comment.