-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtransaction.dto.ts
319 lines (245 loc) · 7 KB
/
transaction.dto.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* eslint-disable */
import { Coins, Contracts } from "@ardenthq/sdk";
import { DateTime } from "@ardenthq/sdk-intl";
import { BigNumber } from "@ardenthq/sdk-helpers";
import { IExchangeRateService, IReadWriteWallet } from "./contracts.js";
import { container } from "./container.js";
import { Identifiers } from "./container.models.js";
export interface ExtendedTransactionRecipient {
address: string;
amount: number;
}
export class ExtendedConfirmedTransactionData implements Contracts.ConfirmedTransactionData {
readonly #wallet: IReadWriteWallet;
readonly #coin: Coins.Coin;
readonly #data: Contracts.ConfirmedTransactionData;
public constructor(wallet: IReadWriteWallet, data: Contracts.ConfirmedTransactionData) {
this.#wallet = wallet;
this.#coin = wallet.coin();
this.#data = data;
}
public id(): string {
return this.#data.id();
}
public blockId(): string | undefined {
return this.#data.blockId();
}
public type(): string {
return this.#data.type();
}
public timestamp(): DateTime | undefined {
return this.#data.timestamp();
}
public confirmations(): BigNumber {
return this.#data.confirmations();
}
public sender(): string {
return this.#data.sender();
}
public recipient(): string {
return this.#data.recipient();
}
// @ts-ignore
public recipients(): ExtendedTransactionRecipient[] {
/* istanbul ignore next */
return this.#data.recipients().map(({ address, amount }) => ({ address, amount: amount.toHuman() }));
}
// @ts-ignore
public amount(): number {
return this.#data.amount().toHuman();
}
public convertedAmount(): number {
return this.#convertAmount(this.amount());
}
// @ts-ignore
public fee(): number {
return this.#data.fee().toHuman();
}
public convertedFee(): number {
return this.#convertAmount(this.fee());
}
public memo(): string | undefined {
// @ts-ignore
return this.#data.memo?.();
}
public asset(): Record<string, unknown> {
return this.#data.asset();
}
public isConfirmed(): boolean {
return this.#data.isConfirmed();
}
public inputs(): Contracts.UnspentTransactionData[] {
return this.#data.inputs();
}
public outputs(): Contracts.UnspentTransactionData[] {
return this.#data.outputs();
}
public isSent(): boolean {
return this.#data.isSent();
}
public isReceived(): boolean {
return this.#data.isReceived();
}
public isReturn(): boolean {
return this.#data.isReturn();
}
public isTransfer(): boolean {
return this.#data.isTransfer();
}
public isSecondSignature(): boolean {
return this.#data.isSecondSignature();
}
public isDelegateRegistration(): boolean {
return this.#data.isDelegateRegistration();
}
public isVoteCombination(): boolean {
return this.#data.isVoteCombination();
}
public isVote(): boolean {
return this.#data.isVote();
}
public isUnvote(): boolean {
return this.#data.isUnvote();
}
public isMultiSignatureRegistration(): boolean {
return this.#data.isMultiSignatureRegistration();
}
public isIpfs(): boolean {
return this.#data.isIpfs();
}
public isMultiPayment(): boolean {
return this.#data.isMultiPayment();
}
public isDelegateResignation(): boolean {
return this.#data.isDelegateResignation();
}
public isHtlcLock(): boolean {
return this.#data.isHtlcLock();
}
public isHtlcClaim(): boolean {
return this.#data.isHtlcClaim();
}
public isHtlcRefund(): boolean {
return this.#data.isHtlcRefund();
}
public isMagistrate(): boolean {
return this.#data.isMagistrate();
}
public username(): string {
return this.data<Contracts.ConfirmedTransactionData>().username();
}
public lockTransactionId(): string {
return this.data<Contracts.ConfirmedTransactionData>().lockTransactionId();
}
public unlockSecret(): string {
return this.data<Contracts.ConfirmedTransactionData>().unlockSecret();
}
public secretHash(): string {
return this.data<Contracts.ConfirmedTransactionData>().secretHash();
}
public expirationType(): number {
return this.data<Contracts.ConfirmedTransactionData>().expirationType();
}
public expirationValue(): number {
return this.data<Contracts.ConfirmedTransactionData>().expirationValue();
}
public hash(): string {
return this.data<Contracts.ConfirmedTransactionData>().hash();
}
// @ts-ignore
public payments(): { recipientId: string; amount: number }[] {
// @ts-ignore
return this.data<Contracts.ConfirmedTransactionData>()
.payments()
.map((payment: { recipientId: string; amount: BigNumber }) => ({
recipientId: payment.recipientId,
amount: payment.amount.toHuman(),
}));
}
public publicKeys(): string[] {
return this.data<Contracts.ConfirmedTransactionData>().publicKeys();
}
public min(): number {
return this.data<Contracts.ConfirmedTransactionData>().min();
}
public secondPublicKey(): string {
return this.data<Contracts.ConfirmedTransactionData>().secondPublicKey();
}
public votes(): string[] {
return this.data<Contracts.ConfirmedTransactionData>().votes();
}
public unvotes(): string[] {
return this.data<Contracts.ConfirmedTransactionData>().unvotes();
}
public explorerLink(): string {
return this.#coin.link().transaction(this.id());
}
public explorerLinkForBlock(): string | undefined {
if (this.blockId()) {
return this.#coin.link().block(this.blockId()!);
}
return undefined;
}
public toObject(): Contracts.KeyValuePair {
return this.#data.toObject();
}
public hasPassed(): boolean {
return this.#data.hasPassed();
}
public hasFailed(): boolean {
return this.#data.hasFailed();
}
public getMeta(key: string): Contracts.TransactionDataMeta {
return this.#data.getMeta(key);
}
public setMeta(key: string, value: Contracts.TransactionDataMeta): void {
return this.#data.setMeta(key, value);
}
/**
* These methods serve as helpers to aggregate commonly used values.
*/
public total(): number {
if (this.isReturn()) {
return this.amount() - this.fee();
}
if (this.isSent()) {
return this.amount() + this.fee();
}
let total = this.amount();
if (this.isMultiPayment()) {
for (const recipient of this.recipients()) {
if (recipient.address !== this.wallet().address()) {
total -= recipient.amount;
}
}
}
return total;
}
public convertedTotal(): number {
return this.#convertAmount(this.total());
}
/**
* These methods serve as helpers to quickly access entities related to the transaction.
*
* These are subject to be removed at any time due to them primarily existing for usage
* in the Desktop and Mobile Wallet. Use them at your own risk in your own applications.
*/
public wallet(): IReadWriteWallet {
return this.#wallet;
}
public coin(): Coins.Coin {
return this.#coin;
}
protected data<T>(): T {
return this.#data as unknown as T;
}
#convertAmount(value: number): number {
const timestamp: DateTime | undefined = this.timestamp();
if (timestamp === undefined) {
return 0;
}
return container
.get<IExchangeRateService>(Identifiers.ExchangeRateService)
.exchange(this.wallet().currency(), this.wallet().exchangeCurrency(), timestamp, value);
}
}