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

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@huma-finance/widgets",
3
- "version": "0.0.48-beta.291+e9f7853",
3
+ "version": "0.0.48-beta.296+a50d41c",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -30,8 +30,8 @@
30
30
  "@ethersproject/experimental": "^5.7.0",
31
31
  "@ethersproject/providers": "^5.6.0",
32
32
  "@ethersproject/units": "^5.6.0",
33
- "@huma-finance/sdk": "^0.0.47",
34
- "@huma-finance/shared": "^0.0.47",
33
+ "@huma-finance/sdk": "^0.0.48-beta.296+a50d41c",
34
+ "@huma-finance/shared": "^0.0.48-beta.296+a50d41c",
35
35
  "@mui/icons-material": "^5.3.0",
36
36
  "@mui/material": "^5.0.6",
37
37
  "@mui/styles": "^5.0.2",
@@ -193,5 +193,5 @@
193
193
  "optionalDependencies": {
194
194
  "encoding": "^0.1.13"
195
195
  },
196
- "gitHead": "e9f78530d12c83443870f12b85436a65788e0b94"
196
+ "gitHead": "a50d41ce9bafdb1b8894da5c38a139b4efcda676"
197
197
  }
@@ -45,11 +45,6 @@ export function ChooseAmount({
45
45
  ethers.utils.formatUnits(payoffAmountBN, decimals),
46
46
  )
47
47
 
48
- console.log('totalDueAmountBN', totalDueAmountBN.toString())
49
- console.log('payoffAmountBN', payoffAmountBN.toString())
50
- console.log('payoffAmount', payoffAmount)
51
- console.log('currentAmount', currentAmount)
52
-
53
48
  useEffect(() => {
54
49
  setCurrentAmount(totalDueAmount)
55
50
  }, [totalDueAmount])
@@ -1,4 +1,4 @@
1
- import { TrancheType, UnderlyingTokenInfo } from '@huma-finance/shared'
1
+ import { UnderlyingTokenInfo } from '@huma-finance/shared'
2
2
  import {
3
3
  css,
4
4
  FormControl,
@@ -10,6 +10,7 @@ import {
10
10
  } from '@mui/material'
11
11
  import React from 'react'
12
12
 
13
+ import { SupplyType } from '.'
13
14
  import { useAppDispatch } from '../../../hooks/useRedux'
14
15
  import { setStep } from '../../../store/widgets.reducers'
15
16
  import { WIDGET_STEP } from '../../../store/widgets.store'
@@ -18,14 +19,16 @@ import { WrapperModal } from '../../WrapperModal'
18
19
 
19
20
  type Props = {
20
21
  poolUnderlyingToken: UnderlyingTokenInfo
21
- selectedTranche: TrancheType | undefined
22
- changeTranche: (tranche: TrancheType) => void
22
+ selectedSupplyType: SupplyType | undefined
23
+ supplyTypes: SupplyType[]
24
+ changeSupplyType: (supplyType: SupplyType) => void
23
25
  }
24
26
 
25
- export function ChooseTranche({
27
+ export function ChooseSupplyType({
26
28
  poolUnderlyingToken,
27
- selectedTranche,
28
- changeTranche,
29
+ selectedSupplyType,
30
+ supplyTypes,
31
+ changeSupplyType,
29
32
  }: Props): React.ReactElement | null {
30
33
  const theme = useTheme()
31
34
  const dispatch = useAppDispatch()
@@ -57,24 +60,19 @@ export function ChooseTranche({
57
60
  `,
58
61
  }
59
62
 
63
+ const handleChangeSupplyType = (supplyTypeValue: string | number) => {
64
+ const supplyType = supplyTypes.find(
65
+ (item) => item.value === supplyTypeValue,
66
+ )
67
+ if (supplyType) {
68
+ changeSupplyType(supplyType)
69
+ }
70
+ }
71
+
60
72
  const handleNext = () => {
61
73
  dispatch(setStep(WIDGET_STEP.ChooseAmount))
62
74
  }
63
75
 
64
- const items: {
65
- label: string
66
- value: TrancheType
67
- }[] = [
68
- {
69
- label: 'Senior',
70
- value: 'senior',
71
- },
72
- {
73
- label: 'Junior',
74
- value: 'junior',
75
- },
76
- ]
77
-
78
76
  return (
79
77
  <WrapperModal title={`Supply ${poolUnderlyingToken.symbol}`}>
80
78
  <FormControl>
@@ -83,9 +81,9 @@ export function ChooseTranche({
83
81
  aria-labelledby='buttons-group-label'
84
82
  name='radio-buttons-group'
85
83
  css={styles.radioGroup}
86
- onChange={(e) => changeTranche(e.target.value as TrancheType)}
84
+ onChange={(e) => handleChangeSupplyType(e.target.value)}
87
85
  >
88
- {items.map((item) => (
86
+ {supplyTypes.map((item) => (
89
87
  <FormControlLabel
90
88
  key={item.label}
91
89
  value={item.value}
@@ -106,7 +104,7 @@ export function ChooseTranche({
106
104
  </FormControl>
107
105
  <BottomButton
108
106
  variant='contained'
109
- disabled={!selectedTranche}
107
+ disabled={!selectedSupplyType}
110
108
  onClick={handleNext}
111
109
  >
112
110
  NEXT
@@ -1,8 +1,9 @@
1
1
  import {
2
+ FirstLossCoverIndex,
3
+ formatNumber,
2
4
  PoolInfoV2,
3
- TrancheType,
4
5
  UnderlyingTokenInfo,
5
- formatNumber,
6
+ useFirstLossCoverAllowanceV2,
6
7
  usePoolSafeAllowanceV2,
7
8
  usePoolUnderlyingTokenBalanceV2,
8
9
  } from '@huma-finance/shared'
@@ -10,6 +11,7 @@ import { useWeb3React } from '@web3-react/core'
10
11
  import { BigNumber, ethers } from 'ethers'
11
12
  import React, { useState } from 'react'
12
13
 
14
+ import { SupplyType } from '.'
13
15
  import { useAppDispatch } from '../../../hooks/useRedux'
14
16
  import { setStep, setSupplyAmount } from '../../../store/widgets.reducers'
15
17
  import { WIDGET_STEP } from '../../../store/widgets.store'
@@ -18,30 +20,31 @@ import { InputAmountModal } from '../../InputAmountModal'
18
20
  type Props = {
19
21
  poolInfo: PoolInfoV2
20
22
  poolUnderlyingToken: UnderlyingTokenInfo
21
- selectedTranche: TrancheType | undefined
23
+ selectedSupplyType: SupplyType
22
24
  }
23
25
 
24
26
  export function ChooseAmount({
25
27
  poolInfo,
26
28
  poolUnderlyingToken,
27
- selectedTranche,
29
+ selectedSupplyType,
28
30
  }: Props): React.ReactElement | null {
29
31
  const dispatch = useAppDispatch()
30
32
  const { account, provider } = useWeb3React()
31
33
  const { symbol, decimals } = poolUnderlyingToken
32
34
  const [currentAmount, setCurrentAmount] = useState<number | string>(0)
33
- const { allowance = BigNumber.from(0) } = usePoolSafeAllowanceV2(
34
- poolInfo.poolName,
35
- account,
36
- provider,
37
- )
38
- const [balance] = usePoolUnderlyingTokenBalanceV2(
39
- poolInfo.poolName,
40
- account,
41
- provider,
42
- )
35
+ const { allowance: poolSafeAllowance = BigNumber.from(0) } =
36
+ usePoolSafeAllowanceV2(poolInfo.poolName, account, provider)
37
+ const [firstLossCoverAllowance = BigNumber.from(0)] =
38
+ useFirstLossCoverAllowanceV2(
39
+ poolInfo.poolName,
40
+ Number(selectedSupplyType.value) as FirstLossCoverIndex,
41
+ account,
42
+ provider,
43
+ )
44
+ const [underlyingTokenBalance = BigNumber.from(0)] =
45
+ usePoolUnderlyingTokenBalanceV2(poolInfo.poolName, account, provider)
43
46
  const maxAmountFormatted = ethers.utils.formatUnits(
44
- balance,
47
+ underlyingTokenBalance,
45
48
  poolUnderlyingToken.decimals,
46
49
  )
47
50
 
@@ -55,16 +58,23 @@ export function ChooseAmount({
55
58
  String(currentAmount),
56
59
  decimals,
57
60
  )
58
- const step = currentAmountBN.gt(allowance)
59
- ? WIDGET_STEP.ApproveAllowance
60
- : WIDGET_STEP.Transfer
61
- dispatch(setStep(step))
61
+ if (selectedSupplyType.type === 'tranche') {
62
+ const step = currentAmountBN.gt(poolSafeAllowance)
63
+ ? WIDGET_STEP.ApproveAllowance
64
+ : WIDGET_STEP.Transfer
65
+ dispatch(setStep(step))
66
+ } else {
67
+ const step = currentAmountBN.gt(firstLossCoverAllowance)
68
+ ? WIDGET_STEP.ApproveAllowance
69
+ : WIDGET_STEP.Transfer
70
+ dispatch(setStep(step))
71
+ }
62
72
  }
63
73
 
64
74
  return (
65
75
  <InputAmountModal
66
76
  title='Enter Amount'
67
- subTitle={`Supplying ${symbol} with ${selectedTranche}`}
77
+ subTitle={`Supplying ${symbol} with ${selectedSupplyType?.label}`}
68
78
  tokenSymbol={symbol}
69
79
  currentAmount={currentAmount}
70
80
  handleChangeAmount={handleChangeAmount}
@@ -1,6 +1,11 @@
1
- import { PoolInfoV2, UnderlyingTokenInfo } from '@huma-finance/shared'
1
+ import {
2
+ FirstLossCoverIndex,
3
+ PoolInfoV2,
4
+ UnderlyingTokenInfo,
5
+ } from '@huma-finance/shared'
2
6
  import React, { useCallback } from 'react'
3
7
 
8
+ import { SupplyType } from '.'
4
9
  import { useAppDispatch } from '../../../hooks/useRedux'
5
10
  import { setStep } from '../../../store/widgets.reducers'
6
11
  import { WIDGET_STEP } from '../../../store/widgets.store'
@@ -9,14 +14,22 @@ import { ApproveAllowanceModalV2 } from '../../ApproveAllowanceModalV2'
9
14
  type Props = {
10
15
  poolInfo: PoolInfoV2
11
16
  poolUnderlyingToken: UnderlyingTokenInfo
17
+ selectedSupplyType: SupplyType
12
18
  }
13
19
 
14
20
  export function ApproveAllowance({
15
21
  poolInfo,
16
22
  poolUnderlyingToken,
23
+ selectedSupplyType,
17
24
  }: Props): React.ReactElement | null {
18
25
  const dispatch = useAppDispatch()
19
- const spender = poolInfo.poolSafe
26
+ let spender = poolInfo.poolSafe
27
+ if (selectedSupplyType.type === 'firstLossCover') {
28
+ spender =
29
+ poolInfo.firstLossCovers[
30
+ Number(selectedSupplyType.value) as FirstLossCoverIndex
31
+ ]
32
+ }
20
33
 
21
34
  const handleSuccess = useCallback(() => {
22
35
  dispatch(setStep(WIDGET_STEP.Transfer))
@@ -1,30 +1,34 @@
1
1
  import {
2
+ FirstLossCoverIndex,
3
+ getFirstLossCoverContractV2,
4
+ getTrancheVaultContractV2,
2
5
  PoolInfoV2,
3
- useTrancheVaultContractV2,
4
6
  TrancheType,
5
7
  UnderlyingTokenInfo,
6
8
  } from '@huma-finance/shared'
7
9
  import { useWeb3React } from '@web3-react/core'
8
10
  import { ethers } from 'ethers'
9
- import React, { useCallback } from 'react'
11
+ import React, { useCallback, useEffect, useState } from 'react'
10
12
 
13
+ import { SupplyType } from '.'
11
14
  import { useAppDispatch, useAppSelector } from '../../../hooks/useRedux'
12
15
  import { setStep } from '../../../store/widgets.reducers'
13
16
  import { selectWidgetState } from '../../../store/widgets.selectors'
14
17
  import { WIDGET_STEP } from '../../../store/widgets.store'
18
+ import { LoadingModal } from '../../LoadingModal'
15
19
  import { TxSendModalV2 } from '../../TxSendModalV2'
16
20
 
17
21
  type Props = {
18
22
  poolInfo: PoolInfoV2
19
23
  poolUnderlyingToken: UnderlyingTokenInfo
20
- trancheType: TrancheType
24
+ selectedSupplyType: SupplyType
21
25
  }
22
26
 
23
27
  export function Transfer({
24
28
  poolInfo,
25
29
  poolUnderlyingToken,
26
- trancheType,
27
- }: Props): React.ReactElement | null {
30
+ selectedSupplyType,
31
+ }: Props): React.ReactElement {
28
32
  const dispatch = useAppDispatch()
29
33
  const { account, provider } = useWeb3React()
30
34
  const { supplyAmount } = useAppSelector(selectWidgetState)
@@ -33,26 +37,66 @@ export function Transfer({
33
37
  String(supplyAmount),
34
38
  decimals,
35
39
  )
36
- const trancheVaultContract = useTrancheVaultContractV2(
40
+ const [contract, setContract] = useState<ethers.Contract | undefined>(
41
+ undefined,
42
+ )
43
+
44
+ useEffect(() => {
45
+ const fetchData = async () => {
46
+ if (account) {
47
+ if (selectedSupplyType.type === 'tranche') {
48
+ const trancheVaultContract = await getTrancheVaultContractV2(
49
+ poolInfo.poolName,
50
+ selectedSupplyType.value as TrancheType,
51
+ provider,
52
+ account,
53
+ )
54
+ if (trancheVaultContract) {
55
+ setContract(trancheVaultContract)
56
+ }
57
+ }
58
+ if (selectedSupplyType.type === 'firstLossCover') {
59
+ const firstLossCoverContract = await getFirstLossCoverContractV2(
60
+ poolInfo.poolName,
61
+ Number(selectedSupplyType.value) as FirstLossCoverIndex,
62
+ provider,
63
+ account,
64
+ )
65
+ if (firstLossCoverContract) {
66
+ setContract(firstLossCoverContract)
67
+ }
68
+ }
69
+ }
70
+ }
71
+ fetchData()
72
+ }, [
73
+ account,
37
74
  poolInfo.poolName,
38
- trancheType,
39
75
  provider,
40
- account,
41
- )
76
+ selectedSupplyType.type,
77
+ selectedSupplyType.value,
78
+ ])
79
+
80
+ const method =
81
+ selectedSupplyType.type === 'tranche' ? 'deposit' : 'depositCover'
82
+ const params =
83
+ selectedSupplyType.type === 'tranche'
84
+ ? [supplyBigNumber, account]
85
+ : [supplyBigNumber]
42
86
 
43
87
  const handleSuccess = useCallback(() => {
44
88
  dispatch(setStep(WIDGET_STEP.Done))
45
89
  }, [dispatch])
46
90
 
47
- if (!trancheVaultContract || !account) {
48
- return null
91
+ if (!contract) {
92
+ return <LoadingModal title='Transaction Pending' />
49
93
  }
50
94
 
51
95
  return (
52
96
  <TxSendModalV2
53
- contract={trancheVaultContract}
54
- method='deposit'
55
- params={[supplyBigNumber, account]}
97
+ contract={contract}
98
+ method={method}
99
+ params={params}
56
100
  handleSuccess={handleSuccess}
57
101
  />
58
102
  )
@@ -45,11 +45,8 @@ export function Success({
45
45
  const events = decodeLogs(txReceipt.logs, TRANSFER_ABI)
46
46
  if (events) {
47
47
  events.forEach((event) => {
48
- const { from, to, value } = event.args
49
- if (
50
- from.toLowerCase() === account?.toLowerCase() &&
51
- to.toLowerCase() === poolInfo.poolSafe.toLowerCase()
52
- ) {
48
+ const { from, value } = event.args
49
+ if (from.toLowerCase() === account?.toLowerCase()) {
53
50
  setSupplyAmount(downScale(value.toString(), decimals))
54
51
  }
55
52
  })
@@ -1,7 +1,9 @@
1
1
  import {
2
- POOL_NAME,
3
- TrancheType,
2
+ capitalizeFirstLetter,
3
+ FirstLossCoverIndex,
4
4
  openInNewTab,
5
+ POOL_NAME,
6
+ useFirstLossCoverStatsV2,
5
7
  useLenderApprovedV2,
6
8
  usePoolInfoV2,
7
9
  usePoolUnderlyingTokenInfoV2,
@@ -16,7 +18,7 @@ import { selectWidgetState } from '../../../store/widgets.selectors'
16
18
  import { WIDGET_STEP } from '../../../store/widgets.store'
17
19
  import { ErrorModal } from '../../ErrorModal'
18
20
  import { WidgetWrapper } from '../../WidgetWrapper'
19
- import { ChooseTranche } from './1-ChooseTranche'
21
+ import { ChooseSupplyType } from './1-ChooseTranche'
20
22
  import { Evaluation } from './2-Evaluation'
21
23
  import { ChooseAmount } from './3-ChooseAmount'
22
24
  import { ApproveAllowance } from './4-ApproveAllowance'
@@ -24,6 +26,12 @@ import { Transfer } from './5-Transfer'
24
26
  import { Success } from './6-Success'
25
27
  import { Notifications } from './7-Notifications'
26
28
 
29
+ export type SupplyType = {
30
+ label: string
31
+ value: string
32
+ type: 'tranche' | 'firstLossCover'
33
+ }
34
+
27
35
  /**
28
36
  * Lend pool supply props
29
37
  * @typedef {Object} LendSupplyPropsV2
@@ -47,7 +55,8 @@ export function LendSupplyV2({
47
55
  const { chainId, provider, account } = useWeb3React()
48
56
  const poolInfo = usePoolInfoV2(poolName, chainId)
49
57
  const { step, errorMessage } = useAppSelector(selectWidgetState)
50
- const [selectedTranche, setSelectedTranche] = useState<TrancheType>()
58
+ const [supplyTypes, setSupplyTypes] = useState<SupplyType[]>([])
59
+ const [selectedSupplyType, setSelectedSupplyType] = useState<SupplyType>()
51
60
  const poolUnderlyingToken = usePoolUnderlyingTokenInfoV2(poolName, provider)
52
61
 
53
62
  const [lenderApprovedSenior] = useLenderApprovedV2(
@@ -62,40 +71,57 @@ export function LendSupplyV2({
62
71
  account,
63
72
  provider,
64
73
  )
74
+ const [flcStats] = useFirstLossCoverStatsV2(poolName, account, provider)
65
75
 
66
76
  const lenderApproveStatusFetched =
67
- lenderApprovedSenior !== undefined && lenderApprovedJunior !== undefined
77
+ lenderApprovedSenior !== undefined &&
78
+ lenderApprovedJunior !== undefined &&
79
+ flcStats !== undefined
68
80
 
69
81
  useEffect(() => {
70
82
  if (!step && poolInfo && lenderApproveStatusFetched) {
71
- if (lenderApprovedJunior && !lenderApprovedSenior) {
72
- setSelectedTranche('junior')
73
- dispatch(setStep(WIDGET_STEP.ChooseAmount))
74
- return
83
+ const items: SupplyType[] = []
84
+ if (lenderApprovedSenior) {
85
+ items.push({
86
+ label: 'Senior Tranche',
87
+ value: 'senior',
88
+ type: 'tranche',
89
+ })
75
90
  }
76
-
77
- if (lenderApprovedSenior && !lenderApprovedJunior) {
78
- setSelectedTranche('senior')
79
- dispatch(setStep(WIDGET_STEP.ChooseAmount))
80
- return
91
+ if (lenderApprovedJunior) {
92
+ items.push({
93
+ label: 'Junior Tranche',
94
+ value: 'junior',
95
+ type: 'tranche',
96
+ })
81
97
  }
98
+ flcStats
99
+ .filter((flc) => flc.isApproved)
100
+ .forEach((flc) => {
101
+ items.push({
102
+ label: `${capitalizeFirstLetter(
103
+ FirstLossCoverIndex[flc.firstLossCoverIndex],
104
+ )} First Loss Cover`,
105
+ value: String(flc.firstLossCoverIndex),
106
+ type: 'firstLossCover',
107
+ })
108
+ })
82
109
 
83
- if (lenderApprovedJunior && lenderApprovedSenior) {
110
+ if (items.length === 1) {
111
+ setSelectedSupplyType(items[0])
112
+ dispatch(setStep(WIDGET_STEP.ChooseAmount))
113
+ } else if (items.length > 1) {
114
+ setSupplyTypes(items)
84
115
  dispatch(setStep(WIDGET_STEP.ChooseTranche))
85
- return
86
- }
87
-
88
- if (poolInfo.KYC) {
116
+ } else if (poolInfo.KYC) {
89
117
  dispatch(setStep(WIDGET_STEP.Evaluation))
90
- return
91
- }
92
-
93
- if (poolInfo.supplyLink) {
118
+ } else if (poolInfo.supplyLink) {
94
119
  openInNewTab(poolInfo.supplyLink)
95
120
  }
96
121
  }
97
122
  }, [
98
123
  dispatch,
124
+ flcStats,
99
125
  lenderApproveStatusFetched,
100
126
  lenderApprovedJunior,
101
127
  lenderApprovedSenior,
@@ -123,33 +149,35 @@ export function LendSupplyV2({
123
149
  handleSuccess={handleSuccess}
124
150
  >
125
151
  {step === WIDGET_STEP.ChooseTranche && (
126
- <ChooseTranche
152
+ <ChooseSupplyType
127
153
  poolUnderlyingToken={poolUnderlyingToken}
128
- selectedTranche={selectedTranche}
129
- changeTranche={setSelectedTranche}
154
+ selectedSupplyType={selectedSupplyType}
155
+ supplyTypes={supplyTypes}
156
+ changeSupplyType={setSelectedSupplyType}
130
157
  />
131
158
  )}
132
159
  {step === WIDGET_STEP.Evaluation && (
133
160
  <Evaluation poolInfo={poolInfo} handleClose={handleClose} />
134
161
  )}
135
- {step === WIDGET_STEP.ChooseAmount && (
162
+ {step === WIDGET_STEP.ChooseAmount && selectedSupplyType && (
136
163
  <ChooseAmount
137
164
  poolInfo={poolInfo}
138
165
  poolUnderlyingToken={poolUnderlyingToken}
139
- selectedTranche={selectedTranche}
166
+ selectedSupplyType={selectedSupplyType}
140
167
  />
141
168
  )}
142
- {step === WIDGET_STEP.ApproveAllowance && (
169
+ {step === WIDGET_STEP.ApproveAllowance && selectedSupplyType && (
143
170
  <ApproveAllowance
144
171
  poolInfo={poolInfo}
145
172
  poolUnderlyingToken={poolUnderlyingToken}
173
+ selectedSupplyType={selectedSupplyType}
146
174
  />
147
175
  )}
148
- {step === WIDGET_STEP.Transfer && selectedTranche && (
176
+ {step === WIDGET_STEP.Transfer && selectedSupplyType && (
149
177
  <Transfer
150
178
  poolInfo={poolInfo}
151
179
  poolUnderlyingToken={poolUnderlyingToken}
152
- trancheType={selectedTranche}
180
+ selectedSupplyType={selectedSupplyType}
153
181
  />
154
182
  )}
155
183
  {step === WIDGET_STEP.Done && (