// const { expect } = require("chai"); const { ethers } = require("hardhat"); describe("PacaBotManager Integration Test", function() { let paca; let botManager; let owner; let testUser; const PACA_BSC_ADDRESS = "0x3fF44D639a4982A4436f6d737430141aBE68b4E1"; const BOT_MANAGER_ADDRESS = "0x4E5d3cD7743934b61041ba2ac3E9df39a0A26dcC"; const TEST_USER_ADDRESS = "0x41970Ce76b656030A79E7C1FA76FC4EB93980255"; before(async function() { // This test requires forking BSC mainnet console.log("๐Ÿงช Running PacaBotManager Integration Test on BSC Fork"); console.log("===================================================="); [owner] = await ethers.getSigners(); console.log(`๐Ÿ“ Test runner: ${owner.address}`); // Connect to existing contracts on the fork const PacaFactory = await ethers.getContractFactory("PacaFinanceWithBoostAndScheduleBsc"); paca = PacaFactory.attach(PACA_BSC_ADDRESS); const BotManagerFactory = await ethers.getContractFactory("PacaBotManager"); botManager = BotManagerFactory.attach(BOT_MANAGER_ADDRESS); // Impersonate the test user for owner operations if needed await hre.network.provider.request({ method: "hardhat_impersonateAccount", params: [TEST_USER_ADDRESS], }); await hre.network.provider.send("hardhat_setBalance", [ TEST_USER_ADDRESS, "0x56BC75E2D630E0000", // 100 ETH ]); testUser = await ethers.getSigner(TEST_USER_ADDRESS); console.log(`๐Ÿ”— PACA Contract: ${PACA_BSC_ADDRESS}`); console.log(`๐Ÿค– Bot Manager: ${BOT_MANAGER_ADDRESS}`); console.log(`๐ŸŽฏ Test User: ${TEST_USER_ADDRESS}`); }); after(async function() { // Clean up impersonation await hre.network.provider.request({ method: "hardhat_stopImpersonatingAccount", params: [TEST_USER_ADDRESS], }); }); describe("Contract Setup", function() { it("Should connect to PACA contract", async function() { const contractCode = await ethers.provider.getCode(PACA_BSC_ADDRESS); expect(contractCode).to.not.equal("0x"); console.log("โœ… PACA contract exists"); }); it("Should connect to BotManager contract", async function() { const contractCode = await ethers.provider.getCode(BOT_MANAGER_ADDRESS); expect(contractCode).to.not.equal("0x"); console.log("โœ… BotManager contract exists"); }); it("Should get contract owners", async function() { const pacaOwner = await paca.owner(); const botOwner = await botManager.owner(); console.log(`๐Ÿ‘ค PACA Owner: ${pacaOwner}`); console.log(`๐Ÿ‘ค BotManager Owner: ${botOwner}`); expect(pacaOwner).to.be.properAddress; expect(botOwner).to.be.properAddress; }); }); describe("Read Stakes Functionality", function() { it("Should read stakes for test user", async function() { console.log(`\n๐Ÿ“Š Getting stakes for: ${TEST_USER_ADDRESS}`); const stakes = await paca.getStakes(TEST_USER_ADDRESS); console.log(`๐Ÿ“ˆ User has ${stakes.length} stakes`); if (stakes.length > 0) { let totalAmount = 0n; let activeStakes = 0; for (let i = 0; i < stakes.length; i++) { const stake = stakes[i]; const amount = stake.amount; const lastClaimed = stake.lastClaimed; const dailyRewardRate = stake.dailyRewardRate; const unlockTime = stake.unlockTime; const complete = stake.complete; const isActive = !complete && amount > 0n; if (isActive) { activeStakes++; totalAmount += amount; } console.log(`\n ๐Ÿ“Œ Stake ${i + 1}:`); console.log(` Amount: ${ethers.formatEther(amount)} ETH`); console.log(` Daily Reward Rate: ${ethers.formatEther(dailyRewardRate)} ETH`); console.log(` Complete: ${complete}`); console.log(` Status: ${isActive ? "๐ŸŸข ACTIVE" : "๐Ÿ”ด COMPLETED"}`); if (lastClaimed > 0) { const lastClaimedDate = new Date(Number(lastClaimed) * 1000); console.log(` Last Claimed: ${lastClaimedDate.toLocaleString()}`); } if (unlockTime > 0) { const unlockDate = new Date(Number(unlockTime) * 1000); console.log(` Unlock Time: ${unlockDate.toLocaleString()}`); } } console.log(`\n๐Ÿ’Ž Summary:`); console.log(` Total Stakes: ${stakes.length}`); console.log(` Active Stakes: ${activeStakes}`); console.log(` Total Active Amount: ${ethers.formatEther(totalAmount)} ETH`); // Store for later tests this.initialStakes = stakes; this.initialActiveStakes = activeStakes; this.initialTotalAmount = totalAmount; } else { console.log("๐Ÿ“ญ No stakes found for this user"); this.initialStakes = []; this.initialActiveStakes = 0; this.initialTotalAmount = 0n; } }); }); describe("Bot Authorization", function() { it("Should check if bot manager is authorized", async function() { const isAuthorized = await paca.authorizedBots(BOT_MANAGER_ADDRESS); console.log(`๐Ÿ” Bot Manager authorized: ${isAuthorized}`); if (!isAuthorized) { console.log("โš ๏ธ Bot Manager not authorized - this test shows read-only functionality"); console.log(" To test clearing, authorize the bot manager first:"); console.log(` paca.addBot("${BOT_MANAGER_ADDRESS}")`); } this.isBotAuthorized = isAuthorized; }); }); describe("Clear Stakes Simulation (DRY RUN)", function() { it("Should simulate what clearStakes would do", async function() { if (!this.initialStakes || this.initialStakes.length === 0) { console.log("โš ๏ธ No stakes to clear - skipping simulation"); return; } console.log(`\n๐Ÿ”ฅ SIMULATING clearStakes for ${TEST_USER_ADDRESS}:`); console.log(" (This is a dry run - no actual clearing will happen)"); const stakes = this.initialStakes; let wouldClear = 0; let wouldZeroAmount = 0n; stakes.forEach((stake, i) => { if (!stake.complete && stake.amount > 0n) { wouldClear++; wouldZeroAmount += stake.amount; console.log(` ๐ŸŽฏ Would clear Stake ${i + 1}: ${ethers.formatEther(stake.amount)} ETH`); } }); console.log(`\n๐Ÿ“Š Simulation Results:`); console.log(` Stakes that would be cleared: ${wouldClear}`); console.log(` Total amount that would be zeroed: ${ethers.formatEther(wouldZeroAmount)} ETH`); console.log(` All stakes would be marked complete: true`); expect(wouldClear).to.be.greaterThan(0, "Should have stakes to clear"); }); }); describe("Actual Clear Stakes (ONLY IF AUTHORIZED)", function() { it("Should clear stakes if bot manager is authorized", async function() { if (!this.isBotAuthorized) { console.log("โš ๏ธ Skipping actual clear test - bot manager not authorized"); console.log(" This is SAFE - no funds will be affected"); return; } if (!this.initialStakes || this.initialActiveStakes === 0) { console.log("โš ๏ธ No active stakes to clear"); return; } console.log(`\n๐Ÿ”ฅ EXECUTING ACTUAL clearStakes...`); console.log(" โš ๏ธ WARNING: This will affect real funds!"); // Get stakes before clearing const stakesBefore = await paca.getStakes(TEST_USER_ADDRESS); // Execute clearStakes console.log("โšก Calling botManager.clearStakes()..."); const clearTx = await botManager.connect(owner).clearStakes(PACA_BSC_ADDRESS, TEST_USER_ADDRESS); const receipt = await clearTx.wait(); console.log(`โœ… clearStakes executed successfully!`); console.log(`โ›ฝ Gas used: ${receipt.gasUsed.toString()}`); console.log(`๐Ÿงพ Transaction: ${receipt.hash}`); // Get stakes after clearing const stakesAfter = await paca.getStakes(TEST_USER_ADDRESS); console.log(`\n๐Ÿ“Š RESULTS COMPARISON:`); console.log(` Stakes Before: ${stakesBefore.length}`); console.log(` Stakes After: ${stakesAfter.length}`); // Verify all stakes are cleared const allComplete = stakesAfter.every(stake => stake.complete === true); const allZeroed = stakesAfter.every(stake => stake.amount === 0n); console.log(` All marked complete: ${allComplete}`); console.log(` All amounts zeroed: ${allZeroed}`); expect(allComplete).to.be.true; expect(allZeroed).to.be.true; console.log("๐ŸŽ‰ SUCCESS: Stakes cleared completely!"); }); }); describe("Test Summary", function() { it("Should provide test summary", async function() { console.log(`\n๐Ÿ“‹ TEST SUMMARY:`); console.log(` ๐ŸŽฏ Test User: ${TEST_USER_ADDRESS}`); console.log(` ๐Ÿ”— PACA Contract: ${PACA_BSC_ADDRESS}`); console.log(` ๐Ÿค– Bot Manager: ${BOT_MANAGER_ADDRESS}`); console.log(` โœ… Bot Authorized: ${this.isBotAuthorized || false}`); console.log(` ๐Ÿ“Š Initial Stakes: ${this.initialActiveStakes || 0} active`); console.log(` ๐ŸŒ Network: BSC Mainnet Fork`); console.log(` โš ๏ธ NOTE: This was a FORK TEST - ${this.isBotAuthorized ? 'real clearing was performed' : 'no real funds affected'}`); }); }); });