Skip to content

Commit 1e11e1c

Browse files
committed
shared allowance test
1 parent ec4a961 commit 1e11e1c

3 files changed

Lines changed: 72 additions & 17 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.17;
3+
4+
import "forge-std/Test.sol";
5+
import "../mocks/MockPermit2.sol";
6+
import {BaseAllowanceUnitTest} from "./BaseAllowanceUnitTest.sol";
7+
import {TokenProvider} from "../utils/TokenProvider.sol";
8+
9+
contract AllowanceUnitTest_ERC20 is BaseAllowanceUnitTest {
10+
function setUp() public override {
11+
permit2 = new MockPermit2();
12+
initializeERC20Tokens();
13+
}
14+
15+
function allowance(address from, address token, address spender)
16+
public
17+
view
18+
override
19+
returns (uint160, uint48, uint48)
20+
{
21+
return MockPermit2(address(permit2)).allowance(from, token, spender);
22+
}
23+
24+
function token() public view override returns (address) {
25+
return address(token1);
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.17;
3+
4+
import "forge-std/Test.sol";
5+
import "../mocks/MockPermit2_ERC721.sol";
6+
import {BaseAllowanceUnitTest} from "./BaseAllowanceUnitTest.sol";
7+
import {TokenProvider} from "../utils/TokenProvider.sol";
8+
9+
contract AllowanceUnitTest_ERC721 is BaseAllowanceUnitTest {
10+
function setUp() public override {
11+
permit2 = new MockPermit2_ERC721();
12+
initializeNFTTokens();
13+
}
14+
15+
function allowance(address from, address token, address spender)
16+
public
17+
view
18+
override
19+
returns (uint160, uint48, uint48)
20+
{
21+
return MockPermit2_ERC721(address(permit2)).allowance(from, token, spender);
22+
}
23+
24+
function token() public view override returns (address) {
25+
return address(nft1);
26+
}
27+
}
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,31 @@
22
pragma solidity ^0.8.17;
33

44
import "forge-std/Test.sol";
5-
import "./mocks/MockPermit2.sol";
6-
import {TokenProvider} from "./utils/TokenProvider.sol";
5+
import "../mocks/IMockPermit2.sol";
6+
import {TokenProvider} from "../utils/TokenProvider.sol";
77

8-
contract AllowanceUnitTest is Test, TokenProvider {
9-
MockPermit2 permit2;
8+
abstract contract BaseAllowanceUnitTest is Test, TokenProvider {
9+
IMockPermit2 permit2;
1010

1111
address from = address(0xBEEE);
1212
address spender = address(0xBBBB);
1313

14-
function setUp() public {
15-
permit2 = new MockPermit2();
16-
initializeERC20Tokens();
17-
}
14+
function setUp() public virtual {}
15+
16+
function allowance(address from, address token, address spender) public virtual returns (uint160, uint48, uint48);
17+
18+
function token() public virtual returns (address);
1819

1920
function testUpdateAmountExpirationRandomly(uint160 amount, uint48 expiration) public {
20-
address token = address(token1);
21+
address token = token();
2122

22-
(,, uint48 nonce) = permit2.allowance(from, token, spender);
23+
(,, uint48 nonce) = allowance(from, token, spender);
2324

2425
permit2.mockUpdateSome(from, token, spender, amount, expiration);
2526

2627
uint48 timestampAfterUpdate = expiration == 0 ? uint48(block.timestamp) : expiration;
2728

28-
(uint160 amount1, uint48 expiration1, uint48 nonce1) = permit2.allowance(from, token, spender);
29+
(uint160 amount1, uint48 expiration1, uint48 nonce1) = allowance(from, token, spender);
2930
assertEq(amount, amount1);
3031
assertEq(timestampAfterUpdate, expiration1);
3132
/// nonce shouldnt change
@@ -37,14 +38,14 @@ contract AllowanceUnitTest is Test, TokenProvider {
3738
// we assume we will never be able to reach 2**48
3839
vm.assume(nonce < type(uint48).max);
3940

40-
address token = address(token1);
41+
address token = token();
4142

4243
permit2.mockUpdateAll(from, token, spender, amount, expiration, nonce);
4344

4445
uint48 nonceAfterUpdate = nonce + 1;
4546
uint48 timestampAfterUpdate = expiration == 0 ? uint48(block.timestamp) : expiration;
4647

47-
(uint160 amount1, uint48 expiration1, uint48 nonce1) = permit2.allowance(from, token, spender);
48+
(uint160 amount1, uint48 expiration1, uint48 nonce1) = allowance(from, token, spender);
4849

4950
assertEq(amount, amount1);
5051
assertEq(timestampAfterUpdate, expiration1);
@@ -54,18 +55,18 @@ contract AllowanceUnitTest is Test, TokenProvider {
5455
function testPackAndUnpack(uint160 amount, uint48 expiration, uint48 nonce) public {
5556
// pack some numbers
5657
uint256 word = Allowance.pack(amount, expiration, nonce);
57-
58+
address token = token();
5859
// store the raw word
59-
permit2.doStore(from, address(token1), spender, word);
60+
permit2.doStore(from, token, spender, word);
6061

6162
// load it as a packed allowance
62-
(uint160 amount1, uint48 expiration1, uint48 nonce1) = permit2.allowance(from, address(token1), spender);
63+
(uint160 amount1, uint48 expiration1, uint48 nonce1) = allowance(from, token, spender);
6364
assertEq(amount, amount1);
6465
assertEq(expiration, expiration1);
6566
assertEq(nonce, nonce1);
6667

6768
// get the stored word
68-
uint256 word1 = permit2.getStore(from, address(token1), spender);
69+
uint256 word1 = permit2.getStore(from, token, spender);
6970
assertEq(word, word1);
7071
}
7172
}

0 commit comments

Comments
 (0)