@@ -26,43 +26,69 @@ import constants from './constants';
2626import { ethers , Transaction } from 'ethers' ;
2727import { formatRequestIdMessage , prepend0x } from '../formatters' ;
2828
29+ /**
30+ * Precheck class for handling various prechecks before sending a raw transaction.
31+ */
2932export class Precheck {
30- private mirrorNodeClient : MirrorNodeClient ;
33+ private readonly mirrorNodeClient : MirrorNodeClient ;
3134 private readonly chain : string ;
3235 private readonly logger : Logger ;
3336
37+ /**
38+ * Creates an instance of Precheck.
39+ * @param {MirrorNodeClient } mirrorNodeClient - The MirrorNodeClient instance.
40+ * @param {Logger } logger - The logger instance.
41+ * @param {string } chainId - The chain ID.
42+ */
3443 constructor ( mirrorNodeClient : MirrorNodeClient , logger : Logger , chainId : string ) {
3544 this . mirrorNodeClient = mirrorNodeClient ;
3645 this . logger = logger ;
3746 this . chain = chainId ;
3847 }
3948
49+ /**
50+ * Parses the transaction if needed.
51+ * @param {string | Transaction } transaction - The transaction to parse.
52+ * @returns {Transaction } The parsed transaction.
53+ */
4054 public static parseTxIfNeeded ( transaction : string | Transaction ) : Transaction {
4155 return typeof transaction === 'string' ? Transaction . from ( transaction ) : transaction ;
4256 }
4357
44- value ( tx : Transaction ) {
58+ /**
59+ * Checks if the value of the transaction is valid.
60+ * @param {Transaction } tx - The transaction.
61+ */
62+ value ( tx : Transaction ) : void {
4563 if ( tx . data === EthImpl . emptyHex && tx . value < constants . TINYBAR_TO_WEIBAR_COEF ) {
4664 throw predefined . VALUE_TOO_LOW ;
4765 }
4866 }
4967
5068 /**
51- * @param transaction
52- * @param gasPrice
69+ * Sends a raw transaction after performing various prechecks.
70+ * @param {ethers.Transaction } parsedTx - The parsed transaction.
71+ * @param {number } gasPrice - The gas price.
72+ * @param {string } [requestId] - The request ID.
5373 */
54- async sendRawTransactionCheck ( parsedTx : ethers . Transaction , gasPrice : number , requestId ?: string ) {
74+ async sendRawTransactionCheck ( parsedTx : ethers . Transaction , gasPrice : number , requestId ?: string ) : Promise < void > {
5575 this . transactionType ( parsedTx , requestId ) ;
5676 this . gasLimit ( parsedTx , requestId ) ;
5777 const mirrorAccountInfo = await this . verifyAccount ( parsedTx , requestId ) ;
58- await this . nonce ( parsedTx , mirrorAccountInfo . ethereum_nonce , requestId ) ;
78+ this . nonce ( parsedTx , mirrorAccountInfo . ethereum_nonce , requestId ) ;
5979 this . chainId ( parsedTx , requestId ) ;
6080 this . value ( parsedTx ) ;
6181 this . gasPrice ( parsedTx , gasPrice , requestId ) ;
62- await this . balance ( parsedTx , mirrorAccountInfo , requestId ) ;
82+ this . balance ( parsedTx , mirrorAccountInfo , requestId ) ;
6383 }
6484
65- async verifyAccount ( tx : Transaction , requestId ?: string ) {
85+ /**
86+ * Verifies the account.
87+ * @param {Transaction } tx - The transaction.
88+ * @param {string } [requestId] - The request ID.
89+ * @returns {Promise<any> } A Promise.
90+ */
91+ async verifyAccount ( tx : Transaction , requestId ?: string ) : Promise < any > {
6692 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
6793 // verify account
6894 const accountInfo = await this . mirrorNodeClient . getAccount ( tx . from ! , requestId ) ;
@@ -81,9 +107,12 @@ export class Precheck {
81107 }
82108
83109 /**
84- * @param tx
110+ * Checks the nonce of the transaction.
111+ * @param {Transaction } tx - The transaction.
112+ * @param {number } accountInfoNonce - The nonce of the account.
113+ * @param {string } [requestId] - The request ID.
85114 */
86- async nonce ( tx : Transaction , accountInfoNonce : number , requestId ?: string ) {
115+ nonce ( tx : Transaction , accountInfoNonce : number , requestId ?: string ) : void {
87116 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
88117 this . logger . trace (
89118 `${ requestIdPrefix } Nonce precheck for sendRawTransaction(tx.nonce=${ tx . nonce } , accountInfoNonce=${ accountInfoNonce } )` ,
@@ -96,9 +125,11 @@ export class Precheck {
96125 }
97126
98127 /**
99- * @param tx
128+ * Checks the chain ID of the transaction.
129+ * @param {Transaction } tx - The transaction.
130+ * @param {string } [requestId] - The request ID.
100131 */
101- chainId ( tx : Transaction , requestId ?: string ) {
132+ chainId ( tx : Transaction , requestId ?: string ) : void {
102133 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
103134 const txChainId = prepend0x ( Number ( tx . chainId ) . toString ( 16 ) ) ;
104135 const passes = this . isLegacyUnprotectedEtx ( tx ) || txChainId === this . chain ;
@@ -124,10 +155,12 @@ export class Precheck {
124155 }
125156
126157 /**
127- * @param tx
128- * @param gasPrice
158+ * Checks the gas price of the transaction.
159+ * @param {Transaction } tx - The transaction.
160+ * @param {number } gasPrice - The gas price.
161+ * @param {string } [requestId] - The request ID.
129162 */
130- gasPrice ( tx : Transaction , gasPrice : number , requestId ?: string ) {
163+ gasPrice ( tx : Transaction , gasPrice : number , requestId ?: string ) : void {
131164 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
132165 const minGasPrice = BigInt ( gasPrice ) ;
133166 const txGasPrice = tx . gasPrice || tx . maxFeePerGas ! + tx . maxPriorityFeePerGas ! ;
@@ -169,10 +202,12 @@ export class Precheck {
169202 }
170203
171204 /**
172- * @param tx
173- * @param callerName
205+ * Checks the balance of the sender account.
206+ * @param {Transaction } tx - The transaction.
207+ * @param {any } account - The account information.
208+ * @param {string } [requestId] - The request ID.
174209 */
175- async balance ( tx : Transaction , account : any , requestId ?: string ) {
210+ balance ( tx : Transaction , account : any , requestId ?: string ) : void {
176211 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
177212 const result = {
178213 passes : false ,
@@ -221,9 +256,11 @@ export class Precheck {
221256 }
222257
223258 /**
224- * @param tx
259+ * Checks the gas limit of the transaction.
260+ * @param {Transaction } tx - The transaction.
261+ * @param {string } [requestId] - The request ID.
225262 */
226- gasLimit ( tx : Transaction , requestId ?: string ) {
263+ gasLimit ( tx : Transaction , requestId ?: string ) : void {
227264 const requestIdPrefix = formatRequestIdMessage ( requestId ) ;
228265 const gasLimit = Number ( tx . gasLimit ) ;
229266 const failBaseLog = 'Failed gasLimit precheck for sendRawTransaction(transaction=%s).' ;
@@ -253,11 +290,12 @@ export class Precheck {
253290 * Calculates the intrinsic gas cost based on the number of bytes in the data field.
254291 * Using a loop that goes through every two characters in the string it counts the zero and non-zero bytes.
255292 * Every two characters that are packed together and are both zero counts towards zero bytes.
256- * @param data
293+ * @param {string } data - The data with the bytes to be calculated
294+ * @returns {number } The intrinsic gas cost.
257295 * @private
258296 */
259- private static transactionIntrinsicGasCost ( data : string ) {
260- let trimmedData = data . replace ( '0x' , '' ) ;
297+ private static transactionIntrinsicGasCost ( data : string ) : number {
298+ const trimmedData = data . replace ( '0x' , '' ) ;
261299
262300 let zeros = 0 ;
263301 let nonZeros = 0 ;
@@ -277,7 +315,8 @@ export class Precheck {
277315
278316 /**
279317 * Converts hex string to bytes array
280- * @param hex the hex string you want to convert
318+ * @param {string } hex - The hex string you want to convert.
319+ * @returns {Uint8Array } The bytes array.
281320 */
282321 hexToBytes ( hex : string ) : Uint8Array {
283322 if ( hex === '' ) {
@@ -293,6 +332,10 @@ export class Precheck {
293332 return Uint8Array . from ( Buffer . from ( hex , 'hex' ) ) ;
294333 }
295334
335+ /**
336+ * Checks the size of the transaction.
337+ * @param {string } transaction - The transaction to check.
338+ */
296339 checkSize ( transaction : string ) : void {
297340 const transactionToBytes : Uint8Array = this . hexToBytes ( transaction ) ;
298341 const transactionSize : number = transactionToBytes . length ;
0 commit comments