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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
import { CString, getSymbol, readCString, type Sanitizer } from "./utils.ts";
export type PendingTransactionPtr = Deno.PointerObject<"transactionInfo">;
export class PendingTransaction {
#pendingTxPtr: PendingTransactionPtr;
sanitizer?: Sanitizer;
constructor(pendingTxPtr: PendingTransactionPtr, sanitizer?: Sanitizer) {
this.sanitizer = sanitizer;
this.#pendingTxPtr = pendingTxPtr;
}
async status(): Promise<number> {
return await getSymbol("PendingTransaction_status")(this.#pendingTxPtr);
}
async errorString(): Promise<string | null> {
if (!await this.status()) return null;
const error = await getSymbol("PendingTransaction_errorString")(this.#pendingTxPtr);
if (!error) return null;
return await readCString(error) || null;
}
async throwIfError(sanitize = true): Promise<void> {
const maybeError = await this.errorString();
if (maybeError) {
if (sanitize) this.sanitizer?.();
throw new Error(maybeError);
}
}
async commit(fileName: string, overwrite: boolean, sanitize = true): Promise<boolean> {
const bool = await getSymbol("PendingTransaction_commit")(
this.#pendingTxPtr,
CString(fileName),
overwrite,
);
await this.throwIfError(sanitize);
return bool;
}
async commitUR(maxFragmentLength: number): Promise<string | null> {
const commitUR = getSymbol("PendingTransaction_commitUR");
if (!commitUR) {
return null;
}
const result = await commitUR(
this.#pendingTxPtr,
maxFragmentLength,
);
if (!result) return null;
await this.throwIfError();
return await readCString(result) || null;
}
async amount(): Promise<bigint> {
return await getSymbol("PendingTransaction_amount")(this.#pendingTxPtr);
}
async dust(): Promise<bigint> {
return await getSymbol("PendingTransaction_dust")(this.#pendingTxPtr);
}
async fee(): Promise<bigint> {
return await getSymbol("PendingTransaction_fee")(this.#pendingTxPtr);
}
async txid(separator: string, sanitize = true): Promise<string | null> {
const result = await getSymbol("PendingTransaction_txid")(
this.#pendingTxPtr,
CString(separator),
);
if (!result) return null;
await this.throwIfError(sanitize);
return await readCString(result) || null;
}
async txCount(): Promise<bigint> {
return await getSymbol("PendingTransaction_txCount")(this.#pendingTxPtr);
}
}
|