Skip to content

Commit

Permalink
chore: smart wallet contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
kostyamospan committed May 28, 2024
1 parent 0550469 commit cd7b643
Show file tree
Hide file tree
Showing 9 changed files with 2,842 additions and 560 deletions.
172 changes: 0 additions & 172 deletions contracts/SmartAccount.sol

This file was deleted.

43 changes: 43 additions & 0 deletions contracts/SmartWalletFactoryV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.23;

import "@openzeppelin/contracts/proxy/Clones.sol";

Check warning on line 4 in contracts/SmartWalletFactoryV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

global import of path @openzeppelin/contracts/proxy/Clones.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "./SmartWalletV1.sol";

Check warning on line 5 in contracts/SmartWalletFactoryV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

global import of path ./SmartWalletV1.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

contract SmartWaletFactoryV1 {
address public immutable implementation;

Check warning on line 8 in contracts/SmartWalletFactoryV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

Immutable variables name are set to be in capitalized SNAKE_CASE

constructor(address _implementation) {
implementation = _implementation;
}

function createWallet(
address[] calldata initAllowlist
) external returns (address) {
SmartWalletV1 wallet = SmartWalletV1(Clones.clone(implementation));
wallet.initialize(msg.sender, initAllowlist);
return address(wallet);
}

function create2Wallet(
address[] calldata initAllowlist,
bytes32 salt
) external returns (address) {
SmartWalletV1 wallet = SmartWalletV1(
Clones.cloneDeterministic(implementation, salt)
);
wallet.initialize(msg.sender, initAllowlist);
return address(wallet);
}

function predictCreate2Wallet(
bytes32 salt
) external view returns (address) {
return
Clones.predictDeterministicAddress(
implementation,
salt,
address(this)
);
}
}
150 changes: 150 additions & 0 deletions contracts/SmartWalletV1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.23;

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

Check warning on line 4 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

global import of path @openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
import "@openzeppelin/contracts/utils/Address.sol";

Check warning on line 5 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

global import of path @openzeppelin/contracts/utils/Address.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)
// import "@chainlink/contracts/src/v0.8/.sol";
import "./libraries/EnumerableMap.sol";

Check warning on line 7 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

global import of path ./libraries/EnumerableMap.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

import "hardhat/console.sol";

Check failure on line 9 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

Unexpected import of console file

Check warning on line 9 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

global import of path hardhat/console.sol is not allowed. Specify names to import individually or bind all exports of the module into a name (import "path" as Name)

contract SmartWalletV1 is OwnableUpgradeable {
using EnumerableMap for EnumerableMap.UintToAutoExecuteMap;

mapping(address => bool) public allowlist;
mapping(address => mapping(bytes4 => bool)) public blacklistedFunctions;
EnumerableMap.UintToAutoExecuteMap autoExecutesMap;

Check warning on line 16 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

Explicitly mark visibility of state

Check failure on line 16 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

'autoExecutesMap' should start with _
uint256 public autoExecuteCounter;

modifier onlyAllowlist() {
require(allowlist[msg.sender], "SW: not a blacklister");

Check warning on line 20 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

GC: Use Custom Errors instead of require statements
_;
}

constructor() {
_disableInitializers();
}

function initialize(
address _owner,
address[] calldata _initialAllowList
) external initializer {
__Ownable_init(_owner);

for (uint i; i < _initialAllowList.length; i++) {

Check warning on line 34 in contracts/SmartWalletV1.sol

View workflow job for this annotation

GitHub Actions / codestyle

Rule is set with explicit type [var/s: uint]
allowlist[_initialAllowList[i]] = true;
}
}

function blacklist(
address[] calldata tos,
bytes4[] calldata funcSelectors
) external onlyAllowlist {
require(tos.length == funcSelectors.length, "SW: mismatch arrays");
for (uint i; i < tos.length; i++) {
blacklistedFunctions[tos[i]][funcSelectors[i]] = true;
}
}

function removeFromBlacklist(
address[] calldata tos,
bytes4[] calldata funcSelectors
) external onlyAllowlist {
require(tos.length == funcSelectors.length, "SW: mismatch arrays");
for (uint i; i < tos.length; i++) {
blacklistedFunctions[tos[i]][funcSelectors[i]] = false;
}
}

function addToAutoExecute(
address callback,
bytes calldata executeData,
address executeTo,
uint256 executeValue,
uint256 executeAfter
) external {
require(
msg.sender == owner() || allowlist[msg.sender],
"SW: invalid sender"
);

require(executeAfter > block.timestamp, "SW: invalid execute time");

AutoExecute memory data = AutoExecute({
id: ++autoExecuteCounter,
creator: msg.sender,
callback: callback,
executeData: executeData,
executeTo: executeTo,
executeValue: executeValue,
executeAfter: executeAfter
});

autoExecutesMap.set(data.id, data);
}

function addToAllowlist(address addr) public onlyOwner {
allowlist[addr] = true;
}

function removeFromAllowlist(address addr) public onlyOwner {
allowlist[addr] = false;
}

function execute(
address to,
uint256 callValue,
bytes calldata data
) public onlyOwner returns (bytes memory returnData) {
require(to != address(this), "SW: to cannot be this");
returnData = Address.functionCallWithValue(to, data, callValue);
}

function executeBatch(
address[] calldata tos,
uint256[] calldata callValues,
bytes[] calldata datas
) public onlyOwner returns (bytes[] memory returnDatas) {
require(
tos.length == callValues.length && tos.length == datas.length,
"SW: mismatch arrays"
);
returnDatas = new bytes[](tos.length);
for (uint i = 0; i < tos.length; i++) {
require(tos[i] != address(this), "SW: to cannot be this");
returnDatas[i] = Address.functionCallWithValue(
tos[i],
datas[i],
callValues[i]
);
}
}

function checkUpkeep(
bytes calldata
) external view returns (bool upkeepNeeded, bytes memory performData) {
uint256[] memory keys = autoExecutesMap.keys();

for (uint i; i < keys.length; i++) {
uint256 key = keys[i];
AutoExecute memory data = autoExecutesMap.get(key);

if (data.executeAfter >= block.timestamp)
return (true, abi.encode(data.id));
}
}

function performUpkeep(bytes calldata performData) external {
uint256 id = abi.decode(performData, (uint256));
AutoExecute memory data = autoExecutesMap.get(id);
require(
data.executeAfter >= block.timestamp,
"SW: to early to execute"
);

_executeUpkeep(data);
autoExecutesMap.remove(id);
}

function _executeUpkeep(AutoExecute memory upkeepData) private {}
}
Loading

0 comments on commit cd7b643

Please sign in to comment.