Skip to main content

JBMetadataResolver

Git Source

Library to parse and create metadata to store {id: data} entries.

*Metadata are built as:

  • 32B of reserved space for the protocol
  • a lookup table Id: offset, defining the offset of the data for a given 4 bytes id. The offset fits 1 bytes, the ID 4 bytes. This table is padded to 32B.
  • the data for each id, padded to 32B each +-----------------------+ offset: 0 | 32B reserved | +-----------------------+ offset: 1 = end of first 32B | (ID1,offset1) | | (ID2,offset2) | | 0's padding | +-----------------------+ offset: offset1 = 1 + number of words taken by the padded table | id1 data1 | | 0's padding | +-----------------------+ offset: offset2 = offset1 + number of words taken by the data1 | id2 data2 | | 0's padding | +-----------------------+*

State Variables

ID_SIZE

uint256 constant ID_SIZE = 4;

ID_OFFSET_SIZE

uint256 constant ID_OFFSET_SIZE = 1;

WORD_SIZE

uint256 constant WORD_SIZE = 32;

TOTAL_ID_SIZE

uint256 constant TOTAL_ID_SIZE = 5;

NEXT_ID_OFFSET

uint256 constant NEXT_ID_OFFSET = 9;

RESERVED_SIZE

uint256 constant RESERVED_SIZE = 32;

MIN_METADATA_LENGTH

uint256 constant MIN_METADATA_LENGTH = 37;

Functions

addToMetadata

Add an {id: data} entry to an existing metadata. This is an append-only mechanism.

function addToMetadata(
bytes memory originalMetadata,
bytes4 idToAdd,
bytes memory dataToAdd
)
internal
pure
returns (bytes memory newMetadata);

Parameters

NameTypeDescription
originalMetadatabytesThe original metadata
idToAddbytes4The id to add
dataToAddbytesThe data to add

Returns

NameTypeDescription
newMetadatabytesThe new metadata with the entry added

createMetadata

Create the metadata for a list of {id:data}

Intended for offchain use (gas heavy)

function createMetadata(bytes4[] memory ids, bytes[] memory datas) internal pure returns (bytes memory metadata);

Parameters

NameTypeDescription
idsbytes4[]The list of ids
datasbytes[]The list of corresponding datas

Returns

NameTypeDescription
metadatabytesThe resulting metadata

getDataFor

Parse the metadata to find the data for a specific ID

Returns false and an empty bytes if no data is found

function getDataFor(bytes4 id, bytes memory metadata) internal pure returns (bool found, bytes memory targetData);

Parameters

NameTypeDescription
idbytes4The ID to find.
metadatabytesThe metadata to parse.

Returns

NameTypeDescription
foundboolWhether the {id:data} was found
targetDatabytesThe data for the ID (can be empty)

getId

Returns an unique id following a suggested format (xor(address(this), purpose name) where purpose name is a string giving context to the id (Permit2, quoteForSwap, etc)

function getId(string memory purpose) internal view returns (bytes4);

Parameters

NameTypeDescription
purposestringA string describing the purpose associated with the id

Returns

NameTypeDescription
<none>bytes4id The resulting ID.

getId

Returns an unique id following a suggested format (xor(address(this), purpose name) where purpose name is a string giving context to the id (Permit2, quoteForSwap, etc)

function getId(string memory purpose, address target) internal pure returns (bytes4);

Parameters

NameTypeDescription
purposestringA string describing the purpose associated with the id
targetaddressThe target which will use the metadata

Returns

NameTypeDescription
<none>bytes4id The resulting ID.

_sliceBytes

Slice bytes from a start index to an end index.

function _sliceBytes(bytes memory data, uint256 start, uint256 end) private pure returns (bytes memory slicedBytes);

Parameters

NameTypeDescription
databytesThe bytes array to slice
startuint256The start index to slice at.
enduint256The end index to slice at.

Errors

JBMetadataResolver_DataNotPadded

error JBMetadataResolver_DataNotPadded();

JBMetadataResolver_LengthMismatch

error JBMetadataResolver_LengthMismatch();

JBMetadataResolver_MetadataTooLong

error JBMetadataResolver_MetadataTooLong();

JBMetadataResolver_MetadataTooShort

error JBMetadataResolver_MetadataTooShort();