const { ethers } = require("hardhat"); async function main() { console.log("๐Ÿงช BEFORE/AFTER Stakes Clearing Test"); console.log("====================================="); console.log("Network: BSC Mainnet Fork"); const targetUser = "0x41970Ce76b656030A79E7C1FA76FC4EB93980255"; const pacaAddress = process.env.PROXY_ADDRESS_BSC || "0x3fF44D639a4982A4436f6d737430141aBE68b4E1"; console.log(`๐ŸŽฏ Target User: ${targetUser}`); console.log(`๐Ÿ”— PACA Contract: ${pacaAddress}`); // Connect to existing PACA contract on the fork const PacaFactory = await ethers.getContractFactory("PacaFinanceWithBoostAndScheduleBsc"); const paca = PacaFactory.attach(pacaAddress); // Deploy BotManager console.log("\n๐Ÿš€ Deploying BotManager..."); const BotManagerFactory = await ethers.getContractFactory("PacaBotManager"); const botManager = await BotManagerFactory.deploy(); await botManager.waitForDeployment(); const botManagerAddress = await botManager.getAddress(); console.log(`โœ… BotManager deployed: ${botManagerAddress}`); // Impersonate the target user (who is the contract owner) to authorize the bot console.log("\n๐Ÿ”‘ Authorizing BotManager as bot..."); await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [targetUser], }); await hre.network.provider.send("hardhat_setBalance", [ targetUser, "0x56BC75E2D630E0000", // 100 ETH for gas ]); const userSigner = await ethers.getSigner(targetUser); const addBotTx = await paca.connect(userSigner).addBot(botManagerAddress); await addBotTx.wait(); console.log(`โœ… BotManager authorized as bot`); // ======================================== // PART 1: GET STAKES BEFORE CLEARING // ======================================== console.log("\n" + "=".repeat(60)); console.log("๐Ÿ“Š GETTING STAKES **BEFORE** CLEARING"); console.log("=".repeat(60)); const stakesBefore = await paca.getStakes(targetUser); console.log(`\n๐Ÿ“‹ getStakes() returned ${stakesBefore.length} stakes BEFORE clearing:`); let totalAmountBefore = 0n; let activeStakesBefore = 0; stakesBefore.forEach((stake, i) => { const isActive = !stake.complete && stake.amount > 0n; if (isActive) { activeStakesBefore++; totalAmountBefore += stake.amount; } console.log(`\n Stake ${i + 1} BEFORE:`); console.log(` amount: ${ethers.formatEther(stake.amount)} ETH`); console.log(` complete: ${stake.complete}`); console.log(` status: ${isActive ? "๐ŸŸข ACTIVE" : "๐Ÿ”ด COMPLETED"}`); }); console.log(`\n๐Ÿ’Ž SUMMARY BEFORE:`); console.log(` Total Stakes: ${stakesBefore.length}`); console.log(` Active Stakes: ${activeStakesBefore}`); console.log(` Total Active Amount: ${ethers.formatEther(totalAmountBefore)} ETH`); // ======================================== // PART 2: EXECUTE CLEARING // ======================================== console.log("\n" + "=".repeat(60)); console.log("๐Ÿ”ฅ EXECUTING clearStakes() VIA BOTMANAGER"); console.log("=".repeat(60)); console.log("\nโšก Calling botManager.clearStakes()..."); const [deployer] = await ethers.getSigners(); const clearTx = await botManager.connect(deployer).clearStakes(pacaAddress, targetUser); const receipt = await clearTx.wait(); console.log(`โœ… clearStakes() executed successfully!`); console.log(`โ›ฝ Gas used: ${receipt.gasUsed.toString()}`); console.log(`๐Ÿงพ Transaction: ${receipt.hash}`); // ======================================== // PART 3: GET STAKES AFTER CLEARING // ======================================== console.log("\n" + "=".repeat(60)); console.log("๐Ÿ“Š GETTING STAKES **AFTER** CLEARING"); console.log("=".repeat(60)); const stakesAfter = await paca.getStakes(targetUser); console.log(`\n๐Ÿ“‹ getStakes() returned ${stakesAfter.length} stakes AFTER clearing:`); let totalAmountAfter = 0n; let activeStakesAfter = 0; let clearedStakes = 0; stakesAfter.forEach((stake, i) => { const isActive = !stake.complete && stake.amount > 0n; const wasCleared = stake.complete && stake.amount === 0n; if (isActive) { activeStakesAfter++; totalAmountAfter += stake.amount; } if (wasCleared) { clearedStakes++; } console.log(`\n Stake ${i + 1} AFTER:`); console.log(` amount: ${ethers.formatEther(stake.amount)} ETH`); console.log(` complete: ${stake.complete}`); console.log(` status: ${wasCleared ? "โœ… CLEARED" : isActive ? "๐ŸŸข ACTIVE" : "โ“ UNKNOWN"}`); }); console.log(`\n๐Ÿ’Ž SUMMARY AFTER:`); console.log(` Total Stakes: ${stakesAfter.length}`); console.log(` Active Stakes: ${activeStakesAfter}`); console.log(` Cleared Stakes: ${clearedStakes}`); console.log(` Total Active Amount: ${ethers.formatEther(totalAmountAfter)} ETH`); // ======================================== // PART 4: COMPARISON & VERIFICATION // ======================================== console.log("\n" + "=".repeat(60)); console.log("๐ŸŽฏ BEFORE vs AFTER COMPARISON"); console.log("=".repeat(60)); console.log(`\n๐Ÿ“Š STAKES COUNT:`); console.log(` Before: ${stakesBefore.length} total`); console.log(` After: ${stakesAfter.length} total`); console.log(` Change: ${stakesAfter.length === stakesBefore.length ? "โœ… SAME (expected)" : "โŒ DIFFERENT"}`); console.log(`\n๐ŸŸข ACTIVE STAKES:`); console.log(` Before: ${activeStakesBefore} active`); console.log(` After: ${activeStakesAfter} active`); console.log(` Change: ${activeStakesBefore - activeStakesAfter} stakes cleared`); console.log(`\n๐Ÿ’ฐ TOTAL AMOUNT STAKED:`); console.log(` Before: ${ethers.formatEther(totalAmountBefore)} ETH`); console.log(` After: ${ethers.formatEther(totalAmountAfter)} ETH`); console.log(` Cleared: ${ethers.formatEther(totalAmountBefore - totalAmountAfter)} ETH`); // FINAL VERIFICATION console.log(`\n๐Ÿ” VERIFICATION:`); const allAmountsZeroed = stakesAfter.every(stake => stake.amount === 0n); const allMarkedComplete = stakesAfter.every(stake => stake.complete === true); const noActiveStakes = activeStakesAfter === 0; console.log(` โœ… All amounts zeroed: ${allAmountsZeroed}`); console.log(` โœ… All marked complete: ${allMarkedComplete}`); console.log(` โœ… No active stakes remaining: ${noActiveStakes}`); if (allAmountsZeroed && allMarkedComplete && noActiveStakes) { console.log(`\n๐ŸŽ‰ SUCCESS! Stakes cleared completely!`); console.log(` - Cleared ${activeStakesBefore} active stakes`); console.log(` - Zeroed out ${ethers.formatEther(totalAmountBefore)} ETH`); console.log(` - All stakes marked as complete`); } else { console.log(`\nโš ๏ธ PARTIAL SUCCESS or FAILURE:`); console.log(` - Some stakes may not have been cleared properly`); } // Clean up await hre.network.provider.request({ method: "hardhat_stopImpersonatingAccount", params: [targetUser], }); console.log("\n" + "=".repeat(60)); console.log("๐Ÿ TEST COMPLETED"); console.log("=".repeat(60)); } main() .then(() => process.exit(0)) .catch((error) => { console.error("๐Ÿ’ฅ Test failed:", error); process.exit(1); });