From 2261dd089737d3855c48e4bcb48663af51b3af51 Mon Sep 17 00:00:00 2001 From: Albert Date: Wed, 11 Sep 2024 18:45:02 +0800 Subject: [PATCH] add more tests --- README.md | 7 ++- src/PowerToken.sol | 12 ++--- test/PowerToken.t.sol | 100 +++++++++++++++++++++++++++++++----------- 3 files changed, 85 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index ecf5af4..3626c3f 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ forge test ### Deploy ```shell -forge script script/Deploy.s.sol:Deploy \ +forge script script/Deploy.s.sol:Deploy --sig 'deployPowerToken()' \ --chain-id $CHAIN_ID \ --rpc-url $RPC_URL \ --private-key $PRIVATE_KEY \ @@ -33,6 +33,11 @@ forge script script/Deploy.s.sol:Deploy \ --verify \ --broadcast --ffi -vvvv +cast calldata 'initialize(string calldata name_, string calldata symbol_, address admin_)' "POWER" "POWER" 0xD4Bc2Ab6e4eAeCC04D83b389A57A59EEcdE91709 +0x077f224a000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000d4bc2ab6e4eaecc04d83b389a57a59eecde917090000000000000000000000000000000000000000000000000000000000000005504f5745520000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005504f574552000000000000000000000000000000000000000000000000000000 + + + # generate easily readable abi to /deployments forge script script/Deploy.s.sol:Deploy --sig 'sync()' --rpc-url $RPC_URL --broadcast --ffi ``` diff --git a/src/PowerToken.sol b/src/PowerToken.sol index a8b7eee..8bc68f0 100644 --- a/src/PowerToken.sol +++ b/src/PowerToken.sol @@ -88,17 +88,13 @@ contract PowerToken is function tip(uint256 amount, address to, bytes32 feedId) external override { if (amount == 0) revert TipAmountIsZero(); if (feedId == bytes32(0) && to == address(0)) revert TipReceiverIsEmpty(); + if (balanceOf(msg.sender) < amount) revert InsufficientBalanceAndPoints(); - uint256 oldPoints = _pointsBalancesV2[msg.sender]; - uint256 newPoints; - if (oldPoints >= amount) { - newPoints = oldPoints - amount; - } else if (balanceOf(msg.sender) >= amount) { - newPoints = 0; + if (_pointsBalancesV2[msg.sender] >= amount) { + _pointsBalancesV2[msg.sender] -= amount; } else { - revert InsufficientBalanceAndPoints(); + _pointsBalancesV2[msg.sender] = 0; } - _pointsBalancesV2[msg.sender] = newPoints; address receiver = to != address(0) ? to : address(this); if (receiver == address(this)) { diff --git a/test/PowerToken.t.sol b/test/PowerToken.t.sol index 2ada70e..0893ce2 100644 --- a/test/PowerToken.t.sol +++ b/test/PowerToken.t.sol @@ -69,34 +69,23 @@ contract PowerTokenTest is Utils, IErrors, IEvents, ERC20Upgradeable { _token.mint(alice, maxSupply + 1); } - function testTip(uint256 amount) public { - uint256 initialPoints = 100; - vm.assume(amount > 10 && amount < initialPoints); + function testTipFeedId(uint256 amount) public { + amount = bound(amount, 1, 10000 ether); + uint256 initialPoints = 10 * amount; _mintPoints(alice, initialPoints); - vm.startPrank(alice); - - vm.expectRevert(abi.encodeWithSelector(TipReceiverIsEmpty.selector)); - _token.tip(amount, address(0x0), ""); - - vm.expectRevert(abi.encodeWithSelector(TipAmountIsZero.selector)); - _token.tip(0, bob, ""); - - vm.expectRevert(abi.encodeWithSelector(InsufficientBalanceAndPoints.selector)); - _token.tip(2 * initialPoints, bob, ""); - expectEmit(); emit Tip(alice, address(0x0), someFeedId1, 10); + vm.prank(alice); _token.tip(10, address(0x0), someFeedId1); - vm.stopPrank(); assertEq(_token.balanceOf(alice), initialPoints - 10); assertEq(_token.balanceOfPoints(alice), initialPoints - 10); assertEq(_token.balanceOfByFeed(someFeedId1), 10); } - function testTipEntryAndBalanceOf() public { + function testTipFeedIdMultiple() public { _mintPoints(alice, 100); _mintPoints(bob, 100); @@ -121,6 +110,8 @@ contract PowerTokenTest is Utils, IErrors, IEvents, ERC20Upgradeable { assertEq(feedBalance1, 10 + 15); assertEq(feedBalance2, 20 + 25); assertEq(feedBalance3, 30 + 35); + + assertEq(_token.balanceOf(address(_token)), 135); } function testTipAddress(uint256 amount) public { @@ -168,19 +159,29 @@ contract PowerTokenTest is Utils, IErrors, IEvents, ERC20Upgradeable { _token.tip(expectedBobTokenBalance, alice, ""); } + function testTipFail() public { + // case 1: TipAmountIsZero + vm.expectRevert(abi.encodeWithSelector(TipAmountIsZero.selector)); + _token.tip(0, bob, ""); + + // case 2: TipReceiverIsEmpty + vm.expectRevert(abi.encodeWithSelector(TipReceiverIsEmpty.selector)); + _token.tip(1, address(0x0), ""); + + // case 3: InsufficientBalanceAndPoints + vm.expectRevert(abi.encodeWithSelector(InsufficientBalanceAndPoints.selector)); + _token.tip(1, bob, ""); + } + function testWithdrawByFeedId(uint256 amount) public { - uint256 initialPoints = 100; - _mintPoints(alice, initialPoints); + amount = bound(amount, 1, 100 ether); + uint256 initialPoints = 10 * amount; - vm.assume(amount > 10 && amount < initialPoints); + _mintPoints(alice, initialPoints); vm.prank(alice); _token.tip(amount, address(0x0), someFeedId1); - vm.prank(appAdmin); - vm.expectRevert(abi.encodeWithSelector(PointsInvalidReceiver.selector, bytes32(0))); - _token.withdrawByFeedId(charlie, ""); - vm.prank(appAdmin); _token.withdrawByFeedId(charlie, someFeedId1); @@ -188,6 +189,13 @@ contract PowerTokenTest is Utils, IErrors, IEvents, ERC20Upgradeable { _checkBalanceAndPoints(charlie, amount, 0); } + function testWithdrawByFeedIdFail() public { + // case 1: PointsInvalidReceiver + vm.prank(appAdmin); + vm.expectRevert(abi.encodeWithSelector(PointsInvalidReceiver.selector, bytes32(0))); + _token.withdrawByFeedId(charlie, ""); + } + function testWithdraw(uint256 amount) public { amount = bound(amount, 1, 100 ether); uint256 tipAmount = bound(amount, 1, amount); @@ -211,12 +219,54 @@ contract PowerTokenTest is Utils, IErrors, IEvents, ERC20Upgradeable { _checkBalanceAndPoints(receiver, withdrawAmount, 0); } - function testWithdrawFail() public { - _mintPoints(alice, 100); + function testWithdrawFail(uint256 amount) public { + amount = bound(amount, 1, 100 ether); + uint256 tipAmount = bound(amount, 1, amount); + _mintPoints(alice, amount); + _mintPoints(bob, amount); + + // case 1: InsufficientBalanceToWithdraw vm.prank(alice); vm.expectRevert(abi.encodeWithSelector(InsufficientBalanceToWithdraw.selector)); _token.withdraw(bob, 1); + + // case 2: InsufficientBalanceToWithdraw + vm.prank(alice); + _token.tip(tipAmount, bob, ""); + + vm.expectRevert(abi.encodeWithSelector(InsufficientBalanceToWithdraw.selector)); + _token.withdraw(bob, tipAmount + 1); + } + + function testTransfer(uint256 amount) public { + amount = bound(amount, 1, 100 ether); + uint256 tipAmount = bound(amount, 1, amount); + + _mintPoints(alice, amount); + _mintPoints(bob, amount); + + vm.prank(alice); + _token.tip(tipAmount, bob, ""); + + uint256 transferAmount = bound(amount, 1, tipAmount); + address receiver = address(0xaaaa); + + vm.expectEmit(); + emit Transfer(bob, receiver, transferAmount); + vm.prank(bob); + _token.transfer(receiver, transferAmount); + } + + function testTransferFail(uint256 amount) public { + amount = bound(amount, 1, 100 ether); + + _mintPoints(alice, amount); + + // can't transfer points + vm.expectRevert(abi.encodeWithSelector(InsufficientBalanceToWithdraw.selector)); + vm.prank(alice); + _token.transfer(bob, 1); } function _mintPoints(address user, uint256 amount) internal {