Commit before cuna
This commit is contained in:
246
test/test_pacabotmanager.js
Normal file
246
test/test_pacabotmanager.js
Normal file
@@ -0,0 +1,246 @@
|
||||
// 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'}`);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user