diff --git a/examples/multiple-accounts/packages/client/src/App.tsx b/examples/multiple-accounts/packages/client/src/App.tsx index f3d5e4bf4e..7a2119cd7b 100644 --- a/examples/multiple-accounts/packages/client/src/App.tsx +++ b/examples/multiple-accounts/packages/client/src/App.tsx @@ -72,6 +72,7 @@ export const App = () => { Caller + tx.sender Time { @@ -79,6 +80,7 @@ export const App = () => { calls.map((call) => ( {call.key.caller} + {call.value.sender} {timestamp2Str(Number(call.value.callTime))} )) diff --git a/examples/multiple-accounts/packages/contracts/mud.config.ts b/examples/multiple-accounts/packages/contracts/mud.config.ts index 3ba46294c4..5d3d4f0cb1 100644 --- a/examples/multiple-accounts/packages/contracts/mud.config.ts +++ b/examples/multiple-accounts/packages/contracts/mud.config.ts @@ -9,6 +9,7 @@ export default mudConfig({ }, valueSchema: { callTime: "uint256", + sender: "address", }, }, }, diff --git a/examples/multiple-accounts/packages/contracts/src/codegen/index.sol b/examples/multiple-accounts/packages/contracts/src/codegen/index.sol index 525fad9dae..bb6619582e 100644 --- a/examples/multiple-accounts/packages/contracts/src/codegen/index.sol +++ b/examples/multiple-accounts/packages/contracts/src/codegen/index.sol @@ -3,4 +3,4 @@ pragma solidity >=0.8.21; /* Autogenerated file. Do not edit manually. */ -import { LastCall, LastCallTableId } from "./tables/LastCall.sol"; +import { LastCall, LastCallData, LastCallTableId } from "./tables/LastCall.sol"; diff --git a/examples/multiple-accounts/packages/contracts/src/codegen/tables/LastCall.sol b/examples/multiple-accounts/packages/contracts/src/codegen/tables/LastCall.sol index f22ae9bd64..bd8c0c9dfd 100644 --- a/examples/multiple-accounts/packages/contracts/src/codegen/tables/LastCall.sol +++ b/examples/multiple-accounts/packages/contracts/src/codegen/tables/LastCall.sol @@ -26,9 +26,14 @@ ResourceId constant _tableId = ResourceId.wrap( ResourceId constant LastCallTableId = _tableId; FieldLayout constant _fieldLayout = FieldLayout.wrap( - 0x0020010020000000000000000000000000000000000000000000000000000000 + 0x0034020020140000000000000000000000000000000000000000000000000000 ); +struct LastCallData { + uint256 callTime; + address sender; +} + library LastCall { /** * @notice Get the table values' field layout. @@ -54,8 +59,9 @@ library LastCall { * @return _valueSchema The value schema for the table. */ function getValueSchema() internal pure returns (Schema) { - SchemaType[] memory _valueSchema = new SchemaType[](1); + SchemaType[] memory _valueSchema = new SchemaType[](2); _valueSchema[0] = SchemaType.UINT256; + _valueSchema[1] = SchemaType.ADDRESS; return SchemaLib.encode(_valueSchema); } @@ -74,8 +80,9 @@ library LastCall { * @return fieldNames An array of strings with the names of value fields. */ function getFieldNames() internal pure returns (string[] memory fieldNames) { - fieldNames = new string[](1); + fieldNames = new string[](2); fieldNames[0] = "callTime"; + fieldNames[1] = "sender"; } /** @@ -115,65 +122,178 @@ library LastCall { } /** - * @notice Get callTime. + * @notice Set callTime. */ - function get(address caller) internal view returns (uint256 callTime) { + function setCallTime(address caller, uint256 callTime) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(caller))); - bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); + StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((callTime)), _fieldLayout); } /** - * @notice Get callTime. + * @notice Set callTime. */ - function _get(address caller) internal view returns (uint256 callTime) { + function _setCallTime(address caller, uint256 callTime) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(caller))); - bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 0, _fieldLayout); - return (uint256(bytes32(_blob))); + StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((callTime)), _fieldLayout); } /** - * @notice Set callTime. + * @notice Get sender. */ - function setCallTime(address caller, uint256 callTime) internal { + function getSender(address caller) internal view returns (address sender) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(caller))); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((callTime)), _fieldLayout); + bytes32 _blob = StoreSwitch.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (address(bytes20(_blob))); } /** - * @notice Set callTime. + * @notice Get sender. */ - function _setCallTime(address caller, uint256 callTime) internal { + function _getSender(address caller) internal view returns (address sender) { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(caller))); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((callTime)), _fieldLayout); + bytes32 _blob = StoreCore.getStaticField(_tableId, _keyTuple, 1, _fieldLayout); + return (address(bytes20(_blob))); } /** - * @notice Set callTime. + * @notice Set sender. */ - function set(address caller, uint256 callTime) internal { + function setSender(address caller, address sender) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(caller))); - StoreSwitch.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((callTime)), _fieldLayout); + StoreSwitch.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((sender)), _fieldLayout); } /** - * @notice Set callTime. + * @notice Set sender. */ - function _set(address caller, uint256 callTime) internal { + function _setSender(address caller, address sender) internal { bytes32[] memory _keyTuple = new bytes32[](1); _keyTuple[0] = bytes32(uint256(uint160(caller))); - StoreCore.setStaticField(_tableId, _keyTuple, 0, abi.encodePacked((callTime)), _fieldLayout); + StoreCore.setStaticField(_tableId, _keyTuple, 1, abi.encodePacked((sender)), _fieldLayout); + } + + /** + * @notice Get the full data. + */ + function get(address caller) internal view returns (LastCallData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(caller))); + + (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreSwitch.getRecord( + _tableId, + _keyTuple, + _fieldLayout + ); + return decode(_staticData, _encodedLengths, _dynamicData); + } + + /** + * @notice Get the full data. + */ + function _get(address caller) internal view returns (LastCallData memory _table) { + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(caller))); + + (bytes memory _staticData, PackedCounter _encodedLengths, bytes memory _dynamicData) = StoreCore.getRecord( + _tableId, + _keyTuple, + _fieldLayout + ); + return decode(_staticData, _encodedLengths, _dynamicData); + } + + /** + * @notice Set the full data using individual values. + */ + function set(address caller, uint256 callTime, address sender) internal { + bytes memory _staticData = encodeStatic(callTime, sender); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(caller))); + + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); + } + + /** + * @notice Set the full data using individual values. + */ + function _set(address caller, uint256 callTime, address sender) internal { + bytes memory _staticData = encodeStatic(callTime, sender); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(caller))); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + + /** + * @notice Set the full data using the data struct. + */ + function set(address caller, LastCallData memory _table) internal { + bytes memory _staticData = encodeStatic(_table.callTime, _table.sender); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(caller))); + + StoreSwitch.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData); + } + + /** + * @notice Set the full data using the data struct. + */ + function _set(address caller, LastCallData memory _table) internal { + bytes memory _staticData = encodeStatic(_table.callTime, _table.sender); + + PackedCounter _encodedLengths; + bytes memory _dynamicData; + + bytes32[] memory _keyTuple = new bytes32[](1); + _keyTuple[0] = bytes32(uint256(uint160(caller))); + + StoreCore.setRecord(_tableId, _keyTuple, _staticData, _encodedLengths, _dynamicData, _fieldLayout); + } + + /** + * @notice Decode the tightly packed blob of static data using this table's field layout. + */ + function decodeStatic(bytes memory _blob) internal pure returns (uint256 callTime, address sender) { + callTime = (uint256(Bytes.slice32(_blob, 0))); + + sender = (address(Bytes.slice20(_blob, 32))); + } + + /** + * @notice Decode the tightly packed blobs using this table's field layout. + * @param _staticData Tightly packed static fields. + * + * + */ + function decode( + bytes memory _staticData, + PackedCounter, + bytes memory + ) internal pure returns (LastCallData memory _table) { + (_table.callTime, _table.sender) = decodeStatic(_staticData); } /** @@ -200,8 +320,8 @@ library LastCall { * @notice Tightly pack static (fixed length) data using this table's schema. * @return The static data, encoded into a sequence of bytes. */ - function encodeStatic(uint256 callTime) internal pure returns (bytes memory) { - return abi.encodePacked(callTime); + function encodeStatic(uint256 callTime, address sender) internal pure returns (bytes memory) { + return abi.encodePacked(callTime, sender); } /** @@ -210,8 +330,8 @@ library LastCall { * @return The lengths of the dynamic fields (packed into a single bytes32 value). * @return The dyanmic (variable length) data, encoded into a sequence of bytes. */ - function encode(uint256 callTime) internal pure returns (bytes memory, PackedCounter, bytes memory) { - bytes memory _staticData = encodeStatic(callTime); + function encode(uint256 callTime, address sender) internal pure returns (bytes memory, PackedCounter, bytes memory) { + bytes memory _staticData = encodeStatic(callTime, sender); PackedCounter _encodedLengths; bytes memory _dynamicData; diff --git a/examples/multiple-accounts/packages/contracts/src/systems/LastCallSystem.sol b/examples/multiple-accounts/packages/contracts/src/systems/LastCallSystem.sol index e40df89975..87344ea8a0 100644 --- a/examples/multiple-accounts/packages/contracts/src/systems/LastCallSystem.sol +++ b/examples/multiple-accounts/packages/contracts/src/systems/LastCallSystem.sol @@ -6,6 +6,6 @@ import { LastCall } from "../codegen/index.sol"; contract LastCallSystem is System { function newCall() public { - LastCall.set(_msgSender(), block.timestamp); + LastCall.set(_msgSender(), block.timestamp, tx.origin); } }