Skip to content

Commit

Permalink
add test for permitAndSpend and ability to init w spendpermission as …
Browse files Browse the repository at this point in the history
…owner
  • Loading branch information
amiecorso committed Oct 16, 2024
1 parent 5aff53b commit e7f0e71
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
7 changes: 3 additions & 4 deletions test/base/SpendPermissionManagerBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ contract SpendPermissionManagerBase is Base {
function _signSpendPermission6492(
SpendPermissionManager.SpendPermission memory spendPermission,
uint256 ownerPk,
uint256 ownerIndex
uint256 ownerIndex,
bytes[] memory allInitialOwners
) internal view returns (bytes memory) {
bytes32 spendPermissionHash = mockSpendPermissionManager.getHash(spendPermission);
// construct replaySafeHash without relying on the account contract being deployed
Expand All @@ -76,9 +77,7 @@ contract SpendPermissionManagerBase is Base {

// wrap inner sig in 6492 format ======================
address factory = address(mockCoinbaseSmartWalletFactory);
bytes[] memory owners = new bytes[](1);
owners[0] = abi.encode(vm.addr(ownerPk));
bytes memory factoryCallData = abi.encodeWithSignature("createAccount(bytes[],uint256)", owners, 0);
bytes memory factoryCallData = abi.encodeWithSignature("createAccount(bytes[],uint256)", allInitialOwners, 0);
bytes memory eip6492Signature = abi.encode(factory, factoryCallData, wrappedSignature);
eip6492Signature = abi.encodePacked(eip6492Signature, EIP6492_MAGIC_VALUE);
return eip6492Signature;
Expand Down
4 changes: 2 additions & 2 deletions test/src/SpendPermissions/permit.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ contract PermitTest is SpendPermissionManagerBase {
period: period,
allowance: allowance
});
bytes memory signature = _signSpendPermission6492(spendPermission, ownerPk, 0);
bytes memory signature = _signSpendPermission6492(spendPermission, ownerPk, 0, owners);
// verify that the account isn't deployed yet
vm.assertEq(counterfactualAccount.code.length, 0);

Expand Down Expand Up @@ -232,7 +232,7 @@ contract PermitTest is SpendPermissionManagerBase {
period: period,
allowance: allowance
});
bytes memory signature = _signSpendPermission6492(spendPermission, ownerPk, 0);
bytes memory signature = _signSpendPermission6492(spendPermission, ownerPk, 0, owners);
// verify that the account is already deployed
vm.assertGt(counterfactualAccount.code.length, 0);

Expand Down
58 changes: 58 additions & 0 deletions test/src/SpendPermissions/permitAndSpend.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,62 @@ contract PermitAndSpendTest is SpendPermissionManagerBase {
assertEq(usage.end, _safeAddUint48(start, period));
assertEq(usage.spend, spend);
}

function test_permitAndSpend_success_ether_eip6492(
uint128 ownerPk,
address spender,
address recipient,
uint48 start,
uint48 end,
uint48 period,
uint160 allowance,
uint160 spend
) public {
assumePayable(recipient);
vm.assume(ownerPk != 0);
vm.assume(start > 0);
vm.assume(end > 0);
vm.assume(start < end);
vm.assume(period > 0);
vm.assume(spend > 0);
vm.assume(allowance > 0);
vm.assume(allowance >= spend);
address ownerAddress = vm.addr(ownerPk);
bytes[] memory owners = new bytes[](2);
owners[0] = abi.encode(ownerAddress);
owners[1] = abi.encode(address(mockSpendPermissionManager));
address counterfactualAccount = mockCoinbaseSmartWalletFactory.getAddress(owners, 0);
vm.assume(recipient != counterfactualAccount); // otherwise balance checks can fail

// create a 6492-compliant signature for the spend permission
SpendPermissionManager.SpendPermission memory spendPermission = SpendPermissionManager.SpendPermission({
account: counterfactualAccount,
spender: spender,
token: NATIVE_TOKEN,
start: start,
end: end,
period: period,
allowance: allowance
});
vm.deal(counterfactualAccount, allowance);
vm.deal(recipient, 0);
assertEq(counterfactualAccount.balance, allowance);
assertEq(recipient.balance, 0);

bytes memory signature = _signSpendPermission6492(spendPermission, ownerPk, 0, owners);
// verify that the account isn't deployed yet
vm.assertEq(counterfactualAccount.code.length, 0);

vm.warp(start);

vm.startPrank(spender);
mockSpendPermissionManager.permitAndSpend(spendPermission, signature, recipient, spend);

assertEq(counterfactualAccount.balance, allowance - spend);
assertEq(recipient.balance, spend);
SpendPermissionManager.PeriodSpend memory usage = mockSpendPermissionManager.getCurrentPeriod(spendPermission);
assertEq(usage.start, start);
assertEq(usage.end, _safeAddUint48(start, period));
assertEq(usage.spend, spend);
}
}

0 comments on commit e7f0e71

Please sign in to comment.