-
Notifications
You must be signed in to change notification settings - Fork 165
/
MintVouchers.t.sol
88 lines (76 loc) · 3.06 KB
/
MintVouchers.t.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.23;
import "../patterns/eip712-signed-messages/MintVouchers.sol";
import "./TestUtils.sol";
contract TestableMintVouchersERC721 is MintVouchersERC721 {
function getVoucherHash(uint256 tokenId, uint256 price) external view returns (bytes32) {
return _getVoucherHash(tokenId, price);
}
}
contract MintVouchersTest is TestUtils {
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
uint256 ownerPrivateKey;
address payable owner;
address payable minter;
TestableMintVouchersERC721 nftContract;
function setUp() external {
ownerPrivateKey = _randomUint256();
minter = _randomAddress();
owner = payable(vm.addr(ownerPrivateKey));
vm.prank(owner);
nftContract = new TestableMintVouchersERC721();
vm.deal(minter, 1e18);
}
function test_cannotMintWithWrongPrice() external {
uint256 tokenId = _randomUint256();
uint256 price = _randomUint256() % 100;
(uint8 v, bytes32 r, bytes32 s) = _signVoucher(tokenId, price);
vm.expectRevert('invalid signature');
vm.prank(minter);
nftContract.mint{value: price + 1}(tokenId, v, r, s);
}
function test_cannotMintWithWrongTokenId() external {
uint256 tokenId = _randomUint256();
uint256 price = _randomUint256() % 100;
(uint8 v, bytes32 r, bytes32 s) = _signVoucher(tokenId, price);
vm.expectRevert('invalid signature');
vm.prank(minter);
nftContract.mint{value: price}(tokenId + 1, v, r, s);
}
function test_canMint() external {
uint256 tokenId = _randomUint256();
uint256 price = _randomUint256() % 100;
(uint8 v, bytes32 r, bytes32 s) = _signVoucher(tokenId, price);
vm.expectEmit(true, true, true, false);
emit Transfer(address(0), minter, tokenId);
vm.prank(minter);
nftContract.mint{value: price}(tokenId, v, r, s);
assertEq(owner.balance, price);
}
function test_cannotMintTwice() external {
uint256 tokenId = _randomUint256();
uint256 price = _randomUint256() % 100;
(uint8 v, bytes32 r, bytes32 s) = _signVoucher(tokenId, price);
vm.prank(minter);
nftContract.mint{value: price}(tokenId, v, r, s);
vm.expectRevert('already minted');
vm.prank(minter);
nftContract.mint{value: price}(tokenId, v, r, s);
}
function test_canCancel() external {
uint256 tokenId = _randomUint256();
uint256 price = _randomUint256() % 100;
(uint8 v, bytes32 r, bytes32 s) = _signVoucher(tokenId, price);
vm.prank(owner);
nftContract.cancel(tokenId, price);
vm.expectRevert('mint voucher has been cancelled');
vm.prank(minter);
nftContract.mint{value: price}(tokenId, v, r, s);
}
function _signVoucher(uint256 tokenId, uint256 price)
private
returns (uint8 v, bytes32 r, bytes32 s)
{
return vm.sign(ownerPrivateKey, nftContract.getVoucherHash(tokenId, price));
}
}