|
| 1 | +import { |
| 2 | + bigDecimal, |
| 3 | + errAsync, |
| 4 | + evmAddress, |
| 5 | + type Reserve, |
| 6 | + useSupply, |
| 7 | +} from '@aave/react'; |
| 8 | +import { useSendTransaction } from '@aave/react/viem'; |
| 9 | +import { useState } from 'react'; |
| 10 | +import type { WalletClient } from 'viem'; |
| 11 | + |
| 12 | +interface SupplyFormProps { |
| 13 | + reserve: Reserve; |
| 14 | + walletClient: WalletClient; |
| 15 | +} |
| 16 | + |
| 17 | +export function SupplyForm({ reserve, walletClient }: SupplyFormProps) { |
| 18 | + const [amount, setAmount] = useState<string>(''); |
| 19 | + const [status, setStatus] = useState<string>(''); |
| 20 | + |
| 21 | + const [supply, supplying] = useSupply(); |
| 22 | + const [sendTransaction, sending] = useSendTransaction(walletClient); |
| 23 | + |
| 24 | + const loading = supplying.loading || sending.loading; |
| 25 | + const error = supplying.error || sending.error; |
| 26 | + |
| 27 | + const handleSupply = async () => { |
| 28 | + if (!amount) { |
| 29 | + setStatus('Please enter an amount'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const result = await supply({ |
| 34 | + chainId: reserve.market.chain.chainId, |
| 35 | + market: reserve.market.address, |
| 36 | + amount: { |
| 37 | + erc20: { |
| 38 | + currency: reserve.underlyingToken.address, |
| 39 | + value: bigDecimal(amount), |
| 40 | + }, |
| 41 | + }, |
| 42 | + supplier: evmAddress(walletClient.account!.address), |
| 43 | + }).andThen((plan) => { |
| 44 | + switch (plan.__typename) { |
| 45 | + case 'TransactionRequest': |
| 46 | + setStatus('Sending transaction...'); |
| 47 | + return sendTransaction(plan); |
| 48 | + |
| 49 | + case 'ApprovalRequired': |
| 50 | + setStatus('Approval required. Sending approval transaction...'); |
| 51 | + |
| 52 | + return sendTransaction(plan.approval).andThen(() => { |
| 53 | + setStatus('Approval sent. Now sending supply transaction...'); |
| 54 | + |
| 55 | + return sendTransaction(plan.originalTransaction); |
| 56 | + }); |
| 57 | + |
| 58 | + case 'InsufficientBalanceError': |
| 59 | + setStatus( |
| 60 | + `Insufficient balance: ${plan.available.value} ${reserve.underlyingToken.symbol}`, |
| 61 | + ); |
| 62 | + return errAsync( |
| 63 | + new Error(`Insufficient balance: ${plan.required.value}`), |
| 64 | + ); |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + if (result.isOk()) { |
| 69 | + setStatus('Supply successful!'); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + <label |
| 76 | + style={{ |
| 77 | + marginBottom: '5px', |
| 78 | + }} |
| 79 | + > |
| 80 | + <strong style={{ display: 'block' }}>Amount:</strong> |
| 81 | + <input |
| 82 | + disabled={loading} |
| 83 | + type='number' |
| 84 | + onChange={(e) => setAmount(e.target.value)} |
| 85 | + style={{ width: '100%', padding: '8px' }} |
| 86 | + placeholder='Amount to supply (in token units)' |
| 87 | + /> |
| 88 | + <small style={{ color: '#666' }}> |
| 89 | + Human-friendly amount (e.g. 1.23, 4.56, 7.89) |
| 90 | + </small> |
| 91 | + </label> |
| 92 | + |
| 93 | + <button type='button' disabled={loading} onClick={handleSupply}> |
| 94 | + Supply |
| 95 | + </button> |
| 96 | + |
| 97 | + {status && <p style={{ marginBottom: '10px' }}>{status}</p>} |
| 98 | + |
| 99 | + {error && <p style={{ color: '#f44336' }}>Error: {error.toString()}</p>} |
| 100 | + </div> |
| 101 | + ); |
| 102 | +} |
0 commit comments