This repository was archived by the owner on Feb 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathTransferTokensModal.tsx
More file actions
187 lines (179 loc) · 6.36 KB
/
TransferTokensModal.tsx
File metadata and controls
187 lines (179 loc) · 6.36 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
import React, { useState } from 'react'
import { PublicKey } from '@solana/web3.js'
import Modal from '@components/Modal'
import Button, { LinkButton, SecondaryButton } from '@components/Button'
import { getMinDurationFmt, getTimeLeftFromNowFmt } from '@utils/dateTools'
import { notify } from '@utils/notifications'
import { PositionWithMeta } from '../sdk/types'
import { getMintMinAmountAsDecimal } from '@tools/sdk/units'
import { precision } from '@utils/formatting'
import Input from '@components/inputs/Input'
import { useSolanaUnixNow } from '@hooks/useSolanaUnixNow'
import { BN } from '@coral-xyz/anchor'
import { useRealmCommunityMintInfoQuery } from '@hooks/queries/mintInfo'
interface TransferTokensModalProps {
isOpen: boolean
positions: PositionWithMeta[]
maxTransferAmount: number
onClose: () => void
onSubmit: (positon: PositionWithMeta, amount: number) => Promise<void>
}
export const TransferTokensModal: React.FC<TransferTokensModalProps> = ({
isOpen,
positions,
maxTransferAmount,
onClose,
onSubmit,
}) => {
const { unixNow = 0 } = useSolanaUnixNow()
const mint = useRealmCommunityMintInfoQuery().data?.result
const [isSubmitting, setIsSubmitting] = useState(false)
const [amount, setAmount] = useState<number | null>(null)
const [selectedPosPk, setSelectedPosPk] = useState<PublicKey | null>(null)
const mintMinAmount = mint ? getMintMinAmountAsDecimal(mint) : 1
const currentPrecision = precision(mintMinAmount)
const handleAmountChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (!Number(e.target.value)) {
return setAmount(Number(e.target.value))
}
setAmount(
parseFloat(
Math.max(
Number(mintMinAmount),
Math.min(Number(maxTransferAmount), Number(e.target.value))
).toFixed(currentPrecision)
)
)
}
const handleOnSubmit = async () => {
if (amount && selectedPosPk) {
try {
setIsSubmitting(true)
await onSubmit(
positions.find((pos) => pos.pubkey.equals(selectedPosPk))!,
amount
)
onClose()
} catch (e) {
setIsSubmitting(false)
notify({
type: 'error',
message: e.message || 'Unable to transfer tokens',
})
}
}
}
const CardLabel = ({ label, value }) => {
return (
<div className="flex flex-col w-1/2 gap-1">
<p className="text-xs text-fgd-2">{label}</p>
<p className="text-xs font-bold text-fgd-1">{value}</p>
</div>
)
}
const labelClasses = 'mb-2 text-fgd-2 text-sm'
const hasTransferablePositions = positions.length > 0
return (
<Modal onClose={onClose} isOpen={isOpen}>
<h2 className="mb-4 flex flex-row items-center">Transfer Tokens</h2>
<div className="bg-bkg-3 rounded-md w-full p-4 mb-4 font-normal text-xs">
{hasTransferablePositions ? (
<>
<div>Select an existing positon to transfer to</div>
<br />
</>
) : null}
<div>
You can only transfer to positions that have a greater than or equal
lockup time.
</div>
<br />
<div className="text-red">
{hasTransferablePositions
? 'You cant transfer to Landrush positions after the Landrush period, and transferring out of one after the Landrush period, will result in losing the multiplier!'
: 'There are no positions that meet this criteria.'}
</div>
</div>
{hasTransferablePositions ? (
<>
<div className="mb-4">
<div className={`${labelClasses} flex justify-between`}>
Amount to Transfer
<LinkButton
className="text-primary-light"
onClick={() => setAmount(maxTransferAmount)}
>
Max: {maxTransferAmount}
</LinkButton>
</div>
<Input
max={maxTransferAmount}
min={mintMinAmount}
value={amount}
type="number"
onChange={handleAmountChange}
step={mintMinAmount}
/>
</div>
<div className="w-full flex flex-col gap-2">
{positions.map((pos) => {
const lockup = pos.lockup
const lockupKind = Object.keys(lockup.kind)[0] as string
const isConstant = lockupKind === 'constant'
const isSelected = selectedPosPk?.equals(pos.pubkey)
return (
<div
className={`border rounded-md flex flex-row w-full p-4 hover:border-fgd-3 hover:bg-bkg-3 hover:cursor-pointer ${
isSelected ? 'bg-bkg-3 border-fgd-3' : 'border-fgd-4'
}`}
onClick={() => setSelectedPosPk(pos.pubkey)}
key={pos.pubkey.toBase58()}
>
<CardLabel
label="Lockup Type"
value={isConstant ? 'Constant' : 'Decaying'}
/>
<CardLabel
label="Vote Multiplier"
value={(
(pos.votingPower.isZero()
? 0
: pos.votingPower
.div(pos.amountDepositedNative)
.toNumber()) /
(pos.genesisEnd.gt(new BN(unixNow))
? pos.votingMint.genesisVotePowerMultiplier
: 1)
).toFixed(2)}
/>
<CardLabel
label={isConstant ? 'Min. Duration' : 'Time left'}
value={
isConstant
? getMinDurationFmt(
pos.lockup.startTs,
pos.lockup.endTs
)
: getTimeLeftFromNowFmt(pos.lockup.endTs)
}
/>
</div>
)
})}
</div>
</>
) : null}
<div className="flex flex-col pt-4">
<Button
className="mb-4"
onClick={handleOnSubmit}
isLoading={isSubmitting}
disabled={!selectedPosPk || !amount || isSubmitting}
>
Transfer Tokens
</Button>
<SecondaryButton onClick={onClose}>Cancel</SecondaryButton>
</div>
</Modal>
)
}