Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: generalize spender #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions test/AllowanceTransferTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowance() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

snapStart("permitCleanWrite");
Expand All @@ -113,7 +113,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowanceCompactSig() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getCompactPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);
assertEq(sig.length, 64);

Expand All @@ -129,7 +129,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowanceIncorrectSigLength() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);
bytes memory sigExtra = bytes.concat(sig, bytes1(uint8(1)));
assertEq(sigExtra.length, 66);
Expand All @@ -140,7 +140,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowanceDirtyWrite() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, dirtyNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, dirtyNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKeyDirty, DOMAIN_SEPARATOR);

snapStart("permitDirtyWrite");
Expand All @@ -155,7 +155,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowanceBatchDifferentNonces() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

permit2.permit(from, permit, sig);
Expand All @@ -167,7 +167,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

address[] memory tokens = AddressBuilder.fill(1, address(token0)).push(address(token1));
IAllowanceTransfer.PermitBatch memory permitBatch =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, 1);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, 1, address(this));
// first token nonce is 1, second token nonce is 0
permitBatch.details[1].nonce = 0;
bytes memory sig1 = getPermitBatchSignature(permitBatch, fromPrivateKey, DOMAIN_SEPARATOR);
Expand All @@ -187,7 +187,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testSetAllowanceBatch() public {
address[] memory tokens = AddressBuilder.fill(1, address(token0)).push(address(token1));
IAllowanceTransfer.PermitBatch memory permit =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitBatchSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

snapStart("permitBatchCleanWrite");
Expand All @@ -209,7 +209,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
uint160[] memory amounts = AmountBuilder.fillUInt160(2, defaultAmount);

IAllowanceTransfer.PermitBatch memory permit =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitBatchSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

vm.expectEmit(true, true, true, true);
Expand All @@ -231,7 +231,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testSetAllowanceBatchDirtyWrite() public {
address[] memory tokens = AddressBuilder.fill(1, address(token0)).push(address(token1));
IAllowanceTransfer.PermitBatch memory permit =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, dirtyNonce);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, dirtyNonce, address(this));
bytes memory sig = getPermitBatchSignature(permit, fromPrivateKeyDirty, DOMAIN_SEPARATOR);

snapStart("permitBatchDirtyWrite");
Expand All @@ -252,7 +252,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
// test setting allowance with ordered nonce and transfer
function testSetAllowanceTransfer() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand All @@ -272,7 +272,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testTransferFromWithGasSnapshot() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand All @@ -294,7 +294,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testBatchTransferFromWithGasSnapshot() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand All @@ -321,7 +321,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
// dirty sstore on nonce, dirty sstore on transfer
function testSetAllowanceTransferDirtyNonceDirtyTransfer() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, dirtyNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, dirtyNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKeyDirty, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(fromDirty);
Expand All @@ -344,7 +344,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowanceInvalidSignature() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);
snapStart("permitInvalidSigner");
vm.expectRevert(SignatureVerification.InvalidSigner.selector);
Expand All @@ -355,7 +355,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testSetAllowanceDeadlinePassed() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 sigDeadline = block.timestamp + 100;
Expand All @@ -370,7 +370,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testMaxAllowance() public {
uint160 maxAllowance = type(uint160).max;
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), maxAllowance, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), maxAllowance, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand All @@ -395,7 +395,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testMaxAllowanceDirtyWrite() public {
uint160 maxAllowance = type(uint160).max;
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), maxAllowance, defaultExpiration, dirtyNonce);
defaultERC20PermitAllowance(address(token0), maxAllowance, defaultExpiration, dirtyNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKeyDirty, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(fromDirty);
Expand All @@ -419,7 +419,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testPartialAllowance() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand All @@ -442,7 +442,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testReuseOrderedNonceInvalid() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

permit2.permit(from, permit, sig);
Expand All @@ -459,7 +459,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testInvalidateNonces() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

// Invalidates the 0th nonce by setting the new nonce to 1.
Expand All @@ -476,15 +476,15 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testInvalidateMultipleNonces() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

// Valid permit, uses nonce 0.
permit2.permit(from, permit, sig);
(,, uint48 nonce1) = permit2.allowance(from, address(token0), address(this));
assertEq(nonce1, 1);

permit = defaultERC20PermitAllowance(address(token1), defaultAmount, defaultExpiration, nonce1);
permit = defaultERC20PermitAllowance(address(token1), defaultAmount, defaultExpiration, nonce1, address(this));
sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

// Invalidates the 9 nonces by setting the new nonce to 33.
Expand All @@ -510,7 +510,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testExcessiveInvalidation() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint32 numInvalidate = type(uint16).max;
Expand All @@ -526,7 +526,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testBatchTransferFrom() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand All @@ -553,7 +553,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testBatchTransferFromMultiToken() public {
address[] memory tokens = AddressBuilder.fill(1, address(token0)).push(address(token1));
IAllowanceTransfer.PermitBatch memory permitBatch =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitBatchSignature(permitBatch, fromPrivateKey, DOMAIN_SEPARATOR);

uint256 startBalanceFrom0 = token0.balanceOf(from);
Expand Down Expand Up @@ -587,11 +587,11 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps

function testBatchTransferFromDifferentOwners() public {
IAllowanceTransfer.PermitSingle memory permit =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

IAllowanceTransfer.PermitSingle memory permitDirty =
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, dirtyNonce);
defaultERC20PermitAllowance(address(token0), defaultAmount, defaultExpiration, dirtyNonce, address(this));
bytes memory sigDirty = getPermitSignature(permitDirty, fromPrivateKeyDirty, DOMAIN_SEPARATOR);

uint256 startBalanceFrom = token0.balanceOf(from);
Expand Down Expand Up @@ -626,7 +626,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testLockdown() public {
address[] memory tokens = AddressBuilder.fill(1, address(token0)).push(address(token1));
IAllowanceTransfer.PermitBatch memory permit =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitBatchSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

permit2.permit(from, permit, sig);
Expand Down Expand Up @@ -662,7 +662,7 @@ contract AllowanceTransferTest is Test, TokenProvider, PermitSignature, GasSnaps
function testLockdownEvent() public {
address[] memory tokens = AddressBuilder.fill(1, address(token0)).push(address(token1));
IAllowanceTransfer.PermitBatch memory permit =
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce);
defaultERC20PermitBatchAllowance(tokens, defaultAmount, defaultExpiration, defaultNonce, address(this));
bytes memory sig = getPermitBatchSignature(permit, fromPrivateKey, DOMAIN_SEPARATOR);

permit2.permit(from, permit, sig);
Expand Down
Loading