@huma-finance/widgets 0.0.48-beta.188 → 0.0.48-beta.189

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.188+fa13511",
3
+ "version": "0.0.48-beta.189+347bf1c",
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.189+347bf1c",
34
+ "@huma-finance/shared": "^0.0.48-beta.189+347bf1c",
35
35
  "@mui/icons-material": "^5.3.0",
36
36
  "@mui/material": "^5.0.6",
37
37
  "@mui/styles": "^5.0.2",
@@ -192,5 +192,5 @@
192
192
  "optionalDependencies": {
193
193
  "encoding": "^0.1.13"
194
194
  },
195
- "gitHead": "fa13511dd738b17865b849f8744c28bdf3393c3f"
195
+ "gitHead": "347bf1c48c9d6a68b97d6f470c2cc7db26262769"
196
196
  }
@@ -85,6 +85,12 @@ export function ApproveAllowanceModal({
85
85
  const amountToIncrease = BigNumber.from(targetAllowanceAmount).sub(
86
86
  currentAllowance ?? 0,
87
87
  )
88
+
89
+ if (amountToIncrease.lte(0)) {
90
+ handleSuccess()
91
+ return
92
+ }
93
+
88
94
  send({
89
95
  contract: poolUnderlyingTokenContract!,
90
96
  method: 'increaseAllowance',
@@ -94,6 +100,7 @@ export function ApproveAllowanceModal({
94
100
  }, [
95
101
  account,
96
102
  allowanceAmount,
103
+ handleSuccess,
97
104
  poolInfo.pool,
98
105
  poolUnderlyingTokenContract,
99
106
  provider,
@@ -1,10 +1,11 @@
1
- import { useWeb3React } from '@web3-react/core'
1
+ import { getAddress } from '@ethersproject/address'
2
2
  import { PoolInfoType } from '@huma-finance/shared'
3
+ import { useWeb3React } from '@web3-react/core'
3
4
  import React, { useEffect } from 'react'
4
5
 
5
6
  import useEA from '../../../hooks/useEA'
6
- import { LoadingModal } from '../../LoadingModal'
7
7
  import { WIDGET_STEP } from '../../../store/widgets.store'
8
+ import { LoadingModal } from '../../LoadingModal'
8
9
  import { SignIn } from '../../SignIn'
9
10
 
10
11
  type Props = {
@@ -35,7 +36,7 @@ export function Evaluation({
35
36
  receivable: {
36
37
  address: poolInfo.assetAddress!,
37
38
  },
38
- payerWalletAddress: payerAddress,
39
+ payerWalletAddress: getAddress(payerAddress),
39
40
  superToken,
40
41
  },
41
42
  },
@@ -28,20 +28,24 @@ export function ChooseAmount({
28
28
  const [chargedFees, setChargedFees] = useState(0)
29
29
  const [currentAmount, setCurrentAmount] = useState(0)
30
30
  const borrowPeriodInSeconds = approval!.terms.intervalInDays * 24 * 60 * 60
31
+ const superTokenDecimals = poolInfo.extra?.superToken?.decimals!
32
+ const underlyingTokenDecimals = poolInfo.poolUnderlyingToken.decimals!
33
+ // To ensure not to borrow all the available flowrate due to contract limitation
34
+ const borrowMaxAmount = Number(approval!.terms.creditLimitFormatted) - 0.01
31
35
 
32
36
  const getBorrowFlowrateAndAmount = useCallback(
33
37
  (borrowAmount: number) => {
34
38
  const currentFlowRateBN = toBigNumber(currentFlowRate)
35
- const borrowAmountBN = toBigNumber(
36
- upScale(borrowAmount, approval!.token.decimal),
39
+ const borrowAmountSuperTokenBN = toBigNumber(
40
+ upScale(borrowAmount, superTokenDecimals),
37
41
  )
38
42
 
39
- const totalAmountInBorrowPeriod = currentFlowRateBN.mul(
43
+ const totalAmountSuperTokenInBorrowPeriod = currentFlowRateBN.mul(
40
44
  borrowPeriodInSeconds,
41
45
  )
42
- let borrowFlowrate = borrowAmountBN
46
+ let borrowFlowrate = borrowAmountSuperTokenBN
43
47
  .mul(currentFlowRateBN)
44
- .div(totalAmountInBorrowPeriod)
48
+ .div(totalAmountSuperTokenInBorrowPeriod)
45
49
  // To ensure enough flowRate due to round down when convert to string
46
50
  .add(1)
47
51
 
@@ -49,26 +53,28 @@ export function ChooseAmount({
49
53
  if (borrowFlowrate.gte(currentFlowRateBN)) {
50
54
  borrowFlowrate = currentFlowRateBN.sub(1)
51
55
  }
52
- if (borrowAmountBN.gte(approval!.terms.creditLimit)) {
53
- borrowFlowrate = borrowFlowrate.sub(1)
54
- }
55
56
 
56
57
  // As borrowFlowrate adds 1, need to calculate the new accurate borrow amount
57
58
  const newBorrowAmountBN = borrowFlowrate
58
- .mul(totalAmountInBorrowPeriod)
59
+ .mul(totalAmountSuperTokenInBorrowPeriod)
59
60
  .div(currentFlowRateBN)
60
- const newBorrowAmount = downScale(
61
- newBorrowAmountBN,
62
- approval!.token.decimal,
61
+ const newBorrowAmount = downScale(newBorrowAmountBN, superTokenDecimals)
62
+ const newBorrowAmountUnderlyingTokenBN = toBigNumber(
63
+ upScale(newBorrowAmount, underlyingTokenDecimals),
63
64
  )
64
65
 
65
66
  return {
66
67
  borrowFlowrate: borrowFlowrate.toString(),
67
68
  borrowAmount: Number(newBorrowAmount),
68
- borrowAmountBN: newBorrowAmountBN,
69
+ borrowAmountBN: newBorrowAmountUnderlyingTokenBN,
69
70
  }
70
71
  },
71
- [approval, borrowPeriodInSeconds, currentFlowRate],
72
+ [
73
+ borrowPeriodInSeconds,
74
+ currentFlowRate,
75
+ superTokenDecimals,
76
+ underlyingTokenDecimals,
77
+ ],
72
78
  )
73
79
 
74
80
  const handleChangeAmount = useCallback(
@@ -114,7 +120,7 @@ export function ChooseAmount({
114
120
  <ChooseAmountModal
115
121
  title='Choose Amount'
116
122
  description1='Access up to 100% of your stream flowrate'
117
- sliderMax={Number(approval.terms.creditLimitFormatted)}
123
+ sliderMax={borrowMaxAmount}
118
124
  currentAmount={currentAmount}
119
125
  tokenSymbol={approval.token.symbol}
120
126
  topLeft='Fees'
@@ -1,5 +1,4 @@
1
- import { CHAINS, isEmpty } from '@huma-finance/shared'
2
- import { useWeb3React } from '@web3-react/core'
1
+ import { isEmpty } from '@huma-finance/shared'
3
2
  import React, { useCallback } from 'react'
4
3
 
5
4
  import { useAppDispatch, useAppSelector } from '../../../hooks/useRedux'
@@ -10,15 +9,11 @@ import { ConfirmTransferModal } from '../../ConfirmTransferModal'
10
9
 
11
10
  export function ConfirmTransfer(): React.ReactElement | null {
12
11
  const dispatch = useAppDispatch()
13
- const { chainId } = useWeb3React()
14
12
  const { borrowAmount, approval } = useAppSelector(selectWidgetState)
15
13
 
16
14
  const handleAction = useCallback(() => {
17
- const step = CHAINS[chainId!].isTestnet
18
- ? WIDGET_STEP.ApproveAllowance
19
- : WIDGET_STEP.Permit
20
- dispatch(setStep(step))
21
- }, [chainId, dispatch])
15
+ dispatch(setStep(WIDGET_STEP.ApproveAllowance))
16
+ }, [dispatch])
22
17
 
23
18
  if (isEmpty(borrowAmount) || isEmpty(approval)) {
24
19
  return null
@@ -1,12 +1,9 @@
1
1
  import {
2
- CHAINS,
3
- Erc2612,
4
2
  PoolInfoType,
5
3
  SuperfluidPoolProcessor,
4
+ SuperfluidPoolProcessor_ABI,
6
5
  useERC2612Permit,
7
6
  useMount,
8
- ERC2612_ABI,
9
- SuperfluidPoolProcessor_ABI,
10
7
  } from '@huma-finance/shared'
11
8
  import { BigNumber, Contract, ethers } from 'ethers'
12
9
  import React from 'react'
@@ -25,7 +22,6 @@ import { LoadingModal } from '../../LoadingModal'
25
22
  type Props = {
26
23
  poolInfo: PoolInfoType
27
24
  payerAddress: string
28
- chainId: number
29
25
  borrower: string
30
26
  superToken: string
31
27
  }
@@ -33,36 +29,15 @@ type Props = {
33
29
  export function Permit({
34
30
  poolInfo,
35
31
  payerAddress,
36
- chainId,
37
32
  borrower,
38
33
  superToken,
39
34
  }: Props): React.ReactElement | null {
40
35
  const dispatch = useDispatch()
41
- const { address: tokenAddress } = poolInfo.poolUnderlyingToken
42
36
  const { borrowAmountBN: borrowAmountBNJson, stream } =
43
37
  useAppSelector(selectWidgetState)
44
- const { getERC2612PermitMessage, getTradableStreamPermitMessage } =
45
- useERC2612Permit()
38
+ const { getTradableStreamPermitMessage } = useERC2612Permit()
46
39
  const borrowAmountBN = BigNumber.from(borrowAmountBNJson)
47
40
 
48
- const getApproveAllowanceCallData = async () => {
49
- const erc2612PermitResult = await getERC2612PermitMessage(
50
- tokenAddress,
51
- poolInfo.poolProcessor!,
52
- borrowAmountBN.toString()!,
53
- )
54
- const tokenContract = new Contract(tokenAddress, ERC2612_ABI) as Erc2612
55
- return tokenContract.interface.encodeFunctionData('permit', [
56
- poolInfo.poolProcessor!,
57
- borrowAmountBN.toString()!,
58
- erc2612PermitResult.nonce,
59
- erc2612PermitResult.deadline,
60
- erc2612PermitResult.v,
61
- erc2612PermitResult.r,
62
- erc2612PermitResult.s,
63
- ])
64
- }
65
-
66
41
  const getDrawdownCallData = async () => {
67
42
  const NFTPermitResult = await getTradableStreamPermitMessage(
68
43
  poolInfo.assetAddress!,
@@ -113,14 +88,6 @@ export function Permit({
113
88
  try {
114
89
  const callData = []
115
90
  const addresses: string[] = []
116
-
117
- // The token in Goerli hasn't integrated ERC-2612
118
- if (!CHAINS[chainId].isTestnet) {
119
- const approveAllowanceCallData = await getApproveAllowanceCallData()
120
- addresses.push(tokenAddress)
121
- callData.push(approveAllowanceCallData)
122
- }
123
-
124
91
  const drawdownCalldata = await getDrawdownCallData()
125
92
  addresses.push(poolInfo.poolProcessor!)
126
93
  callData.push(drawdownCalldata)
@@ -39,7 +39,7 @@ export function StreamFactoringBorrow({
39
39
  handleSuccess,
40
40
  }: StreamFactoringBorrowProps): React.ReactElement | null {
41
41
  const dispatch = useDispatch()
42
- const { account, chainId } = useWeb3React()
42
+ const { account } = useWeb3React()
43
43
  const poolInfo = usePoolInfo(poolName, poolType)
44
44
  const { step, errorMessage } = useAppSelector(selectWidgetState)
45
45
 
@@ -71,11 +71,10 @@ export function StreamFactoringBorrow({
71
71
  {step === WIDGET_STEP.ApproveAllowance && (
72
72
  <ApproveAllowance poolInfo={poolInfo} />
73
73
  )}
74
- {step === WIDGET_STEP.Permit && account && chainId && (
74
+ {step === WIDGET_STEP.Permit && account && (
75
75
  <Permit
76
76
  poolInfo={poolInfo}
77
77
  payerAddress={payerAddress}
78
- chainId={chainId}
79
78
  borrower={account}
80
79
  superToken={superToken}
81
80
  />
@@ -50,9 +50,9 @@ export function SuperfluidFactoredList({
50
50
  const setItem = useCallback(
51
51
  () =>
52
52
  Promise.resolve({
53
- name: poolInfo?.extra?.superTokens?.[0].symbol ?? '',
53
+ name: poolInfo?.extra?.superToken?.symbol ?? '',
54
54
  }),
55
- [poolInfo?.extra?.superTokens],
55
+ [poolInfo?.extra?.superToken],
56
56
  )
57
57
 
58
58
  return (
@@ -106,7 +106,9 @@ export function SuperfluidUpcomingList({
106
106
  const { data } = useQuery<{ streams: Stream[] }>(GET_RECEIVED_STREAMS, {
107
107
  variables: {
108
108
  receiver: account ? account.toLowerCase() : undefined,
109
- tokenIds: poolInfo?.extra?.superTokens?.map((item) => item.id) ?? [],
109
+ tokenIds: poolInfo?.extra?.superToken
110
+ ? [poolInfo.extra.superToken.id]
111
+ : [],
110
112
  },
111
113
  })
112
114
 
package/src/index.tsx CHANGED
@@ -10,6 +10,7 @@ import { Provider as Eip1193Provider } from '@web3-react/types'
10
10
  import { Provider as AtomProvider } from 'jotai'
11
11
  import { Provider as ReduxProvider } from 'react-redux'
12
12
 
13
+ import { useEffect, useState } from 'react'
13
14
  import { ChainSupportProvider } from './components/ChainSupportProvider'
14
15
  import {
15
16
  CreditLineApprove,
@@ -63,11 +64,17 @@ type WidgetProps = {
63
64
 
64
65
  function Widget(props: WCProps<WidgetProps>) {
65
66
  const { children, provider } = props
67
+ const [chainId, setChainId] = useState<number | undefined>(undefined)
66
68
 
67
- let chainId
68
- if (provider instanceof JsonRpcProvider) {
69
- chainId = provider.network.chainId
70
- }
69
+ useEffect(() => {
70
+ const getChainId = async () => {
71
+ if (provider instanceof JsonRpcProvider) {
72
+ const network = await provider.getNetwork()
73
+ setChainId(network?.chainId)
74
+ }
75
+ }
76
+ getChainId()
77
+ }, [provider])
71
78
 
72
79
  return (
73
80
  <ThemeProvider theme={themeHuma}>