Skip to content

Commit

Permalink
Update METADEPOSIT_TYPEHASH (#44)
Browse files Browse the repository at this point in the history
* Update METADEPOSIT_TYPEHASH

* cleanup of MetaDepositParams
  • Loading branch information
kyzia551 authored Aug 7, 2024
1 parent 9933336 commit 65ae395
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 75 deletions.
5 changes: 2 additions & 3 deletions src/periphery/contracts/static-a-token/StaticATokenLM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract StaticATokenLM is

bytes32 public constant METADEPOSIT_TYPEHASH =
keccak256(
'Deposit(address depositor,address receiver,uint256 assets,uint16 referralCode,bool depositToAave,uint256 nonce,uint256 deadline,PermitParams permit)'
'Deposit(address depositor,address receiver,uint256 assets,uint16 referralCode,bool depositToAave,uint256 nonce,uint256 deadline)'
);
bytes32 public constant METAWITHDRAWAL_TYPEHASH =
keccak256(
Expand Down Expand Up @@ -149,8 +149,7 @@ contract StaticATokenLM is
referralCode,
depositToAave,
nonce,
deadline,
permit
deadline
)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface IStaticATokenLM is IInitializableStaticATokenLM, IERC4626 {
}

struct PermitParams {
address owner;
address spender;
uint256 value;
uint256 deadline;
uint8 v;
Expand Down
93 changes: 43 additions & 50 deletions tests/periphery/static-a-token/StaticATokenMetaTransactions.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,37 @@ contract StaticATokenMetaTransactions is BaseTest {
IStaticATokenLM.PermitParams memory permitParams;

// generate combined permit
SigUtils.DepositPermit memory depositPermit = SigUtils.DepositPermit({
owner: user,
spender: spender,
value: 1e6,
SigUtils.MetaDepositParams memory metaDepositParams = SigUtils.MetaDepositParams({
depositor: user,
receiver: spender,
assets: 1e6,
referralCode: 0,
fromUnderlying: true,
nonce: staticATokenLM.nonces(user),
deadline: block.timestamp + 1 days,
permit: permitParams
deadline: block.timestamp + 1 days
});
bytes32 digest = SigUtils.getTypedDepositHash(
depositPermit,
metaDepositParams,
staticATokenLM.METADEPOSIT_TYPEHASH(),
staticATokenLM.DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, digest);

IStaticATokenLM.SignatureParams memory sigParams = IStaticATokenLM.SignatureParams(v, r, s);

uint256 previewDeposit = staticATokenLM.previewDeposit(depositPermit.value);
uint256 previewDeposit = staticATokenLM.previewDeposit(metaDepositParams.assets);
staticATokenLM.metaDeposit(
depositPermit.owner,
depositPermit.spender,
depositPermit.value,
depositPermit.referralCode,
depositPermit.fromUnderlying,
depositPermit.deadline,
metaDepositParams.depositor,
metaDepositParams.receiver,
metaDepositParams.assets,
metaDepositParams.referralCode,
metaDepositParams.fromUnderlying,
metaDepositParams.deadline,
permitParams,
sigParams
);

assertEq(staticATokenLM.balanceOf(depositPermit.spender), previewDeposit);
assertEq(staticATokenLM.balanceOf(metaDepositParams.receiver), previewDeposit);
}

function test_metaDepositATokenUnderlying() public {
Expand All @@ -99,8 +98,6 @@ contract StaticATokenMetaTransactions is BaseTest {
(uint8 pV, bytes32 pR, bytes32 pS) = vm.sign(userPrivateKey, permitDigest);

IStaticATokenLM.PermitParams memory permitParams = IStaticATokenLM.PermitParams(
permit.owner,
permit.spender,
permit.value,
permit.deadline,
pV,
Expand All @@ -109,40 +106,39 @@ contract StaticATokenMetaTransactions is BaseTest {
);

// generate combined permit
SigUtils.DepositPermit memory depositPermit = SigUtils.DepositPermit({
owner: user,
spender: spender,
value: permit.value,
SigUtils.MetaDepositParams memory metaDepositParams = SigUtils.MetaDepositParams({
depositor: user,
receiver: spender,
assets: permit.value,
referralCode: 0,
fromUnderlying: true,
nonce: staticATokenLM.nonces(user),
deadline: permit.deadline,
permit: permitParams
deadline: permit.deadline
});
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
userPrivateKey,
SigUtils.getTypedDepositHash(
depositPermit,
metaDepositParams,
staticATokenLM.METADEPOSIT_TYPEHASH(),
staticATokenLM.DOMAIN_SEPARATOR()
)
);

IStaticATokenLM.SignatureParams memory sigParams = IStaticATokenLM.SignatureParams(v, r, s);

uint256 previewDeposit = staticATokenLM.previewDeposit(depositPermit.value);
uint256 previewDeposit = staticATokenLM.previewDeposit(metaDepositParams.assets);
uint256 shares = staticATokenLM.metaDeposit(
depositPermit.owner,
depositPermit.spender,
depositPermit.value,
depositPermit.referralCode,
depositPermit.fromUnderlying,
depositPermit.deadline,
metaDepositParams.depositor,
metaDepositParams.receiver,
metaDepositParams.assets,
metaDepositParams.referralCode,
metaDepositParams.fromUnderlying,
metaDepositParams.deadline,
permitParams,
sigParams
);
assertEq(shares, previewDeposit);
assertEq(staticATokenLM.balanceOf(depositPermit.spender), previewDeposit);
assertEq(staticATokenLM.balanceOf(metaDepositParams.receiver), previewDeposit);
}

function test_metaDepositAToken() public {
Expand All @@ -168,8 +164,6 @@ contract StaticATokenMetaTransactions is BaseTest {
(uint8 pV, bytes32 pR, bytes32 pS) = vm.sign(userPrivateKey, permitDigest);

IStaticATokenLM.PermitParams memory permitParams = IStaticATokenLM.PermitParams(
permit.owner,
permit.spender,
permit.value,
permit.deadline,
pV,
Expand All @@ -178,39 +172,38 @@ contract StaticATokenMetaTransactions is BaseTest {
);

// generate combined permit
SigUtils.DepositPermit memory depositPermit = SigUtils.DepositPermit({
owner: user,
spender: spender,
value: permit.value,
SigUtils.MetaDepositParams memory metaDepositParams = SigUtils.MetaDepositParams({
depositor: user,
receiver: spender,
assets: permit.value,
referralCode: 0,
fromUnderlying: false,
nonce: staticATokenLM.nonces(user),
deadline: permit.deadline,
permit: permitParams
deadline: permit.deadline
});
bytes32 digest = SigUtils.getTypedDepositHash(
depositPermit,
metaDepositParams,
staticATokenLM.METADEPOSIT_TYPEHASH(),
staticATokenLM.DOMAIN_SEPARATOR()
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(userPrivateKey, digest);

IStaticATokenLM.SignatureParams memory sigParams = IStaticATokenLM.SignatureParams(v, r, s);

uint256 previewDeposit = staticATokenLM.previewDeposit(depositPermit.value);
uint256 previewDeposit = staticATokenLM.previewDeposit(metaDepositParams.assets);

staticATokenLM.metaDeposit(
depositPermit.owner,
depositPermit.spender,
depositPermit.value,
depositPermit.referralCode,
depositPermit.fromUnderlying,
depositPermit.deadline,
metaDepositParams.depositor,
metaDepositParams.receiver,
metaDepositParams.assets,
metaDepositParams.referralCode,
metaDepositParams.fromUnderlying,
metaDepositParams.deadline,
permitParams,
sigParams
);

assertEq(staticATokenLM.balanceOf(depositPermit.spender), previewDeposit);
assertEq(staticATokenLM.balanceOf(metaDepositParams.receiver), previewDeposit);
}

function test_metaWithdraw() public {
Expand All @@ -219,7 +212,7 @@ contract StaticATokenMetaTransactions is BaseTest {

_depositAToken(amountToDeposit, user);

SigUtils.WithdrawPermit memory permit = SigUtils.WithdrawPermit({
SigUtils.MetaWithdrawParams memory permit = SigUtils.MetaWithdrawParams({
owner: user,
spender: spender,
staticAmount: 0,
Expand Down
38 changes: 18 additions & 20 deletions tests/utils/SigUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ library SigUtils {
uint256 deadline;
}

struct WithdrawPermit {
struct MetaWithdrawParams {
address owner;
address spender;
uint256 staticAmount;
Expand All @@ -22,15 +22,14 @@ library SigUtils {
uint256 deadline;
}

struct DepositPermit {
address owner;
address spender;
uint256 value;
struct MetaDepositParams {
address depositor;
address receiver;
uint256 assets;
uint16 referralCode;
bool fromUnderlying;
uint256 nonce;
uint256 deadline;
IStaticATokenLM.PermitParams permit;
}

// computes the hash of a permit
Expand All @@ -49,7 +48,7 @@ library SigUtils {
}

function getWithdrawHash(
WithdrawPermit memory permit,
MetaWithdrawParams memory permit,
bytes32 typehash
) internal pure returns (bytes32) {
return
Expand All @@ -68,21 +67,20 @@ library SigUtils {
}

function getDepositHash(
DepositPermit memory permit,
MetaDepositParams memory params,
bytes32 typehash
) internal pure returns (bytes32) {
return
keccak256(
abi.encode(
typehash,
permit.owner,
permit.spender,
permit.value,
permit.referralCode,
permit.fromUnderlying,
permit.nonce,
permit.deadline,
permit.permit
params.depositor,
params.receiver,
params.assets,
params.referralCode,
params.fromUnderlying,
params.nonce,
params.deadline
)
);
}
Expand All @@ -98,20 +96,20 @@ library SigUtils {
}

function getTypedWithdrawHash(
WithdrawPermit memory permit,
MetaWithdrawParams memory params,
bytes32 typehash,
bytes32 domainSeparator
) public pure returns (bytes32) {
return
keccak256(abi.encodePacked('\x19\x01', domainSeparator, getWithdrawHash(permit, typehash)));
keccak256(abi.encodePacked('\x19\x01', domainSeparator, getWithdrawHash(params, typehash)));
}

function getTypedDepositHash(
DepositPermit memory permit,
MetaDepositParams memory params,
bytes32 typehash,
bytes32 domainSeparator
) public pure returns (bytes32) {
return
keccak256(abi.encodePacked('\x19\x01', domainSeparator, getDepositHash(permit, typehash)));
keccak256(abi.encodePacked('\x19\x01', domainSeparator, getDepositHash(params, typehash)));
}
}

0 comments on commit 65ae395

Please sign in to comment.