import 'dotenv/config'; import { ethers } from 'ethers'; /** * Approve TaskEscrow contract to spend cUSD tokens * This needs to be done once before creating tasks */ async function approveCUSD() { try { console.log('šŸ” Approving TaskEscrow to spend cUSD...\n'); // Initialize provider and signer const provider = new ethers.JsonRpcProvider( process.env.CELO_RPC_URL || 'https://forno.celo-sepolia.celo-testnet.org' ); const privateKey = process.env.PRIVATE_KEY; if (!privateKey) { throw new Error('PRIVATE_KEY not configured in .env'); } const signer = new ethers.Wallet(privateKey, provider); console.log(`šŸ“ Approving from: ${signer.address}`); // Get contract addresses const cUSDAddress = process.env.CUSD_SEPOLIA_ADDRESS; const taskEscrowAddress = process.env.CONTRACT_ADDRESS; if (!cUSDAddress) { throw new Error('CUSD_SEPOLIA_ADDRESS not configured in .env'); } if (!taskEscrowAddress) { throw new Error('CONTRACT_ADDRESS not configured in .env'); } console.log(`šŸ’° cUSD Token: ${cUSDAddress}`); console.log(`šŸ“‹ TaskEscrow Contract: ${taskEscrowAddress}\n`); // Create cUSD contract instance const cUSDContract = new ethers.Contract( cUSDAddress, [ 'function approve(address spender, uint256 amount) returns (bool)', 'function allowance(address owner, address spender) view returns (uint256)', 'function balanceOf(address account) view returns (uint256)', ], signer ); // Check current balance const balance = await cUSDContract.balanceOf(signer.address); console.log(`šŸ’µ Your cUSD Balance: ${ethers.formatEther(balance)} cUSD`); // Check current allowance const currentAllowance = await cUSDContract.allowance(signer.address, taskEscrowAddress); console.log(`šŸ”“ Current Allowance: ${ethers.formatEther(currentAllowance)} cUSD\n`); // Approve a large amount (10 million cUSD) so we don't need to approve again const approvalAmount = ethers.parseEther('10000000'); console.log(`ā³ Approving ${ethers.formatEther(approvalAmount)} cUSD...`); const tx = await cUSDContract.approve(taskEscrowAddress, approvalAmount); console.log(`šŸ“ Transaction sent: ${tx.hash}`); console.log('ā³ Waiting for confirmation...\n'); const receipt = await tx.wait(); console.log(`āœ… Approval confirmed in block ${receipt.blockNumber}\n`); // Verify new allowance const newAllowance = await cUSDContract.allowance(signer.address, taskEscrowAddress); console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━'); console.log('āœ… SUCCESS! TaskEscrow approved to spend cUSD'); console.log('━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n'); console.log(`New Allowance: ${ethers.formatEther(newAllowance)} cUSD`); console.log(`Transaction Hash: ${receipt.hash}\n`); console.log('šŸŽÆ Next Steps:'); console.log('You can now create tasks with blockchain integration! šŸŽ‰\n'); process.exit(0); } catch (error: any) { console.error('\nāŒ Approval failed:', error.message); if (error.data) { console.error('Error data:', error.data); } process.exit(1); } } approveCUSD();