Skip to content

Commit 6b18aa6

Browse files
authored
factory : factory staker added (#38)
1 parent 9d2f89d commit 6b18aa6

5 files changed

Lines changed: 96 additions & 55 deletions

File tree

script/Deploy.s.sol

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
pragma solidity ^0.8.0;
32

43
import "forge-std/Script.sol";
@@ -11,9 +10,8 @@ contract DeployDeterministic is Script {
1110

1211
function run() external {
1312
vm.startBroadcast(DEPLOYER);
14-
Kernel kernel = new Kernel{salt:0}(IEntryPoint(payable(ENTRYPOINT_0_7_ADDR)));
13+
Kernel kernel = new Kernel{salt: 0}(IEntryPoint(payable(ENTRYPOINT_0_7_ADDR)));
1514
console.log("Kernel :", address(kernel));
1615
vm.stopBroadcast();
1716
}
1817
}
19-

src/Kernel.sol

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
256256
function isValidSignature(bytes32 hash, bytes calldata signature) external view override returns (bytes4) {
257257
ValidationStorage storage vs = _validationStorage();
258258
(ValidationId vId, bytes calldata sig) = ValidatorLib.decodeSignature(signature);
259-
console.logBytes(signature);
260-
console.log("signatureLength : ", signature.length);
261-
console.logBytes(sig);
262-
console.log("sigLength : ", sig.length);
263259
if (ValidatorLib.getType(vId) == VALIDATION_TYPE_SUDO) {
264260
vId = vs.rootValidator;
265261
}

src/factory/FactoryStaker.sol

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
pragma solidity ^0.8.0;
2+
3+
import "./KernelFactory.sol";
4+
import "../interfaces/IEntryPoint.sol";
5+
import "solady/auth/Ownable.sol";
6+
7+
contract FactoryStaker is Ownable {
8+
mapping(KernelFactory => bool) public approved;
9+
10+
error NotApprovedFactory();
11+
12+
constructor(address _owner) {
13+
_initializeOwner(_owner);
14+
}
15+
16+
function deployWithFactory(KernelFactory factory, bytes calldata createData, bytes32 salt)
17+
external
18+
payable
19+
returns (address)
20+
{
21+
if (!approved[factory]) {
22+
revert NotApprovedFactory();
23+
}
24+
return factory.createAccount(createData, salt);
25+
}
26+
27+
function approveFactory(KernelFactory factory, bool approval) external payable onlyOwner {
28+
approved[factory] = approval;
29+
}
30+
31+
function stake(IEntryPoint entryPoint, uint32 unstakeDelay) external payable onlyOwner {
32+
entryPoint.addStake{value: msg.value}(unstakeDelay);
33+
}
34+
35+
function unlockStake(IEntryPoint entryPoint) external payable onlyOwner {
36+
entryPoint.unlockStake();
37+
}
38+
39+
function withdrawStake(IEntryPoint entryPoint, address payable recipient) external payable onlyOwner {
40+
entryPoint.withdrawStake(recipient);
41+
}
42+
}

src/factory/KernelFactory.sol

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
pragma solidity ^0.8.0;
2+
3+
import {LibClone} from "solady/utils/LibClone.sol";
4+
5+
contract KernelFactory {
6+
error InitializeError();
7+
8+
address public immutable implementation;
9+
10+
constructor(address _impl) {
11+
implementation = _impl;
12+
}
13+
14+
function createAccount(bytes calldata data, bytes32 salt) public payable returns (address) {
15+
bytes32 actualSalt = keccak256(abi.encodePacked(data, salt));
16+
(bool alreadyDeployed, address account) =
17+
LibClone.createDeterministicERC1967(msg.value, implementation, actualSalt);
18+
if (!alreadyDeployed) {
19+
(bool success,) = account.call(data);
20+
if (!success) {
21+
revert InitializeError();
22+
}
23+
}
24+
return account;
25+
}
26+
27+
function getAddress(bytes calldata data, bytes32 salt) public view virtual returns (address) {
28+
bytes32 actualSalt = keccak256(abi.encodePacked(data, salt));
29+
return LibClone.predictDeterministicAddressERC1967(implementation, actualSalt, address(this));
30+
}
31+
}

src/sdk/TestBase/KernelTestBase.sol

Lines changed: 22 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pragma solidity ^0.8.0;
33

44
import "src/Kernel.sol";
5+
import "src/factory/KernelFactory.sol";
56
import "forge-std/Test.sol";
67
import "src/mock/MockValidator.sol";
78
import "src/mock/MockPolicy.sol";
@@ -13,34 +14,6 @@ import "src/mock/MockFallback.sol";
1314
import "src/core/ValidationManager.sol";
1415
import "./erc4337Util.sol";
1516

16-
contract SimpleProxy {
17-
bytes32 constant IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
18-
19-
constructor(address _target) {
20-
assembly {
21-
sstore(IMPLEMENTATION_SLOT, _target)
22-
}
23-
}
24-
25-
function _getImplementation() internal view returns (address target) {
26-
bytes32 slot = IMPLEMENTATION_SLOT;
27-
assembly {
28-
target := sload(slot)
29-
}
30-
}
31-
32-
receive() external payable {
33-
(bool success,) = _getImplementation().delegatecall("");
34-
require(success, "delegatecall failed");
35-
}
36-
37-
fallback(bytes calldata) external payable returns (bytes memory) {
38-
(bool success, bytes memory ret) = _getImplementation().delegatecall(msg.data);
39-
require(success, "delegatecall failed");
40-
return ret;
41-
}
42-
}
43-
4417
contract MockCallee {
4518
uint256 public value;
4619

@@ -51,6 +24,7 @@ contract MockCallee {
5124

5225
abstract contract KernelTestBase is Test {
5326
Kernel kernel;
27+
KernelFactory factory;
5428
IEntryPoint entrypoint;
5529
ValidationId rootValidation;
5630

@@ -88,22 +62,36 @@ abstract contract KernelTestBase is Test {
8862
// todo selectorData
8963

9064
modifier whenInitialized() {
91-
kernel.initialize(
92-
rootValidation, rootValidationConfig.hook, rootValidationConfig.validatorData, rootValidationConfig.hookData
65+
bytes memory initData = abi.encodeWithSelector(
66+
Kernel.initialize.selector,
67+
rootValidation,
68+
rootValidationConfig.hook,
69+
rootValidationConfig.validatorData,
70+
rootValidationConfig.hookData
9371
);
72+
address deployed = factory.createAccount(initData, bytes32(0));
73+
assertEq(deployed, address(kernel));
9474
assertEq(kernel.currentNonce(), 1);
9575
_;
9676
}
9777

9878
function setUp() public {
9979
entrypoint = IEntryPoint(EntryPointLib.deploy());
10080
Kernel impl = new Kernel(entrypoint);
81+
factory = new KernelFactory(address(impl));
10182
callee = new MockCallee();
102-
kernel = Kernel(payable(address(new SimpleProxy(address(impl)))));
10383
mockHook = new MockHook();
10484
_setRootValidationConfig();
10585
_setEnableValidatorConfig();
10686
_setEnablePermissionConfig();
87+
bytes memory initData = abi.encodeWithSelector(
88+
Kernel.initialize.selector,
89+
rootValidation,
90+
rootValidationConfig.hook,
91+
rootValidationConfig.validatorData,
92+
rootValidationConfig.hookData
93+
);
94+
kernel = Kernel(payable(factory.getAddress(initData, bytes32(0))));
10795
}
10896

10997
// things to override on test
@@ -133,20 +121,6 @@ abstract contract KernelTestBase is Test {
133121
permissionConfig.signerData = "signer";
134122
}
135123

136-
// kernel initialize scenario
137-
function testInitialize() external {
138-
ValidationId vId = ValidatorLib.validatorToIdentifier(mockValidator);
139-
140-
kernel.initialize(vId, IHook(address(0)), hex"", hex"");
141-
assertTrue(kernel.rootValidator() == vId);
142-
ValidationManager.ValidationConfig memory config;
143-
config = kernel.validationConfig(vId);
144-
assertEq(config.nonce, 1);
145-
assertEq(address(config.hook), address(1));
146-
assertEq(mockValidator.isInitialized(address(kernel)), true);
147-
assertEq(kernel.currentNonce(), 1);
148-
}
149-
150124
// root validator cases
151125
function _rootValidatorFailurePreCondition() internal virtual {
152126
mockValidator.sudoSetSuccess(false);
@@ -173,7 +147,7 @@ abstract contract KernelTestBase is Test {
173147
assertEq(0, callee.value());
174148
}
175149

176-
function getRootSignature(bytes32 hash) internal returns(bytes memory) {
150+
function getRootSignature(bytes32 hash) internal returns (bytes memory) {
177151
return abi.encodePacked("rootSig", hash);
178152
}
179153

@@ -308,7 +282,7 @@ abstract contract KernelTestBase is Test {
308282
abi.encodePacked(kernel.execute.selector),
309283
getEnableSig(bytes32(0), true),
310284
getValidatorSig(op, true)
311-
)
285+
)
312286
});
313287
}
314288

@@ -338,7 +312,7 @@ abstract contract KernelTestBase is Test {
338312
abi.encodePacked(kernel.execute.selector),
339313
getEnableSig(bytes32(0), true),
340314
getPermissionSig(op, true)
341-
)
315+
)
342316
});
343317
}
344318

0 commit comments

Comments
 (0)