@@ -38,14 +38,13 @@ class Transaction {
3838 value : this . tx . value || '0x0' ,
3939 } ) ;
4040 }
41- async getOPfees ( ) : Promise < BNType > {
41+ async getOPfees (
42+ fTx : LegacyTransaction | FeeMarketEIP1559Transaction ,
43+ ) : Promise < BNType > {
4244 const OPContract = new this . web3 . Contract (
4345 OPTIMISM_PRICE_ORACLE_ABI as any ,
4446 OPTIMISM_PRICE_ORACLE ,
4547 ) ;
46- const fTx = await this . getFinalizedTransaction ( {
47- gasPriceType : GasPriceTypes . ECONOMY ,
48- } ) ;
4948 const serializedTx = fTx . serialize ( ) ;
5049 return OPContract . methods
5150 . getL1Fee ( bufferToHex ( serializedTx ) )
@@ -87,6 +86,7 @@ class Transaction {
8786 maxFeePerGas ?: string ;
8887 gasLimit : string ;
8988 formattedFeeHistory ?: FormattedFeeHistory ;
89+ finalizedTransaction : LegacyTransaction | FeeMarketEIP1559Transaction ;
9090 } > {
9191 const latestBlock = await this . web3 . getBlock ( 'latest' , false ) ;
9292 const { isFeeMarketNetwork, feeHistory } = await this . web3
@@ -123,10 +123,20 @@ class Transaction {
123123 nonce : this . tx . nonce || ( numberToHex ( nonce ) as `0x${string } `) ,
124124 value : this . tx . value || '0x0' ,
125125 } ;
126+ const common = Common . custom ( {
127+ chainId : BigInt ( this . tx . chainId ) ,
128+ } ) ;
129+ const finalizedTransaction = LegacyTransaction . fromTxData (
130+ legacyTx as FinalizedLegacyEthereumTransaction ,
131+ {
132+ common,
133+ } ,
134+ ) ;
126135 return {
127136 transaction : legacyTx ,
128137 gasPrice : gasPrice ,
129138 gasLimit : legacyTx . gasLimit ,
139+ finalizedTransaction,
130140 } ;
131141 } else {
132142 // Fee market transaction (post EIP1559)
@@ -141,7 +151,7 @@ class Transaction {
141151 const gasLimit =
142152 this . tx . gasLimit ||
143153 ( numberToHex ( await this . estimateGas ( ) ) as `0x${string } `) ;
144- const maxFeePerGas = ! options . totalGasPrice
154+ let maxFeePerGas = ! options . totalGasPrice
145155 ? feeMarket . maxFeePerGas
146156 : options . totalGasPrice . div ( toBN ( gasLimit ) ) ;
147157 const maxPriorityFeePerGas = feeMarket . maxPriorityFeePerGas ;
@@ -162,13 +172,43 @@ class Transaction {
162172 type : '0x02' ,
163173 accessList : this . tx . accessList || [ ] ,
164174 } ;
175+ const common = Common . custom ( {
176+ chainId : BigInt ( this . tx . chainId ) ,
177+ defaultHardfork : Hardfork . London ,
178+ } ) ;
179+ let finalizedTransaction = FeeMarketEIP1559Transaction . fromTxData (
180+ feeMarketTx as FinalizedFeeMarketEthereumTransaction ,
181+ {
182+ common,
183+ } ,
184+ ) ;
185+ if ( options . totalGasPrice ) {
186+ const opFee = await this . getOPfees ( finalizedTransaction ) ;
187+ if ( opFee . gtn ( 0 ) ) {
188+ const gasFeeWithoutOPFee = options . totalGasPrice . sub ( opFee ) ;
189+ maxFeePerGas = gasFeeWithoutOPFee . div ( toBN ( gasLimit ) ) ;
190+ feeMarketTx . maxFeePerGas = numberToHex ( maxFeePerGas ) as `0x${string } `;
191+ feeMarketTx . maxPriorityFeePerGas = numberToHex (
192+ maxPriorityFeePerGas . gt ( maxFeePerGas )
193+ ? maxFeePerGas
194+ : maxPriorityFeePerGas ,
195+ ) as `0x${string } `;
196+ finalizedTransaction = FeeMarketEIP1559Transaction . fromTxData (
197+ feeMarketTx as FinalizedFeeMarketEthereumTransaction ,
198+ {
199+ common,
200+ } ,
201+ ) ;
202+ }
203+ }
165204 return {
166205 transaction : feeMarketTx ,
167206 gasLimit : feeMarketTx . gasLimit ,
168207 baseFeePerGas : numberToHex ( baseFeePerGas ! ) ,
169208 maxFeePerGas : numberToHex ( feeMarket . maxFeePerGas ) ,
170209 maxPriorityFeePerGas : numberToHex ( feeMarket . maxPriorityFeePerGas ) ,
171210 formattedFeeHistory,
211+ finalizedTransaction,
172212 } ;
173213 }
174214 }
@@ -182,30 +222,8 @@ class Transaction {
182222 async getFinalizedTransaction (
183223 options : TransactionOptions ,
184224 ) : Promise < LegacyTransaction | FeeMarketEIP1559Transaction > {
185- const { transaction } = await this . finalizeTransaction ( options ) ;
186-
187- if ( ! transaction . maxFeePerGas ) {
188- const common = Common . custom ( {
189- chainId : BigInt ( transaction . chainId ) ,
190- } ) ;
191- return LegacyTransaction . fromTxData (
192- transaction as FinalizedLegacyEthereumTransaction ,
193- {
194- common,
195- } ,
196- ) ;
197- } else {
198- const common = Common . custom ( {
199- chainId : BigInt ( transaction . chainId ) ,
200- defaultHardfork : Hardfork . London ,
201- } ) ;
202- return FeeMarketEIP1559Transaction . fromTxData (
203- transaction as FinalizedFeeMarketEthereumTransaction ,
204- {
205- common,
206- } ,
207- ) ;
208- }
225+ const { finalizedTransaction } = await this . finalizeTransaction ( options ) ;
226+ return finalizedTransaction ;
209227 }
210228
211229 async getMessageToSign ( options : TransactionOptions ) : Promise < Uint8Array > {
@@ -214,35 +232,38 @@ class Transaction {
214232 }
215233
216234 async getGasCosts ( ) : Promise < GasCosts > {
217- const { gasLimit, gasPrice, baseFeePerGas, formattedFeeHistory } =
218- await this . finalizeTransaction ( {
219- gasPriceType : GasPriceTypes . ECONOMY ,
220- } ) ;
221- const opFee = await this . getOPfees ( ) ;
235+ const {
236+ gasLimit,
237+ gasPrice,
238+ baseFeePerGas,
239+ formattedFeeHistory,
240+ finalizedTransaction,
241+ } = await this . finalizeTransaction ( {
242+ gasPriceType : GasPriceTypes . ECONOMY ,
243+ } ) ;
222244 if ( gasPrice ) {
223245 return {
224246 [ GasPriceTypes . ECONOMY ] : numberToHex (
225- getGasBasedOnType ( gasPrice , GasPriceTypes . ECONOMY )
226- . mul ( toBN ( gasLimit ) )
227- . add ( opFee ) ,
247+ getGasBasedOnType ( gasPrice , GasPriceTypes . ECONOMY ) . mul (
248+ toBN ( gasLimit ) ,
249+ ) ,
228250 ) ,
229251 [ GasPriceTypes . REGULAR ] : numberToHex (
230- getGasBasedOnType ( gasPrice , GasPriceTypes . REGULAR )
231- . mul ( toBN ( gasLimit ) )
232- . add ( opFee ) ,
252+ getGasBasedOnType ( gasPrice , GasPriceTypes . REGULAR ) . mul (
253+ toBN ( gasLimit ) ,
254+ ) ,
233255 ) ,
234256 [ GasPriceTypes . FAST ] : numberToHex (
235- getGasBasedOnType ( gasPrice , GasPriceTypes . FAST )
236- . mul ( toBN ( gasLimit ) )
237- . add ( opFee ) ,
257+ getGasBasedOnType ( gasPrice , GasPriceTypes . FAST ) . mul ( toBN ( gasLimit ) ) ,
238258 ) ,
239259 [ GasPriceTypes . FASTEST ] : numberToHex (
240- getGasBasedOnType ( gasPrice , GasPriceTypes . FASTEST )
241- . mul ( toBN ( gasLimit ) )
242- . add ( opFee ) ,
260+ getGasBasedOnType ( gasPrice , GasPriceTypes . FASTEST ) . mul (
261+ toBN ( gasLimit ) ,
262+ ) ,
243263 ) ,
244264 } ;
245265 } else {
266+ const opFee = await this . getOPfees ( finalizedTransaction ) ;
246267 return {
247268 [ GasPriceTypes . ECONOMY ] : numberToHex (
248269 this . getFeeMarketGasInfo (
0 commit comments