-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLockedModal.tsx
More file actions
332 lines (319 loc) · 11.9 KB
/
Copy pathLockedModal.tsx
File metadata and controls
332 lines (319 loc) · 11.9 KB
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
import {
PythBalance,
StakeAccount,
VestingAccountState,
} from '@pythnetwork/staking'
import { BaseModal } from './BaseModal'
import Tooltip from '@components/Tooltip'
import { useUnvestedLockAllMutation } from 'hooks/useUnvestedLockAllMutation'
import { useUnvestedPreUnlockAllMutation } from 'hooks/useUnvestedPreUnlockAllMutation'
import { useUnvestedUnlockAllMutation } from 'hooks/useUnvestedUnlockAllMutation'
import { useBalance } from 'hooks/useBalance'
import { useNextVestingEvent } from 'hooks/useNextVestingEvent'
import { useStakeConnection } from 'hooks/useStakeConnection'
import { MainStakeAccount } from 'pages'
import { LlcModal } from './LlcModal'
import { useCallback, useState } from 'react'
import toast from 'react-hot-toast'
export type LockedModalProps = {
isLockedModalOpen: boolean
setIsLockedModalOpen: (open: boolean) => void
currentVestingAccountState: VestingAccountState | undefined
mainStakeAccount: MainStakeAccount
}
export function LockedModal({
isLockedModalOpen,
setIsLockedModalOpen,
currentVestingAccountState,
mainStakeAccount,
}: LockedModalProps) {
const { data: balanceData, isLoading: _isBalanceLoading } =
useBalance(mainStakeAccount)
const {
unvestedTotalPythBalance,
unvestedLockingPythBalance,
unvestedLockedPythBalance,
unvestedPreUnlockingPythBalance,
unvestedUnlockingPythBalance,
unvestedUnlockedPythBalance,
} = balanceData ?? {}
const { data: nextVestingEvent } = useNextVestingEvent(mainStakeAccount)
const { nextVestingDate, nextVestingAmount } = nextVestingEvent ?? {}
return (
<BaseModal
title="Locked tokens"
isModalOpen={isLockedModalOpen}
setIsModalOpen={setIsLockedModalOpen}
>
<p className="mb-4">
You currently have {unvestedTotalPythBalance?.toString()} locked tokens.{' '}
{nextVestingDate && !unvestedTotalPythBalance?.isZero()
? `${nextVestingAmount?.toString()} tokens
will unlock on ${nextVestingDate?.toLocaleString()}.`
: null}
<br />
<br />
<LockedModalCurrentState
currentVestingAccountState={currentVestingAccountState}
unvestedLockedPythBalance={
unvestedLockedPythBalance ?? PythBalance.zero()
}
unvestedLockingPythBalance={
unvestedLockingPythBalance ?? PythBalance.zero()
}
unvestedUnlockedPythBalance={
unvestedUnlockedPythBalance ?? PythBalance.zero()
}
unvestedPreUnlockingPythBalance={
unvestedPreUnlockingPythBalance ?? PythBalance.zero()
}
unvestedUnlockingPythBalance={
unvestedUnlockingPythBalance ?? PythBalance.zero()
}
nextVestingAmount={nextVestingAmount ?? PythBalance.zero()}
nextVestingDate={nextVestingDate}
/>
</p>
<div className="flex flex-col items-center space-y-4 text-center md:block md:space-x-10">
<LockedModalButton
currentVestingAccountState={currentVestingAccountState}
mainStakeAccount={mainStakeAccount}
/>
</div>
</BaseModal>
)
}
type LockedModalCurrentStateProps = {
currentVestingAccountState: VestingAccountState | undefined
unvestedLockedPythBalance: PythBalance
unvestedLockingPythBalance: PythBalance
unvestedUnlockedPythBalance: PythBalance
unvestedPreUnlockingPythBalance: PythBalance
unvestedUnlockingPythBalance: PythBalance
nextVestingAmount: PythBalance
nextVestingDate: Date | undefined
}
function LockedModalCurrentState({
currentVestingAccountState,
unvestedLockedPythBalance,
unvestedLockingPythBalance,
unvestedUnlockedPythBalance,
unvestedPreUnlockingPythBalance,
unvestedUnlockingPythBalance,
nextVestingAmount,
nextVestingDate,
}: LockedModalCurrentStateProps) {
switch (currentVestingAccountState) {
case VestingAccountState.UnvestedTokensPartiallyLocked:
return (
<>
{unvestedLockedPythBalance.add(unvestedLockingPythBalance).toString()}{' '}
locked tokens are staked in governance. <br />
{unvestedUnlockedPythBalance.toString()} locked tokens are unstaked.{' '}
<br />
{unvestedPreUnlockingPythBalance
.add(unvestedUnlockingPythBalance)
.toString()}{' '}
locked tokens are in cooldown period.
<br />
<br />
Your {nextVestingAmount.toString()} tokens scheduled to unlock on{' '}
{nextVestingDate?.toLocaleString()} will be withdrawable.
<br />
<br />
</>
)
case VestingAccountState.UnvestedTokensFullyLockedExceptCooldown:
return (
<>
{unvestedLockedPythBalance.add(unvestedLockingPythBalance).toString()}{' '}
tokens are staked in governance. <br />
{unvestedPreUnlockingPythBalance
.add(unvestedUnlockingPythBalance)
.toString()}{' '}
tokens are in cooldown period.
</>
)
case VestingAccountState.UnvestedTokensFullyLocked:
return (
<>
Your locked tokens are staked in the contract to participate in
governance. On vest, they will become staked tokens, which require a
full epoch cooldown to be unstaked. (Epochs start every Thursday at
00:00 UTC and last 7 days)
<br />
<br />
If you would like to withdraw them immediately on unlock, you may
choose to preliminary unstake them now. This action will cause your{' '}
{nextVestingAmount.toString()} tokens scheduled to unstake on{' '}
{nextVestingDate?.toLocaleString()} to become withdrawable on unlock.
<br />
<br />
You may also choose to unstake all of your locked tokens, immediately
reducing your governance power.
</>
)
case VestingAccountState.UnvestedTokensFullyUnlocked:
return (
<>
Your locked tokens are not participating in governance. On unlock,
they will become withdrawable tokens.
<br />
<br />
Participating in governance requires you to stake your locked tokens.
This means that when your tokens unlock, you will have to manually
unstake them by interacting with the UI and wait for a cooldown of one
full epoch before being able to withdraw them. (Epochs start every
Thursday at 00:00 UTC and last 7 days).
</>
)
case VestingAccountState.UnvestedTokensFullyUnlockedExceptCooldown:
return <>All of your locked tokens are currently in cooldown period.</>
}
}
type LockedModalButtonProps = {
currentVestingAccountState: VestingAccountState | undefined
mainStakeAccount: MainStakeAccount
}
function LockedModalButton({
currentVestingAccountState,
mainStakeAccount,
}: LockedModalButtonProps) {
const { data: stakeConnection } = useStakeConnection()
const unvestedLockAll = useUnvestedLockAllMutation()
const unvestedPreUnlockAll = useUnvestedPreUnlockAllMutation()
const unvestedUnlockAll = useUnvestedUnlockAllMutation()
const [isLlcModalOpen, setIsLlcModalOpen] = useState(false)
const onStakeAll = useCallback(async () => {
if (mainStakeAccount === 'NA' || mainStakeAccount === undefined) return
const isLlcMember = stakeConnection!.isLlcMember(mainStakeAccount)
if (isLlcMember === true)
unvestedLockAll.mutate({
mainStakeAccount: mainStakeAccount,
stakeConnection: stakeConnection!,
})
else setIsLlcModalOpen(true)
}, [stakeConnection, mainStakeAccount])
if (mainStakeAccount === 'NA') return <></>
switch (currentVestingAccountState) {
case VestingAccountState.UnvestedTokensFullyLocked:
return (
<>
<button
type="button"
className="primary-btn px-8 py-3 text-base font-semibold hover:bg-blueGemHover"
onClick={() =>
unvestedPreUnlockAll.mutate({
mainStakeAccount: mainStakeAccount as StakeAccount,
stakeConnection: stakeConnection!,
})
}
disabled={
mainStakeAccount === undefined || stakeConnection === undefined
}
>
Preliminary unstake
</button>
<button
type="button"
className="primary-btn px-8 py-3 text-base font-semibold hover:bg-blueGemHover"
onClick={() =>
unvestedUnlockAll.mutate({
mainStakeAccount: mainStakeAccount as StakeAccount,
stakeConnection: stakeConnection!,
})
}
disabled={
mainStakeAccount === undefined || stakeConnection === undefined
}
>
Unstake all
</button>
</>
)
case VestingAccountState.FullyVested:
return null
default:
return (
<>
<button
type="button"
className="primary-btn min-w-[145px] px-8 py-3 text-base font-semibold hover:bg-blueGemHover disabled:bg-valhalla"
onClick={onStakeAll}
disabled={
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyLockedExceptCooldown ||
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlockedExceptCooldown ||
mainStakeAccount === undefined ||
stakeConnection === undefined
}
>
{currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyLockedExceptCooldown ||
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlockedExceptCooldown ? (
<Tooltip
content="Your tokens are in the process of being unlocked."
className="m-4"
>
Stake all
</Tooltip>
) : (
'Stake all'
)}
</button>
<button
type="button"
className="primary-btn min-w-[145px] px-8 py-3 text-base font-semibold hover:bg-blueGemHover disabled:bg-valhalla"
onClick={() =>
unvestedUnlockAll.mutate({
mainStakeAccount: mainStakeAccount as StakeAccount,
stakeConnection: stakeConnection!,
})
}
disabled={
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlocked ||
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlockedExceptCooldown ||
mainStakeAccount === undefined ||
stakeConnection === undefined
}
>
{currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlocked ||
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlockedExceptCooldown ? (
<Tooltip
content={
currentVestingAccountState ==
VestingAccountState.UnvestedTokensFullyUnlocked
? "You don't have any locked tokens to unlock."
: 'Your tokens are in the process of being unlocked.'
}
className="m-4"
>
Unstake all
</Tooltip>
) : (
'Unstake all'
)}
</button>
<LlcModal
isLlcModalOpen={isLlcModalOpen}
setIsLlcModalOpen={setIsLlcModalOpen}
onSignLlc={() => {
// Once the user clicks sign llc
// Sign and stake will happen in the same transaction
unvestedLockAll.mutate({
mainStakeAccount: mainStakeAccount as StakeAccount,
stakeConnection: stakeConnection!,
})
setIsLlcModalOpen(false)
}}
/>
</>
)
}
}