Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/on received #108

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/Kernel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
_;
}

modifier onlyEntryPointOrSelf() {
if (msg.sender != address(entrypoint) && msg.sender != address(this)) {
revert InvalidCaller();
}
_;
}

modifier onlyEntryPointOrSelfOrRoot() {
IValidator validator = ValidatorLib.getValidator(_validationStorage().rootValidator);
if (
Expand Down Expand Up @@ -133,6 +126,22 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
emit Received(msg.sender, msg.value);
}

function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
return this.onERC721Received.selector;
}

function onERC1155Received(address, address, uint256, uint256, bytes calldata) external pure returns (bytes4) {
return this.onERC1155Received.selector;
}

function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
external
pure
returns (bytes4)
{
return this.onERC1155BatchReceived.selector;
}

fallback() external payable {
SelectorConfig memory config = _selectorConfig(msg.sig);
bool success;
Expand Down
20 changes: 20 additions & 0 deletions src/mock/MockERC1155.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "solady/tokens/ERC1155.sol";

contract MockERC1155 is ERC1155 {
function test_ignore() public {}

function uri(uint256) public pure override returns (string memory) {
return "https://example.com";
}

function mint(address to, uint256 id, uint256 amount, bytes memory data) public {
_mint(to, id, amount, data);
}

function batchMint(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public {
_batchMint(to, ids, amounts, data);
}
}
22 changes: 22 additions & 0 deletions src/mock/MockERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "solady/tokens/ERC20.sol";

contract MockERC20 is ERC20 {
constructor() ERC20() {}

function test_ignore() public {}

function name() public pure override returns (string memory) {
return "MockERC20";
}

function symbol() public pure override returns (string memory) {
return "MOCK";
}

function mint(address _to, uint256 _amount) external {
_mint(_to, _amount);
}
}
30 changes: 30 additions & 0 deletions src/mock/MockERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "solady/tokens/ERC721.sol";

contract MockERC721 is ERC721 {
constructor() ERC721() {}

function test_ignore() public {}

function name() public pure override returns (string memory) {
return "MockERC721";
}

function symbol() public pure override returns (string memory) {
return "MOCK";
}

function tokenURI(uint256) public pure override returns (string memory) {
return "";
}

function mint(address _to, uint256 _id) external {
_mint(_to, _id);
}

function safeMint(address _to, uint256 _id) external {
_safeMint(_to, _id);
}
}
28 changes: 28 additions & 0 deletions src/sdk/KernelTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import "../mock/MockAction.sol";
import "../mock/MockHook.sol";
import "../mock/MockExecutor.sol";
import "../mock/MockFallback.sol";
import "../mock/MockERC20.sol";
import "../mock/MockERC721.sol";
import "../mock/MockERC1155.sol";
import "../core/ValidationManager.sol";
import "./TestBase/erc4337Util.sol";

Expand Down Expand Up @@ -43,6 +46,9 @@ abstract contract KernelTestBase is Test {
MockHook mockHook;
MockFallback mockFallback;
MockExecutor mockExecutor;
MockERC20 mockERC20;
MockERC721 mockERC721;
MockERC1155 mockERC1155;

IValidator enabledValidator;
EnableValidatorConfig validationConfig;
Expand Down Expand Up @@ -296,6 +302,9 @@ abstract contract KernelTestBase is Test {
mockHook = new MockHook();
mockFallback = new MockFallback();
mockExecutor = new MockExecutor();
mockERC20 = new MockERC20();
mockERC721 = new MockERC721();
mockERC1155 = new MockERC1155();
_setRootValidationConfig();
_setEnableValidatorConfig();
_setEnablePermissionConfig();
Expand All @@ -315,6 +324,25 @@ abstract contract KernelTestBase is Test {
entrypoint.handleOps(ops, payable(address(0xdeadbeef)));
}

function test_receive() external whenInitialized {
vm.expectEmit(false, false, false, true, address(kernel));
emit Kernel.Received(address(this), 1);
(bool success,) = address(kernel).call{value: 1}(hex"");
require(success, "eth transfer failed");

mockERC721.mint(address(kernel), 100);
mockERC721.safeMint(address(kernel), 999);

mockERC1155.mint(address(kernel), 100, 1, hex"");
uint256[] memory ids = new uint256[](2);
uint256[] memory amounts = new uint256[](2);
ids[0] = 200;
ids[1] = 201;
amounts[0] = 1;
amounts[1] = 1000;
mockERC1155.batchMint(address(kernel), ids, amounts, hex"");
}

function initData() internal view returns (bytes memory) {
return abi.encodeWithSelector(
Kernel.initialize.selector,
Expand Down
2 changes: 0 additions & 2 deletions src/sdk/TestBase/ActionTestBase.sol

This file was deleted.

2 changes: 0 additions & 2 deletions src/sdk/TestBase/ExecutorTestBase.sol

This file was deleted.

2 changes: 0 additions & 2 deletions src/sdk/TestBase/FallbackTestBase.sol

This file was deleted.

2 changes: 0 additions & 2 deletions src/sdk/TestBase/HookTestBase.sol

This file was deleted.

2 changes: 0 additions & 2 deletions src/sdk/TestBase/PolicyTestBase.sol

This file was deleted.

2 changes: 0 additions & 2 deletions src/sdk/TestBase/SignerTestBase.sol

This file was deleted.

2 changes: 0 additions & 2 deletions src/sdk/TestBase/ValidatorTestBase.sol

This file was deleted.