@@ -25,6 +25,9 @@ export interface TxEvents extends EventsRecord {
25
25
rejected : [ any ] ;
26
26
}
27
27
28
+ export type TxSendResult < Input extends "sent" | "included" | "none" > =
29
+ Input extends "none" ? void : { hash : string } ;
30
+
28
31
@injectable ( )
29
32
export class MinaTransactionSender {
30
33
private txStatusEmitters : Record < string , EventEmitter < TxEvents > > = { } ;
@@ -113,10 +116,12 @@ export class MinaTransactionSender {
113
116
return eventEmitter ;
114
117
}
115
118
116
- public async proveAndSendTransaction (
119
+ public async proveAndSendTransaction <
120
+ Wait extends "sent" | "included" | "none" ,
121
+ > (
117
122
transaction : Transaction < false , true > ,
118
- waitOnStatus : "sent" | "included" | "none" = "none"
119
- ) {
123
+ waitOnStatus : Wait
124
+ ) : Promise < TxSendResult < Wait > > {
120
125
const { publicKey, nonce } = transaction . transaction . feePayer . body ;
121
126
122
127
log . debug (
@@ -167,16 +172,24 @@ export class MinaTransactionSender {
167
172
const txStatus = await this . sendOrQueue ( result . transaction ) ;
168
173
169
174
if ( waitOnStatus !== "none" ) {
170
- await new Promise < void > ( ( resolve , reject ) => {
171
- txStatus . on ( waitOnStatus , ( ) => {
172
- log . info ( "Tx included" ) ;
173
- resolve ( ) ;
174
- } ) ;
175
- txStatus . on ( "rejected" , ( error ) => {
176
- reject ( error ) ;
177
- } ) ;
178
- } ) ;
175
+ const waitInstruction : "sent" | "included" = waitOnStatus ;
176
+ const hash = await new Promise < TxSendResult < "sent" | "included" > > (
177
+ ( resolve , reject ) => {
178
+ txStatus . on ( waitInstruction , ( result ) => {
179
+ log . info ( `Tx ${ result . hash } included` ) ;
180
+ resolve ( result ) ;
181
+ } ) ;
182
+ txStatus . on ( "rejected" , ( error ) => {
183
+ reject ( error ) ;
184
+ } ) ;
185
+ }
186
+ ) ;
187
+
188
+ // Yeah that's not super clean, but couldn't figure out a better way tbh
189
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
190
+ return hash as TxSendResult < Wait > ;
179
191
}
180
- return txStatus ;
192
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
193
+ return undefined as TxSendResult < Wait > ;
181
194
}
182
195
}
0 commit comments