/
TransactionReceipt.ts
59 lines (54 loc) 路 1.45 KB
/
TransactionReceipt.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
import type {
TransactionResponse,
TransactionSignature,
} from "@solana/web3.js";
import invariant from "tiny-invariant";
import type { Event, EventParser, ReadonlyProvider } from "../interfaces";
/**
* A transaction that has been processed by the cluster.
*/
export class TransactionReceipt {
constructor(
/**
* Current provider.
*/
public readonly provider: ReadonlyProvider,
/**
* Signature (id) of the transaction.
*/
public readonly signature: TransactionSignature,
/**
* Raw response from web3.js
*/
public readonly response: TransactionResponse
) {}
/**
* Gets the events associated with this transaction.
*/
public getEvents<E extends Event>(eventParser: EventParser<E>): readonly E[] {
const logs = this.response.meta?.logMessages;
if (logs && logs.length > 0) {
return eventParser(logs);
}
return [];
}
/**
* Prints the logs associated with this transaction.
*/
public printLogs(): void {
console.log(this.response.meta?.logMessages?.join("\n"));
}
/**
* Gets the compute units used by the transaction.
* @returns
*/
public get computeUnits(): number {
const logs = this.response.meta?.logMessages;
invariant(logs, "no logs");
const consumeLog = logs[logs.length - 2];
invariant(consumeLog, "no consume log");
const amtStr = consumeLog.split(" ")[3];
invariant(amtStr, "no amount");
return parseInt(amtStr);
}
}