Skip to content

Commit a826a08

Browse files
committed
feat(erc2330): add erc2330 alternative
1 parent 5a7ae7d commit a826a08

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

src/ERC2330.sol

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// SPDX-License-Identifier: AGPL-3.0-only
2+
pragma solidity ^0.8.0;
3+
4+
import {IERC2330} from "src/interfaces/IERC2330.sol";
5+
6+
/// @dev Gas-optimized extsload getters to allow anyone to read storage from this contract.
7+
/// Enables the benefit of https://eips.ethereum.org/EIPS/eip-2330 without requiring changes to the execution layer.
8+
contract ERC2330 is IERC2330 {
9+
/* EXTERNAL */
10+
11+
/// @inheritdoc IERC2330
12+
function extsload(bytes32 slot) external view returns (bytes32 value) {
13+
/// @solidity memory-safe-assembly
14+
assembly {
15+
value := sload(slot)
16+
}
17+
}
18+
19+
/// @inheritdoc IERC2330
20+
function extsload(bytes32 startSlot, uint256 nSlots) external view returns (bytes memory value) {
21+
value = new bytes(32 * nSlots);
22+
23+
/// @solidity memory-safe-assembly
24+
assembly {
25+
for { let i := 0 } lt(i, nSlots) { i := add(i, 1) } {
26+
mstore(add(value, mul(add(i, 1), 32)), sload(add(startSlot, i)))
27+
}
28+
}
29+
}
30+
}

src/interfaces/IERC2330.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: AGPL-3.0-only
2+
pragma solidity ^0.8.0;
3+
4+
interface IERC2330 {
5+
/* FUNCTIONS */
6+
7+
/// @dev Returns the 32-bytes value stored in this contract, at the given storage slot.
8+
function extsload(bytes32 slot) external view returns (bytes32 value);
9+
10+
/// @dev Returns the `nSlots` 32-bytes values stored in this contract, starting from the given start slot.
11+
function extsload(bytes32 startSlot, uint256 nSlots) external view returns (bytes memory value);
12+
}

test/TestERC2330.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: AGPL-3.0-only
2+
pragma solidity ^0.8.0;
3+
4+
import {IERC2330} from "src/interfaces/IERC2330.sol";
5+
6+
import {ERC2330Mock} from "test/mocks/ERC2330Mock.sol";
7+
8+
import "forge-std/Test.sol";
9+
10+
contract TestERC2330 is Test {
11+
ERC2330Mock mock;
12+
13+
function setUp() public {
14+
mock = new ERC2330Mock();
15+
}
16+
17+
function testExtsload() public {
18+
assertEq(
19+
mock.extsload(0x0000000000000000000000000000000000000000000000000000000000000000),
20+
0x0000000000000000000000000000000100000000000000000000000000000001
21+
);
22+
assertEq(
23+
mock.extsload(0x0000000000000000000000000000000000000000000000000000000000000001),
24+
0x0000000000000000000000000000000000000000000000000000000000000002
25+
);
26+
}
27+
28+
function testExtsloadMultiple() public {
29+
(bytes32 var1, bytes32 var2) = abi.decode(
30+
mock.extsload(0x0000000000000000000000000000000000000000000000000000000000000000, 2), (bytes32, bytes32)
31+
);
32+
33+
assertEq(var1, 0x0000000000000000000000000000000100000000000000000000000000000001);
34+
assertEq(var2, 0x0000000000000000000000000000000000000000000000000000000000000002);
35+
}
36+
}

test/mocks/ERC2330Mock.sol

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: AGPL-3.0-only
2+
pragma solidity ^0.8.0;
3+
4+
import {ERC2330} from "src/ERC2330.sol";
5+
6+
contract ERC2330Mock is ERC2330 {
7+
uint128 private _var11;
8+
uint128 private _var12;
9+
uint256 private _var2;
10+
11+
constructor() {
12+
_var11 = 1;
13+
_var12 = 1;
14+
_var2 = 2;
15+
}
16+
}

0 commit comments

Comments
 (0)