@huma-finance/widgets 0.0.80-beta.1079 → 0.0.80-beta.1083

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.
@@ -0,0 +1,170 @@
1
+ import {
2
+ CloseModalOptions,
3
+ formatNumberFixed,
4
+ STELLAR_CHAINS_INFO,
5
+ StellarPoolInfo,
6
+ tokenDecimalUtils,
7
+ TrancheType,
8
+ } from '@huma-finance/shared'
9
+ import { Client as TrancheVaultClient } from '@huma-finance/soroban-tranche-vault'
10
+ import {
11
+ StellarConnectionContext,
12
+ StellarPoolState,
13
+ } from '@huma-finance/web-shared'
14
+ import React, { useCallback, useContext, useEffect, useState } from 'react'
15
+ import { useDispatch } from 'react-redux'
16
+
17
+ import { useAppSelector } from '../../../hooks/useRedux'
18
+ import { setStep, setWithdrawAmount } from '../../../store/widgets.reducers'
19
+ import { selectWidgetState } from '../../../store/widgets.selectors'
20
+ import { WIDGET_STEP } from '../../../store/widgets.store'
21
+ import { ErrorModal } from '../../ErrorModal'
22
+ import { WidgetWrapper } from '../../WidgetWrapper'
23
+ import { Confirm } from './1-Confirm'
24
+ import { Transfer } from './2-Transfer'
25
+ import { Done } from './3-Done'
26
+
27
+ /**
28
+ * Stellar lend pool withdraw props – disburse (pool open) or withdraw after pool closure.
29
+ * @property {StellarPoolInfo} poolInfo The metadata of the pool.
30
+ * @property {StellarPoolState} poolState The current state config of the pool.
31
+ * @property {TrancheType} tranche The tranche to withdraw from (junior or senior).
32
+ * @property {function((CloseModalOptions|undefined)):void} handleClose Function to close the widget modal.
33
+ * @property {function():void|undefined} handleSuccess Optional callback when withdraw succeeds.
34
+ */
35
+ export interface StellarLendWithdrawProps {
36
+ poolInfo: StellarPoolInfo
37
+ poolState: StellarPoolState
38
+ tranche: TrancheType
39
+ handleClose: (options?: CloseModalOptions) => void
40
+ handleSuccess?: () => void
41
+ }
42
+
43
+ export function StellarLendWithdraw({
44
+ poolInfo,
45
+ poolState,
46
+ tranche,
47
+ handleClose,
48
+ handleSuccess,
49
+ }: StellarLendWithdrawProps): React.ReactElement | null {
50
+ const dispatch = useDispatch()
51
+ const { address: stellarAddress } = useContext(StellarConnectionContext)
52
+ const { step, errorMessage, withdrawAmount } =
53
+ useAppSelector(selectWidgetState)
54
+ const [withdrawableAmount, setWithdrawableAmount] = useState<bigint>(
55
+ BigInt(0),
56
+ )
57
+ const [isLoadingWithdrawable, setIsLoadingWithdrawable] = useState(true)
58
+
59
+ const { symbol, decimals } = poolInfo.underlyingToken
60
+ const title = `Withdraw ${symbol}`
61
+ const poolIsClosed = poolState?.status === 'closed'
62
+ const withdrawableAmountFormatted = formatNumberFixed(
63
+ tokenDecimalUtils.formatUnits(withdrawableAmount, decimals),
64
+ 2,
65
+ )
66
+
67
+ useEffect(() => {
68
+ if (!step && stellarAddress) {
69
+ dispatch(setStep(WIDGET_STEP.ConfirmWithdrawOnly))
70
+ }
71
+ }, [dispatch, step, stellarAddress])
72
+
73
+ useEffect(() => {
74
+ async function fetchWithdrawable() {
75
+ if (!stellarAddress || !poolInfo) {
76
+ setIsLoadingWithdrawable(false)
77
+ return
78
+ }
79
+ setIsLoadingWithdrawable(true)
80
+ try {
81
+ const chainMetadata = STELLAR_CHAINS_INFO[poolInfo.chainId]
82
+ const contractId =
83
+ tranche === 'senior'
84
+ ? poolInfo.seniorTranche!
85
+ : poolInfo.juniorTranche
86
+ const client = new TrancheVaultClient({
87
+ publicKey: stellarAddress,
88
+ contractId,
89
+ networkPassphrase: chainMetadata.networkPassphrase,
90
+ rpcUrl: chainMetadata.rpc,
91
+ })
92
+ const res = await client.get_latest_redemption_record({
93
+ lender: stellarAddress,
94
+ })
95
+ const record = res.result
96
+ const withdrawable =
97
+ BigInt(record.total_amount_processed) -
98
+ BigInt(record.total_amount_withdrawn)
99
+ setWithdrawableAmount(withdrawable)
100
+ } catch (err) {
101
+ console.error('Failed to fetch withdrawable amount:', err)
102
+ setWithdrawableAmount(BigInt(0))
103
+ } finally {
104
+ setIsLoadingWithdrawable(false)
105
+ }
106
+ }
107
+ fetchWithdrawable()
108
+ }, [stellarAddress, poolInfo, tranche])
109
+
110
+ const handleConfirmWithdraw = useCallback(() => {
111
+ dispatch(setWithdrawAmount(Number(withdrawableAmountFormatted)))
112
+ dispatch(setStep(WIDGET_STEP.Transfer))
113
+ }, [dispatch, withdrawableAmountFormatted])
114
+
115
+ const handleWithdrawSuccess = useCallback(() => {
116
+ handleSuccess?.()
117
+ }, [handleSuccess])
118
+
119
+ if (isLoadingWithdrawable && step === WIDGET_STEP.ConfirmWithdrawOnly) {
120
+ return (
121
+ <WidgetWrapper
122
+ isOpen
123
+ loadingTitle={title}
124
+ handleClose={handleClose}
125
+ handleSuccess={handleWithdrawSuccess}
126
+ />
127
+ )
128
+ }
129
+
130
+ return (
131
+ <WidgetWrapper
132
+ isOpen
133
+ loadingTitle={title}
134
+ handleClose={handleClose}
135
+ handleSuccess={handleWithdrawSuccess}
136
+ >
137
+ {step === WIDGET_STEP.ConfirmWithdrawOnly && (
138
+ <Confirm
139
+ poolInfo={poolInfo}
140
+ withdrawableAmountFormatted={String(
141
+ withdrawableAmountFormatted ?? '0',
142
+ )}
143
+ onConfirm={handleConfirmWithdraw}
144
+ />
145
+ )}
146
+ {step === WIDGET_STEP.Transfer && (
147
+ <Transfer
148
+ poolInfo={poolInfo}
149
+ tranche={tranche}
150
+ poolIsClosed={poolIsClosed}
151
+ />
152
+ )}
153
+ {step === WIDGET_STEP.Done && (
154
+ <Done
155
+ poolInfo={poolInfo}
156
+ withdrawAmount={withdrawAmount}
157
+ handleAction={handleClose}
158
+ />
159
+ )}
160
+ {step === WIDGET_STEP.Error && (
161
+ <ErrorModal
162
+ title={title}
163
+ errorReason='Sorry there was an error'
164
+ errorMessage={errorMessage}
165
+ handleOk={handleClose}
166
+ />
167
+ )}
168
+ </WidgetWrapper>
169
+ )
170
+ }
package/src/index.tsx CHANGED
@@ -77,10 +77,18 @@ import {
77
77
  SolanaLendWithdraw,
78
78
  SolanaLendWithdrawProps,
79
79
  } from './components/Lend/solanaWithdraw'
