Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iavl committed Sep 11, 2024
1 parent 8930cbf commit 2261dd0
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 34 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand All @@ -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
```
12 changes: 4 additions & 8 deletions src/PowerToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
100 changes: 75 additions & 25 deletions test/PowerToken.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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 {
Expand Down Expand Up @@ -168,26 +159,43 @@ 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);

_checkBalanceAndPoints(alice, initialPoints - amount, initialPoints - amount);
_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);
Expand All @@ -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 {
Expand Down

0 comments on commit 2261dd0

Please sign in to comment.