11import { PrivateKey , PublicKey } from "o1js" ;
2- import { CommandResponse , FAILURE , PENDING } from "./types" ;
2+ import { CommandRequest , CommandResponse , FAILURE , PENDING } from "./types" ;
33import { qry } from "chain" ;
44
55interface TxTask {
66 txfn : ( ) => Promise < void > ;
7+ cmdReq : CommandRequest ;
8+ timeSubmit : number ;
79 resolve : ( value : CommandResponse ) => void ;
810 reject : ( reason ?: any ) => void ;
911}
@@ -39,11 +41,21 @@ export class TxHandler {
3941 ) { }
4042
4143 // helper function to send transactions
42- public async submitTx ( txfn : ( ) => Promise < void > ) : Promise < CommandResponse > {
44+ public async submitTx (
45+ txfn : ( ) => Promise < void > ,
46+ cmdReq : CommandRequest ,
47+ ) : Promise < CommandResponse > {
48+ const id = cmdReq . id ?? "?" ;
4349 return new Promise ( ( resolve , reject ) => {
44- this . txQueue . push ( { txfn, resolve, reject } ) ;
50+ const timeSubmit = Date . now ( ) ;
51+ this . txQueue . push ( { txfn, cmdReq, timeSubmit, resolve, reject } ) ;
52+ console . log (
53+ `--- [${ id } ] Submission` ,
54+ `queue=${ this . txQueue . length } ` ,
55+ `$ ${ cmdReq . command } ` ,
56+ ) ;
4557 this . processQueue ( ) . catch ( ( err ) => {
46- console . error ( " Error processing transaction queue:" , err ) ;
58+ console . error ( `--- [ ${ id } ] ❌ Error processing tx queue:` , err ) ;
4759 while ( this . txQueue . length > 0 ) {
4860 const task = this . txQueue . shift ( ) ;
4961 task ?. reject ( err ) ;
@@ -52,10 +64,15 @@ export class TxHandler {
5264 } ) ;
5365 }
5466
55- private async processTx ( txfn : ( ) => Promise < void > ) : Promise < CommandResponse > {
67+ private async processTx (
68+ txfn : ( ) => Promise < void > ,
69+ cmdReq : CommandRequest ,
70+ timeSubmit : number ,
71+ ) : Promise < CommandResponse > {
72+ const timeProcess = Date . now ( ) ;
73+ const id = cmdReq . id ?? "?" ;
5674 const nonce = this . nonce ;
5775 const tx = await this . client . transaction ( this . publicKey , txfn , { nonce } ) ;
58- console . log ( "tx.nonce" , tx . transaction ! . nonce . toString ( ) ) ;
5976 tx . transaction = tx . transaction ?. sign ( this . privateKey ) ;
6077 await tx . send ( ) ;
6178
@@ -65,14 +82,37 @@ export class TxHandler {
6582 // but enables submission of many txns without waiting for their confirmation.
6683 if ( this . opts . nonce ) this . nonce = Number ( tx . transaction . nonce ) + 1 ;
6784
68- const { status, statusMessage } = await qry . indexer . getTxStatus (
85+ const h7 = ( tx . transaction . hash ( ) . toString ( ) as string ) . substring ( 0 , 7 ) ;
86+
87+ const { status, statusMessage } = await qry . processor . getTxStatus (
6988 tx . transaction . hash ( ) . toString ( ) ,
7089 ( ) => {
71- console . log ( "⏳ Waiting for tx status..." ) ;
90+ console . log (
91+ `--- [${ id } ] ⏳ Awaiting status for` ,
92+ `tx=${ h7 } ...` ,
93+ `n=${ tx . transaction . nonce } ` ,
94+ `q=${ this . txQueue . length } ` ,
95+ `$ ${ cmdReq . command } ` ,
96+ ) ;
7297 } ,
7398 parseInt ( this . opts . txStatusInterval ) ,
7499 parseInt ( this . opts . txStatusRetries ) ,
75100 ) ;
101+
102+ console . log (
103+ `--- [${ id } ] ${ status === FAILURE ? "❌" : "✅" } Returning status for` ,
104+ `tx=${ h7 } ...` ,
105+ `n=${ tx . transaction . nonce } ` ,
106+ `q=${ this . txQueue . length } ` ,
107+ `t=${ timeProcess - timeSubmit } ` +
108+ "/" +
109+ `${ Date . now ( ) - timeProcess } ` +
110+ "/" +
111+ `${ Date . now ( ) - timeSubmit } ` +
112+ "ms" ,
113+ `$ ${ cmdReq . command } ` ,
114+ ) ;
115+
76116 return {
77117 status,
78118 data : status !== FAILURE ? statusMessage : undefined ,
@@ -81,6 +121,7 @@ export class TxHandler {
81121 } ;
82122 }
83123
124+ console . error ( `--- [${ id } ] ❌ !tx.transaction` ) ;
84125 return { status : PENDING } ;
85126 }
86127
@@ -89,9 +130,9 @@ export class TxHandler {
89130
90131 this . isProcessing = true ;
91132 while ( this . txQueue . length > 0 ) {
92- const { txfn, resolve, reject } = this . txQueue [ 0 ] ;
133+ const { txfn, cmdReq , timeSubmit , resolve, reject } = this . txQueue [ 0 ] ;
93134 try {
94- const result = await this . processTx ( txfn ) ;
135+ const result = await this . processTx ( txfn , cmdReq , timeSubmit ) ;
95136 resolve ( result ) ;
96137 } catch ( err ) {
97138 reject ( err ) ;
0 commit comments