/
VotingPage.js
401 lines (380 loc) · 13.6 KB
/
VotingPage.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
// @flow
import type { Node } from 'react';
import { Component } from 'react';
import { observer } from 'mobx-react';
import { computed } from 'mobx';
import { defineMessages, intlShape } from 'react-intl';
import type { $npm$ReactIntl$IntlFormat } from 'react-intl';
import type { InjectedOrGenerated } from '../../../types/injectedPropsType';
import Voting from '../../../components/wallet/voting/Voting';
import VotingRegistrationDialogContainer from '../dialogs/voting/VotingRegistrationDialogContainer';
import type { GeneratedData as VotingRegistrationDialogContainerData } from '../dialogs/voting/VotingRegistrationDialogContainer';
import { handleExternalLinkClick } from '../../../utils/routing';
import {
WalletTypeOption,
} from '../../../api/ada/lib/storage/models/ConceptualWallet/interfaces';
import { PublicDeriver } from '../../../api/ada/lib/storage/models/PublicDeriver/index';
import LoadingSpinner from '../../../components/widgets/LoadingSpinner';
import VerticallyCenteredLayout from '../../../components/layout/VerticallyCenteredLayout';
import { CATALYST_MIN_AMOUNT, CATALYST_DISPLAYED_MIN_AMOUNT } from '../../../config/numbersConfig';
import InsufficientFundsPage from './InsufficientFundsPage';
import { getTokenName, genLookupOrFail } from '../../../stores/stateless/tokenHelpers';
import type { TokenInfoMap } from '../../../stores/toplevel/TokenInfoStore';
import environment from '../../../environment';
import { MultiToken } from '../../../api/common/lib/MultiToken';
import RegistrationOver from './RegistrationOver';
import type { DelegationRequests } from '../../../stores/toplevel/DelegationStore';
import {
isLedgerNanoWallet,
isTrezorTWallet,
} from '../../../api/ada/lib/storage/models/ConceptualWallet/index';
import type { CatalystRoundInfoResponse } from '../../../api/ada/lib/state-fetch/types'
export type GeneratedData = typeof VotingPage.prototype.generated;
type Props = {|
...InjectedOrGenerated<GeneratedData>,
|};
const messages: * = defineMessages({
mainTitle: {
id: 'wallet.registrationOver.mainTitle',
defaultMessage: '!!!Registration is now closed.',
},
mainSubtitle: {
id: 'wallet.registrationOver.mainSubtitle',
defaultMessage: '!!!The registration period for fund {roundNumber} has ended. For more information, check the Catalyst app.',
},
unavailableTitle: {
id: 'wallet.registrationOver.unavailableTitle',
defaultMessage: '!!!Catalyst Round information is currently unavailable.',
},
unavailableSubtitle: {
id: 'wallet.registrationOver.unavailableSubtitle',
defaultMessage: '!!!Please check the Catalyst app for more info',
},
earlyForRegistrationTitle: {
id: 'wallet.registrationOver.earlyForRegistrationTitle',
defaultMessage: '!!!Registration hasn\'t started yet.'
},
earlyForRegistrationSubTitle: {
id: 'wallet.registrationOver.earlyForRegistrationSubTitle',
defaultMessage: '!!!Registration for Round {roundNumber} begins at {registrationStart}.'
},
beforeVotingSubtitle: {
id: 'wallet.registrationOver.beforeVotingSubtitle',
defaultMessage: '!!!Registration has ended. Voting starts at {votingStart}'
},
betweenVotingSubtitle: {
id: 'wallet.registrationOver.betweenVotingSubtitle',
defaultMessage: '!!!"Registration has ended. Voting ends at {votingEnd}'
},
nextFundRegistration: {
id: 'wallet.registrationOver.nextFundRegistration',
defaultMessage: 'Round {roundNumber} starts at {registrationStart}'
}
});
@observer
export default class VotingPage extends Component<Props> {
static contextTypes: {| intl: $npm$ReactIntl$IntlFormat |} = { intl: intlShape.isRequired };
onClose: void => void = () => {
this.generated.actions.dialogs.closeActiveDialog.trigger();
};
start: void => void = () => {
this.generated.actions.dialogs.open.trigger({ dialog: VotingRegistrationDialogContainer });
};
get isDelegated(): ?boolean {
const publicDeriver = this.generated.stores.wallets.selected;
const delegationStore = this.generated.stores.delegation;
if (!publicDeriver) {
throw new Error(`${nameof(this.isDelegated)} no public deriver. Should never happen`);
}
const delegationRequests = delegationStore.getDelegationRequests(publicDeriver);
if (delegationRequests == null) {
throw new Error(`${nameof(this.isDelegated)} called for non-reward wallet`);
}
const currentDelegation = delegationRequests.getCurrentDelegation;
if (
!currentDelegation.wasExecuted ||
currentDelegation.isExecuting ||
currentDelegation.result == null
) {
return undefined;
}
if(
!currentDelegation.result.currEpoch ||
currentDelegation.result.currEpoch.pools.length === 0
) {
return false;
}
return true;
}
render(): Node {
const { intl } = this.context
const {
uiDialogs,
wallets: { selected },
} = this.generated.stores;
let activeDialog = null;
if(selected == null){
throw new Error(`${nameof(VotingPage)} no wallet selected`);
}
const balance = this.generated.balance;
if (balance == null) {
return (
<VerticallyCenteredLayout>
<LoadingSpinner />
</VerticallyCenteredLayout>
);
}
// keep enabled on the testnet
const { catalystRoundInfo, loadingCatalystRoundInfo } =
this.generated.stores.substores.ada.votingStore;
if (loadingCatalystRoundInfo) {
return (
<VerticallyCenteredLayout>
<LoadingSpinner />
</VerticallyCenteredLayout>
);
}
if (!environment.isTest()) {
if (!catalystRoundInfo || (!catalystRoundInfo.currentFund && !catalystRoundInfo.nextFund)){
return (
<RegistrationOver
title={intl.formatMessage(messages.unavailableTitle)}
subtitle={intl.formatMessage(messages.unavailableSubtitle)}
/>
);
}
const { currentFund, nextFund } = catalystRoundInfo;
const nextFundRegistrationSubtitle = intl.formatMessage(messages.nextFundRegistration, {
roundNumber: nextFund?.id,
registrationStart: nextFund?.registrationStart
})
if (currentFund) {
const isLate = new Date() >= new Date(Date.parse(currentFund.registrationEnd))
const isEarly = new Date() <= new Date(Date.parse(currentFund.registrationStart))
const isBeforeVoting = new Date() <= new Date(Date.parse(currentFund.votingStart))
const isAfterVoting = new Date() >= new Date(Date.parse(currentFund.votingEnd))
const isBetweenVoting = !isBeforeVoting && !isAfterVoting;
if (isEarly) {
return (
<RegistrationOver
title={intl.formatMessage(messages.earlyForRegistrationTitle)}
subtitle={intl.formatMessage(messages.earlyForRegistrationSubTitle, {
roundNumber: currentFund.id,
registrationStart: currentFund.registrationStart
})}
/>
);
}
// registeration is ended -> check for voting start and end dates
if (isLate) {
if (isBeforeVoting) {
return (
<RegistrationOver
title={intl.formatMessage(messages.mainTitle)}
subtitle={intl.formatMessage(messages.beforeVotingSubtitle, {
votingStart: currentFund.votingStart
})}
/>
);
}
if (isBetweenVoting) {
return (
<RegistrationOver
title={intl.formatMessage(messages.mainTitle)}
subtitle={intl.formatMessage(messages.betweenVotingSubtitle, {
votingEnd: currentFund.votingEnd
})}
/>
);
}
if (isAfterVoting) {
/* if we after the voting date (= between funds) and no next funds
will dispaly "round is over" */
let subtitle = intl.formatMessage(messages.mainSubtitle, {
roundNumber: currentFund.id
})
// Check for the next funds if we are after voting
if(nextFund) {
subtitle = nextFundRegistrationSubtitle
}
return (
<RegistrationOver
title={intl.formatMessage(messages.mainTitle)}
subtitle={subtitle}
/>
);
}
}
} else if (nextFund) {
// No current funds -> check for next funds
return (
<RegistrationOver
title={intl.formatMessage(messages.mainTitle)}
subtitle={nextFundRegistrationSubtitle}
/>
);
}
}
// disable the minimum on E2E tests
if (!environment.isTest() && balance.getDefaultEntry().amount.lt(CATALYST_MIN_AMOUNT)) {
const getTokenInfo = genLookupOrFail(this.generated.stores.tokenInfoStore.tokenInfo);
const tokenInfo = getTokenInfo({
identifier: selected.getParent().getDefaultToken().defaultIdentifier,
networkId: selected.getParent().getDefaultToken().defaultNetworkId,
});
return <InsufficientFundsPage
currentBalance={
balance.getDefaultEntry().amount.shiftedBy(-tokenInfo.Metadata.numberOfDecimals)
}
requiredBalance={
CATALYST_DISPLAYED_MIN_AMOUNT.shiftedBy(-tokenInfo.Metadata.numberOfDecimals)
}
tokenName={getTokenName(tokenInfo)}
/>;
}
let walletType;
if (
selected.getParent().getWalletType() !== WalletTypeOption.HARDWARE_WALLET
) {
walletType = 'mnemonic';
} else if (isTrezorTWallet(selected.getParent())) {
walletType = 'trezorT';
} else if (isLedgerNanoWallet(selected.getParent())) {
walletType = 'ledgerNano';
} else {
throw new Error(`${nameof(VotingPage)} unexpected wallet type`);
}
if (uiDialogs.isOpen(VotingRegistrationDialogContainer)) {
activeDialog = (
<VotingRegistrationDialogContainer
{...this.generated.VotingRegistrationDialogProps}
onClose={this.onClose}
walletType={walletType}
/>
);
}
/*
At this point we are sure that we have current funds
I added the "5" for two reasons
1. As a placeholder as the component will not be rendered without it.
2. this page you can see it in test environment even if you
out of the registration dates.
*/
const round = catalystRoundInfo?.currentFund?.id || catalystRoundInfo?.nextFund?.id || 5
const fundName = catalystRoundInfo?.currentFund?.name || round.toString();
return (
//
<div>
{activeDialog}
<Voting
start={this.start}
hasAnyPending={this.generated.hasAnyPending}
onExternalLinkClick={handleExternalLinkClick}
isDelegated={this.isDelegated === true}
name={fundName}
walletType={walletType}
/>
</div>
);
}
@computed get generated(): {|
VotingRegistrationDialogProps: InjectedOrGenerated<VotingRegistrationDialogContainerData>,
actions: {|
dialogs: {|
closeActiveDialog: {|
trigger: (params: void) => void,
|},
open: {|
trigger: (params: {|
dialog: any,
params?: any,
|}) => void,
|},
|},
|},
hasAnyPending: boolean,
balance: ?MultiToken,
stores: {|
uiDialogs: {|
isOpen: any => boolean,
|},
tokenInfoStore: {|
tokenInfo: TokenInfoMap,
|},
wallets: {|
selected: null | PublicDeriver<>,
|},
delegation: {|
getDelegationRequests: (PublicDeriver<>) => void | DelegationRequests,
|},
substores: {|
ada: {|
votingStore: {|
catalystRoundInfo: ?CatalystRoundInfoResponse,
loadingCatalystRoundInfo: boolean,
|}
|}
|}
|},
|} {
if (this.props.generated !== undefined) {
return this.props.generated;
}
if (this.props.stores == null || this.props.actions == null) {
throw new Error(`${nameof(VotingPage)} no way to generated props`);
}
const { stores, actions } = this.props;
const txInfo = (() => {
const selected = stores.wallets.selected;
if (selected == null) return {
hasAnyPending: false,
balance: null,
};
const txRequests = stores.transactions.getTxRequests(selected);
return {
hasAnyPending: (txRequests.requests.pendingRequest.result ?? []).length > 0,
// note: Catalyst balance depends on UTXO balance -- not on rewards
balance: txRequests.requests.getBalanceRequest.result,
};
})();
return Object.freeze({
...txInfo,
actions: {
dialogs: {
closeActiveDialog: {
trigger: actions.dialogs.closeActiveDialog.trigger,
},
open: {
trigger: actions.dialogs.open.trigger,
},
},
},
stores: {
uiDialogs: {
isOpen: stores.uiDialogs.isOpen,
},
wallets: {
selected: stores.wallets.selected,
},
tokenInfoStore: {
tokenInfo: stores.tokenInfoStore.tokenInfo,
},
delegation: {
getDelegationRequests: stores.delegation.getDelegationRequests,
},
substores: {
ada: {
votingStore: {
catalystRoundInfo: stores.substores.ada.votingStore.catalystRoundInfo,
loadingCatalystRoundInfo: stores.substores.ada.votingStore.loadingCatalystRoundInfo,
}
}
}
},
VotingRegistrationDialogProps: ({
actions,
stores,
}: InjectedOrGenerated<VotingRegistrationDialogContainerData>),
});
}
}