Skip to content

Commit

Permalink
chore: format module examples
Browse files Browse the repository at this point in the history
  • Loading branch information
kopy-kat committed Mar 7, 2024
1 parent 0627421 commit 6ced63e
Show file tree
Hide file tree
Showing 17 changed files with 555 additions and 258 deletions.
30 changes: 25 additions & 5 deletions examples/src/AutoSavings/AutoSavings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import { Execution } from "modulekit/src/Accounts.sol";
import { ERC7579ExecutorBase, SessionKeyBase } from "modulekit/src/Modules.sol";

contract AutoSavingToVault is ERC7579ExecutorBase, SessionKeyBase {
using ERC4626Integration for *;

/*//////////////////////////////////////////////////////////////////////////
CONSTANTS & STORAGE
//////////////////////////////////////////////////////////////////////////*/

struct Params {
address token;
uint256 amountReceived;
Expand All @@ -26,10 +32,12 @@ contract AutoSavingToVault is ERC7579ExecutorBase, SessionKeyBase {
uint128 sqrtPriceLimitX96;
}

using ERC4626Integration for *;

mapping(address account => mapping(address token => Config)) internal _config;

/*//////////////////////////////////////////////////////////////////////////
CONFIG
//////////////////////////////////////////////////////////////////////////*/

function getConfig(address account, address token) public view returns (Config memory) {
return _config[account][token];
}
Expand All @@ -48,7 +56,17 @@ contract AutoSavingToVault is ERC7579ExecutorBase, SessionKeyBase {
}
}

function onUninstall(bytes calldata data) external override { }
function onUninstall(bytes calldata data) external override {
// Todo
}

function isInitialized(address smartAccount) external view returns (bool) {
// Todo
}

/*//////////////////////////////////////////////////////////////////////////
MODULE LOGIC
//////////////////////////////////////////////////////////////////////////*/

function calcDepositAmount(
uint256 amountReceived,
Expand Down Expand Up @@ -130,12 +148,14 @@ contract AutoSavingToVault is ERC7579ExecutorBase, SessionKeyBase {
return access.sessionKeySigner;
}

/*//////////////////////////////////////////////////////////////////////////
METADATA
//////////////////////////////////////////////////////////////////////////*/

function isModuleType(uint256 typeID) external pure override returns (bool) {
return typeID == TYPE_EXECUTOR;
}

function isInitialized(address smartAccount) external view returns (bool) { }

function name() external pure virtual returns (string memory) {
return "AutoSaving";
}
Expand Down
42 changes: 31 additions & 11 deletions examples/src/AutoSend/AutoSend.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import { ModeLib } from "erc7579/lib/ModeLib.sol";
import { ExecutionLib } from "erc7579/lib/ExecutionLib.sol";

contract AutoSendSessionKey is ERC7579ExecutorBase, SessionKeyBase {
/*//////////////////////////////////////////////////////////////////////////
CONSTANTS & STORAGE
//////////////////////////////////////////////////////////////////////////*/

struct ExecutorAccess {
address sessionKeySigner;
address token;
Expand All @@ -27,6 +31,10 @@ contract AutoSendSessionKey is ERC7579ExecutorBase, SessionKeyBase {

mapping(address account => mapping(address token => SpentLog)) internal _log;

/*//////////////////////////////////////////////////////////////////////////
CONFIG
//////////////////////////////////////////////////////////////////////////*/

function encode(ExecutorAccess memory transaction) public pure returns (bytes memory) {
return abi.encode(transaction);
}
Expand All @@ -35,6 +43,26 @@ contract AutoSendSessionKey is ERC7579ExecutorBase, SessionKeyBase {
return _log[account][token];
}

function onInstall(bytes calldata data) external override {
(address[] memory tokens, SpentLog[] memory log) = abi.decode(data, (address[], SpentLog[]));

for (uint256 i; i < tokens.length; i++) {
_log[msg.sender][tokens[i]] = log[i];
}
}

function onUninstall(bytes calldata data) external override {
// Todo
}

function isInitialized(address smartAccount) external view returns (bool) {
// Todo
}

/*//////////////////////////////////////////////////////////////////////////
MODULE LOGIC
//////////////////////////////////////////////////////////////////////////*/

function autoSend(Params calldata params) external {
IERC7579Account smartAccount = IERC7579Account(msg.sender);

Expand Down Expand Up @@ -93,22 +121,14 @@ contract AutoSendSessionKey is ERC7579ExecutorBase, SessionKeyBase {
return access.sessionKeySigner;
}

function onInstall(bytes calldata data) external override {
(address[] memory tokens, SpentLog[] memory log) = abi.decode(data, (address[], SpentLog[]));

for (uint256 i; i < tokens.length; i++) {
_log[msg.sender][tokens[i]] = log[i];
}
}

function onUninstall(bytes calldata data) external override { }
/*//////////////////////////////////////////////////////////////////////////
METADATA
//////////////////////////////////////////////////////////////////////////*/

function isModuleType(uint256 typeID) external pure override returns (bool) {
return typeID == TYPE_EXECUTOR;
}

function isInitialized(address smartAccount) external view returns (bool) { }

function name() external pure virtual returns (string memory) {
return "AutoSend";
}
Expand Down
40 changes: 27 additions & 13 deletions examples/src/ColdStorage/ColdStorageExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ pragma solidity ^0.8.23;
import { IERC7579Account } from "modulekit/src/Accounts.sol";
import { ERC7579ExecutorBase } from "modulekit/src/Modules.sol";
import { ModeLib } from "erc7579/lib/ModeLib.sol";
import { ExecutionLib } from "erc7579/lib/ExecutionLib.sol";

contract ColdStorageExecutor is ERC7579ExecutorBase {
/*//////////////////////////////////////////////////////////////////////////
CONSTANTS & STORAGE
//////////////////////////////////////////////////////////////////////////*/

error UnauthorizedAccess();

mapping(address subAccount => address owner) private _subAccountOwner;

function executeOnSubAccount(address subAccount, bytes calldata callData) external payable {
if (msg.sender != _subAccountOwner[subAccount]) {
revert UnauthorizedAccess();
}

IERC7579Account(subAccount).executeFromExecutor(ModeLib.encodeSimpleSingle(), callData);
}

/*//////////////////////////////////////////////////////////////////////////
CONFIG
//////////////////////////////////////////////////////////////////////////*/
function onInstall(bytes calldata data) external override {
address owner = address(bytes20(data[0:20]));
_subAccountOwner[msg.sender] = owner;
Expand All @@ -28,11 +26,27 @@ contract ColdStorageExecutor is ERC7579ExecutorBase {
delete _subAccountOwner[msg.sender];
}

function isModuleType(uint256 typeID) external pure override returns (bool) {
return typeID == TYPE_EXECUTOR;
}

function isInitialized(address smartAccount) external view returns (bool) {
return _subAccountOwner[smartAccount] != address(0);
}

/*//////////////////////////////////////////////////////////////////////////
MODULE LOGIC
//////////////////////////////////////////////////////////////////////////*/

function executeOnSubAccount(address subAccount, bytes calldata callData) external payable {
if (msg.sender != _subAccountOwner[subAccount]) {
revert UnauthorizedAccess();
}

IERC7579Account(subAccount).executeFromExecutor(ModeLib.encodeSimpleSingle(), callData);
}

/*//////////////////////////////////////////////////////////////////////////
METADATA
//////////////////////////////////////////////////////////////////////////*/

function isModuleType(uint256 typeID) external pure override returns (bool) {
return typeID == TYPE_EXECUTOR;
}
}
86 changes: 54 additions & 32 deletions examples/src/ColdStorage/ColdStorageHook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import { ERC7579HookDestruct } from "modulekit/src/modules/ERC7579HookDestruct.s
import { Execution } from "modulekit/src/Accounts.sol";

contract ColdStorageHook is ERC7579HookDestruct {
/*//////////////////////////////////////////////////////////////////////////
CONSTANTS & STORAGE
//////////////////////////////////////////////////////////////////////////*/

error UnsupportedExecution();
error UnauthorizedAccess();
error InvalidExecutionHash(bytes32 executionHash);
Expand Down Expand Up @@ -38,6 +42,24 @@ contract ColdStorageHook is ERC7579HookDestruct {
address indexed subAccount, address target, uint256 value, bytes callData
);

/*//////////////////////////////////////////////////////////////////////////
CONFIG
//////////////////////////////////////////////////////////////////////////*/

function onInstall(bytes calldata data) external override {
VaultConfig storage _config = vaultConfig[msg.sender];
(_config.waitPeriod, _config.owner) = abi.decode(data, (uint128, address));
}

function onUninstall(bytes calldata data) external override {
delete vaultConfig[msg.sender].waitPeriod;
delete vaultConfig[msg.sender].owner;
}

function isInitialized(address smartAccount) external view returns (bool) {
return vaultConfig[smartAccount].owner != address(0);
}

function checkHash(
address account,
Execution calldata exec
Expand All @@ -51,22 +73,21 @@ contract ColdStorageHook is ERC7579HookDestruct {
(success, entry) = executions[account].tryGet(executionHash);
}

function _getTokenTxReceiver(bytes calldata callData)
internal
pure
returns (address receiver)
{
bytes4 functionSig = bytes4(callData[0:4]);
bytes calldata params = callData[4:];
if (functionSig == IERC20.transfer.selector) {
(receiver,) = abi.decode(params, (address, uint256));
} else if (functionSig == IERC20.transferFrom.selector) {
(, receiver,) = abi.decode(params, (address, address, uint256));
} else if (functionSig == IERC721.transferFrom.selector) {
(, receiver,) = abi.decode(params, (address, address, uint256));
function setWaitPeriod(uint256 waitPeriod) external {
if (waitPeriod == 0) {
revert("Wait period cannot be 0");
}
vaultConfig[msg.sender].waitPeriod = uint128(waitPeriod);
}

function getLockTime(address subAccount) public view returns (uint256) {
return vaultConfig[subAccount].waitPeriod;
}

/*//////////////////////////////////////////////////////////////////////////
MODULE LOGIC
//////////////////////////////////////////////////////////////////////////*/

/**
* Function that must be triggered from subaccount.
* requests an execution to happen in the future
Expand Down Expand Up @@ -104,12 +125,9 @@ contract ColdStorageHook is ERC7579HookDestruct {
emit ExecutionRequested(msg.sender, _exec.target, _exec.value, _exec.callData, executeAfter);
}

function setWaitPeriod(uint256 waitPeriod) external {
if (waitPeriod == 0) {
revert("Wait period cannot be 0");
}
vaultConfig[msg.sender].waitPeriod = uint128(waitPeriod);
}
/*//////////////////////////////////////////////////////////////////////////
INTERNAL
//////////////////////////////////////////////////////////////////////////*/

function _execDigest(
address to,
Expand All @@ -136,14 +154,20 @@ contract ColdStorageHook is ERC7579HookDestruct {
digest = keccak256(abi.encodePacked(to, value, callData));
}

function onInstall(bytes calldata data) external override {
VaultConfig storage _config = vaultConfig[msg.sender];
(_config.waitPeriod, _config.owner) = abi.decode(data, (uint128, address));
}

function onUninstall(bytes calldata data) external override {
delete vaultConfig[msg.sender].waitPeriod;
delete vaultConfig[msg.sender].owner;
function _getTokenTxReceiver(bytes calldata callData)
internal
pure
returns (address receiver)
{
bytes4 functionSig = bytes4(callData[0:4]);
bytes calldata params = callData[4:];
if (functionSig == IERC20.transfer.selector) {
(receiver,) = abi.decode(params, (address, uint256));
} else if (functionSig == IERC20.transferFrom.selector) {
(, receiver,) = abi.decode(params, (address, address, uint256));
} else if (functionSig == IERC721.transferFrom.selector) {
(, receiver,) = abi.decode(params, (address, address, uint256));
}
}

function onPostCheck(bytes calldata hookData)
Expand Down Expand Up @@ -263,9 +287,9 @@ contract ColdStorageHook is ERC7579HookDestruct {
revert UnsupportedExecution();
}

function getLockTime(address subAccount) public view returns (uint256) {
return vaultConfig[subAccount].waitPeriod;
}
/*//////////////////////////////////////////////////////////////////////////
METADATA
//////////////////////////////////////////////////////////////////////////*/

function version() external pure virtual returns (string memory) {
return "1.0.0";
Expand All @@ -278,6 +302,4 @@ contract ColdStorageHook is ERC7579HookDestruct {
function isModuleType(uint256 isType) external pure virtual override returns (bool) {
return isType == TYPE_HOOK;
}

function isInitialized(address smartAccount) external view returns (bool) { }
}
Loading

0 comments on commit 6ced63e

Please sign in to comment.