Skip to content

Commit

Permalink
fix: linting
Browse files Browse the repository at this point in the history
  • Loading branch information
sakulstra committed Aug 16, 2024
1 parent 37b55db commit 174cd79
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ abstract contract ERC4626StataTokenUpgradeable is ERC4626Upgradeable, IERC4626St
SignatureParams memory sig,
bool depositToAave
) public returns (uint256) {
IERC20Permit assetToDeposit = IERC20Permit(depositToAave ? asset() : address(_getERC4626StataTokenStorage()._aToken));
IERC20Permit assetToDeposit = IERC20Permit(
depositToAave ? asset() : address(_getERC4626StataTokenStorage()._aToken)
);

try
assetToDeposit.permit(_msgSender(), address(this), assets, deadline, sig.v, sig.r, sig.s)
Expand Down
202 changes: 116 additions & 86 deletions tests/periphery/static-a-token/ERC4626StataTokenUpgradeable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,98 +94,128 @@ contract ERC4626StataTokenUpgradeableTest is TestnetProcedures {
}

function test_depositWithPermit_shouldRevert_emptyPermit_noPreApproval(uint128 assets) external {
TestEnv memory env = _setupTestEnv(assets);
_fundAToken(env.amount, user);
IERC4626StataToken.SignatureParams memory sig;
vm.expectRevert(); // will underflow
vm.prank(user);
erc4626Upgradeable.depositWithPermit(env.amount, user, block.timestamp + 1000, sig, false);
}

function test_depositWithPermit_emptyPermit_underlying_preApproval(uint128 assets, address receiver) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.amount, user);
IERC4626StataToken.SignatureParams memory sig;
vm.prank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), env.amount);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(env.amount, receiver, block.timestamp + 1000, sig, true);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(aToken).balanceOf(user), 0);
}

function test_depositWithPermit_emptyPermit_aToken_preApproval(uint128 assets, address receiver) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundAToken(env.amount, user);
IERC4626StataToken.SignatureParams memory sig;
vm.prank(user);
IERC20(aToken).approve(address(erc4626Upgradeable), env.amount);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(env.amount, receiver, block.timestamp + 1000, sig, false);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(aToken).balanceOf(user), 0);
TestEnv memory env = _setupTestEnv(assets);
_fundAToken(env.amount, user);
IERC4626StataToken.SignatureParams memory sig;
vm.expectRevert(); // will underflow
vm.prank(user);
erc4626Upgradeable.depositWithPermit(env.amount, user, block.timestamp + 1000, sig, false);
}

function test_depositWithPermit_emptyPermit_underlying_preApproval(
uint128 assets,
address receiver
) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.amount, user);
IERC4626StataToken.SignatureParams memory sig;
vm.prank(user);
IERC20(underlying).approve(address(erc4626Upgradeable), env.amount);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amount,
receiver,
block.timestamp + 1000,
sig,
true
);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(aToken).balanceOf(user), 0);
}

function test_depositWithPermit_emptyPermit_aToken_preApproval(
uint128 assets,
address receiver
) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundAToken(env.amount, user);
IERC4626StataToken.SignatureParams memory sig;
vm.prank(user);
IERC20(aToken).approve(address(erc4626Upgradeable), env.amount);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amount,
receiver,
block.timestamp + 1000,
sig,
false
);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(aToken).balanceOf(user), 0);
}

function test_depositWithPermit_underlying(uint128 assets, address receiver) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.amount, user);

SigUtils.Permit memory permit = SigUtils.Permit({
owner: user,
spender: address(erc4626Upgradeable),
value: env.amount,
nonce: IERC20Permit(underlying).nonces(user),
deadline: block.timestamp + 100
});

bytes32 permitDigest = SigUtils.getTypedDataHash(
permit,
SigUtils.PERMIT_TYPEHASH,
IERC20Permit(underlying).DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, permitDigest);
IERC4626StataToken.SignatureParams memory sig = IERC4626StataToken.SignatureParams(v,r,s);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(env.amount, receiver, permit.deadline, sig, true);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(underlying).balanceOf(user), 0);
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundUnderlying(env.amount, user);

SigUtils.Permit memory permit = SigUtils.Permit({
owner: user,
spender: address(erc4626Upgradeable),
value: env.amount,
nonce: IERC20Permit(underlying).nonces(user),
deadline: block.timestamp + 100
});

bytes32 permitDigest = SigUtils.getTypedDataHash(
permit,
SigUtils.PERMIT_TYPEHASH,
IERC20Permit(underlying).DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, permitDigest);
IERC4626StataToken.SignatureParams memory sig = IERC4626StataToken.SignatureParams(v, r, s);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amount,
receiver,
permit.deadline,
sig,
true
);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(underlying).balanceOf(user), 0);
}

function test_depositWithPermit_aToken(uint128 assets, address receiver) external {
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundAToken(env.amount, user);

SigUtils.Permit memory permit = SigUtils.Permit({
owner: user,
spender: address(erc4626Upgradeable),
value: env.amount,
nonce: IERC20Permit(aToken).nonces(user),
deadline: block.timestamp + 100
});

bytes32 permitDigest = SigUtils.getTypedDataHash(
permit,
SigUtils.PERMIT_TYPEHASH,
IERC20Permit(aToken).DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, permitDigest);
IERC4626StataToken.SignatureParams memory sig = IERC4626StataToken.SignatureParams(v,r,s);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(env.amount, receiver, permit.deadline, sig, false);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(aToken).balanceOf(user), 0);
vm.assume(receiver != address(0));
TestEnv memory env = _setupTestEnv(assets);
_fundAToken(env.amount, user);

SigUtils.Permit memory permit = SigUtils.Permit({
owner: user,
spender: address(erc4626Upgradeable),
value: env.amount,
nonce: IERC20Permit(aToken).nonces(user),
deadline: block.timestamp + 100
});

bytes32 permitDigest = SigUtils.getTypedDataHash(
permit,
SigUtils.PERMIT_TYPEHASH,
IERC20Permit(aToken).DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, permitDigest);
IERC4626StataToken.SignatureParams memory sig = IERC4626StataToken.SignatureParams(v, r, s);
vm.prank(user);
uint256 shares = erc4626Upgradeable.depositWithPermit(
env.amount,
receiver,
permit.deadline,
sig,
false
);

assertEq(erc4626Upgradeable.balanceOf(receiver), shares);
assertEq(IERC20(aToken).balanceOf(address(erc4626Upgradeable)), env.amount);
assertEq(IERC20(aToken).balanceOf(user), 0);
}

// ### REDEEM TESTS ###
Expand Down
4 changes: 2 additions & 2 deletions tests/utils/SigUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity ^0.8.10;
import {IERC20AaveLM} from '../../src/periphery/contracts/static-a-token/interfaces/IERC20AaveLM.sol';

library SigUtils {
bytes32 internal constant PERMIT_TYPEHASH =
keccak256('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)');
bytes32 internal constant PERMIT_TYPEHASH =
keccak256('Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)');

struct Permit {
address owner;
Expand Down

0 comments on commit 174cd79

Please sign in to comment.