-
Notifications
You must be signed in to change notification settings - Fork 119
/
ClientActions.js
836 lines (761 loc) · 27.4 KB
/
ClientActions.js
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
import { wallet } from "wallet-preload-shim";
import * as sel from "selectors";
import eq from "lodash/fp/eq";
import isUndefined from "lodash/fp/isUndefined";
import {
getNextAddressAttempt,
getPeerInfo,
publishUnminedTransactionsAttempt,
monitorLockableAccounts
} from "./ControlActions";
import {
transactionNtfnsStart,
accountNtfnsStart
} from "./NotificationActions";
import {
refreshStakepoolPurchaseInformation,
setStakePoolVoteChoices,
getStakepoolStats,
getVSPTicketsByFeeStatus,
setVSPDVoteChoices
} from "./VSPActions";
import { startDex } from "./DexActions";
import { getStartupTransactions } from "./TransactionActions";
import { getAccountMixerServiceAttempt } from "./AccountMixerActions";
import { checkLnWallet } from "./LNActions";
import { push as pushHistory, goBack } from "connected-react-router";
import { getStartupStats } from "./StatisticsActions";
import { getTokenAndInitialBatch } from "./GovernanceActions";
import { discoverAvailableVSPs } from "./VSPActions";
import * as da from "../middleware/dcrdataapi";
import { EXTERNALREQUEST_DCRDATA, EXTERNALREQUEST_POLITEIA } from "constants";
import {
TESTNET,
MAINNET,
VSP_FEE_PROCESS_ERRORED,
VSP_FEE_PROCESS_STARTED,
VSP_FEE_PROCESS_PAID,
VSP_FEE_PROCESS_CONFIRMED
} from "constants";
import * as cfgConstants from "constants/config";
export const goToTransactionHistory = () => (dispatch) => {
dispatch(pushHistory("/transactions/history"));
};
export const goToMyTickets = () => (dispatch) => {
dispatch(pushHistory("/tickets/mytickets"));
};
export const goToError = () => (dispatch) => {
dispatch(pushHistory("/error"));
};
export const goToAccounts = () => (dispatch) => {
dispatch(pushHistory("/accounts"));
};
export const GETWALLETSERVICE_ATTEMPT = "GETWALLETSERVICE_ATTEMPT";
export const GETWALLETSERVICE_FAILED = "GETWALLETSERVICE_FAILED";
export const GETWALLETSERVICE_SUCCESS = "GETWALLETSERVICE_SUCCESS";
export const STARTWALLETSERVICE_ATTEMPT = "STARTWALLETSERVICE_ATTEMPT";
export const STARTWALLETSERVICE_FAILED = "STARTWALLETSERVICE_FAILED";
export const STARTWALLETSERVICE_SUCCESS = "STARTWALLETSERVICE_SUCCESS";
const startWalletServicesTrigger = () => (dispatch, getState) =>
new Promise((resolve, reject) => {
const startServicesAsync = async () => {
const { spvSynced, privacyEnabled, dexEnabled } = getState().walletLoader;
if (!spvSynced) {
dispatch(getTicketBuyerServiceAttempt());
}
if (privacyEnabled) {
dispatch(getAccountMixerServiceAttempt());
}
dispatch(discoverAvailableVSPs());
await dispatch(getNextAddressAttempt(0));
await dispatch(getPeerInfo());
await dispatch(getTicketPriceAttempt());
await dispatch(getNetworkAttempt());
await dispatch(refreshStakepoolPurchaseInformation());
await dispatch(getVotingServiceAttempt());
await dispatch(getAgendaServiceAttempt());
await dispatch(getDecodeMessageServiceAttempt());
await dispatch(getStakepoolStats());
await dispatch(getStartupWalletInfo());
await dispatch(transactionNtfnsStart());
await dispatch(accountNtfnsStart());
// get vsp tickets fee status errored so we can resync them
await dispatch(getVSPTicketsByFeeStatus(VSP_FEE_PROCESS_ERRORED));
await dispatch(getVSPTicketsByFeeStatus(VSP_FEE_PROCESS_STARTED));
await dispatch(getVSPTicketsByFeeStatus(VSP_FEE_PROCESS_PAID));
await dispatch(getVSPTicketsByFeeStatus(VSP_FEE_PROCESS_CONFIRMED));
await dispatch(getVoteChoicesAttempt());
await dispatch(monitorLockableAccounts());
// Start Dex if dexEnabled and NOT SPV mode
if (dexEnabled && !sel.isSPV(getState())) {
await dispatch(startDex());
}
};
startServicesAsync()
.then(() => resolve())
.catch((error) => reject(error));
});
// TODO move startWalletServices to WalletLoaderActions, as it is not related
// to ClientActions.
export const startWalletServices = () => (dispatch, getState) =>
new Promise((resolve, reject) => {
const { startWalletServiceAttempt } = getState().grpc;
if (startWalletServiceAttempt) {
return;
}
dispatch({ type: STARTWALLETSERVICE_ATTEMPT });
dispatch(startWalletServicesTrigger())
.then(() => {
dispatch({ type: STARTWALLETSERVICE_SUCCESS });
resolve();
})
.catch((error) => {
dispatch({ type: STARTWALLETSERVICE_FAILED, error });
reject({ error });
});
});
export const GETSTARTUPWALLETINFO_ATTEMPT = "GETSTARTUPWALLETINFO_ATTEMPT";
export const GETSTARTUPWALLETINFO_SUCCESS = "GETSTARTUPWALLETINFO_SUCCESS";
export const GETSTARTUPWALLETINFO_FAILED = "GETSTARTUPWALLETINFO_FAILED";
export const getStartupWalletInfo = () => (dispatch) => {
dispatch({ type: GETSTARTUPWALLETINFO_ATTEMPT });
const config = wallet.getGlobalCfg();
const dcrdataEnabled =
config.get("allowed_external_requests").indexOf(EXTERNALREQUEST_DCRDATA) >
-1;
const politeiaEnabled =
config.get("allowed_external_requests").indexOf(EXTERNALREQUEST_POLITEIA) >
-1;
return new Promise((resolve, reject) => {
setTimeout(async () => {
try {
await dispatch(getStakeInfoAttempt());
await dispatch(getAccountsAttempt(true));
await dispatch(getStartupTransactions());
await dispatch(getStartupStats());
await dispatch(publishUnminedTransactionsAttempt());
if (dcrdataEnabled) {
dispatch(getTreasuryBalance());
dispatch(getAllAgendasAttempt());
}
if (politeiaEnabled) {
dispatch(getTokenAndInitialBatch());
}
dispatch(checkLnWallet());
dispatch({ type: GETSTARTUPWALLETINFO_SUCCESS });
resolve();
} catch (error) {
dispatch({ error, type: GETSTARTUPWALLETINFO_FAILED });
reject(error);
}
}, 1000);
});
};
export const getWalletServiceAttempt = () => (dispatch, getState) => {
const {
grpc: { address, port }
} = getState();
const {
daemon: { walletName }
} = getState();
dispatch({ type: GETWALLETSERVICE_ATTEMPT });
const grpcCertAndKey = wallet.getDcrwalletGrpcKeyCert();
wallet
.getWalletService(
sel.isTestNet(getState()),
walletName,
address,
port,
grpcCertAndKey,
grpcCertAndKey
)
.then((walletService) =>
dispatch({ walletService, type: GETWALLETSERVICE_SUCCESS })
)
.catch((error) => dispatch({ error, type: GETWALLETSERVICE_FAILED }));
};
export const GETTICKETBUYERSERVICE_ATTEMPT = "GETTICKETBUYERSERVICE_ATTEMPT";
export const GETTICKETBUYERSERVICE_FAILED = "GETTICKETBUYERSERVICE_FAILED";
export const GETTICKETBUYERSERVICE_SUCCESS = "GETTICKETBUYERSERVICE_SUCCESS";
export const getTicketBuyerServiceAttempt = () => (dispatch, getState) => {
const {
grpc: { address, port }
} = getState();
const {
daemon: { walletName }
} = getState();
dispatch({ type: GETTICKETBUYERSERVICE_ATTEMPT });
const grpcCertAndKey = wallet.getDcrwalletGrpcKeyCert();
wallet
.getTicketBuyerService(
sel.isTestNet(getState()),
walletName,
address,
port,
grpcCertAndKey,
grpcCertAndKey
)
.then((ticketBuyerService) => {
dispatch({ ticketBuyerService, type: GETTICKETBUYERSERVICE_SUCCESS });
})
.catch((error) => dispatch({ error, type: GETTICKETBUYERSERVICE_FAILED }));
};
export const getAccountNumbersBalances = (accountNumbers) => (dispatch) => {
accountNumbers.forEach((a) => dispatch(getBalanceUpdateAttempt(a, 0)));
};
const getAccountsBalances = (accounts) => (dispatch, getState) => {
const walletService = sel.walletService(getState());
const chainParams = sel.chainParams(getState());
const hiddenAccounts = sel.hiddenAccounts(getState());
const promises = accounts.map(async (account) => {
const resp = await wallet.getBalance(
walletService,
account.accountNumber,
0
);
return {
accountNumber: account.accountNumber,
accountName: account.accountName,
externalKeys: account.externalKeyCount,
internalKeys: account.internalKeyCount,
importedKeys: account.importedKeyCount,
encrypted: account.accountEncrypted,
unlocked: account.accountUnlocked,
hidden: !!hiddenAccounts.find(eq(account.accountNumber)),
HDPath:
"m / 44' / " +
chainParams.HDCoinType +
"' / " +
account.accountNumber +
"'",
total: resp.total,
spendable: resp.spendable,
immatureReward: resp.immatureReward,
immatureStakeGeneration: resp.immatureStakeGeneration,
lockedByTickets: resp.lockedByTickets,
votingAuthority: resp.votingAuthority,
unconfirmed: resp.unconfirmed
};
});
return Promise.all(promises)
.then((balances) => dispatch({ balances, type: GETBALANCE_SUCCESS }))
.catch((error) => dispatch({ error, type: GETBALANCE_FAILED }));
};
export const GETBALANCE_ATTEMPT = "GETBALANCE_ATTEMPT";
export const GETBALANCE_FAILED = "GETBALANCE_FAILED";
export const GETBALANCE_SUCCESS = "GETBALANCE_SUCCESS";
const getBalanceUpdateSuccess = (accountNumber, getBalanceResponse) => (
dispatch
) => {
const updatedBalance = {
accountNumber,
total: getBalanceResponse.total,
spendable: getBalanceResponse.spendable,
immatureReward: getBalanceResponse.immatureReward,
immatureStakeGeneration: getBalanceResponse.immatureStakeGeneration,
lockedByTickets: getBalanceResponse.lockedByTickets,
votingAuthority: getBalanceResponse.votingAuthority,
unconfirmed: getBalanceResponse.unconfirmed
};
dispatch(updateAccount(updatedBalance));
return updatedBalance;
};
export const getBalanceUpdateAttempt = (accountNumber, requiredConfs) => (
dispatch,
getState
) =>
wallet
.getBalance(sel.walletService(getState()), accountNumber, requiredConfs)
.then((resp) => dispatch(getBalanceUpdateSuccess(accountNumber, resp)))
.catch((error) => dispatch({ error, type: GETBALANCE_FAILED }));
export const GETACCOUNTNUMBER_ATTEMPT = "GETACCOUNTNUMBER_ATTEMPT";
export const GETACCOUNTNUMBER_FAILED = "GETACCOUNTNUMBER_FAILED";
export const GETACCOUNTNUMBER_SUCCESS = "GETACCOUNTNUMBER_SUCCESS";
export const getAccountNumberAttempt = (accountName) => (
dispatch,
getState
) => {
dispatch({ type: GETACCOUNTNUMBER_ATTEMPT });
wallet
.getAccountNumber(sel.walletService(getState()), accountName)
.then((resp) =>
dispatch({
getAccountNumberResponse: resp,
type: GETACCOUNTNUMBER_SUCCESS
})
)
.catch((error) => dispatch({ error, type: GETACCOUNTNUMBER_FAILED }));
};
export const GETBESTBLOCK_ATTEMPT = "GETBESTBLOCK_ATTEMPT";
export const GETBESTBLOCK_FAILED = "GETBESTBLOCK_FAILED";
export const GETBESTBLOCK_SUCCESS = "GETBESTBLOCK_SUCCESS";
export const getBestBlockHeightAttempt = (cb) => (dispatch, getState) =>
new Promise((resolve, reject) => {
dispatch({ type: GETBESTBLOCK_ATTEMPT });
wallet
.bestBlock(sel.walletService(getState()))
.then(async (resp) => {
dispatch({ height: resp.height, type: GETBESTBLOCK_SUCCESS });
if (cb) {
await dispatch(cb());
return resolve();
}
return resolve();
})
.catch((error) => {
dispatch({ error, type: GETBESTBLOCK_FAILED });
reject({ error });
throw error;
});
});
export const GETNETWORK_ATTEMPT = "GETNETWORK_ATTEMPT";
export const GETNETWORK_FAILED = "GETNETWORK_FAILED";
export const GETNETWORK_SUCCESS = "GETNETWORK_SUCCESS";
function getNetworkSuccess(getNetworkResponse) {
return (dispatch, getState) => {
const { testnet, mainnet } = getState().grpc;
const { currentSettings } = getState().settings;
const network = currentSettings.network;
const currentNetwork = getNetworkResponse.activeNetwork;
// XXX remove network magic numbers here
let networkStr = "";
if (
(currentNetwork == testnet && network == TESTNET) ||
(currentNetwork == mainnet && network == MAINNET)
) {
networkStr = network;
getNetworkResponse.networkStr = networkStr;
dispatch({
getNetworkResponse: getNetworkResponse,
type: GETNETWORK_SUCCESS
});
} else {
dispatch({ error: "Invalid network detected", type: GETNETWORK_FAILED });
setTimeout(() => {
dispatch(pushHistory("/walletError"));
}, 1000);
}
};
}
export const getNetworkAttempt = () => (dispatch, getState) => {
dispatch({ type: GETNETWORK_ATTEMPT });
wallet
.getNetwork(sel.walletService(getState()))
.then((resp) => dispatch(getNetworkSuccess(resp)))
.catch((error) => {
dispatch({ error, type: GETNETWORK_FAILED });
setTimeout(() => {
dispatch(pushHistory("/walletError"));
}, 1000);
});
};
export const GETSTAKEINFO_ATTEMPT = "GETSTAKEINFO_ATTEMPT";
export const GETSTAKEINFO_FAILED = "GETSTAKEINFO_FAILED";
export const GETSTAKEINFO_SUCCESS = "GETSTAKEINFO_SUCCESS";
export const getStakeInfoAttempt = () => (dispatch, getState) => {
dispatch({ type: GETSTAKEINFO_ATTEMPT });
wallet
.getStakeInfo(sel.walletService(getState()))
.then((resp) =>
dispatch({ getStakeInfoResponse: resp, type: GETSTAKEINFO_SUCCESS })
)
.catch((error) => dispatch({ error, type: GETSTAKEINFO_FAILED }));
};
export const GETTICKETPRICE_ATTEMPT = "GETTICKETPRICE_ATTEMPT";
export const GETTICKETPRICE_FAILED = "GETTICKETPRICE_FAILED";
export const GETTICKETPRICE_SUCCESS = "GETTICKETPRICE_SUCCESS";
export const getTicketPriceAttempt = () => (dispatch, getState) => {
dispatch({ type: GETTICKETPRICE_ATTEMPT });
wallet
.getTicketPrice(sel.walletService(getState()))
.then((res) =>
dispatch({ getTicketPriceResponse: res, type: GETTICKETPRICE_SUCCESS })
)
.catch((error) => dispatch({ error, type: GETTICKETPRICE_FAILED }));
};
export const GETACCOUNTS_ATTEMPT = "GETACCOUNTS_ATTEMPT";
export const GETACCOUNTS_FAILED = "GETACCOUNTS_FAILED";
export const GETACCOUNTS_SUCCESS = "GETACCOUNTS_SUCCESS";
export const getAccountsAttempt = (startup) => async (dispatch, getState) => {
dispatch({ type: GETACCOUNTS_ATTEMPT });
try {
const response = await wallet.getAccounts(sel.walletService(getState()));
if (startup) await dispatch(getAccountsBalances(response.accountsList));
dispatch({
accounts: response.accountsList,
response,
type: GETACCOUNTS_SUCCESS
});
} catch (error) {
dispatch({ error, type: GETACCOUNTS_FAILED });
}
};
export const UPDATEHIDDENACCOUNTS = "UPDATEHIDDENACCOUNTS";
export const UPDATEACCOUNT_SUCCESS = "UPDATEACCOUNT_SUCCESS";
export function updateAccount(account) {
return (dispatch, getState) => {
const {
grpc: { balances }
} = getState();
const existingAccount = balances.find(
(a) => a.accountNumber === account.accountNumber
);
let updatedBalances;
if (!existingAccount) {
const chainParams = sel.chainParams(getState());
const newAccount = {
immatureReward: 0,
immatureStakeGeneration: 0,
lockedByTickets: 0,
spendable: 0,
total: 0,
votingAuthority: 0,
unconfirmed: 0,
HDPath:
"m / 44' / " +
chainParams.HDCoinType +
"' / " +
account.accountNumber +
"'",
...account
};
updatedBalances = [...balances, newAccount];
} else {
const updatedAccount = { ...existingAccount, ...account };
updatedBalances = balances.map((a) =>
a.accountNumber === account.accountNumber ? updatedAccount : a
);
}
dispatch({ balances: updatedBalances, type: GETBALANCE_SUCCESS });
};
}
export function hideAccount(accountNumber) {
return (dispatch, getState) => {
const {
daemon: { walletName, hiddenAccounts }
} = getState();
const updatedHiddenAccounts = [...hiddenAccounts];
if (updatedHiddenAccounts.indexOf(accountNumber) === -1) {
updatedHiddenAccounts.push(accountNumber);
}
const cfg = wallet.getWalletCfg(sel.isTestNet(getState()), walletName);
cfg.set(cfgConstants.HIDDEN_ACCOUNTS, updatedHiddenAccounts);
dispatch({
hiddenAccounts: updatedHiddenAccounts,
type: UPDATEHIDDENACCOUNTS
});
dispatch(updateAccount({ accountNumber, hidden: true }));
};
}
export function showAccount(accountNumber) {
return (dispatch, getState) => {
const {
daemon: { walletName, hiddenAccounts }
} = getState();
const updatedHiddenAccounts = Array();
for (let i = 0; i < hiddenAccounts.length; i++) {
if (hiddenAccounts[i] !== accountNumber) {
updatedHiddenAccounts.push(hiddenAccounts[i]);
}
}
const cfg = wallet.getWalletCfg(sel.isTestNet(getState()), walletName);
cfg.set(cfgConstants.HIDDEN_ACCOUNTS, updatedHiddenAccounts);
dispatch({
hiddenAccounts: updatedHiddenAccounts,
type: UPDATEHIDDENACCOUNTS
});
dispatch(updateAccount({ accountNumber, hidden: false }));
};
}
export const UPDATETIMESINCEBLOCK = "UPDATETIMESINCEBLOCK";
export function updateBlockTimeSince() {
return (dispatch, getState) => {
const { transactionNtfnsResponse } = getState().notifications;
const { recentBlockTimestamp } = getState().grpc;
if (
transactionNtfnsResponse !== null &&
transactionNtfnsResponse.attachedBlocksList.length > 0
) {
const attachedBlocks = transactionNtfnsResponse.attachedBlocksList;
const lastBlockTimestamp = attachedBlocks[0].timestamp;
if (recentBlockTimestamp != lastBlockTimestamp) {
dispatch({
recentBlockTimestamp: lastBlockTimestamp,
type: UPDATETIMESINCEBLOCK
});
}
}
};
}
export const GETAGENDASERVICE_ATTEMPT = "GETAGENDASERVICE_ATTEMPT";
export const GETAGENDASERVICE_FAILED = "GETAGENDASERVICE_FAILED";
export const GETAGENDASERVICE_SUCCESS = "GETAGENDASERVICE_SUCCESS";
export const getAgendaServiceAttempt = () => (dispatch, getState) => {
const {
grpc: { address, port }
} = getState();
const {
daemon: { walletName }
} = getState();
dispatch({ type: GETAGENDASERVICE_ATTEMPT });
const grpcCertAndKey = wallet.getDcrwalletGrpcKeyCert();
wallet
.getAgendaService(
sel.isTestNet(getState()),
walletName,
address,
port,
grpcCertAndKey,
grpcCertAndKey
)
.then((agendaService) => {
dispatch({ agendaService, type: GETAGENDASERVICE_SUCCESS });
setTimeout(() => {
dispatch(getAgendasAttempt());
}, 10);
})
.catch((error) => dispatch({ error, type: GETAGENDASERVICE_FAILED }));
};
export const GETVOTINGSERVICE_ATTEMPT = "GETVOTINGSERVICE_ATTEMPT";
export const GETVOTINGSERVICE_FAILED = "GETVOTINGSERVICE_FAILED";
export const GETVOTINGSERVICE_SUCCESS = "GETVOTINGSERVICE_SUCCESS";
export const getVotingServiceAttempt = () => (dispatch, getState) => {
const {
grpc: { address, port }
} = getState();
const {
daemon: { walletName }
} = getState();
dispatch({ type: GETVOTINGSERVICE_ATTEMPT });
const grpcCertAndKey = wallet.getDcrwalletGrpcKeyCert();
wallet
.getVotingService(
sel.isTestNet(getState()),
walletName,
address,
port,
grpcCertAndKey,
grpcCertAndKey
)
.then((votingService) =>
dispatch({ votingService, type: GETVOTINGSERVICE_SUCCESS })
)
.catch((error) => dispatch({ error, type: GETVOTINGSERVICE_FAILED }));
};
export const GETDECODEMSGSERVICE_ATTEMPT = "GETDECODEMSGSERVICE_ATTEMPT";
export const GETDECODEMSGSERVICE_FAILED = "GETDECODEMSGSERVICE_FAILED";
export const GETDECODEMSGSERVICE_SUCCESS = "GETDECODEMSGSERVICE_SUCCESS";
export const getDecodeMessageServiceAttempt = () => (dispatch, getState) => {
const {
grpc: { address, port }
} = getState();
const {
daemon: { walletName }
} = getState();
dispatch({ type: GETDECODEMSGSERVICE_ATTEMPT });
const grpcCertAndKey = wallet.getDcrwalletGrpcKeyCert();
wallet
.getDecodeMessageService(
sel.isTestNet(getState()),
walletName,
address,
port,
grpcCertAndKey,
grpcCertAndKey
)
.then((decodeMessageService) =>
dispatch({ decodeMessageService, type: GETDECODEMSGSERVICE_SUCCESS })
)
.catch((error) => dispatch({ error, type: GETDECODEMSGSERVICE_FAILED }));
};
export const GETAGENDAS_ATTEMPT = "GETAGENDAS_ATTEMPT";
export const GETAGENDAS_FAILED = "GETAGENDAS_FAILED";
export const GETAGENDAS_SUCCESS = "GETAGENDAS_SUCCESS";
// getAgendasAttempt gets the current agenda.
export const getAgendasAttempt = () => (dispatch, getState) => {
dispatch({ type: GETAGENDAS_ATTEMPT });
wallet
.getAgendas(sel.agendaService(getState()))
.then((agendas) => dispatch({ agendas, type: GETAGENDAS_SUCCESS }))
.catch((error) => dispatch({ error, type: GETAGENDAS_FAILED }));
};
export const GETALLAGENDAS_ATTEMPT = "GETALLAGENDAS_ATTEMPT";
export const GETALLAGENDAS_FAILED = "GETALLAGENDAS_FAILED";
export const GETALLAGENDAS_SUCCESS = "GETALLAGENDAS_SUCCESS";
// getAllAgendasAttempt gets all agendas.
export const getAllAgendasAttempt = () => (dispatch, getState) => {
dispatch({ type: GETALLAGENDAS_ATTEMPT });
const dURL = sel.dcrdataURL(getState());
da.getAdendasInfo(dURL)
.then((response) => {
const { data } = response;
dispatch({ allAgendas: data, type: GETALLAGENDAS_SUCCESS });
})
.catch((error) => dispatch({ error, type: GETALLAGENDAS_FAILED }));
};
export const GETVOTECHOICES_ATTEMPT = "GETVOTECHOICES_ATTEMPT";
export const GETVOTECHOICES_FAILED = "GETVOTECHOICES_FAILED";
export const GETVOTECHOICES_SUCCESS = "GETVOTECHOICES_SUCCESS";
export const getVoteChoicesAttempt = (stakePool) => (dispatch, getState) => {
dispatch({ type: GETVOTECHOICES_ATTEMPT });
wallet
.getVoteChoices(sel.votingService(getState()))
.then((voteChoices) => {
if (stakePool) {
dispatch(setStakePoolVoteChoices(stakePool, voteChoices));
}
const voteChoicesConfig = voteChoices.choicesList.map((choice) => ({
agendaId: choice.agendaId,
choiceId: choice.choiceId
}));
dispatch({ voteChoicesConfig, type: GETVOTECHOICES_SUCCESS });
})
.catch((error) => dispatch({ error, type: GETVOTECHOICES_FAILED }));
};
export const SETVOTECHOICES_ATTEMPT = "SETVOTECHOICES_ATTEMPT";
export const SETVOTECHOICES_FAILED = "SETVOTECHOICES_FAILED";
export const SETVOTECHOICES_SUCCESS = "SETVOTECHOICES_SUCCESS";
export const setVoteChoicesAttempt = (agendaId, choiceId, passphrase) => (
dispatch,
getState
) => {
dispatch({ payload: { agendaId, choiceId }, type: SETVOTECHOICES_ATTEMPT });
wallet
.setAgendaVote(sel.votingService(getState()), agendaId, choiceId)
.then(() => {
dispatch(setVSPDVoteChoices(passphrase));
const stakePools = sel.configuredStakePools(getState());
for (let i = 0; i < stakePools.length; i++) {
dispatch(getVoteChoicesAttempt(stakePools[i]));
}
dispatch(getVoteChoicesAttempt());
})
.catch((error) => dispatch({ error, type: SETVOTECHOICES_FAILED }));
};
export const GETMESSAGEVERIFICATIONSERVICE_ATTEMPT =
"GETMESSAGEVERIFICATIONSERVICE_ATTEMPT";
export const GETMESSAGEVERIFICATIONSERVICE_FAILED =
"GETMESSAGEVERIFICATIONSERVICE_FAILED";
export const GETMESSAGEVERIFICATIONSERVICE_SUCCESS =
"GETMESSAGEVERIFICATIONSERVICE_SUCCESS";
export const getMessageVerificationServiceAttempt = (dispatch, getState) => {
const {
grpc: { address, port }
} = getState();
const {
daemon: { walletName }
} = getState();
const grpcCertAndKey = wallet.getDcrwalletGrpcKeyCert();
dispatch({ type: GETMESSAGEVERIFICATIONSERVICE_ATTEMPT });
wallet
.getMessageVerificationService(
sel.isTestNet(getState()),
walletName,
address,
port,
grpcCertAndKey,
grpcCertAndKey
)
.then((messageVerificationService) =>
dispatch({
messageVerificationService,
type: GETMESSAGEVERIFICATIONSERVICE_SUCCESS
})
)
.catch((error) =>
dispatch({ error, type: GETMESSAGEVERIFICATIONSERVICE_FAILED })
);
};
export const listenForAppReloadRequest = (cb) => () =>
wallet.onAppReloadRequested(cb);
export const showTicketList = (status) => (dispatch) =>
dispatch(pushHistory("/tickets/mytickets/" + status));
export const showPurchaseTicketsPage = () => (dispatch) =>
dispatch(pushHistory("/tickets/purchase"));
export const goBackHistory = () => (dispatch) => dispatch(goBack());
export const SEEDCOPIEDTOCLIPBOARD = "SEEDCOPIEDTOCLIPBOARD";
export const copySeedToClipboard = (mnemonic) => (dispatch) => {
wallet.copyToClipboard(mnemonic);
dispatch({ type: SEEDCOPIEDTOCLIPBOARD });
};
export const GETTREASURY_BALANCE_SUCCESS = "GETTREASURY_BALANCE_SUCCESS";
export const getTreasuryBalance = () => (dispatch, getState) => {
const treasuryAddress = sel.chainParams(getState()).TreasuryAddress;
const dURL = sel.dcrdataURL(getState());
da.getTreasuryInfo(dURL, treasuryAddress).then((treasuryInfo) => {
// Manually convert DCR to atom amounts to avoid floating point multiplication errors (eg. 589926.57667882*1e8 => 58992657667881.99)
const unspentTreasury = treasuryInfo["data"]["dcr_unspent"];
if (!unspentTreasury) return;
const splitedTreasuryInfo = unspentTreasury.toString().split(".");
const integerPart = splitedTreasuryInfo[0];
// dcrdata can send numbers with its decimal part less than 8 decimals, so we manually add it.
let decimalPart = splitedTreasuryInfo[1];
decimalPart += "0".repeat(8 - decimalPart.length);
const treasuryBalance = integerPart + decimalPart;
dispatch({ treasuryBalance, type: GETTREASURY_BALANCE_SUCCESS });
});
};
export const RESET_TREASURY_BALANCE = "RESET_TREASURY_BALANCE";
export const resetTreasuryBalance = () => (dispatch) => {
dispatch({ type: RESET_TREASURY_BALANCE });
};
export const ABANDONTRANSACTION_ATTEMPT = "ABANDONTRANSACTION_ATTEMPT";
export const ABANDONTRANSACTION_SUCCESS = "ABANDONTRANSACTION_SUCCESS";
export const ABANDONTRANSACTION_FAILED = "ABANDONTRANSACTION_FAILED";
export const abandonTransactionAttempt = (txid) => (dispatch, getState) => {
dispatch({ type: ABANDONTRANSACTION_ATTEMPT });
const state = getState();
wallet
.abandonTransaction(sel.walletService(state), txid)
.then(() => {
const { regularTransactions, recentRegularTransactions } = state.grpc;
// remove from transactions
delete regularTransactions[txid];
const newRecentRegularTransactions = recentRegularTransactions.filter(
(t) => t.txHash !== txid
);
dispatch({
type: ABANDONTRANSACTION_SUCCESS,
regularTransactions,
recentRegularTransactions: newRecentRegularTransactions
});
dispatch(goBack());
})
.catch((error) => dispatch({ error, type: ABANDONTRANSACTION_FAILED }));
};
export const getAcctSpendableBalance = (acctId) => async (
dispatch,
getState
) => {
const acct = await wallet.getBalance(
sel.walletService(getState()),
acctId,
0
);
return acct?.spendable;
};
export const MIXERACCOUNTS_SPENDABLE_BALANCE =
"MIXERACCOUNTS_SPENDABLE_BALANCE";
export const getMixerAcctsSpendableBalances = () => async (
dispatch,
getState
) => {
const mixedAccount = sel.getMixedAccount(getState());
const changeAccount = sel.getChangeAccount(getState());
const balances = {};
if (!isUndefined(mixedAccount)) {
balances.mixedAccountSpendableBalance = await dispatch(
getAcctSpendableBalance(mixedAccount)
);
}
if (!isUndefined(changeAccount)) {
balances.changeAccountSpendableBalance = await dispatch(
getAcctSpendableBalance(changeAccount)
);
}
dispatch({
balances,
type: MIXERACCOUNTS_SPENDABLE_BALANCE
});
};
export const goToHomePage = () => (dispatch) => dispatch(pushHistory("/home"));