|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import "forge-std/Script.sol"; |
| 5 | +// import "forge-std/console.sol"; |
| 6 | +import {Test} from "forge-std/Test.sol"; |
| 7 | +import {Treasury} from "../src/treasury.sol"; |
| 8 | +import {ReentrantAttacker} from "../src/mocks/ReentrantAttacker.sol"; |
| 9 | +import {ProposalManager} from "../src/ProposalManager.sol"; |
| 10 | + |
| 11 | +contract CounterTest is Test { |
| 12 | + Treasury public treasury; |
| 13 | + |
| 14 | + address admin = address(1); |
| 15 | + address user = address(2); |
| 16 | + address recipient = address(3); |
| 17 | + |
| 18 | + function setUp() public { |
| 19 | + vm.prank(admin); |
| 20 | + |
| 21 | + treasury = new Treasury(); |
| 22 | + |
| 23 | + vm.deal(address(treasury), 10 ether); |
| 24 | + |
| 25 | + // vm.deal(address(treasury), 10 ether); |
| 26 | + } |
| 27 | + |
| 28 | + function test_Admin_Send() public { |
| 29 | + uint balB = recipient.balance; |
| 30 | + |
| 31 | + vm.prank(admin); |
| 32 | + |
| 33 | + treasury.sendFunds(recipient, 2 ether); |
| 34 | + |
| 35 | + // treasury.sendFunds(recipient, 2 ether); |
| 36 | + uint balA = recipient.balance; |
| 37 | + |
| 38 | + assertEq(balA, balB + 2 ether); |
| 39 | + } |
| 40 | + |
| 41 | + function test_RevertIfNotAdmin() public { |
| 42 | + vm.prank(user); |
| 43 | + |
| 44 | + vm.expectRevert(); |
| 45 | + |
| 46 | + treasury.sendFunds(recipient, 2 ether); |
| 47 | + } |
| 48 | + |
| 49 | + function test_TreasuryReceiveFunds() public { |
| 50 | + // uint balB = recipient.balance; |
| 51 | + vm.deal(user, 4 ether); |
| 52 | + |
| 53 | + vm.prank(user); |
| 54 | + |
| 55 | + (bool success, ) = payable(address(treasury)).call{value: 1 ether}(""); |
| 56 | + |
| 57 | + assertEq(address(treasury).balance, 11 ether); |
| 58 | + } |
| 59 | + |
| 60 | + function test_ReentrancyAttackFails() public { |
| 61 | + ReentrantAttacker attacker = new ReentrantAttacker( |
| 62 | + payable(address(treasury)) |
| 63 | + ); |
| 64 | + vm.prank((admin)); |
| 65 | + |
| 66 | + // vm.deal(address(treasury), 10 ether); |
| 67 | + |
| 68 | + treasury.sendFunds(address(attacker), 1 ether); |
| 69 | + |
| 70 | + assertEq(address(attacker).balance, 1 ether); |
| 71 | + |
| 72 | + assertEq(address(treasury).balance, 9 ether); |
| 73 | + } |
| 74 | + |
| 75 | + // function test_ReentrancyAttackDrainsSuccessfully() public { |
| 76 | + // console.log("Treasury bal before", address(treasury).balance / 1e18); |
| 77 | + |
| 78 | + // ReentrantAttacker attacker = new ReentrantAttacker( |
| 79 | + // payable(address(treasury)) |
| 80 | + // ); |
| 81 | + // console.log("Attacker bal before", address(attacker).balance / 1e18); |
| 82 | + |
| 83 | + // attacker.attack(); |
| 84 | + // // vm.expectRevert(); |
| 85 | + |
| 86 | + // console.log("Attacker bal after", address(attacker).balance / 1e18); |
| 87 | + // console.log("Treasury bal after", address(treasury).balance / 1e18); |
| 88 | + // } |
| 89 | + |
| 90 | + // ========Proposal Manager ============= |
| 91 | + |
| 92 | + enum ProposalStatus { |
| 93 | + CREATED, |
| 94 | + APPROVED, |
| 95 | + EXECUTED |
| 96 | + } |
| 97 | + |
| 98 | + function test_ProposalManagerCreateProposal() public { |
| 99 | + ProposalManager proposalManager = new ProposalManager(); |
| 100 | + |
| 101 | + vm.prank(user); |
| 102 | + proposalManager.createProposal(recipient, 2); |
| 103 | + |
| 104 | + assertEq(proposalManager.getProposalByid(1).recipient, recipient); |
| 105 | + assertEq(proposalManager.getProposalByid(1).amount, 2); |
| 106 | + |
| 107 | + assertEq(uint(proposalManager.getProposalByid(1).status), uint(ProposalStatus.CREATED)); |
| 108 | + } |
| 109 | + |
| 110 | + function test_ProposalManagerApproveProposal() public { |
| 111 | + ProposalManager proposalManager = new ProposalManager(); |
| 112 | + |
| 113 | + vm.prank(user); |
| 114 | + proposalManager.createProposal(recipient, 2); |
| 115 | + |
| 116 | + |
| 117 | + proposalManager.approveProposal(1); |
| 118 | + |
| 119 | + assertEq(uint(proposalManager.getProposalByid(1).status), uint(ProposalStatus.APPROVED)); |
| 120 | + } |
| 121 | + |
| 122 | + function test_ProposalManagerExecutedProposal() public { |
| 123 | + ProposalManager proposalManager = new ProposalManager(); |
| 124 | + |
| 125 | + vm.prank(user); |
| 126 | + proposalManager.createProposal(recipient, 2); |
| 127 | + |
| 128 | + |
| 129 | + proposalManager.executeProposal(1); |
| 130 | + |
| 131 | + // console.log("bbb", proposalManager.getProposalByid(1)); |
| 132 | + assertEq(uint(proposalManager.getProposalByid(1).status), uint(ProposalStatus.EXECUTED)); |
| 133 | + } |
| 134 | + |
| 135 | + // function testProposalStatus() public { |
| 136 | + |
| 137 | + // ProposalManager proposalManager = new ProposalManager(); |
| 138 | + // ProposalManager.Proposal memory proposal = proposalManager |
| 139 | + // .getProposalByid(1); |
| 140 | + |
| 141 | + // assertEq(proposal.status, 0); |
| 142 | + // } |
| 143 | +} |
0 commit comments