@huma-finance/widgets 0.0.11 → 0.0.12

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.11",
3
+ "version": "0.0.12",
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.11",
34
- "@huma-finance/shared": "^0.0.11",
33
+ "@huma-finance/sdk": "^0.0.12",
34
+ "@huma-finance/shared": "^0.0.12",
35
35
  "@mui/icons-material": "^5.3.0",
36
36
  "@mui/material": "^5.0.6",
37
37
  "@mui/styles": "^5.0.2",
@@ -191,5 +191,5 @@
191
191
  "optionalDependencies": {
192
192
  "encoding": "^0.1.13"
193
193
  },
194
- "gitHead": "12afb4430fa58ad8403bde9aadb52c7ddb1f048c"
194
+ "gitHead": "475917ee813766459198232d0cdf8b6e671b9e7d"
195
195
  }
@@ -10,7 +10,7 @@ import {
10
10
  useTheme,
11
11
  } from '@mui/material'
12
12
  import { useMQ } from '@huma-finance/shared'
13
- import React from 'react'
13
+ import React, { useState } from 'react'
14
14
 
15
15
  import { IconType } from '../utilTypes'
16
16
  import { WrapperModal } from './WrapperModal'
@@ -56,6 +56,7 @@ export function ChooseAmountModal({
56
56
  }: Props): React.ReactElement {
57
57
  const theme = useTheme()
58
58
  const { isXsSize } = useMQ()
59
+ const [inputTouched, setInputTouched] = useState(false)
59
60
 
60
61
  const styles = {
61
62
  subTitle: css`
@@ -187,6 +188,7 @@ export function ChooseAmountModal({
187
188
  const handleInputChange = (
188
189
  event: React.ChangeEvent<HTMLTextAreaElement | HTMLInputElement>,
189
190
  ) => {
191
+ setInputTouched(true)
190
192
  // @ts-ignore
191
193
  handleChangeAmount(event.target.value)
192
194
  }
@@ -254,7 +256,7 @@ export function ChooseAmountModal({
254
256
  </Box>
255
257
  </Box>
256
258
  <Slider
257
- max={sliderMax}
259
+ max={Number(sliderMax)}
258
260
  min={0}
259
261
  css={styles.slider}
260
262
  aria-label='Amount'
@@ -269,8 +271,9 @@ export function ChooseAmountModal({
269
271
  <Input
270
272
  css={styles.inputField}
271
273
  type='number'
272
- value={currentAmount}
274
+ value={!inputTouched && currentAmount === 0 ? '' : currentAmount}
273
275
  onChange={handleInputChange}
276
+ placeholder='0'
274
277
  />
275
278
  <Button
276
279
  variant='contained'
@@ -301,7 +304,11 @@ export function ChooseAmountModal({
301
304
  variant='contained'
302
305
  css={styles.okButton}
303
306
  onClick={handleAction}
304
- disabled={currentAmount === 0 || currentAmount > sliderMax}
307
+ disabled={
308
+ !currentAmount ||
309
+ Number(currentAmount) <= 0 ||
310
+ Number(currentAmount) > Number(sliderMax)
311
+ }
305
312
  >
306
313
  {actionText.toUpperCase()}
307
314
  </Button>
@@ -1,10 +1,8 @@
1
- import { useWeb3React } from '@web3-react/core'
2
1
  import {
3
2
  AccountStats,
4
3
  PoolInfoType,
5
4
  upScale,
6
5
  useCLFeeManager,
7
- useCLPoolAllowance,
8
6
  } from '@huma-finance/shared'
9
7
  import React, { useCallback, useState } from 'react'
10
8
 
@@ -23,10 +21,8 @@ export function ChooseAmount({
23
21
  accountStats,
24
22
  }: Props): React.ReactElement | null {
25
23
  const dispatch = useAppDispatch()
26
- const { account } = useWeb3React()
27
24
  const { poolUnderlyingToken } = poolInfo
28
25
  const { symbol, decimals } = poolUnderlyingToken
29
- const { approved } = useCLPoolAllowance(poolInfo.poolName, account)
30
26
  const { getFeesCharged } = useCLFeeManager(poolInfo.poolName)
31
27
  const [chargedFees, setChargedFees] = useState(0)
32
28
  const [currentAmount, setCurrentAmount] = useState(0)
@@ -42,9 +38,7 @@ export function ChooseAmount({
42
38
  )
43
39
 
44
40
  const handleAction = useCallback(() => {
45
- const nextStep = approved
46
- ? WIDGET_STEP.Transfer
47
- : WIDGET_STEP.ApproveAllowance
41
+ const nextStep = WIDGET_STEP.Transfer
48
42
  dispatch(
49
43
  setBorrowInfo({
50
44
  borrowAmount: currentAmount,
@@ -53,7 +47,7 @@ export function ChooseAmount({
53
47
  nextStep,
54
48
  }),
55
49
  )
56
- }, [approved, chargedFees, currentAmount, decimals, dispatch])
50
+ }, [chargedFees, currentAmount, decimals, dispatch])
57
51
 
58
52
  return (
59
53
  <ChooseAmountModal
@@ -1,6 +1,13 @@
1
- import { PoolInfoType } from '@huma-finance/shared'
1
+ import {
2
+ PoolInfoType,
3
+ toBigNumber,
4
+ txAtom,
5
+ useCLPoolAllowance,
6
+ } from '@huma-finance/shared'
2
7
  import React, { useCallback } from 'react'
3
8
 
9
+ import { useResetAtom } from 'jotai/utils'
10
+ import { useWeb3React } from '@web3-react/core'
4
11
  import { useAppDispatch, useAppSelector } from '../../../hooks/useRedux'
5
12
  import { setStep } from '../../../store/widgets.reducers'
6
13
  import { selectWidgetState } from '../../../store/widgets.selectors'
@@ -13,11 +20,18 @@ type Props = {
13
20
 
14
21
  export function Transfer({ poolInfo }: Props): React.ReactElement {
15
22
  const dispatch = useAppDispatch()
23
+ const reset = useResetAtom(txAtom)
24
+ const { account } = useWeb3React()
16
25
  const { borrowAmountBN } = useAppSelector(selectWidgetState)
26
+ const { allowance } = useCLPoolAllowance(poolInfo.poolName, account)
17
27
 
18
28
  const handleSuccess = useCallback(() => {
19
- dispatch(setStep(WIDGET_STEP.Done))
20
- }, [dispatch])
29
+ reset()
30
+ const nextStep = allowance.gte(toBigNumber(borrowAmountBN!))
31
+ ? WIDGET_STEP.Done
32
+ : WIDGET_STEP.ApproveAllowance
33
+ dispatch(setStep(nextStep))
34
+ }, [allowance, borrowAmountBN, dispatch, reset])
21
35
 
22
36
  return (
23
37
  <TxSendModal
@@ -16,7 +16,7 @@ export function ApproveAllowance({
16
16
  const dispatch = useAppDispatch()
17
17
 
18
18
  const handleSuccess = useCallback(() => {
19
- dispatch(setStep(WIDGET_STEP.Transfer))
19
+ dispatch(setStep(WIDGET_STEP.Done))
20
20
  }, [dispatch])
21
21
 
22
22
  return (
@@ -18,8 +18,8 @@ import { WidgetWrapper } from '../../WidgetWrapper'
18
18
  import { Evaluation } from './1-Evaluation'
19
19
  import { AllowList } from './2-AllowList'
20
20
  import { ChooseAmount } from './3-ChooseAmount'
21
- import { ApproveAllowance } from './4-ApproveAllowance'
22
- import { Transfer } from './5-Transfer'
21
+ import { ApproveAllowance } from './5-ApproveAllowance'
22
+ import { Transfer } from './4-Transfer'
23
23
  import { Success } from './6-Success'
24
24
 
25
25
  /**
@@ -24,17 +24,10 @@ function Fixture() {
24
24
  const [chainId, setChainId] = useState(0)
25
25
  const [signer, setSigner] = useState<Web3Provider | null>(null)
26
26
 
27
- if (TEST_PRIVATE_KEY === 'REPLACE_WITH_YOUR_PRIVATE_KEY') {
28
- console.error('Please replace TEST_PRIVATE_KEY with your own private key')
29
- }
30
- if (INFURA_PROVIDER_KEY === 'REPLACE_WITH_YOUR_INFURA_PROVIDER_KEY') {
31
- console.error('Please replace INFURA_PROVIDER_KEY with your own key')
32
- }
33
-
34
27
  // useCreditRecordDetails
35
28
  const [userAddress, setUserAddress] = useState('')
36
- const [poolName, setPoolName] = useState<POOL_NAME>(POOL_NAME.RequestNetwork)
37
- const [poolType, setPoolType] = useState<POOL_TYPE>(POOL_TYPE.Invoice)
29
+ const [poolName, setPoolName] = useState<POOL_NAME>(POOL_NAME.HumaCreditLine)
30
+ const [poolType, setPoolType] = useState<POOL_TYPE>(POOL_TYPE.CreditLine)
38
31
  const provider = new JsonRpcProvider(
39
32
  `https://goerli.infura.io/v3/${INFURA_PROVIDER_KEY}`,
40
33
  {
@@ -51,15 +44,15 @@ function Fixture() {
51
44
  const [maturityDate, setMaturityDate] = useState(Date.now())
52
45
  const [metadata, setMetadata] = useState(JSON.stringify({ exampleKey: 1 }))
53
46
  // Used to set an example tag on the metadata uploaded to ARweave which we can use to query by later
54
- const [internalID, setInternalID] = useState('3')
47
+ const [referenceId, setReferenceId] = useState('3')
55
48
  const [mintResult, setMintResult] = useState<any>('')
56
49
 
57
50
  // usePoolContract
58
51
  const poolContract = usePoolContract(
59
52
  signer!,
60
53
  chainId,
61
- POOL_NAME.RequestNetwork,
62
- POOL_TYPE.Invoice,
54
+ POOL_NAME.HumaCreditLine,
55
+ POOL_TYPE.CreditLine,
63
56
  )
64
57
 
65
58
  // EAService.approve
@@ -125,7 +118,7 @@ function Fixture() {
125
118
  receivableAmount,
126
119
  maturityDate,
127
120
  JSON.parse(metadata),
128
- internalID,
121
+ referenceId,
129
122
  [],
130
123
  )
131
124
  const txResult = await data.wait()
@@ -145,10 +138,8 @@ function Fixture() {
145
138
  try {
146
139
  const data = await ARWeaveService.queryForMetadata(
147
140
  chainId,
148
- poolName,
149
- poolType,
150
141
  address,
151
- internalID,
142
+ referenceId,
152
143
  )
153
144
  setQueryResult(data)
154
145
  console.log(data)
@@ -177,6 +168,27 @@ function Fixture() {
177
168
  }
178
169
  }
179
170
 
171
+ const [paymentAmount, setPaymentAmount] = useState<number>(0)
172
+ const [paymentResult, setPaymentResult] = useState<string>('')
173
+ const declarePaymentSubmit = async (event: any) => {
174
+ event.preventDefault()
175
+
176
+ try {
177
+ const data =
178
+ await ReceivableService.declareReceivablePaymentByReferenceId(
179
+ signer?.getSigner()!,
180
+ referenceId,
181
+ paymentAmount,
182
+ )
183
+ const txResult = await data.wait()
184
+ setPaymentResult(`Success, Tx Hash: ${txResult.transactionHash}`)
185
+ console.log(data)
186
+ } catch (error) {
187
+ setPaymentResult(`Failed, check console for errors`)
188
+ console.log(error)
189
+ }
190
+ }
191
+
180
192
  return (
181
193
  <div
182
194
  style={{
@@ -427,9 +439,9 @@ function Fixture() {
427
439
  Reference ID (For querying through ARWeave):
428
440
  <input
429
441
  type='text'
430
- value={internalID}
442
+ value={referenceId}
431
443
  onChange={(event: any) => {
432
- setInternalID(event.target.value)
444
+ setReferenceId(event.target.value)
433
445
  }}
434
446
  />
435
447
  </label>
@@ -492,9 +504,9 @@ function Fixture() {
492
504
  Reference ID (For querying through ARWeave):
493
505
  <input
494
506
  type='text'
495
- value={internalID}
507
+ value={referenceId}
496
508
  onChange={(event: any) => {
497
- setInternalID(event.target.value)
509
+ setReferenceId(event.target.value)
498
510
  }}
499
511
  />
500
512
  </label>
@@ -519,7 +531,53 @@ function Fixture() {
519
531
  borderRadius: '10px',
520
532
  }}
521
533
  >
522
- <h3>Example 5. Prefund ARWeave</h3>
534
+ <h3>Example 5. Declare payment for Receivable by reference ID</h3>
535
+ <form
536
+ onSubmit={declarePaymentSubmit}
537
+ style={{ display: 'flex', flexDirection: 'column', gap: '5px' }}
538
+ >
539
+ <label>
540
+ Transaction sender:
541
+ <input type='text' value={address} disabled />
542
+ </label>
543
+ <br />
544
+ <label>
545
+ Reference ID (For querying through ARWeave):
546
+ <input
547
+ type='text'
548
+ value={referenceId}
549
+ onChange={(event: any) => {
550
+ setReferenceId(event.target.value)
551
+ }}
552
+ />
553
+ </label>
554
+ <br />
555
+ <label>
556
+ Payment Amount:
557
+ <input
558
+ type='text'
559
+ value={paymentAmount}
560
+ onChange={(event: any) => {
561
+ setPaymentAmount(event.target.value)
562
+ }}
563
+ />
564
+ </label>
565
+ <br />
566
+ <button type='submit' style={{ width: 'fit-content' }}>
567
+ Declare Payment
568
+ </button>
569
+ <br />
570
+ {paymentResult}
571
+ </form>
572
+ </div>
573
+ <div
574
+ style={{
575
+ border: '1px solid gray',
576
+ padding: '0px 10px 20px 10px',
577
+ borderRadius: '10px',
578
+ }}
579
+ >
580
+ <h3>Example 6. Prefund ARWeave</h3>
523
581
  <form
524
582
  onSubmit={prefundARWeaveSubmit}
525
583
  style={{ display: 'flex', flexDirection: 'column', gap: '5px' }}