Skip to content

Commit

Permalink
Merge pull request #147 from morpho-labs/refactor/extsload
Browse files Browse the repository at this point in the history
feat(extsload): expose generic storage getter
  • Loading branch information
MathisGD authored Jul 25, 2023
2 parents 5d00928 + 8042549 commit 98d3132
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Blue.sol
Original file line number Diff line number Diff line change
Expand Up @@ -300,4 +300,21 @@ contract Blue {

return collateralValue.mulWadDown(market.lltv) >= borrowValue;
}

// Storage view.

function extsload(bytes32[] calldata slots) external view returns (bytes32[] memory res) {
uint256 nSlots = slots.length;

res = new bytes32[](nSlots);

for (uint256 i; i < nSlots;) {
bytes32 slot = slots[i++];

/// @solidity memory-safe-assembly
assembly {
mstore(add(res, mul(i, 32)), sload(slot))
}
}
}
}
16 changes: 16 additions & 0 deletions test/forge/Blue.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,22 @@ contract BlueTest is Test {

vm.stopPrank();
}

function testExtsLoad(uint256 slot, bytes32 value0) public {
bytes32[] memory slots = new bytes32[](2);
slots[0] = bytes32(slot);
slots[1] = bytes32(slot / 2);

bytes32 value1 = keccak256(abi.encode(value0));
vm.store(address(blue), slots[0], value0);
vm.store(address(blue), slots[1], value1);

bytes32[] memory values = blue.extsload(slots);

assertEq(values.length, 2, "values.length");
assertEq(values[0], slot > 0 ? value0 : value1, "value0");
assertEq(values[1], value1, "value1");
}
}

function neq(Market memory a, Market memory b) pure returns (bool) {
Expand Down

0 comments on commit 98d3132

Please sign in to comment.