@@ -21,7 +21,6 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
2121 * @typedef {Object } NewTransactionData
2222 * @property {bigint | number | string } gasLimit - Max gas allowed for transactions.
2323 * @property {bigint | number | string } gasUsed - Actual gas used.
24- * @property {bigint | number | string } baseFeePerGas - Base fee per gas unit.
2524 * @property {bigint | number | string } maxPriorityFeePerGas - Priority fee paid to the miner.
2625 * @property {bigint | number | string } maxFeePerGas - Max total fee per gas unit allowed.
2726 * @property {string } address - Hex address that created the tx.
@@ -34,7 +33,6 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
3433 * @typedef {Object } TransactionData
3534 * @property {bigint } gasLimit - Max gas allowed for transactions.
3635 * @property {bigint } gasUsed - Actual gas used.
37- * @property {bigint } baseFeePerGas - Base fee per gas unit.
3836 * @property {bigint } maxPriorityFeePerGas - Priority fee paid to the miner.
3937 * @property {bigint } maxFeePerGas - Max total fee per gas unit allowed.
4038 * @property {string } address - Hex address that created the block.
@@ -57,6 +55,7 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
5755 * @property {bigint | number | string } [reward=0n] - Block reward.
5856 * @property {bigint | number | string } [nonce=0n] - Starting nonce.
5957 * @property {bigint | number | string } [chainId] - The chain ID.
58+ * @property {bigint | number | string } [baseFeePerGas] - Base fee per gas unit.
6059 * @property {TxIndexMap } [txs] - A map where each key is a transaction index.
6160 * @property {SignIndexMap } [sigs] - A map where each key is a transaction signature.
6261 * @property {number } [time=Date.now()] - Unix timestamp of the block.
@@ -76,6 +75,7 @@ import TinySecp256k1 from './Secp256k1/index.mjs';
7675 * reward: bigint,
7776 * miner: string|null,
7877 * chainId: bigint,
78+ * baseFeePerGas: bigint,
7979 * txs: TxIndexMap,
8080 * sigs: SignIndexMap,
8181 * }} GetTransactionData
@@ -184,13 +184,7 @@ class TinyChainBlock {
184184 throw new Error ( `"payload" in data entry at index ${ index } must be a string.` ) ;
185185
186186 /** @type {Array<string> } */
187- const bigintFields = [
188- 'gasLimit' ,
189- 'gasUsed' ,
190- 'baseFeePerGas' ,
191- 'maxFeePerGas' ,
192- 'maxPriorityFeePerGas' ,
193- ] ;
187+ const bigintFields = [ 'gasLimit' , 'gasUsed' , 'maxFeePerGas' , 'maxPriorityFeePerGas' ] ;
194188
195189 for ( const field of bigintFields ) {
196190 // @ts -ignore
@@ -203,8 +197,6 @@ class TinyChainBlock {
203197
204198 const gasLimit = typeof t . gasLimit === 'bigint' ? t . gasLimit : BigInt ( t . gasLimit ) ;
205199 const gasUsed = typeof t . gasUsed === 'bigint' ? t . gasUsed : BigInt ( t . gasUsed ) ;
206- const baseFeePerGas =
207- typeof t . baseFeePerGas === 'bigint' ? t . baseFeePerGas : BigInt ( t . baseFeePerGas ) ;
208200 const maxFeePerGas =
209201 typeof t . maxFeePerGas === 'bigint' ? t . maxFeePerGas : BigInt ( t . maxFeePerGas ) ;
210202 const maxPriorityFeePerGas =
@@ -219,7 +211,6 @@ class TinyChainBlock {
219211 payload : this . #payloadString ? t . payload : undefined ,
220212 gasLimit,
221213 gasUsed,
222- baseFeePerGas,
223214 maxFeePerGas,
224215 maxPriorityFeePerGas,
225216 } ;
@@ -286,6 +277,7 @@ class TinyChainBlock {
286277 index = 0n ,
287278 prevHash = '' ,
288279 diff = 1n ,
280+ baseFeePerGas = 0n ,
289281 reward = 0n ,
290282 nonce = 0n ,
291283 hash = null ,
@@ -307,6 +299,12 @@ class TinyChainBlock {
307299 if ( typeof index !== 'bigint' && ! ( typeof index === 'string' && / ^ [ 0 - 9 ] + $ / . test ( index ) ) )
308300 throw new Error ( 'index must be a bigint or a numeric string.' ) ;
309301 this . index = typeof index === 'bigint' ? index : BigInt ( index ) ;
302+ if (
303+ typeof baseFeePerGas !== 'bigint' &&
304+ ! ( typeof baseFeePerGas === 'string' && / ^ [ 0 - 9 ] + $ / . test ( baseFeePerGas ) )
305+ )
306+ throw new Error ( 'baseFeePerGas must be a bigint or a numeric string.' ) ;
307+ this . baseFeePerGas = typeof baseFeePerGas === 'bigint' ? baseFeePerGas : BigInt ( baseFeePerGas ) ;
310308 if ( typeof prevHash !== 'string' ) throw new Error ( 'prevHash must be a hash string.' ) ;
311309 this . prevHash = prevHash ;
312310 if ( typeof diff !== 'bigint' && ! ( typeof diff === 'string' && / ^ [ 0 - 9 ] + $ / . test ( diff ) ) )
@@ -393,6 +391,7 @@ class TinyChainBlock {
393391 index : this . getIndex ( ) ,
394392 time : this . getTime ( ) ,
395393 data : this . getData ( ) ,
394+ baseFeePerGas : this . getBaseFeePerGas ( ) ,
396395 prevHash : this . getPrevHash ( ) ,
397396 diff : this . getDiff ( ) ,
398397 nonce : this . getNonce ( ) ,
@@ -424,6 +423,7 @@ class TinyChainBlock {
424423 value += this . #parser. serializeDeep ( this . data ) ;
425424 value += this . #parser. serialize ( this . sigs ) ;
426425 value += this . getIndex ( ) . toString ( ) ;
426+ value += this . getBaseFeePerGas ( ) . toString ( ) ;
427427 value += this . getNonce ( ) . toString ( ) ;
428428 value += this . getChainId ( ) . toString ( ) ;
429429 return createHash ( 'sha256' ) . update ( Buffer . from ( value , 'utf-8' ) ) . digest ( 'hex' ) ;
@@ -664,6 +664,15 @@ class TinyChainBlock {
664664 if ( typeof this . hash !== 'string' ) throw new Error ( 'Hash must be a string' ) ;
665665 return this . hash ;
666666 }
667+
668+ /**
669+ * Returns the base fee per gas (in gwei).
670+ * @returns {bigint }
671+ */
672+ getBaseFeePerGas ( ) {
673+ if ( typeof this . baseFeePerGas !== 'bigint' ) throw new Error ( 'baseFeePerGas must be a bigint' ) ;
674+ return this . baseFeePerGas ;
675+ }
667676}
668677
669678export default TinyChainBlock ;
0 commit comments