import { useState } from 'react';
import { isValidPrivacyAddress } from '@testinprod-io/privacy-boost';
function TransferForm() {
const [recipient, setRecipient] = useState('');
const [amount, setAmount] = useState('');
const [loading, setLoading] = useState(false);
const [error, setError] = useState('');
const handleTransfer = async () => {
setError('');
// Validate recipient
if (!isValidPrivacyAddress(recipient)) {
setError('Invalid privacy address');
return;
}
setLoading(true);
try {
const parsedAmount = await sdk.vault.parseAmount(tokenAddress, amount);
await sdk.vault.send({
to: recipient,
tokenAddress,
amount: parsedAmount,
});
alert('Transfer successful!');
setRecipient('');
setAmount('');
} catch (err) {
setError(err.message);
} finally {
setLoading(false);
}
};
return (
<div>
<input
type="text"
value={recipient}
onChange={(e) => setRecipient(e.target.value)}
placeholder="Recipient privacy address"
disabled={loading}
/>
<input
type="text"
value={amount}
onChange={(e) => setAmount(e.target.value)}
placeholder="Amount"
disabled={loading}
/>
{error && <p style={{ color: 'red' }}>{error}</p>}
<button onClick={handleTransfer} disabled={loading}>
{loading ? 'Sending...' : 'Send'}
</button>
</div>
);
}