Skip to content

Commit 07d5d9e

Browse files
authored
Feat/on received (#108)
* feat: added erc721/erc1155 onreceived * added test case for receive
1 parent 00dd9e4 commit 07d5d9e

File tree

12 files changed

+116
-21
lines changed

12 files changed

+116
-21
lines changed

src/Kernel.sol

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
7575
_;
7676
}
7777

78-
modifier onlyEntryPointOrSelf() {
79-
if (msg.sender != address(entrypoint) && msg.sender != address(this)) {
80-
revert InvalidCaller();
81-
}
82-
_;
83-
}
84-
8578
modifier onlyEntryPointOrSelfOrRoot() {
8679
IValidator validator = ValidatorLib.getValidator(_validationStorage().rootValidator);
8780
if (
@@ -133,6 +126,22 @@ contract Kernel is IAccount, IAccountExecute, IERC7579Account, ValidationManager
133126
emit Received(msg.sender, msg.value);
134127
}
135128

129+
function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
130+
return this.onERC721Received.selector;
131+
}
132+
133+
function onERC1155Received(address, address, uint256, uint256, bytes calldata) external pure returns (bytes4) {
134+
return this.onERC1155Received.selector;
135+
}
136+
137+
function onERC1155BatchReceived(address, address, uint256[] calldata, uint256[] calldata, bytes calldata)
138+
external
139+
pure
140+
returns (bytes4)
141+
{
142+
return this.onERC1155BatchReceived.selector;
143+
}
144+
136145
fallback() external payable {
137146
SelectorConfig memory config = _selectorConfig(msg.sig);
138147
bool success;

src/mock/MockERC1155.sol

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import "solady/tokens/ERC1155.sol";
5+
6+
contract MockERC1155 is ERC1155 {
7+
function test_ignore() public {}
8+
9+
function uri(uint256) public pure override returns (string memory) {
10+
return "https://example.com";
11+
}
12+
13+
function mint(address to, uint256 id, uint256 amount, bytes memory data) public {
14+
_mint(to, id, amount, data);
15+
}
16+
17+
function batchMint(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data) public {
18+
_batchMint(to, ids, amounts, data);
19+
}
20+
}

src/mock/MockERC20.sol

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import "solady/tokens/ERC20.sol";
5+
6+
contract MockERC20 is ERC20 {
7+
constructor() ERC20() {}
8+
9+
function test_ignore() public {}
10+
11+
function name() public pure override returns (string memory) {
12+
return "MockERC20";
13+
}
14+
15+
function symbol() public pure override returns (string memory) {
16+
return "MOCK";
17+
}
18+
19+
function mint(address _to, uint256 _amount) external {
20+
_mint(_to, _amount);
21+
}
22+
}

src/mock/MockERC721.sol

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
import "solady/tokens/ERC721.sol";
5+
6+
contract MockERC721 is ERC721 {
7+
constructor() ERC721() {}
8+
9+
function test_ignore() public {}
10+
11+
function name() public pure override returns (string memory) {
12+
return "MockERC721";
13+
}
14+
15+
function symbol() public pure override returns (string memory) {
16+
return "MOCK";
17+
}
18+
19+
function tokenURI(uint256) public pure override returns (string memory) {
20+
return "";
21+
}
22+
23+
function mint(address _to, uint256 _id) external {
24+
_mint(_to, _id);
25+
}
26+
27+
function safeMint(address _to, uint256 _id) external {
28+
_safeMint(_to, _id);
29+
}
30+
}

src/sdk/KernelTestBase.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import "../mock/MockAction.sol";
1212
import "../mock/MockHook.sol";
1313
import "../mock/MockExecutor.sol";
1414
import "../mock/MockFallback.sol";
15+
import "../mock/MockERC20.sol";
16+
import "../mock/MockERC721.sol";
17+
import "../mock/MockERC1155.sol";
1518
import "../core/ValidationManager.sol";
1619
import "./TestBase/erc4337Util.sol";
1720

@@ -43,6 +46,9 @@ abstract contract KernelTestBase is Test {
4346
MockHook mockHook;
4447
MockFallback mockFallback;
4548
MockExecutor mockExecutor;
49+
MockERC20 mockERC20;
50+
MockERC721 mockERC721;
51+
MockERC1155 mockERC1155;
4652

4753
IValidator enabledValidator;
4854
EnableValidatorConfig validationConfig;
@@ -296,6 +302,9 @@ abstract contract KernelTestBase is Test {
296302
mockHook = new MockHook();
297303
mockFallback = new MockFallback();
298304
mockExecutor = new MockExecutor();
305+
mockERC20 = new MockERC20();
306+
mockERC721 = new MockERC721();
307+
mockERC1155 = new MockERC1155();
299308
_setRootValidationConfig();
300309
_setEnableValidatorConfig();
301310
_setEnablePermissionConfig();
@@ -315,6 +324,25 @@ abstract contract KernelTestBase is Test {
315324
entrypoint.handleOps(ops, payable(address(0xdeadbeef)));
316325
}
317326

327+
function test_receive() external whenInitialized {
328+
vm.expectEmit(false, false, false, true, address(kernel));
329+
emit Kernel.Received(address(this), 1);
330+
(bool success,) = address(kernel).call{value: 1}(hex"");
331+
require(success, "eth transfer failed");
332+
333+
mockERC721.mint(address(kernel), 100);
334+
mockERC721.safeMint(address(kernel), 999);
335+
336+
mockERC1155.mint(address(kernel), 100, 1, hex"");
337+
uint256[] memory ids = new uint256[](2);
338+
uint256[] memory amounts = new uint256[](2);
339+
ids[0] = 200;
340+
ids[1] = 201;
341+
amounts[0] = 1;
342+
amounts[1] = 1000;
343+
mockERC1155.batchMint(address(kernel), ids, amounts, hex"");
344+
}
345+
318346
function initData() internal view returns (bytes memory) {
319347
return abi.encodeWithSelector(
320348
Kernel.initialize.selector,

src/sdk/TestBase/ActionTestBase.sol

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/sdk/TestBase/ExecutorTestBase.sol

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/sdk/TestBase/FallbackTestBase.sol

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/sdk/TestBase/HookTestBase.sol

Lines changed: 0 additions & 2 deletions
This file was deleted.

src/sdk/TestBase/PolicyTestBase.sol

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)