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

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 (34) hide show
  1. package/API.md +69 -22
  2. package/dist/cjs/index.cjs +2062 -2185
  3. package/dist/cjs/index.cjs.map +1 -1
  4. package/dist/index.d.ts +45 -14
  5. package/dist/index.js +2061 -2185
  6. package/dist/index.js.map +1 -1
  7. package/package.json +4 -4
  8. package/src/components/CreditLine/paymentV2/1-ChooseAmount.tsx +5 -0
  9. package/src/components/InputAmountModal.tsx +3 -1
  10. package/src/components/Lend/{withdrawV2 → addRedemptionV2}/1-ChooseTranche.tsx +25 -30
  11. package/src/components/Lend/addRedemptionV2/2-ChooseAmount.tsx +61 -0
  12. package/src/components/Lend/{redeemV2 → addRedemptionV2}/3-Transfer.tsx +7 -12
  13. package/src/components/Lend/addRedemptionV2/4-Done.tsx +20 -0
  14. package/src/components/Lend/addRedemptionV2/index.tsx +172 -0
  15. package/src/components/Lend/cancelRedemptionV2 /1-Transfer.tsx +51 -0
  16. package/src/components/Lend/cancelRedemptionV2 /2-Done.tsx +29 -0
  17. package/src/components/Lend/cancelRedemptionV2 /index.tsx +120 -0
  18. package/src/components/Lend/supplyV2/1-ChooseTranche.tsx +23 -21
  19. package/src/components/Lend/supplyV2/3-ChooseAmount.tsx +20 -30
  20. package/src/components/Lend/supplyV2/4-ApproveAllowance.tsx +2 -15
  21. package/src/components/Lend/supplyV2/5-Transfer.tsx +14 -58
  22. package/src/components/Lend/supplyV2/6-Success.tsx +5 -2
  23. package/src/components/Lend/supplyV2/index.tsx +31 -59
  24. package/src/components/Lend/withdrawV2/1-ConfirmTransfer.tsx +131 -0
  25. package/src/components/Lend/withdrawV2/2-Transfer.tsx +48 -0
  26. package/src/components/Lend/withdrawV2/index.tsx +53 -136
  27. package/src/index.tsx +44 -15
  28. package/src/components/Lend/redeemV2/1-ChooseAction.tsx +0 -237
  29. package/src/components/Lend/redeemV2/2-ChooseAmount.tsx +0 -101
  30. package/src/components/Lend/redeemV2/4-Done.tsx +0 -33
  31. package/src/components/Lend/redeemV2/index.tsx +0 -207
  32. package/src/components/Lend/withdrawV2/2-ChooseAmount.tsx +0 -88
  33. package/src/components/Lend/withdrawV2/3-Transfer.tsx +0 -110
  34. /package/src/components/Lend/withdrawV2/{4-Done.tsx → 3-Done.tsx} +0 -0
@@ -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
- }
@@ -1,110 +0,0 @@
1
- import {
2
- FirstLossCoverIndex,
3
- getFirstLossCoverContractV2,
4
- getTrancheVaultContractV2,
5
- PoolInfoV2,
6
- TrancheType,
7
- UnderlyingTokenInfo,
8
- } from '@huma-finance/shared'
9
- import { useWeb3React } from '@web3-react/core'
10
- import { BigNumber, ethers } from 'ethers'
11
- import React, { useCallback, useEffect, useState } from 'react'
12
-
13
- import { WithdrawType } from '.'
14
- import { useAppDispatch, useAppSelector } from '../../../hooks/useRedux'
15
- import { setStep } from '../../../store/widgets.reducers'
16
- import { selectWidgetState } from '../../../store/widgets.selectors'
17
- import { WIDGET_STEP } from '../../../store/widgets.store'
18
- import { LoadingModal } from '../../LoadingModal'
19
- import { TxSendModalV2 } from '../../TxSendModalV2'
20
-
21
- type Props = {
22
- poolInfo: PoolInfoV2
23
- poolUnderlyingToken: UnderlyingTokenInfo
24
- selectedWithdrawType: WithdrawType
25
- }
26
-
27
- export function Transfer({
28
- poolInfo,
29
- poolUnderlyingToken,
30
- selectedWithdrawType,
31
- }: Props): React.ReactElement | null {
32
- const dispatch = useAppDispatch()
33
- const { account, provider } = useWeb3React()
34
- const { withdrawAmount } = useAppSelector(selectWidgetState)
35
- const [firstLossCoverShares, setFirstLossCoverShares] = React.useState<
36
- BigNumber | undefined
37
- >(undefined)
38
- const [contract, setContract] = useState<ethers.Contract | undefined>(
39
- undefined,
40
- )
41
-
42
- useEffect(() => {
43
- const fetchData = async () => {
44
- if (account) {
45
- if (selectedWithdrawType.type === 'tranche') {
46
- const trancheVaultContract = await getTrancheVaultContractV2(
47
- poolInfo.poolName,
48
- selectedWithdrawType.value as TrancheType,
49
- provider,
50
- account,
51
- )
52
- if (trancheVaultContract) {
53
- setContract(trancheVaultContract)
54
- }
55
- }
56
- if (selectedWithdrawType.type === 'firstLossCover') {
57
- const firstLossCoverContract = await getFirstLossCoverContractV2(
58
- poolInfo.poolName,
59
- Number(selectedWithdrawType.value) as FirstLossCoverIndex,
60
- provider,
61
- account,
62
- )
63
- if (firstLossCoverContract) {
64
- const shares = await firstLossCoverContract.convertToShares(
65
- ethers.utils.parseUnits(
66
- String(withdrawAmount),
67
- poolUnderlyingToken.decimals,
68
- ),
69
- )
70
- setFirstLossCoverShares(shares)
71
- setContract(firstLossCoverContract)
72
- }
73
- }
74
- }
75
- }
76
- fetchData()
77
- }, [
78
- account,
79
- poolInfo.poolName,
80
- poolUnderlyingToken.decimals,
81
- provider,
82
- selectedWithdrawType.type,
83
- selectedWithdrawType.value,
84
- withdrawAmount,
85
- ])
86
-
87
- const method =
88
- selectedWithdrawType.type === 'tranche' ? 'disburse' : 'redeemCover'
89
- const params =
90
- selectedWithdrawType.type === 'tranche'
91
- ? []
92
- : [firstLossCoverShares, account]
93
-
94
- const handleSuccess = useCallback(() => {
95
- dispatch(setStep(WIDGET_STEP.Done))
96
- }, [dispatch])
97
-
98
- if (!account || !contract) {
99
- return <LoadingModal title='Transaction Pending' />
100
- }
101
-
102
- return (
103
- <TxSendModalV2
104
- contract={contract}
105
- method={method}
106
- params={params}
107
- handleSuccess={handleSuccess}
108
- />
109
- )
110
- }