1
0
forked from mico/idle_moloch

Events test added

This commit is contained in:
syahirAmali 2024-10-29 03:03:59 +08:00
parent 9a0ab2963a
commit 0b1499a122

View File

@ -11,6 +11,24 @@ contract raid_geldTest is Test {
address public player2; address public player2;
address public owner; address public owner;
event PlayerRegistered(address indexed player, uint256 initialGeld);
event RaidPerformed(
address indexed player,
uint256 totalMinted,
uint256 geldBalance
);
event UnitAdded(
address indexed player,
uint8 unitType,
uint16 nUnits,
uint256 cost,
uint256 geldBalance,
uint16 molochDenierLevel,
uint16 apprenticeLevel,
uint16 anointedLevel,
uint16 championLevel
);
function setUp() public { function setUp() public {
owner = address(0x126); owner = address(0x126);
player1 = address(0x123); player1 = address(0x123);
@ -41,6 +59,11 @@ contract raid_geldTest is Test {
uint256 initialBalance = address(raid_geld).balance; uint256 initialBalance = address(raid_geld).balance;
// Making sure event is emitted when player is registered
vm.expectEmit(address(raid_geld));
emit PlayerRegistered(player1, raid_geld.INITIAL_GELD());
// Send registration fee ETH to the contract // Send registration fee ETH to the contract
registerPlayer(); registerPlayer();
@ -48,7 +71,10 @@ contract raid_geldTest is Test {
assertEq(raid_geld.balanceOf(player1), raid_geld.INITIAL_GELD()); assertEq(raid_geld.balanceOf(player1), raid_geld.INITIAL_GELD());
// Verify the contract balance is updated // Verify the contract balance is updated
assertEq(address(raid_geld).balance, initialBalance + raid_geld.BUY_IN_AMOUNT()); assertEq(
address(raid_geld).balance,
initialBalance + raid_geld.BUY_IN_AMOUNT()
);
// Verify player is set initially // Verify player is set initially
Player memory player = raid_geld.getPlayer(player1); Player memory player = raid_geld.getPlayer(player1);
@ -69,6 +95,12 @@ contract raid_geldTest is Test {
// Switch to Player 1 and register it // Switch to Player 1 and register it
vm.startPrank(player1); vm.startPrank(player1);
// Making sure event is emitted when player is registered
vm.expectEmit(address(raid_geld));
emit PlayerRegistered(player1, raid_geld.INITIAL_GELD());
registerPlayer(); registerPlayer();
// Switch back to owner and withdraw funds // Switch back to owner and withdraw funds
@ -86,6 +118,12 @@ contract raid_geldTest is Test {
function test_04_only_owner_can_withdraw() public { function test_04_only_owner_can_withdraw() public {
// Register player 1 // Register player 1
vm.startPrank(player1); vm.startPrank(player1);
// Making sure event is emitted when player is registered
vm.expectEmit(address(raid_geld));
emit PlayerRegistered(player1, raid_geld.INITIAL_GELD());
registerPlayer(); registerPlayer();
// attempt to withdraw with player 1, it should fail // attempt to withdraw with player 1, it should fail
@ -97,6 +135,12 @@ contract raid_geldTest is Test {
bool is_registered = raid_geld.isRegistered(player1); bool is_registered = raid_geld.isRegistered(player1);
assertEq(is_registered, false); assertEq(is_registered, false);
vm.startPrank(player1); vm.startPrank(player1);
// Making sure event is emitted when player is registered
vm.expectEmit(address(raid_geld));
emit PlayerRegistered(player1, raid_geld.INITIAL_GELD());
registerPlayer(); registerPlayer();
is_registered = raid_geld.isRegistered(player1); is_registered = raid_geld.isRegistered(player1);
assertEq(is_registered, true); assertEq(is_registered, true);
@ -104,6 +148,12 @@ contract raid_geldTest is Test {
function test_06_add_unit() public { function test_06_add_unit() public {
vm.startPrank(player1); vm.startPrank(player1);
// Making sure event is emitted when player is registered
vm.expectEmit(address(raid_geld));
emit PlayerRegistered(player1, raid_geld.INITIAL_GELD());
registerPlayer(); registerPlayer();
vm.expectRevert(); vm.expectRevert();
@ -118,6 +168,25 @@ contract raid_geldTest is Test {
uint256 unit_level = army.moloch_denier.level; uint256 unit_level = army.moloch_denier.level;
uint256 balance = raid_geld.balanceOf(player1); uint256 balance = raid_geld.balanceOf(player1);
uint256 income_per_sec = army.profit_per_second; uint256 income_per_sec = army.profit_per_second;
uint256 cost = RaidGeldUtils.calculateUnitPrice(0, 0, 1);
uint256 playerBalance = raid_geld.balanceOf(address(player1));
// Making sure event is emitted when player adds a unit
vm.expectEmit(address(raid_geld));
emit UnitAdded(
address(player1),
0,
1,
cost,
playerBalance - cost,
1,
0,
0,
0
);
// Add 1 unit // Add 1 unit
raid_geld.addUnit(0, 1); raid_geld.addUnit(0, 1);
uint256 unitPrice = RaidGeldUtils.calculateUnitPrice(0, 0, 1); uint256 unitPrice = RaidGeldUtils.calculateUnitPrice(0, 0, 1);
@ -146,23 +215,63 @@ contract raid_geldTest is Test {
vm.startPrank(player1); vm.startPrank(player1);
registerPlayer(); registerPlayer();
uint256 cost = RaidGeldUtils.calculateUnitPrice(0, 0, 1);
uint256 playerBalance = raid_geld.balanceOf(address(player1));
// Making sure event is emitted when player adds a unit
vm.expectEmit(address(raid_geld));
emit UnitAdded(
address(player1),
0,
1,
cost,
playerBalance - cost,
1,
0,
0,
0
);
// bought 1 moloch_denier // bought 1 moloch_denier
raid_geld.addUnit(0, 1); raid_geld.addUnit(0, 1);
vm.warp(block.timestamp + 15); vm.warp(block.timestamp + 15);
uint256 balance = raid_geld.balanceOf(player1); uint256 balance = raid_geld.balanceOf(player1);
Army memory army = raid_geld.getArmy(player1);
Player memory player = raid_geld.getPlayer(player1);
uint256 amountMinted = army.profit_per_second * 15;
// Making sure event is emitted when player performs a raid
vm.expectEmit(address(raid_geld));
emit RaidPerformed(
address(player1),
player.total_minted + amountMinted,
balance + amountMinted
);
// Trigger raid funds minting // Trigger raid funds minting
raid_geld.raid(); raid_geld.raid();
// New balance should be larger // New balance should be larger
uint256 newBalance = raid_geld.balanceOf(player1); uint256 newBalance = raid_geld.balanceOf(player1);
Player memory player = raid_geld.getPlayer(player1); player = raid_geld.getPlayer(player1);
uint256 last_raided_at = player.last_raided_at; uint256 last_raided_at = player.last_raided_at;
assertLt(balance, newBalance); assertLt(balance, newBalance);
// After wait time passes raid should bring in profits again // After wait time passes raid should bring in profits again
vm.warp(block.timestamp + 15); vm.warp(block.timestamp + 15);
amountMinted = army.profit_per_second * 15;
emit RaidPerformed(
address(player1),
player.total_minted + amountMinted,
balance + amountMinted
);
raid_geld.raid(); raid_geld.raid();
// Balance should reflect that // Balance should reflect that