const { ethers } = require("hardhat"); async function main() { console.log("๐Ÿ”ง Testing clearAllSellStakes function on LOCAL SONIC FORK"); console.log("โš ๏ธ THIS RUNS ON LOCAL FORK ONLY - NOT MAINNET"); // Contract address on Sonic mainnet const SONIC_PACA_ADDRESS = "0xa26F8128Ecb2FF2FC5618498758cC82Cf1FDad5F"; // Owner address from the contract (from line 186 in sonic_paca.sol) const OWNER_ADDRESS = "0x41970Ce76b656030A79E7C1FA76FC4EB93980255"; console.log("๐Ÿ“ Contract address:", SONIC_PACA_ADDRESS); console.log("๐Ÿ‘‘ Contract owner:", OWNER_ADDRESS); // Impersonate the owner account await ethers.provider.send("hardhat_impersonateAccount", [OWNER_ADDRESS]); const owner = await ethers.getSigner(OWNER_ADDRESS); // Give the owner some ETH for gas await ethers.provider.send("hardhat_setBalance", [ OWNER_ADDRESS, "0x1000000000000000000", // 1 ETH ]); console.log("๐Ÿ”‘ Impersonating owner:", owner.address); console.log("๐Ÿ’ฐ Owner balance:", ethers.formatEther(await ethers.provider.getBalance(owner.address)), "ETH"); // Get contract instance const contractFactory = await ethers.getContractFactory("PacaFinanceWithBoostAndScheduleSonic"); const contract = contractFactory.attach(SONIC_PACA_ADDRESS).connect(owner); console.log("\n๐Ÿ“Š STEP 1: Querying current sellStakes..."); try { // Query all sell stakes before clearing const beforeData = await contract.getAllSellStakesWithKeys(); const totalBefore = beforeData[0].length; console.log("๐Ÿ“ˆ Total sellStakes before:", totalBefore); if (totalBefore === 0) { console.log("โ„น๏ธ No sellStakes found - contract may be empty"); return; } // Count zero address stakes let zeroAddressCount = 0; const zeroStakes = []; for (let i = 0; i < beforeData[0].length; i++) { const seller = beforeData[0][i]; const stakeId = beforeData[1][i]; const stakeData = beforeData[2][i]; if (seller === "0x0000000000000000000000000000000000000000") { zeroAddressCount++; zeroStakes.push({ index: i, stakeId: stakeId.toString(), amount: ethers.formatEther(stakeData.amount), price: ethers.formatEther(stakeData.price) }); } } console.log(`๐Ÿ” Found ${zeroAddressCount} zero address sellStakes:`); zeroStakes.forEach((stake, idx) => { console.log(` ${idx + 1}. StakeId: ${stake.stakeId}, Amount: ${stake.amount} ETH, Price: ${stake.price} ETH`); }); if (zeroAddressCount === 0) { console.log("โœ… No zero address sellStakes found - test not needed"); return; } console.log("\n๐Ÿงน STEP 2: Executing clearAllSellStakes(address(0))..."); // Estimate gas first try { const gasEstimate = await contract.clearAllSellStakes.estimateGas("0x0000000000000000000000000000000000000000"); console.log("โ›ฝ Estimated gas:", gasEstimate.toString()); } catch (gasError) { console.log("โš ๏ธ Gas estimation failed:", gasError.message); } // Call clearAllSellStakes for zero address const tx = await contract.clearAllSellStakes("0x0000000000000000000000000000000000000000"); console.log("๐Ÿ“ Transaction hash:", tx.hash); // Wait for transaction to be mined console.log("โณ Waiting for transaction to be mined..."); const receipt = await tx.wait(); console.log("โœ… Transaction mined in block:", receipt.blockNumber); console.log("โ›ฝ Gas used:", receipt.gasUsed.toString()); // Parse events console.log(`๐Ÿ“ข Events emitted: ${receipt.logs.length}`); for (let i = 0; i < receipt.logs.length; i++) { try { const parsedLog = contract.interface.parseLog(receipt.logs[i]); if (parsedLog.name === "StakeSaleCancelled") { console.log(` - StakeSaleCancelled: address=${parsedLog.args[0]}, stakeId=${parsedLog.args[1]}`); } } catch (e) { // Ignore unparseable logs } } console.log("\n๐Ÿ“Š STEP 3: Querying sellStakes after clearing..."); // Query all sell stakes after clearing const afterData = await contract.getAllSellStakesWithKeys(); const totalAfter = afterData[0].length; console.log("๐Ÿ“ˆ Total sellStakes after:", totalAfter); // Count zero address stakes after let zeroAddressCountAfter = 0; for (let i = 0; i < afterData[0].length; i++) { if (afterData[0][i] === "0x0000000000000000000000000000000000000000") { zeroAddressCountAfter++; console.log(` - Remaining zero address stake: stakeId=${afterData[1][i]}`); } } console.log(`๐Ÿ” Zero address sellStakes remaining: ${zeroAddressCountAfter}`); console.log("\n๐Ÿ“‹ FINAL SUMMARY:"); console.log("=" .repeat(50)); console.log(`๐Ÿ“Š Total sellStakes: ${totalBefore} โ†’ ${totalAfter} (${totalAfter - totalBefore >= 0 ? '+' : ''}${totalAfter - totalBefore})`); console.log(`๐Ÿ—‘๏ธ Zero address stakes: ${zeroAddressCount} โ†’ ${zeroAddressCountAfter} (${zeroAddressCountAfter - zeroAddressCount >= 0 ? '+' : ''}${zeroAddressCountAfter - zeroAddressCount})`); console.log(`โœจ Cleared: ${zeroAddressCount - zeroAddressCountAfter} zero address sellStakes`); console.log("=" .repeat(50)); if (zeroAddressCountAfter === 0 && zeroAddressCount > 0) { console.log("๐ŸŽ‰ SUCCESS: All zero address sellStakes have been cleared!"); } else if (zeroAddressCount === 0) { console.log("โ„น๏ธ INFO: No zero address sellStakes were found to clear"); } else { console.log("โš ๏ธ WARNING: Some zero address sellStakes may still remain"); } } catch (error) { console.error("โŒ Error during test:", error.message); if (error.reason) { console.error("๐Ÿ’ก Reason:", error.reason); } } // Stop impersonating await ethers.provider.send("hardhat_stopImpersonatingAccount", [OWNER_ADDRESS]); } main() .then(() => process.exit(0)) .catch((error) => { console.error("โŒ Script failed:", error); process.exit(1); });