@huma-finance/widgets 0.0.48-beta.296 → 0.0.48-beta.298

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/API.md +69 -22
  2. package/dist/cjs/index.cjs +2105 -2188
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/index.d.ts +45 -14
  5. package/dist/index.js +2105 -2189
  6. package/dist/index.js.map +1 -1
  7. package/package.json +4 -4
  8. package/src/components/CreditLine/borrowV2/3-Transfer.tsx +1 -1
  9. package/src/components/CreditLine/paymentV2/1-ChooseAmount.tsx +5 -0
  10. package/src/components/InputAmountModal.tsx +32 -5
  11. package/src/components/Lend/{withdrawV2 → addRedemptionV2}/1-ChooseTranche.tsx +25 -30
  12. package/src/components/Lend/addRedemptionV2/2-ChooseAmount.tsx +62 -0
  13. package/src/components/Lend/{redeemV2 → addRedemptionV2}/3-Transfer.tsx +7 -12
  14. package/src/components/Lend/addRedemptionV2/4-Done.tsx +20 -0
  15. package/src/components/Lend/addRedemptionV2/index.tsx +172 -0
  16. package/src/components/Lend/cancelRedemptionV2 /1-Transfer.tsx +51 -0
  17. package/src/components/Lend/cancelRedemptionV2 /2-Done.tsx +29 -0
  18. package/src/components/Lend/cancelRedemptionV2 /index.tsx +120 -0
  19. package/src/components/Lend/supplyV2/1-ChooseTranche.tsx +23 -21
  20. package/src/components/Lend/supplyV2/3-ChooseAmount.tsx +20 -30
  21. package/src/components/Lend/supplyV2/4-ApproveAllowance.tsx +2 -15
  22. package/src/components/Lend/supplyV2/5-Transfer.tsx +14 -58
  23. package/src/components/Lend/supplyV2/6-Success.tsx +5 -2
  24. package/src/components/Lend/supplyV2/index.tsx +31 -59
  25. package/src/components/Lend/withdrawV2/1-ConfirmTransfer.tsx +135 -0
  26. package/src/components/Lend/withdrawV2/2-Transfer.tsx +50 -0
  27. package/src/components/Lend/withdrawV2/index.tsx +89 -130
  28. package/src/index.tsx +44 -15
  29. package/src/components/Lend/redeemV2/1-ChooseAction.tsx +0 -237
  30. package/src/components/Lend/redeemV2/2-ChooseAmount.tsx +0 -101
  31. package/src/components/Lend/redeemV2/4-Done.tsx +0 -33
  32. package/src/components/Lend/redeemV2/index.tsx +0 -207
  33. package/src/components/Lend/withdrawV2/2-ChooseAmount.tsx +0 -88
  34. package/src/components/Lend/withdrawV2/3-Transfer.tsx +0 -110
  35. /package/src/components/Lend/withdrawV2/{4-Done.tsx → 3-Done.tsx} +0 -0
