|
| 1 | +import { bigDecimal, evmAddress, reserveId, useSupply } from '@aave/react'; |
| 2 | +import { useState } from 'react'; |
| 3 | +import type { WalletClient } from 'viem'; |
| 4 | +import { useExecutionPlan } from './useExecutionPlan'; |
| 5 | + |
| 6 | +const RESERVE_ID = reserveId( |
| 7 | + 'MTIzNDU2Nzg5OjoweEJhOTdjNUU1MmNkNUJDM0Q3OTUwQWU3MDc3OUY4RmZFOTJkNDBDZEM6OjY=', |
| 8 | +); |
| 9 | + |
| 10 | +type SupplyFormProps = { |
| 11 | + walletClient: WalletClient; |
| 12 | +}; |
| 13 | + |
| 14 | +export function SupplyForm({ walletClient }: SupplyFormProps) { |
| 15 | + const [status, setStatus] = useState<string>(''); |
| 16 | + |
| 17 | + const handler = useExecutionPlan(walletClient); |
| 18 | + const [supply, { loading, error }] = useSupply(handler); |
| 19 | + |
| 20 | + const submit = async (e: React.FormEvent<HTMLFormElement>) => { |
| 21 | + e.preventDefault(); |
| 22 | + |
| 23 | + const amount = e.currentTarget.amount.value as string; |
| 24 | + if (!amount) { |
| 25 | + setStatus('Please enter an amount'); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + const result = await supply({ |
| 30 | + reserve: RESERVE_ID, |
| 31 | + amount: { |
| 32 | + erc20: { |
| 33 | + value: bigDecimal(amount), |
| 34 | + }, |
| 35 | + }, |
| 36 | + sender: evmAddress(walletClient.account!.address), |
| 37 | + }); |
| 38 | + |
| 39 | + if (result.isOk()) { |
| 40 | + setStatus('Supply successful!'); |
| 41 | + } else { |
| 42 | + setStatus('Supply failed!'); |
| 43 | + } |
| 44 | + }; |
| 45 | + |
| 46 | + return ( |
| 47 | + <form onSubmit={submit}> |
| 48 | + <label |
| 49 | + style={{ |
| 50 | + marginBottom: '5px', |
| 51 | + }} |
| 52 | + > |
| 53 | + <strong style={{ display: 'block' }}>Amount:</strong> |
| 54 | + <input |
| 55 | + name='amount' |
| 56 | + type='number' |
| 57 | + step='0.000000000000000001' |
| 58 | + defaultValue='1' |
| 59 | + disabled={loading} |
| 60 | + style={{ width: '100%', padding: '8px' }} |
| 61 | + placeholder='Amount to supply (in token units)' |
| 62 | + /> |
| 63 | + <small style={{ color: '#666' }}> |
| 64 | + Human-friendly amount (e.g. 1.23, 4.56, 7.89) |
| 65 | + </small> |
| 66 | + </label> |
| 67 | + |
| 68 | + <button type='submit' disabled={loading}> |
| 69 | + Supply |
| 70 | + </button> |
| 71 | + |
| 72 | + {status && <p style={{ marginBottom: '10px' }}>{status}</p>} |
| 73 | + |
| 74 | + {error && <p style={{ color: '#f44336' }}>Error: {error.toString()}</p>} |
| 75 | + </form> |
| 76 | + ); |
| 77 | +} |
0 commit comments