import { useBuyCollateral } from 'compound-react';
function BuyCollateralComponent() {
const { buyCollateral, loading, error, txHash } = useBuyCollateral();
const handleBuyCollateral = async () => {
// Buy WETH collateral with 1000 USDC
const collateralAsset = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'; // WETH
const minAmount = ethers.utils.parseUnits('0.5', 18); // Minimum 0.5 WETH to receive
const baseAmount = ethers.utils.parseUnits('1000', 6); // 1000 USDC to spend
const recipient = '0xYourAddress...'; // Address to receive the collateral
const tx = await buyCollateral(collateralAsset, minAmount, baseAmount, recipient);
console.log('Buy collateral transaction:', tx);
};
return (
<div>
<button onClick={handleBuyCollateral} disabled={loading}>
{loading ? 'Buying...' : 'Buy WETH Collateral'}
</button>
{error && <p>Error: {error.message}</p>}
{txHash && <p>Transaction: {txHash}</p>}
</div>
);
}