const { ethers } = require("hardhat"); async function main() { console.log("๐Ÿ”ง Testing clearStakes for Real User"); console.log("==================================="); const targetUser = "0x41970Ce76b656030A79E7C1FA76FC4EB93980255"; console.log(`๐ŸŽฏ Target user: ${targetUser}`); // Get deployer account const [deployer] = await ethers.getSigners(); console.log(`๐Ÿ“ Deployer: ${deployer.address}`); // Connect to the existing BSC PACA contract const pacaAddress = process.env.PROXY_ADDRESS_BSC || "0x7b00A99882cF35Fa084dEBf797968B0ddEc9F957"; console.log(`๐Ÿ”— Using PACA contract at: ${pacaAddress}`); // Get contract factory and connect to existing contract const PacaFactory = await ethers.getContractFactory("PacaFinanceWithBoostAndScheduleBsc"); const paca = PacaFactory.attach(pacaAddress); // Deploy BotManager console.log("\n๐Ÿค– Deploying PacaBotManager..."); const BotManagerFactory = await ethers.getContractFactory("PacaBotManager"); const botManager = await BotManagerFactory.deploy(); await botManager.waitForDeployment(); const botManagerAddress = await botManager.getAddress(); console.log(`โœ… BotManager deployed: ${botManagerAddress}`); // Add BotManager as authorized bot (using deployer as owner) console.log("\n๐Ÿ”— Adding BotManager as authorized bot..."); try { const addBotTx = await paca.addBot(botManagerAddress); await addBotTx.wait(); console.log("โœ… BotManager authorized as bot"); } catch (error) { console.log(`โš ๏ธ Could not add bot directly: ${error.message}`); console.log(" This might be expected if deployer is not the owner"); // Try with the target user as owner (they might be the contract owner) console.log(" Trying with target user as owner..."); await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [targetUser], }); await hre.network.provider.send("hardhat_setBalance", [ targetUser, "0x56BC75E2D630E0000", // 100 ETH ]); const userSigner = await ethers.getSigner(targetUser); const addBotTx2 = await paca.connect(userSigner).addBot(botManagerAddress); await addBotTx2.wait(); console.log("โœ… BotManager authorized as bot (via user account)"); await hre.network.provider.request({ method: "hardhat_stopImpersonatingAccount", params: [targetUser], }); } // Check if bot is authorized const isBotAuthorized = await paca.authorizedBots(botManagerAddress); console.log(`๐Ÿ” Bot authorization verified: ${isBotAuthorized}`); // Get stakes BEFORE clearing console.log(`\n๐Ÿ“Š Getting stakes for ${targetUser} BEFORE clearing:`); console.log("=" * 60); let stakesBefore; try { stakesBefore = await paca.getStakes(targetUser); console.log(`๐Ÿ“ˆ User has ${stakesBefore.length} stakes BEFORE clearing`); if (stakesBefore.length > 0) { let totalStakedBefore = 0n; let activeStakes = 0; for (let i = 0; i < stakesBefore.length; i++) { const stake = stakesBefore[i]; const isActive = !stake.complete && stake.amount > 0; if (isActive) activeStakes++; console.log(` Stake ${i + 1}:`); console.log(` Amount: ${ethers.formatEther(stake.amount)} ETH`); console.log(` Complete: ${stake.complete}`); console.log(` Active: ${isActive ? "YES" : "NO"}`); console.log(""); totalStakedBefore += stake.amount; } console.log(`๐Ÿ’Ž BEFORE - Total staked: ${ethers.formatEther(totalStakedBefore)} ETH`); console.log(`๐Ÿ”ฅ BEFORE - Active stakes: ${activeStakes} out of ${stakesBefore.length}`); } else { console.log("๐Ÿ“ญ No stakes found for this user"); console.log("โš ๏ธ This might mean:"); console.log(" 1. User has no stakes"); console.log(" 2. We're not connected to the right network/contract"); console.log(" 3. Contract doesn't have a getStakes function"); } } catch (error) { console.log(`โŒ Error getting stakes: ${error.message}`); stakesBefore = []; } // Execute clearStakes via BotManager console.log(`\n๐Ÿš€ Executing clearStakes via BotManager...`); try { const clearTx = await botManager.clearStakes(pacaAddress, targetUser); const receipt = await clearTx.wait(); console.log(`โœ… clearStakes executed successfully!`); console.log(`โ›ฝ Gas used: ${receipt.gasUsed.toString()}`); console.log(`๐Ÿงพ Transaction hash: ${receipt.hash}`); } catch (error) { console.log(`โŒ Failed to execute clearStakes: ${error.message}`); if (error.data) { console.log(`๐Ÿ” Error data: ${error.data}`); } return; } // Get stakes AFTER clearing console.log(`\n๐Ÿ“Š Getting stakes for ${targetUser} AFTER clearing:`); console.log("=" * 60); try { const stakesAfter = await paca.getStakes(targetUser); console.log(`๐Ÿ“‰ User has ${stakesAfter.length} stakes AFTER clearing`); if (stakesAfter.length > 0) { let totalStakedAfter = 0n; let activeStakes = 0; let clearedStakes = 0; for (let i = 0; i < stakesAfter.length; i++) { const stake = stakesAfter[i]; const isActive = !stake.complete && stake.amount > 0; const wasCleared = stake.complete && stake.amount === 0n; if (isActive) activeStakes++; if (wasCleared) clearedStakes++; console.log(` Stake ${i + 1}:`); console.log(` Amount: ${ethers.formatEther(stake.amount)} ETH`); console.log(` Complete: ${stake.complete}`); console.log(` Status: ${wasCleared ? "CLEARED โœ…" : isActive ? "ACTIVE โšก" : "UNKNOWN โ“"}`); console.log(""); totalStakedAfter += stake.amount; } console.log(`๐Ÿ’Ž AFTER - Total staked: ${ethers.formatEther(totalStakedAfter)} ETH`); console.log(`๐Ÿ”ฅ AFTER - Active stakes: ${activeStakes} out of ${stakesAfter.length}`); console.log(`โœ… AFTER - Cleared stakes: ${clearedStakes} out of ${stakesAfter.length}`); // Summary console.log(`\n๐ŸŽฏ CLEARING RESULTS:`); if (totalStakedAfter === 0n && clearedStakes === stakesAfter.length) { console.log(`๐ŸŽ‰ SUCCESS! All stakes cleared completely!`); } else if (totalStakedAfter === 0n) { console.log(`โœ… SUCCESS! All stake amounts zeroed!`); } else if (clearedStakes > 0) { console.log(`โšก PARTIAL! ${clearedStakes} stakes cleared, ${activeStakes} still active`); } else { console.log(`โŒ NO CHANGE! Stakes were not cleared`); } } else { console.log("๐Ÿ“ญ No stakes found after clearing"); } } catch (error) { console.log(`โŒ Error getting stakes after clearing: ${error.message}`); } console.log("\n๐Ÿ“‹ Test Summary"); console.log("==============="); console.log(`๐ŸŽฏ Target User: ${targetUser}`); console.log(`๐Ÿ”ง PACA Contract: ${pacaAddress}`); console.log(`๐Ÿค– BotManager: ${botManagerAddress}`); console.log(`โœ… Bot Authorized: ${isBotAuthorized}`); console.log(`๐Ÿ“Š Stakes Before: ${stakesBefore?.length || 0}`); console.log("๐ŸŽ‰ Test completed!"); } main() .then(() => process.exit(0)) .catch((error) => { console.error("๐Ÿ’ฅ Test failed:", error); process.exit(1); });