@@ -1,237 +0,0 @@
1
- import {
2
- PoolInfoV2,
3
- timestampToLL,
4
- UnderlyingTokenInfo,
5
- useLenderDepositRecordV2,
6
- useLPConfigV2,
7
- useNextEpochStartTimeV2,
8
- } from '@huma-finance/shared'
9
- import {
10
- Box,
11
- css,
12
- FormControl,
13
- FormControlLabel,
14
- FormLabel,
15
- Radio,
16
- RadioGroup,
17
- useTheme,
18
- } from '@mui/material'
19
- import { useWeb3React } from '@web3-react/core'
20
- import { BigNumber, ethers } from 'ethers'
21
- import React from 'react'
22
-
23
- import { REDEMPTION_TYPE, RedemptionInfo } from '.'
24
- import { useAppDispatch } from '../../../hooks/useRedux'
25
- import { setError, setStep } from '../../../store/widgets.reducers'
26
- import { WIDGET_STEP } from '../../../store/widgets.store'
27
- import { BottomButton } from '../../BottomButton'
28
- import { LoadingModal } from '../../LoadingModal'
29
- import { WrapperModal } from '../../WrapperModal'
30
-
31
- type Props = {
32
- poolInfo: PoolInfoV2
33
- poolUnderlyingToken: UnderlyingTokenInfo
34
- seniorRedemptionInfo: RedemptionInfo | undefined
35
- juniorRedemptionInfo: RedemptionInfo | undefined
36
- redemptionType: REDEMPTION_TYPE | undefined
37
- seniorPosition: BigNumber | undefined
38
- juniorPosition: BigNumber | undefined
39
- changeRedemptionType: (tranche: REDEMPTION_TYPE) => void
40
- }
41
-
42
- export function ChooseAction({
43
- poolInfo,
44
- poolUnderlyingToken,
45
- seniorRedemptionInfo,
46
- juniorRedemptionInfo,
47
- redemptionType,
48
- seniorPosition,
49
- juniorPosition,
50
- changeRedemptionType,
51
- }: Props): React.ReactElement | null {
52
- const theme = useTheme()
53
- const dispatch = useAppDispatch()
54
- const { symbol } = poolUnderlyingToken
55
- const { account, provider } = useWeb3React()
56
- const [seniorDepositRecord] = useLenderDepositRecordV2(
57
- poolInfo.poolName,
58
- 'senior',
59
- account,
60
- provider,
61
- )
62
- const [juniorDepositRecord] = useLenderDepositRecordV2(
63
- poolInfo.poolName,
64
- 'junior',
65
- account,
66
- provider,
67
- )
68
- const lpConfig = useLPConfigV2(poolInfo.poolName, provider)
69
- const nextEpochStartTime = useNextEpochStartTimeV2(
70
- poolInfo.poolName,
71
- provider,
72
- )
73
-
74
- const isLoading =
75
- seniorRedemptionInfo === undefined ||
76
- juniorRedemptionInfo === undefined ||
77
- seniorDepositRecord === undefined ||
78
- juniorDepositRecord === undefined ||
79
- lpConfig === undefined ||
80
- nextEpochStartTime === undefined
81
-
82
- const styles = {
83
- subTitle: css`
84
- color: ${theme.palette.text.primary} !important;
85
- font-family: 'Uni-Neue-Regular';
86
- font-size: 16px;
87
- line-height: 150%;
88
- letter-spacing: 0.15px;
89
- margin-top: ${theme.spacing(5)};
90
- margin-left: ${theme.spacing(2)};
91
- `,
92
- radioGroup: css`
93
- margin-left: ${theme.spacing(3)};
94
- margin-top: ${theme.spacing(3)};
95
- `,
96
- formControl: css`
97
- margin-bottom: ${theme.spacing(1)};
98
-
99
- .MuiFormControlLabel-label {
100
- color: ${theme.palette.text.primary};
101
- font-family: 'Uni-Neue-Regular';
102
- font-size: 16px;
103
- line-height: 150%;
104
- letter-spacing: 0.15px;
105
- }
106
- `,
107
- }
108
-
109
- const handleNext = () => {
110
- if (isLoading) {
111
- return
112
- }
113
-
114
- const depositRecord =
115
- redemptionType === REDEMPTION_TYPE.AddSeniorRedemption
116
- ? seniorDepositRecord
117
- : juniorDepositRecord
118
-
119
- const SECONDS_IN_A_DAY = 24 * 60 * 60
120
- if (
121
- nextEpochStartTime <
122
- depositRecord.lastDepositTime.toNumber() +
123
- lpConfig.withdrawalLockoutPeriodInDays * SECONDS_IN_A_DAY
124
- ) {
125
- dispatch(setStep(WIDGET_STEP.Error))
126
- dispatch(
127
- setError({
128
- errorReason: 'Redemption request too soon',
129
- errorMessage: `Your last deposit was on ${timestampToLL(
130
- depositRecord.lastDepositTime.toNumber(),
131
- )}. Depositors need to wait ${
132
- lpConfig.withdrawalLockoutPeriodInDays
133
- } days before redemption request`,
134
- }),
135
- )
136
- return
137
- }
138
-
139
- dispatch(setStep(WIDGET_STEP.ChooseAmount))
140
- }
141
-
142
- const formatAmount = (amount?: BigNumber) => {
143
- if (!amount || amount.lte(0)) {
144
- return undefined
145
- }
146
- return ethers.utils.formatUnits(amount, poolUnderlyingToken.decimals)
147
- }
148
-
149
- const items: {
150
- label: string
151
- value: REDEMPTION_TYPE
152
- amount?: string
153
- show: boolean
154
- }[] = [
155
- {
156
- label: 'Request Senior Tranche Redemption',
157
- value: REDEMPTION_TYPE.AddSeniorRedemption,
158
- show: seniorPosition?.gt(0) ?? false,
159
- },
160
- {
161
- label: 'Cancel Senior Tranche Redemption',
162
- value: REDEMPTION_TYPE.CancelSeniorRedemption,
163
- amount: formatAmount(seniorRedemptionInfo?.amount),
164
- show: seniorRedemptionInfo?.amount.gt(0) ?? false,
165
- },
166
- {
167
- label: 'Request Junior Tranche Redemption',
168
- value: REDEMPTION_TYPE.AddJuniorRedemption,
169
- show: juniorPosition?.gt(0) ?? false,
170
- },
171
- {
172
- label: 'Cancel Junior Tranche Redemption',
173
- value: REDEMPTION_TYPE.CancelJuniorRedemption,
174
- amount: formatAmount(juniorRedemptionInfo?.amount),
175
- show: juniorRedemptionInfo?.amount.gt(0) ?? false,
176
- },
177
- ]
178
-
179
- if (isLoading) {
180
- return <LoadingModal title={`Redeem ${symbol}`} />
181
- }
182
-
183
- return (
184
- <WrapperModal title={`Redeem ${symbol}`}>
185
- <FormControl>
186
- <FormLabel css={styles.subTitle}>Select Redemption Type</FormLabel>
187
- <RadioGroup
188
- aria-labelledby='buttons-group-label'
189
- name='radio-buttons-group'
190
- css={styles.radioGroup}
191
- onChange={(e) => changeRedemptionType(Number(e.target.value))}
192
- >
193
- {items
194
- .filter((item) => item.show)
195
- .map((item) => {
196
- const { label } = item
197
- let labelNode = <Box>{label}</Box>
198
- if (item.amount) {
199
- labelNode = (
200
- <>
201
- <Box>{label}</Box>
202
- <Box>
203
- ({item.amount} {symbol} available)
204
- </Box>
205
- </>
206
- )
207
- }
208
- return (
209
- <FormControlLabel
210
- key={item.label}
211
- value={item.value}
212
- control={
213
- <Radio
214
- sx={{
215
- '& .MuiSvgIcon-root': {
216
- fontSize: 24,
217
- },
218
- }}
219
- />
220
- }
221
- label={labelNode}
222
- css={styles.formControl}
223
- />
224
- )
225
- })}
226
- </RadioGroup>
227
- </FormControl>
228
- <BottomButton
229
- variant='contained'
230
- disabled={redemptionType === undefined}
231
- onClick={handleNext}
232
- >
233
- NEXT
234
- </BottomButton>
235
- </WrapperModal>
236
- )
237
- }
@@ -1,101 +0,0 @@
1
- import {
2
- getTrancheVaultContractV2,
3
- PoolInfoV2,
4
- UnderlyingTokenInfo,
5
- } from '@huma-finance/shared'
6
- import { useWeb3React } from '@web3-react/core'
7
- import { BigNumber, ethers } from 'ethers'
8
- import React, { useEffect, useState } from 'react'
9
-
10
- import { REDEMPTION_TYPE, RedemptionActionInfo } from '.'
11
- import { useAppDispatch } from '../../../hooks/useRedux'
12
- import {
13
- setRedeemAmount,
14
- setRedeemShares,
15
- setStep,
16
- } from '../../../store/widgets.reducers'
17
- import { WIDGET_STEP } from '../../../store/widgets.store'
18
- import { InputAmountModal } from '../../InputAmountModal'
19
- import { LoadingModal } from '../../LoadingModal'
20
-
21
- type Props = {
22
- poolInfo: PoolInfoV2
23
- poolUnderlyingToken: UnderlyingTokenInfo
24
- redemptionType: REDEMPTION_TYPE
25
- maxAmount: BigNumber
26
- }
27
-
28
- export function ChooseAmount({
29
- poolInfo,
30
- poolUnderlyingToken,
31
- redemptionType,
32
- maxAmount,
33
- }: Props): React.ReactElement | null {
34
- const dispatch = useAppDispatch()
35
- const { provider } = useWeb3React()
36
- const { symbol } = poolUnderlyingToken
37
- const [sharePrice, setSharePrice] = useState(0)
38
- const [currentAmount, setCurrentAmount] = useState<number>(0)
39
- const [currentShares, setCurrentShares] = useState<number>(0)
40
- const redemptionActionInfo = RedemptionActionInfo[redemptionType]
41
- const maxAmountFormatted = ethers.utils.formatUnits(
42
- maxAmount,
43
- poolUnderlyingToken.decimals,
44
- )
45
-
46
- useEffect(() => {
47
- if (maxAmount.gt(0)) {
48
- const fetchData = async () => {
49
- const vaultContract = await getTrancheVaultContractV2(
50
- poolInfo.poolName,
51
- redemptionActionInfo.tranche,
52
- provider,
53
- )
54
- if (vaultContract) {
55
- const shares = await vaultContract.convertToShares(maxAmount)
56
- const price = maxAmount.div(shares)
57
- setSharePrice(price.toNumber())
58
- }
59
- }
60
- fetchData()
61
- }
62
- }, [maxAmount, poolInfo.poolName, provider, redemptionActionInfo.tranche])
63
-
64
- const handleChangeAmount = (newAmount: number) => {
65
- const shares = Number(newAmount) / sharePrice
66
- setCurrentShares(shares)
67
- setCurrentAmount(Number(newAmount))
68
- dispatch(setRedeemAmount(Number(newAmount)))
69
- dispatch(setRedeemShares(shares))
70
- }
71
-
72
- const handleAction = () => {
73
- dispatch(setStep(WIDGET_STEP.Transfer))
74
- }
75
-
76
- if (sharePrice === 0) {
77
- return (
78
- <LoadingModal
79
- title={`${redemptionActionInfo.action} Redemption Request`}
80
- />
81
- )
82
- }
83
-
84
- return (
85
- <InputAmountModal
86
- title={`${redemptionActionInfo.action} Redemption Request`}
87
- subTitle={
88
- redemptionActionInfo.action === 'Create'
89
- ? `Input ${symbol} amount to redeem from ${redemptionActionInfo.tranche} tranche`
90
- : `Input the amount of the redemption request in ${symbol} that you wish to cancel from the ${redemptionActionInfo.tranche} tranche`
91
- }
92
- tokenSymbol={symbol}
93
- currentAmount={currentAmount}
94
- handleChangeAmount={handleChangeAmount}
95
- maxAmount={maxAmountFormatted}
96
- info={`${currentShares.toFixed(1)} Shares`}
97
- handleAction={handleAction}
98
- actionText='SUBMIT'
99
- />
100
- )
101
- }
@@ -1,33 +0,0 @@
1
- import { formatMoney, UnderlyingTokenInfo } from '@huma-finance/shared'
2
- import React from 'react'
3
-
4
- import { REDEMPTION_TYPE, RedemptionActionInfo } from '.'
5
- import { useAppSelector } from '../../../hooks/useRedux'
6
- import { selectWidgetState } from '../../../store/widgets.selectors'
7
- import { TxDoneModal } from '../../TxDoneModal'
8
-
9
- type Props = {
10
- poolUnderlyingToken: UnderlyingTokenInfo
11
- redemptionType: REDEMPTION_TYPE
12
- handleAction: () => void
13
- }
14
-
15
- export function Done({
16
- poolUnderlyingToken,
17
- redemptionType,
18
- handleAction,
19
- }: Props): React.ReactElement {
20
- const { symbol } = poolUnderlyingToken
21
- const { redeemAmount, redeemShares } = useAppSelector(selectWidgetState)
22
- const redemptionActionInfo = RedemptionActionInfo[redemptionType]
23
- const isCreate = redemptionActionInfo.action === 'Create'
24
-
25
- const content = [
26
- `You successfully ${isCreate ? 'created' : 'canceled'} ${formatMoney(
27
- redeemAmount,
28
- )} ${symbol} (${redeemShares} shares) redemption request.`,
29
- isCreate ? `Note: Exact redemption amounts may vary once processed.` : '',
30
- ]
31
-
32
- return <TxDoneModal handleAction={handleAction} content={content} />
33
- }
@@ -1,207 +0,0 @@
1
- import {
2
- POOL_NAME,
3
- useCancellableRedemptionInfoV2,
4
- useLenderPositionV2,
5
- usePoolInfoV2,
6
- usePoolUnderlyingTokenInfoV2,
7
- } from '@huma-finance/shared'
8
- import { useWeb3React } from '@web3-react/core'
9
- import { BigNumber } from 'ethers'
10
- import React, { useCallback, useEffect, useState } from 'react'
11
- import { useDispatch } from 'react-redux'
12
-
13
- import { useAppSelector } from '../../../hooks/useRedux'
14
- import { setStep } from '../../../store/widgets.reducers'
15
- import { selectWidgetState } from '../../../store/widgets.selectors'
16
- import { WIDGET_STEP } from '../../../store/widgets.store'
17
- import { ErrorModal } from '../../ErrorModal'
18
- import { WidgetWrapper } from '../../WidgetWrapper'
19
- import { ChooseAction } from './1-ChooseAction'
20
- import { ChooseAmount } from './2-ChooseAmount'
21
- import { Transfer } from './3-Transfer'
22
- import { Done } from './4-Done'
23
-
24
- /**
25
- * Lend pool withdraw props
26
- * @typedef {Object} LendRedeemPropsV2
27
- * @property {POOL_NAME} poolName The name of the pool.
28
- * @property {function():void} handleClose Function to notify to close the widget modal when user clicks the 'x' close button.
29
- * @property {function((number|undefined)):void|undefined} handleSuccess Optional function to notify that the lending pool withdraw action is successful.
30
- */
31
- export type LendRedeemPropsV2 = {
32
- poolName: keyof typeof POOL_NAME
33
- handleClose: () => void
34
- handleSuccess?: (blockNumber?: number) => void
35
- }
36
-
37
- export enum REDEMPTION_TYPE {
38
- AddSeniorRedemption = 1,
39
- CancelSeniorRedemption = 2,
40
- AddJuniorRedemption = 3,
41
- CancelJuniorRedemption = 4,
42
- }
43
-
44
- export const RedemptionActionInfo: {
45
- [key in REDEMPTION_TYPE]: {
46
- action: 'Create' | 'Cancel'
47
- tranche: 'senior' | 'junior'
48
- }
49
- } = {
50
- [REDEMPTION_TYPE.AddSeniorRedemption]: {
51
- action: 'Create',
52
- tranche: 'senior',
53
- },
54
- [REDEMPTION_TYPE.CancelSeniorRedemption]: {
55
- action: 'Cancel',
56
- tranche: 'senior',
57
- },
58
- [REDEMPTION_TYPE.AddJuniorRedemption]: {
59
- action: 'Create',
60
- tranche: 'junior',
61
- },
62
- [REDEMPTION_TYPE.CancelJuniorRedemption]: {
63
- action: 'Cancel',
64
- tranche: 'junior',
65
- },
66
- }
67
-
68
- export type RedemptionInfo = {
69
- shares: BigNumber
70
- amount: BigNumber
71
- }
72
-
73
- export function LendRedeemV2({
74
- poolName: poolNameStr,
75
- handleClose,
76
- handleSuccess,
77
- }: LendRedeemPropsV2): React.ReactElement | null {
78
- const dispatch = useDispatch()
79
- const { account, chainId, provider } = useWeb3React()
80
- const poolName = POOL_NAME[poolNameStr]
81
- const poolInfo = usePoolInfoV2(poolName, chainId)
82
- const { step, errorMessage, errorReason } = useAppSelector(selectWidgetState)
83
- const [seniorRedemptionInfo, refreshSeniorRedemptionInfo] =
84
- useCancellableRedemptionInfoV2(poolName, 'senior', account, provider)
85
- const [juniorRedemptionInfo, refreshJuniorRedemptionInfo] =
86
- useCancellableRedemptionInfoV2(poolName, 'junior', account, provider)
87
- const poolUnderlyingToken = usePoolUnderlyingTokenInfoV2(poolName, provider)
88
- const [seniorPosition, refreshSeniorPosition] = useLenderPositionV2(
89
- poolName,
90
- 'senior',
91
- account,
92
- provider,
93
- )
94
- const [juniorPosition, refreshJuniorPosition] = useLenderPositionV2(
95
- poolName,
96
- 'junior',
97
- account,
98
- provider,
99
- )
100
- const [redemptionType, setRedemptionType] = useState<REDEMPTION_TYPE>()
101
- const redemptionActionInfo = redemptionType
102
- ? RedemptionActionInfo[redemptionType]
103
- : undefined
104
-
105
- useEffect(() => {
106
- if (!step) {
107
- dispatch(setStep(WIDGET_STEP.ChooseTranche))
108
- }
109
- }, [dispatch, step])
110
-
111
- const handleRedeemSuccess = useCallback(
112
- (blockNumber: number) => {
113
- refreshSeniorRedemptionInfo()
114
- refreshJuniorRedemptionInfo()
115
- refreshSeniorPosition()
116
- refreshJuniorPosition()
117
- if (handleSuccess) {
118
- handleSuccess(blockNumber)
119
- }
120
- },
121
- [
122
- handleSuccess,
123
- refreshJuniorPosition,
124
- refreshJuniorRedemptionInfo,
125
- refreshSeniorPosition,
126
- refreshSeniorRedemptionInfo,
127
- ],
128
- )
129
-
130
- const getMaxAmount = () => {
131
- if (redemptionActionInfo?.tranche === 'senior') {
132
- return redemptionActionInfo?.action === 'Create'
133
- ? seniorPosition
134
- : seniorRedemptionInfo?.amount
135
- }
136
- if (redemptionActionInfo?.tranche === 'junior') {
137
- return redemptionActionInfo?.action === 'Create'
138
- ? juniorPosition
139
- : juniorRedemptionInfo?.amount
140
- }
141
- return undefined
142
- }
143
-
144
- if (!poolInfo || !poolUnderlyingToken) {
145
- return (
146
- <WidgetWrapper
147
- isOpen
148
- isLoading
149
- loadingTitle='Redeem'
150
- handleClose={handleClose}
151
- handleSuccess={handleSuccess}
152
- />
153
- )
154
- }
155
-
156
- return (
157
- <WidgetWrapper
158
- isOpen
159
- loadingTitle='Redeem'
160
- handleClose={handleClose}
161
- handleSuccess={handleRedeemSuccess}
162
- >
163
- {step === WIDGET_STEP.ChooseTranche && (
164
- <ChooseAction
165
- poolInfo={poolInfo}
166
- poolUnderlyingToken={poolUnderlyingToken}
167
- seniorRedemptionInfo={seniorRedemptionInfo}
168
- juniorRedemptionInfo={juniorRedemptionInfo}
169
- redemptionType={redemptionType}
170
- changeRedemptionType={setRedemptionType}
171
- seniorPosition={seniorPosition}
172
- juniorPosition={juniorPosition}
173
- />
174
- )}
175
- {step === WIDGET_STEP.ChooseAmount && redemptionType && (
176
- <ChooseAmount
177
- poolInfo={poolInfo}
178
- poolUnderlyingToken={poolUnderlyingToken}
179
- redemptionType={redemptionType}
180
- maxAmount={getMaxAmount() ?? BigNumber.from(0)}
181
- />
182
- )}
183
- {step === WIDGET_STEP.Transfer && redemptionType && (
184
- <Transfer
185
- poolInfo={poolInfo}
186
- poolUnderlyingToken={poolUnderlyingToken}
187
- redemptionType={redemptionType}
188
- />
189
- )}
190
- {step === WIDGET_STEP.Done && redemptionType && (
191
- <Done
192
- poolUnderlyingToken={poolUnderlyingToken}
193
- handleAction={handleClose}
194
- redemptionType={redemptionType}
195
- />
196
- )}
197
- {step === WIDGET_STEP.Error && (
198
- <ErrorModal
199
- title='Redeem'
200
- errorReason={errorReason}
201
- errorMessage={errorMessage}
202
- handleOk={handleClose}
203
- />
204
- )}
205
- </WidgetWrapper>
206
- )
207
- }
@@ -1,88 +0,0 @@
1
- import {
2
- FirstLossCoverIndex,
3
- getFirstLossCoverContractV2,
4
- PoolInfoV2,
5
- UnderlyingTokenInfo,
6
- } from '@huma-finance/shared'
7
- import { useWeb3React } from '@web3-react/core'
8
- import { ethers } from 'ethers'
9
- import React, { useEffect, useState } from 'react'
10
-
11
- import { WithdrawType } from '.'
12
- import { useAppDispatch } from '../../../hooks/useRedux'
13
- import { setStep, setWithdrawAmount } from '../../../store/widgets.reducers'
14
- import { WIDGET_STEP } from '../../../store/widgets.store'
15
- import { InputAmountModal } from '../../InputAmountModal'
16
-
17
- type Props = {
18
- poolInfo: PoolInfoV2
19
- poolUnderlyingToken: UnderlyingTokenInfo
20
- selectedWithdrawType: WithdrawType
21
- }
22
-
23
- export function ChooseAmount({
24
- poolInfo,
25
- poolUnderlyingToken,
26
- selectedWithdrawType,
27
- }: Props): React.ReactElement | null {
28
- const dispatch = useAppDispatch()
29
- const { provider } = useWeb3React()
30
- const { symbol } = poolUnderlyingToken
31
- const [sharePrice, setSharePrice] = useState(0)
32
- const [currentAmount, setCurrentAmount] = useState<number>(0)
33
- const [currentShares, setCurrentShares] = useState<number>(0)
34
- const maxAmountFormatted = ethers.utils.formatUnits(
35
- selectedWithdrawType.withdrawableAmount,
36
- poolUnderlyingToken.decimals,
37
- )
38
-
39
- useEffect(() => {
40
- if (selectedWithdrawType.withdrawableAmount.gt(0)) {
41
- const fetchData = async () => {
42
- const firstLossCoverContract = await getFirstLossCoverContractV2(
43
- poolInfo.poolName,
44
- Number(selectedWithdrawType.value) as FirstLossCoverIndex,
45
- provider,
46
- )
47
- if (firstLossCoverContract) {
48
- const shares = await firstLossCoverContract.convertToShares(
49
- selectedWithdrawType.withdrawableAmount,
50
- )
51
- const price = selectedWithdrawType.withdrawableAmount.div(shares)
52
- setSharePrice(price.toNumber())
53
- }
54
- }
55
- fetchData()
56
- }
57
- }, [
58
- poolInfo.poolName,
59
- provider,
60
- selectedWithdrawType.value,
61
- selectedWithdrawType.withdrawableAmount,
62
- ])
63
-
64
- const handleChangeAmount = (newAmount: number) => {
65
- const shares = Number(newAmount) / sharePrice
66
- setCurrentShares(shares)
67
- setCurrentAmount(newAmount)
68
- dispatch(setWithdrawAmount(Number(newAmount)))
69
- }
70
-
71
- const handleAction = () => {
72
- dispatch(setStep(WIDGET_STEP.Transfer))
73
- }
74
-
75
- return (
76
- <InputAmountModal
77
- title={`Withdraw ${symbol}`}
78
- subTitle={selectedWithdrawType.label}
79
- tokenSymbol={symbol}
80
- currentAmount={currentAmount}
81
- handleChangeAmount={handleChangeAmount}
82
- maxAmount={maxAmountFormatted}
83
- info={`${currentShares.toFixed(1)} Shares`}
84
- handleAction={handleAction}
85
- actionText='WITHDRAW'
86
- />
87
- )
88
- }