80
+ import {
81
+ StellarLendAddRedemption,
82
+ StellarLendAddRedemptionProps,
83
+ } from './components/Lend/stellarAddRedemption'
80
84
  import {
81
85
  StellarLendSupply,
82
86
  StellarLendSupplyProps,
83
87
  } from './components/Lend/stellarSupply'
88
+ import {
89
+ StellarLendWithdraw,
90
+ StellarLendWithdrawProps,
91
+ } from './components/Lend/stellarWithdraw'
84
92
  import { LendSupply, LendSupplyProps } from './components/Lend/supply'
85
93
  import { LendSupplyPropsV2, LendSupplyV2 } from './components/Lend/supplyV2'
86
94
  import { LendWithdraw, LendWithdrawProps } from './components/Lend/withdraw'
@@ -713,6 +721,50 @@ export function StellarLendSupplyWidget(props: StellarLendSupplyWidgetProps) {
713
721
  )
714
722
  }
715
723
 
724
+ /**
725
+ * Lend pool add redemption widget props for Stellar pools
726
+ * @typedef {Object} StellarLendAddRedemptionWidgetProps
727
+ */
728
+ type StellarLendAddRedemptionWidgetProps = StellarLendAddRedemptionProps &
729
+ GenericWidgetProps
730
+
731
+ /**
732
+ * Lend pool add redemption widget for Stellar pools
733
+ *
734
+ * @param {StellarLendAddRedemptionWidgetProps} props - Widget props
735
+ */
736
+ export function StellarLendAddRedemptionWidget(
737
+ props: StellarLendAddRedemptionWidgetProps,
738
+ ) {
739
+ return (
740
+ <GenericWidget {...props}>
741
+ <StellarLendAddRedemption {...props} />
742
+ </GenericWidget>
743
+ )
744
+ }
745
+
746
+ /**
747
+ * Lend pool withdraw widget props for Stellar pools
748
+ * @typedef {Object} StellarLendWithdrawWidgetProps
749
+ */
750
+ type StellarLendWithdrawWidgetProps = StellarLendWithdrawProps &
751
+ GenericWidgetProps
752
+
753
+ /**
754
+ * Lend pool withdraw widget for Stellar pools (disburse when pool open, withdraw_after_pool_closure when closed)
755
+ *
756
+ * @param {StellarLendWithdrawWidgetProps} props - Widget props
757
+ */
758
+ export function StellarLendWithdrawWidget(
759
+ props: StellarLendWithdrawWidgetProps,
760
+ ) {
761
+ return (
762
+ <GenericWidget {...props}>
763
+ <StellarLendWithdraw {...props} />
764
+ </GenericWidget>
765
+ )
766
+ }
767
+
716
768
  /**
717
769
  * Borrow widget props for Stellar pools
718
770
  * @typedef {Object} StellarLendSupplyWidgetProps