-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get MagicSpend test coverage to 100% (#14)
* Add OwnerWithdraw test cases * Add test file for GetHash * Add test file for IsValidWithdrawalSignature * Add test for InsufficientBalance revert case * Rename getHash.t.sol to GetHash.t.sol * Remove commented lines * Format * Added coverage and coveralls to CI * Remove unecessary filter to lcov
- Loading branch information
1 parent
0f214c9
commit 2662df1
Showing
5 changed files
with
139 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
import {MagicSpendTest} from "./MagicSpend.t.sol"; | ||
import {MagicSpend} from "../src/MagicSpend.sol"; | ||
import {MockERC20} from "solady/test/utils/mocks/MockERC20.sol"; | ||
import {SignatureCheckerLib} from "solady/src/utils/SignatureCheckerLib.sol"; | ||
|
||
contract GetHashTest is MagicSpendTest { | ||
MockERC20 token = new MockERC20("test", "TEST", 18); | ||
|
||
function test_returnsValidHash() public { | ||
asset = address(token); | ||
MagicSpend.WithdrawRequest memory request = _getRequest(); | ||
bytes32 expectedHash = SignatureCheckerLib.toEthSignedMessageHash( | ||
abi.encode( | ||
address(magic), withdrawer, block.chainid, address(token), request.amount, request.nonce, request.expiry | ||
) | ||
); | ||
bytes32 testHash = magic.getHash(withdrawer, request); | ||
assertEq(testHash, expectedHash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
import "./MagicSpend.t.sol"; | ||
import {MockERC20} from "solady/test/utils/mocks/MockERC20.sol"; | ||
|
||
contract IsValidWithdrawalSignature is MagicSpendTest { | ||
MockERC20 token = new MockERC20("test", "TEST", 18); | ||
|
||
function test_returnsTrueWithValidSignature() public { | ||
asset = address(token); | ||
MagicSpend.WithdrawRequest memory request = _getRequest(); | ||
bool success = magic.isValidWithdrawSignature(withdrawer, request); | ||
assert(success); | ||
} | ||
|
||
function test_returnsFalseWithInvalidSignature() public { | ||
asset = address(token); | ||
address invalidSender = address(0xdead); | ||
MagicSpend.WithdrawRequest memory request = _getRequest(); | ||
bool success = magic.isValidWithdrawSignature(invalidSender, request); | ||
assertFalse(success); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.8.21; | ||
|
||
import {Ownable} from "./MagicSpend.t.sol"; | ||
import {MagicSpendTest} from "./Validate.t.sol"; | ||
import {MockERC20} from "solady/test/utils/mocks/MockERC20.sol"; | ||
|
||
contract OwnerWithdrawTest is MagicSpendTest { | ||
MockERC20 token = new MockERC20("test", "TEST", 18); | ||
|
||
function test_revertsIfNotOwner() public { | ||
vm.startPrank(withdrawer); | ||
vm.expectRevert(Ownable.Unauthorized.selector); | ||
magic.ownerWithdraw(address(token), withdrawer, 1); | ||
} | ||
|
||
function test_transfersERC20Successfully(uint256 amount_) public { | ||
vm.startPrank(owner); | ||
amount = amount_; | ||
token.mint(address(magic), amount); | ||
asset = address(token); | ||
assertEq(token.balanceOf(owner), 0); | ||
magic.ownerWithdraw(asset, owner, amount); | ||
assertEq(token.balanceOf(owner), amount); | ||
} | ||
|
||
function test_transfersETHSuccessfully(uint256 amount_) public { | ||
vm.deal(address(magic), amount_); | ||
vm.startPrank(owner); | ||
amount = amount_; | ||
asset = address(0); | ||
assertEq(owner.balance, 0); | ||
magic.ownerWithdraw(asset, owner, amount); | ||
assertEq(owner.balance, amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters