Skip to content

Commit

Permalink
use hardhat_reset and hardhat_setBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
laruh committed Nov 25, 2024
1 parent 9881905 commit 2af148a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
9 changes: 7 additions & 2 deletions contracts/SwapFeeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ contract SwapFeeManager is Ownable {
address public immutable dexFeeWallet;
address public immutable burnFeeWallet;

uint256 private constant BURN_FEE_PERCENT = 25;
uint256 private constant TOTAL_PERCENT = 100;

event FeesSplit(uint256 dexFeeAmount, uint256 burnFeeAmount);

constructor(
Expand Down Expand Up @@ -40,7 +43,8 @@ contract SwapFeeManager is Ownable {
uint256 totalBalance = address(this).balance;
require(totalBalance > 0, "No fees to split");

uint256 burnFeeAmount = (totalBalance * 25) / 100;
uint256 burnFeeAmount = (totalBalance * BURN_FEE_PERCENT) /
TOTAL_PERCENT;
uint256 dexFeeAmount = totalBalance - burnFeeAmount;

emit FeesSplit(dexFeeAmount, burnFeeAmount);
Expand All @@ -60,7 +64,8 @@ contract SwapFeeManager is Ownable {
uint256 totalBalance = token.balanceOf(address(this));
require(totalBalance > 0, "No token fees to split");

uint256 burnFeeAmount = (totalBalance * 25) / 100;
uint256 burnFeeAmount = (totalBalance * BURN_FEE_PERCENT) /
TOTAL_PERCENT;
uint256 dexFeeAmount = totalBalance - burnFeeAmount;

emit FeesSplit(dexFeeAmount, burnFeeAmount);
Expand Down
33 changes: 16 additions & 17 deletions test/SwapFeeManagerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ require('chai').use(require('chai-as-promised')).should();

describe("SwapFeeManager", function () {
beforeEach(async function () {
// Resets the Hardhat Network to its initial state
await network.provider.send("hardhat_reset");
accounts = await ethers.getSigners();

// Set balances for dexFeeWallet and burnFeeWallet to 0
await network.provider.send("hardhat_setBalance", [accounts[2].address, "0x0"]);
await network.provider.send("hardhat_setBalance", [accounts[3].address, "0x0"]);

SwapFeeManager = await ethers.getContractFactory("SwapFeeManager");
swapFeeManager = await SwapFeeManager.deploy(
accounts[2].address, // dexFeeWallet
Expand All @@ -30,18 +36,15 @@ describe("SwapFeeManager", function () {
const managerBalance = await ethers.provider.getBalance(swapFeeManager.target);
expect(managerBalance).to.equal(ethers.parseEther("1"));

const initialDexFeeBalance = await ethers.provider.getBalance(accounts[2].address);
const initialBurnFeeBalance = await ethers.provider.getBalance(accounts[3].address);

await swapFeeManager.connect(accounts[0]).splitAndWithdraw().should.be.fulfilled;

const finalDexFeeBalance = await ethers.provider.getBalance(accounts[2].address);
const finalBurnFeeBalance = await ethers.provider.getBalance(accounts[3].address);
const dexFeeBalance = await ethers.provider.getBalance(accounts[2].address);
const burnFeeBalance = await ethers.provider.getBalance(accounts[3].address);

expect(finalDexFeeBalance - initialDexFeeBalance).to.equal(ethers.parseEther("0.75"));
expect(finalBurnFeeBalance - initialBurnFeeBalance).to.equal(ethers.parseEther("0.25"));
expect(dexFeeBalance).to.equal(ethers.parseEther("0.75"));
expect(burnFeeBalance).to.equal(ethers.parseEther("0.25"));

// Ensure the contract's Ether balance is now zero
// Ensure the fee manager contract's Ether balance is now zero
const managerBalanceAfter = await ethers.provider.getBalance(swapFeeManager.target);
expect(managerBalanceAfter).to.equal(ethers.parseEther("0"));
});
Expand All @@ -54,19 +57,15 @@ describe("SwapFeeManager", function () {
const managerTokenBalance = await token.balanceOf(swapFeeManager.target);
expect(managerTokenBalance).to.equal(ethers.parseEther("1"));

const initialDexFeeTokenBalance = await token.balanceOf(accounts[2].address);
const initialBurnFeeTokenBalance = await token.balanceOf(accounts[3].address);

await swapFeeManager.connect(accounts[0]).splitAndWithdrawToken(token.target).should.be.fulfilled;

const finalDexFeeTokenBalance = await token.balanceOf(accounts[2].address);
const finalBurnFeeTokenBalance = await token.balanceOf(accounts[3].address);
const dexFeeTokenBalance = await token.balanceOf(accounts[2].address);
const burnFeeTokenBalance = await token.balanceOf(accounts[3].address);

// Check balances of dexFeeWallet and burnFeeWallet for tokens
expect(finalDexFeeTokenBalance - initialDexFeeTokenBalance).to.equal(ethers.parseEther("0.75"));
expect(finalBurnFeeTokenBalance - initialBurnFeeTokenBalance).to.equal(ethers.parseEther("0.25"));
expect(dexFeeTokenBalance).to.equal(ethers.parseEther("0.75"));
expect(burnFeeTokenBalance).to.equal(ethers.parseEther("0.25"));

// Ensure the contract's token balance is now zero
// Ensure the fee manager contract's token balance is now zero
const managerTokenBalanceAfter = await token.balanceOf(swapFeeManager.target);
expect(managerTokenBalanceAfter).to.equal(ethers.parseEther("0"));
});
Expand Down

0 comments on commit 2af148a

Please sign in to comment.