Skip to content

Commit 320d19b

Browse files
spalladinoclaude
andcommitted
refactor(sequencer-client): move FeeProviderImpl to separate file
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6feb805 commit 320d19b

3 files changed

Lines changed: 81 additions & 76 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { RollupContract } from '@aztec/ethereum/contracts';
2+
import type { ViemPublicClient } from '@aztec/ethereum/types';
3+
import { SlotNumber } from '@aztec/foundation/branded-types';
4+
import type { DateProvider } from '@aztec/foundation/timer';
5+
import { getNextL1SlotTimestamp } from '@aztec/stdlib/epoch-helpers';
6+
import { GasFees, ManaUsageEstimate } from '@aztec/stdlib/gas';
7+
import type { FeeProvider } from '@aztec/stdlib/tx';
8+
9+
import { FeePredictor } from './fee_predictor.js';
10+
import type { GlobalVariableBuilderConfig } from './global_builder.js';
11+
12+
/** Provides current and predicted fee information based on on-chain state. */
13+
export class FeeProviderImpl implements FeeProvider {
14+
private currentMinFees: Promise<GasFees> = Promise.resolve(new GasFees(0, 0));
15+
private currentL1BlockNumber: bigint | undefined = undefined;
16+
17+
private readonly rollupContract: RollupContract;
18+
private readonly feePredictor: FeePredictor;
19+
private readonly ethereumSlotDuration: number;
20+
private readonly l1GenesisTime: bigint;
21+
22+
constructor(
23+
private readonly dateProvider: DateProvider,
24+
private readonly publicClient: ViemPublicClient,
25+
config: GlobalVariableBuilderConfig,
26+
) {
27+
this.ethereumSlotDuration = config.ethereumSlotDuration;
28+
this.l1GenesisTime = config.l1GenesisTime;
29+
30+
this.rollupContract = new RollupContract(this.publicClient, config.l1Contracts.rollupAddress);
31+
this.feePredictor = new FeePredictor(
32+
this.rollupContract,
33+
this.publicClient,
34+
this.dateProvider,
35+
config.slotDuration,
36+
config.l1GenesisTime,
37+
config.ethereumSlotDuration,
38+
);
39+
}
40+
41+
/**
42+
* Computes the "current" min fees, e.g., the price that you currently should pay to get include in the next block
43+
* @returns Min fees for the next block
44+
*/
45+
private async computeCurrentMinFees(): Promise<GasFees> {
46+
// Since this might be called in the middle of a slot where a block might have been published,
47+
// we need to fetch the last block written, and estimate the earliest timestamp for the next block.
48+
// The timestamp of that last block will act as a lower bound for the next block.
49+
50+
const lastCheckpoint = await this.rollupContract.getPendingCheckpoint();
51+
const earliestTimestamp = await this.rollupContract.getTimestampForSlot(
52+
SlotNumber.fromBigInt(BigInt(lastCheckpoint.slotNumber) + 1n),
53+
);
54+
const nextEthTimestamp = getNextL1SlotTimestamp(this.dateProvider.nowInSeconds(), {
55+
l1GenesisTime: this.l1GenesisTime,
56+
ethereumSlotDuration: this.ethereumSlotDuration,
57+
});
58+
const timestamp = earliestTimestamp > nextEthTimestamp ? earliestTimestamp : nextEthTimestamp;
59+
60+
return new GasFees(0, await this.rollupContract.getManaMinFeeAt(timestamp, true));
61+
}
62+
63+
public async getCurrentMinFees(): Promise<GasFees> {
64+
// Get the current block number
65+
const blockNumber = await this.publicClient.getBlockNumber();
66+
67+
// If the L1 block number has changed then chain a new promise to get the current min fees
68+
if (this.currentL1BlockNumber === undefined || blockNumber > this.currentL1BlockNumber) {
69+
this.currentL1BlockNumber = blockNumber;
70+
this.currentMinFees = this.currentMinFees.then(() => this.computeCurrentMinFees());
71+
}
72+
return this.currentMinFees;
73+
}
74+
75+
public getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
76+
return this.feePredictor.getPredictedMinFees(manaUsage ?? ManaUsageEstimate.Target);
77+
}
78+
}

