-
Notifications
You must be signed in to change notification settings - Fork 249
/
Copy pathWalletService.ts
527 lines (453 loc) · 14.6 KB
/
WalletService.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
import BigNumber from 'bignumber.js';
import type AutoClaim from '../@types/AutoClaim';
import type Connection from '../@types/Connection';
import type FarmedAmount from '../@types/FarmedAmount';
import type OfferSummaryRecord from '../@types/OfferSummaryRecord';
import type PoolWalletStatus from '../@types/PoolWalletStatus';
import type PrivateKey from '../@types/PrivateKey';
import type PuzzleDecorator from '../@types/PuzzleDecorator';
import type TradeRecord from '../@types/TradeRecord';
import type Transaction from '../@types/Transaction';
import type { WalletListItem } from '../@types/Wallet';
import type WalletBalance from '../@types/WalletBalance';
import type WalletCreate from '../@types/WalletCreate';
import Client from '../Client';
import type Message from '../Message';
import ServiceName from '../constants/ServiceName';
import Service from './Service';
import type { Options } from './Service';
export default class Wallet extends Service {
constructor(client: Client, options?: Options) {
super(ServiceName.WALLET, client, options);
}
async getLoggedInFingerprint() {
return this.command<{ fingerprint: number }>('get_logged_in_fingerprint');
}
async getWallets(args?: { includeData: boolean }) {
const { includeData = false } = args || {};
return this.command<{ fingerprint: number; wallets: WalletListItem[] }>('get_wallets', {
includeData,
});
}
async getTransaction(args: { transactionId: string }) {
return this.command<{ transaction: Transaction; transactionId: string }>('get_transaction', args);
}
async getTransactionMemo(args: { transactionId: string }) {
return this.command<{
[transactionId: string]: {
[coinId: string]: string[];
};
}>('get_transaction_memo', args);
}
async getPwStatus(args: { walletId: number }) {
return this.command<{ state: PoolWalletStatus; unconfirmedTransactions: Transaction[] }>('pw_status', args);
}
async pwAbsorbRewards(args: { walletId: number; fee?: string }) {
return this.command<{ state: PoolWalletStatus; transaction: Transaction }>('pw_absorb_rewards', args);
}
async pwJoinPool(args: {
walletId: number;
poolUrl: string;
relativeLockHeight: number;
targetPuzzlehash?: string;
fee?: string;
}) {
return this.command<{ totalFee: number; transaction: Transaction }>('pw_join_pool', args);
}
async pwSelfPool(args: { walletId: number; fee?: string }) {
return this.command<{ totalFee: number; transaction: Transaction }>('pw_self_pool', args);
}
async createNewWallet(args: {
walletType: 'pool_wallet' | 'rl_wallet' | 'did_wallet' | 'cat_wallet';
options: Object;
}) {
return this.command<WalletCreate>('create_new_wallet', {
walletType: args.walletType,
...args.options,
});
}
async deleteUnconfirmedTransactions(args: { walletId: number }) {
return this.command<void>('delete_unconfirmed_transactions', args);
}
async getWalletBalance(args: { walletId: number }) {
return this.command<{ walletBalance: WalletBalance }>('get_wallet_balance', args);
}
async getWalletBalances(args?: { walletIds: number[] }) {
return this.command<{ walletBalances: WalletBalance[] }>('get_wallet_balances', args);
}
async getFarmedAmount() {
return this.command<FarmedAmount>('get_farmed_amount');
}
async sendTransaction(args: {
walletId: number;
amount: BigNumber;
fee: BigNumber;
address: string;
memos?: string[];
puzzleDecorator?: PuzzleDecorator[];
}) {
return this.command<{ transaction: Transaction; transactionId: string }>('send_transaction', args);
}
async generateMnemonic() {
return this.command<{
mnemonic: string[];
}>('generate_mnemonic');
}
async getPublicKeys() {
return this.command<{
publicKeyFingerprints: number[];
}>('get_public_keys');
}
async addKey(args: { mnemonic: string[]; type: 'new_wallet' | 'skip' | 'restore_backup' }) {
return this.command<{ fingerprint: number }>('add_key', args);
}
async deleteKey(args: { fingerprint: number }) {
return this.command<void>('delete_key', args);
}
async checkDeleteKey(args: { fingerprint: number }) {
return this.command<{
fingerprint: number;
usedForFarmerRewards: boolean;
usedForPoolRewards: boolean;
walletBalance: boolean;
}>('check_delete_key', args);
}
async deleteAllKeys() {
return this.command<void>('delete_all_keys');
}
async logIn(args: {
fingerprint: number;
type?: 'normal' | 'skip' | 'restore_backup'; // skip is used to skip import
}) {
const { fingerprint, type = 'normal' } = args;
return this.command<{ fingerprint: number }>('log_in', { fingerprint, type });
}
async getPrivateKey(args: { fingerprint: number }) {
return this.command<{ privateKey: PrivateKey }>('get_private_key', args);
}
async getTransactions(args: {
walletId: number;
start?: number;
end?: number;
sortKey?: 'CONFIRMED_AT_HEIGHT' | 'RELEVANCE';
typeFilter?: {
mode: number;
values: number[];
};
reverse?: boolean;
confirmed?: boolean;
}) {
return this.command<{ transactions: Transaction[]; walletId: number }>('get_transactions', args);
}
async getTransactionsCount(args: {
walletId: number;
typeFilter?: {
mode: number;
values: number[];
};
confirmed?: boolean;
}) {
return this.command<{ count: number; walletId: number }>('get_transaction_count', args);
}
async getNextAddress(args: { walletId: number; newAddress: boolean }) {
return this.command<{
address: string;
walletId: number;
}>('get_next_address', args);
}
async farmBlock(args: { address: string }) {
return this.command<void>('farm_block', args);
}
async getTimestampForHeight(args: { height: number }) {
return this.command<{
timestamp: number;
}>('get_timestamp_for_height', args);
}
async getHeightInfo() {
return this.command<{ height: number }>('get_height_info');
}
async getNetworkInfo() {
return this.command<{ networkName: string; networkPrefix: string }>('get_network_info');
}
async getSyncStatus() {
return this.command<{
genesisInitialized: boolean;
synced: boolean;
syncing: boolean;
}>('get_sync_status');
}
async getConnections() {
return this.command<{ connections: Connection[] }>('get_connections');
}
async getAllOffers(args: {
start?: number;
end?: number;
sortKey?: 'CONFIRMED_AT_HEIGHT' | 'RELEVANCE';
reverse?: boolean;
includeMyOffers?: boolean;
includeTakenOffers?: boolean;
}) {
return this.command<{ offers: string[]; tradeRecords: TradeRecord[] }>('get_all_offers', {
includeCompleted: true,
fileContents: true,
start: args.start,
end: args.end,
sortKey: args.sortKey,
reverse: args.reverse,
excludeMyOffers: !args.includeMyOffers,
excludeTakenOffers: !args.includeTakenOffers,
});
}
async getOffersCount() {
return this.command<{ myOffersCount: number; takenOffersCount: number; total: number }>('get_offers_count');
}
async createOfferForIds(args: {
offer: { [key: string]: number | BigNumber };
fee: number | BigNumber;
driverDict: any;
validateOnly?: boolean;
disableJSONFormatting?: boolean;
maxTime?: number;
offerOnly?: boolean;
}) {
const { disableJSONFormatting, driverDict, ...restArgs } = args;
return this.command<{ offer: string; tradeRecord: TradeRecord }>(
'create_offer_for_ids',
{ driver_dict: driverDict, ...restArgs },
false,
undefined,
disableJSONFormatting,
);
}
async cancelOffer(args: { tradeId: string; secure: boolean; fee: number | string }) {
return this.command<void>('cancel_offer', args);
}
async checkOfferValidity(args: { offer: string }) {
return this.command<{ id: string; valid: boolean }>('check_offer_validity', args);
}
async takeOffer(args: { offer: string; fee: number | string }) {
return this.command<{ tradeRecord: TradeRecord }>('take_offer', args);
}
async getOfferSummary({ offerData }: { offerData: string }) {
return this.command<{ id: string; summary: OfferSummaryRecord }>('get_offer_summary', {
offer: offerData,
});
}
// TODO refactor the getOfferData and getOfferRecord into get_offer, to match the backend
async getOfferData({ offerId }: { offerId: string }) {
return this.command<{ offer: string; tradeRecord: TradeRecord }>('get_offer', {
tradeId: offerId,
fileContents: true,
});
}
async getOfferRecord({ offerId }: { offerId: string }) {
return this.command<{ offer: null; tradeRecord: TradeRecord }>('get_offer', {
tradeId: offerId,
fileContents: false,
});
}
async getCurrentDerivationIndex() {
return this.command<{ index: number }>('get_current_derivation_index');
}
async extendDerivationIndex(args: { index: number }) {
return this.command<{ index: number }>('extend_derivation_index', args);
}
async signMessageByAddress(args: { address: string; message: string }) {
return this.command<{
pubkey: string;
signature: string;
isHex: boolean;
safeMode: boolean;
}>('sign_message_by_address', args);
}
async signMessageById(args: { id: string; message: string }) {
return this.command<{
pubkey: string;
signature: string;
latestCoinId: string;
}>('sign_message_by_id', args);
}
// notifications
async getNotifications(args: { ids?: string[]; start?: number; end?: number }) {
return this.command<{
notifications: {
id: string;
message: string;
amount: string;
height: number;
}[];
}>('get_notifications', args);
}
async deleteNotifications(args: { ids?: string[] }) {
return this.command<void>('delete_notifications', args);
}
async sendNotification(args: { target: string; message: string; amount: string | number; fee: string | number }) {
return this.command<{
tx: Transaction;
}>('send_notification', args);
}
async verifySignature(args: {
message: string;
pubkey: string;
signature: string;
address?: string;
signingMode?: string;
}) {
return this.command<{ isValid: boolean }>('verify_signature', args);
}
async resyncWallet() {
return this.command<void>('set_wallet_resync_on_startup');
}
// Clawback
async setAutoClaim(args: AutoClaim) {
return this.command<AutoClaim>('set_auto_claim', args);
}
async getAutoClaim() {
return this.command<AutoClaim>('get_auto_claim');
}
async spendClawbackCoins(args: { coinIds: string[]; fee: number | BigNumber }) {
return this.command<{
transactionIds: string[];
}>('spend_clawback_coins', args);
}
onSyncChanged(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onStateChanged('sync_changed', callback, processData);
}
onNewBlock(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onStateChanged('new_block', callback, processData);
}
onNewPeak(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onStateChanged('new_peak', callback, processData);
}
onCoinAdded(
callback: (
data: {
additionalData: Object;
state: 'coin_added';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('coin_added', callback);
}
onCoinRemoved(
callback: (
data: {
additionalData: Object;
state: 'coin_removed';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('coin_removed', callback);
}
onWalletCreated(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onStateChanged('wallet_created', callback, processData);
}
onConnections(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onCommand('get_connections', callback, processData);
}
onTransactionUpdate(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onStateChanged('tx_update', callback, processData);
}
onPendingTransaction(callback: (data: any, message: Message) => void, processData?: (data: any) => any) {
return this.onStateChanged('pending_transaction', callback, processData);
}
onOfferAdded(callback: (data: any, message: Message) => void) {
return this.onStateChanged('offer_added', callback);
}
onOfferUpdated(callback: (data: any, message: Message) => void) {
return this.onStateChanged('offer_cancelled', callback);
}
onNewOnChainNotification(callback: (data: any, message: Message) => void) {
return this.onStateChanged('new_on_chain_notification', callback);
}
onNFTCoinAdded(
callback: (
data: {
additionalData: Object;
state: 'nft_coin_added';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('nft_coin_added', callback);
}
onNFTCoinRemoved(
callback: (
data: {
additionalData: Object;
state: 'nft_coin_removed';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('nft_coin_removed', callback);
}
onNFTCoinUpdated(
callback: (
data: {
additionalData: Object;
state: 'nft_coin_updated';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('nft_coin_updated', callback);
}
onVCCoinAdded(
callback: (
data: {
additionalData: Object;
state: 'vc_coin_added';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('vc_coin_added', callback);
}
onVCCoinRemoved(
callback: (
data: {
additionalData: Object;
state: 'vc_coin_removed';
walletId: number;
},
message: Message,
) => void,
) {
return this.onStateChanged('vc_coin_removed', callback);
}
onNewDerivationIndex(
callback: (
data: {
additionalData: {
index: number;
};
},
message: Message,
) => void,
processData?: (data: any) => any,
) {
return this.onStateChanged('new_derivation_index', callback, processData);
}
onNFTCoinDIDSet(
callback: (
data: {
additionalData: {
index: number;
};
},
message: Message,
) => void,
processData?: (data: any) => any,
) {
return this.onStateChanged('nft_coin_did_set', callback, processData);
}
}