-
Notifications
You must be signed in to change notification settings - Fork 266
/
sent_tx.ts
72 lines (66 loc) · 3.01 KB
/
sent_tx.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { retryUntil } from '@aztec/foundation/retry';
import { AztecRPC, TxHash, TxReceipt, TxStatus } from '@aztec/types';
/**
* The SentTx class represents a sent transaction through the AztecRPCClient, providing methods to fetch
* its hash, receipt, and mining status.
*/
export class SentTx {
constructor(protected arc: AztecRPC, protected txHashPromise: Promise<TxHash>) {}
/**
* Retrieves the transaction hash of the SentTx instance.
* The function internally awaits for the 'txHashPromise' to resolve, and then returns the resolved transaction hash.
*
* @returns A promise that resolves to the transaction hash of the SentTx instance.
*/
public async getTxHash() {
return await this.txHashPromise;
}
/**
* Retrieve the transaction receipt associated with the current SentTx instance.
* The function fetches the transaction hash using 'getTxHash' and then queries
* the AztecRPCClient to get the corresponding transaction receipt.
*
* @returns A promise that resolves to a TxReceipt object representing the fetched transaction receipt.
*/
public async getReceipt(): Promise<TxReceipt> {
const txHash = await this.getTxHash();
return await this.arc.getTxReceipt(txHash);
}
/**
* Awaits for a tx to be mined and returns the receipt. Throws if tx is not mined.
* @param timeout - The maximum time (in seconds) to wait for the transaction to be mined. A value of 0 means no timeout.
* @param interval - The time interval (in seconds) between retries to fetch the transaction receipt.
* @returns The transaction receipt.
*/
public async wait(timeout = 0, interval = 1): Promise<TxReceipt> {
const receipt = await this.waitForReceipt(timeout, interval);
if (receipt.status !== TxStatus.MINED)
throw new Error(`Transaction ${await this.getTxHash()} was ${receipt.status}`);
return receipt;
}
/**
* Checks whether the transaction is mined or not within the specified timeout and retry interval.
* Resolves to true if the transaction status is 'MINED', false otherwise.
* Throws an error if the transaction receipt cannot be fetched after the given timeout.
*
* @param timeout - The maximum time (in seconds) to wait for the transaction to be mined. A value of 0 means no timeout.
* @param interval - The time interval (in seconds) between retries to fetch the transaction receipt.
* @returns A Promise that resolves to a boolean indicating if the transaction is mined or not.
*/
public async isMined(timeout = 0, interval = 1): Promise<boolean> {
const receipt = await this.waitForReceipt(timeout, interval);
return receipt.status === TxStatus.MINED;
}
protected async waitForReceipt(timeout = 0, interval = 1): Promise<TxReceipt> {
const txHash = await this.getTxHash();
return await retryUntil(
async () => {
const txReceipt = await this.arc.getTxReceipt(txHash);
return txReceipt.status != TxStatus.PENDING ? txReceipt : undefined;
},
'isMined',
timeout,
interval,
);
}
}