yarn-project/sequencer-client/src/global_variable_builder/global_builder.ts

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,102 +4,28 @@ import type { ViemPublicClient } from '@aztec/ethereum/types';
44
import { BlockNumber, SlotNumber } from '@aztec/foundation/branded-types';
55
import { Fr } from '@aztec/foundation/curves/bn254';
66
import type { EthAddress } from '@aztec/foundation/eth-address';
7-
import { createLogger } from '@aztec/foundation/log';
87
import type { DateProvider } from '@aztec/foundation/timer';
98
import type { AztecAddress } from '@aztec/stdlib/aztec-address';
109
import { type L1RollupConstants, getNextL1SlotTimestamp, getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
11-
import { GasFees, ManaUsageEstimate } from '@aztec/stdlib/gas';
10+
import { GasFees } from '@aztec/stdlib/gas';
1211
import type {
1312
BuildCheckpointGlobalVariablesOpts,
1413
CheckpointGlobalVariables,
15-
FeeProvider,
1614
GlobalVariableBuilder as GlobalVariableBuilderInterface,
1715
} from '@aztec/stdlib/tx';
1816
import { GlobalVariables } from '@aztec/stdlib/tx';
1917

20-
import { FeePredictor } from './fee_predictor.js';
21-
2218
/** Configuration for the GlobalVariableBuilder (excludes L1 client config). */
2319
export type GlobalVariableBuilderConfig = {
2420
l1Contracts: Pick<L1ContractAddresses, 'rollupAddress'>;
2521
ethereumSlotDuration: number;
2622
rollupVersion: bigint;
2723
} & Pick<L1RollupConstants, 'slotDuration' | 'l1GenesisTime'>;
2824

29-
/** Provides current and predicted fee information based on on-chain state. */
30-
export class FeeProviderImpl implements FeeProvider {
31-
private currentMinFees: Promise<GasFees> = Promise.resolve(new GasFees(0, 0));
32-
private currentL1BlockNumber: bigint | undefined = undefined;
33-
34-
private readonly rollupContract: RollupContract;
35-
private readonly feePredictor: FeePredictor;
36-
private readonly ethereumSlotDuration: number;
37-
private readonly l1GenesisTime: bigint;
38-
39-
constructor(
40-
private readonly dateProvider: DateProvider,
41-
private readonly publicClient: ViemPublicClient,
42-
config: GlobalVariableBuilderConfig,
43-
) {
44-
this.ethereumSlotDuration = config.ethereumSlotDuration;
45-
this.l1GenesisTime = config.l1GenesisTime;
46-
47-
this.rollupContract = new RollupContract(this.publicClient, config.l1Contracts.rollupAddress);
48-
this.feePredictor = new FeePredictor(
49-
this.rollupContract,
50-
this.publicClient,
51-
this.dateProvider,
52-
config.slotDuration,
53-
config.l1GenesisTime,
54-
config.ethereumSlotDuration,
55-
);
56-
}
57-
58-
/**
59-
* Computes the "current" min fees, e.g., the price that you currently should pay to get include in the next block
60-
* @returns Min fees for the next block
61-
*/
62-
private async computeCurrentMinFees(): Promise<GasFees> {
63-
// Since this might be called in the middle of a slot where a block might have been published,
64-
// we need to fetch the last block written, and estimate the earliest timestamp for the next block.
65-
// The timestamp of that last block will act as a lower bound for the next block.
66-
67-
const lastCheckpoint = await this.rollupContract.getPendingCheckpoint();
68-
const earliestTimestamp = await this.rollupContract.getTimestampForSlot(
69-
SlotNumber.fromBigInt(BigInt(lastCheckpoint.slotNumber) + 1n),
70-
);
71-
const nextEthTimestamp = getNextL1SlotTimestamp(this.dateProvider.nowInSeconds(), {
72-
l1GenesisTime: this.l1GenesisTime,
73-
ethereumSlotDuration: this.ethereumSlotDuration,
74-
});
75-
const timestamp = earliestTimestamp > nextEthTimestamp ? earliestTimestamp : nextEthTimestamp;
76-
77-
return new GasFees(0, await this.rollupContract.getManaMinFeeAt(timestamp, true));
78-
}
79-
80-
public async getCurrentMinFees(): Promise<GasFees> {
81-
// Get the current block number
82-
const blockNumber = await this.publicClient.getBlockNumber();
83-
84-
// If the L1 block number has changed then chain a new promise to get the current min fees
85-
if (this.currentL1BlockNumber === undefined || blockNumber > this.currentL1BlockNumber) {
86-
this.currentL1BlockNumber = blockNumber;
87-
this.currentMinFees = this.currentMinFees.then(() => this.computeCurrentMinFees());
88-
}
89-
return this.currentMinFees;
90-
}
91-
92-
public getPredictedMinFees(manaUsage?: ManaUsageEstimate): Promise<GasFees[]> {
93-
return this.feePredictor.getPredictedMinFees(manaUsage ?? ManaUsageEstimate.Target);
94-
}
95-
}
96-
9725
/**
9826
* Simple global variables builder.
9927
*/
10028
export class GlobalVariableBuilder implements GlobalVariableBuilderInterface {
101-
private log = createLogger('sequencer:global_variable_builder');
102-
10329
private readonly rollupContract: RollupContract;
10430
private readonly ethereumSlotDuration: number;
10531
private readonly aztecSlotDuration: number;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
export { FeeProviderImpl, GlobalVariableBuilder, type GlobalVariableBuilderConfig } from './global_builder.js';
1+
export { FeeProviderImpl } from './fee_provider.js';
2+
export { GlobalVariableBuilder, type GlobalVariableBuilderConfig } from './global_builder.js';
23
export { FeePredictor } from './fee_predictor.js';

0 commit comments

Comments
 (0)