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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import { fns } from "./bindings.ts";
import { C_SEPARATOR, CString, maybeMultipleStrings, readCString, SEPARATOR } from "./utils.ts";
export type TransactionInfoPtr = Deno.PointerObject<"pendingTransaction">;
export interface TransferData {
address: string | null;
amount: bigint;
}
export class TransactionInfo<MultDest extends boolean = boolean> {
#ptr: TransactionInfoPtr;
#amount!: bigint;
#fee!: bigint;
#timestamp!: bigint;
#transfersCount!: number;
#paymentId!: string | null;
#hash!: string | null;
#subaddrAccount!: number;
#subaddrIndex!: string | null;
#transfers!: readonly TransferData[];
constructor(ptr: TransactionInfoPtr) {
this.#ptr = ptr;
}
static async new(ptr: TransactionInfoPtr): Promise<TransactionInfo> {
const instance = new TransactionInfo(ptr);
const [amount, paymentId, fee, hash, subaddrIndex, subaddrAccount, timestamp, transfersCount] = await Promise.all([
fns.TransactionInfo_amount(ptr),
fns.TransactionInfo_paymentId(ptr).then(readCString),
fns.TransactionInfo_fee(ptr),
fns.TransactionInfo_hash(ptr).then(readCString),
fns.TransactionInfo_subaddrIndex(ptr, C_SEPARATOR).then(readCString),
fns.TransactionInfo_subaddrAccount(ptr),
fns.TransactionInfo_timestamp(ptr),
fns.TransactionInfo_transfers_count(ptr),
]);
instance.#amount = amount;
instance.#fee = fee;
instance.#timestamp = timestamp;
instance.#transfersCount = transfersCount;
instance.#paymentId = paymentId;
instance.#hash = hash;
instance.#subaddrAccount = subaddrAccount;
instance.#subaddrIndex = subaddrIndex;
const transfers = [];
for (let i = 0; i < transfersCount; ++i) {
const [amount, address] = await Promise.all([
fns.TransactionInfo_transfers_amount(ptr, i),
fns.TransactionInfo_transfers_address(ptr, i).then(readCString),
]);
transfers.push({ amount, address });
}
Object.freeze(transfers);
instance.#transfers = transfers;
return instance;
}
get amount(): bigint {
return this.#amount;
}
get fee(): bigint {
return this.#fee;
}
get timestamp(): bigint {
return this.#timestamp;
}
get transfersCount(): number {
return this.#transfersCount;
}
get paymentId(): string | null {
return this.#paymentId;
}
get hash(): string | null {
return this.#hash;
}
get subaddrAccount(): number {
return this.#subaddrAccount;
}
get subaddrIndex(): string | null {
return this.#subaddrIndex;
}
get transfers(): readonly TransferData[] {
return this.#transfers;
}
async direction(): Promise<"in" | "out"> {
switch (await fns.TransactionInfo_direction(this.#ptr)) {
case 0:
return "in";
case 1:
return "out";
default:
throw new Error("Invalid TransactionInfo direction");
}
}
async description(): Promise<string | null> {
return await readCString(
await fns.TransactionInfo_description(this.#ptr),
);
}
async label(): Promise<string | null> {
return await readCString(
await fns.TransactionInfo_label(this.#ptr),
);
}
async confirmations(): Promise<bigint> {
return await fns.TransactionInfo_confirmations(this.#ptr);
}
async unlockTime(): Promise<bigint> {
return await fns.TransactionInfo_unlockTime(this.#ptr);
}
async isPending(): Promise<boolean> {
return await fns.TransactionInfo_isPending(this.#ptr);
}
async isFailed(): Promise<boolean> {
return await fns.TransactionInfo_isFailed(this.#ptr);
}
async isCoinbase(): Promise<boolean> {
return await fns.TransactionInfo_isCoinbase(this.#ptr);
}
}
|