Skip to content

Commit 2acaacf

Browse files
committed
Add optional Diamond Storage to ERC-8048
- Add Optional Diamond Storage section specifying namespace 'erc8048.onchain.metadata.storage' - Add Diamond Storage reference implementation with predictable storage locations - Update pragma to ^0.8.25 in Basic Implementation - Add security considerations for Diamond Storage (ERC-8042) - Clarify benefits for cross-chain applications using inclusion proofs - Add both Basic and Diamond Storage implementation examples
1 parent 9458f70 commit 2acaacf

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

ERCS/erc-8048.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ event MetadataSet(uint256 indexed tokenId, string indexed indexedKey, string key
5454

5555
This ERC specifies that the key is a string type and the value is bytes type. This provides flexibility for storing any type of data while maintaining an intuitive string-based key interface.
5656

57+
### Optional Diamond Storage
58+
59+
Contracts implementing this ERC MAY use Diamond Storage pattern for predictable storage locations. If implemented, contracts MUST use the namespace ID `"erc8048.onchain.metadata.storage"`.
60+
61+
The Diamond Storage pattern provides predictable storage locations for data, which is useful for cross-chain applications using inclusion proofs. For more details on Diamond Storage, see ERC-8042.
62+
5763
### Examples
5864

5965
The inspiration for this standard was trustless AI agents. The registry extends ERC-721 by adding getMetadata and setMetadata functions for optional extra on-chain agent metadata.
@@ -89,11 +95,13 @@ This design prioritizes simplicity and flexibility by using a string-key, bytes-
8995

9096
## Reference Implementation
9197

92-
The interface is defined in the Required Metadata Function and Event section above. Here is a reference implementation:
98+
The interface is defined in the Required Metadata Function and Event section above. Here are reference implementations:
99+
100+
### Basic Implementation
93101

94102
```solidity
95103
// SPDX-License-Identifier: MIT
96-
pragma solidity ^0.8.0;
104+
pragma solidity ^0.8.25;
97105
98106
import "./IOnchainMetadata.sol";
99107
@@ -116,12 +124,53 @@ contract OnchainMetadataExample is IOnchainMetadata {
116124
}
117125
```
118126

127+
### Diamond Storage Implementation
128+
129+
```solidity
130+
// SPDX-License-Identifier: MIT
131+
pragma solidity ^0.8.20;
132+
133+
import "./IOnchainMetadata.sol";
134+
135+
contract OnchainMetadataDiamondExample is IOnchainMetadata {
136+
struct OnchainMetadataStorage {
137+
mapping(uint256 tokenId => mapping(string key => bytes value)) metadata;
138+
}
139+
140+
// keccak256("erc8048.onchain.metadata.storage")
141+
bytes32 private constant ONCHAIN_METADATA_STORAGE_LOCATION =
142+
keccak256("erc8048.onchain.metadata.storage");
143+
144+
function _getOnchainMetadataStorage() private pure returns (OnchainMetadataStorage storage $) {
145+
bytes32 location = ONCHAIN_METADATA_STORAGE_LOCATION;
146+
assembly {
147+
$.slot := location
148+
}
149+
}
150+
151+
function getMetadata(uint256 tokenId, string calldata key)
152+
external view override returns (bytes memory) {
153+
OnchainMetadataStorage storage $ = _getOnchainMetadataStorage();
154+
return $.metadata[tokenId][key];
155+
}
156+
157+
function setMetadata(uint256 tokenId, string calldata key, bytes calldata value)
158+
external {
159+
OnchainMetadataStorage storage $ = _getOnchainMetadataStorage();
160+
$.metadata[tokenId][key] = value;
161+
emit MetadataSet(tokenId, key, key, value);
162+
}
163+
}
164+
```
165+
119166
Implementations should follow the standard ERC-721, ERC-1155, ERC-6909, or ERC-8004 patterns while adding the required metadata function and event.
120167

121168
## Security Considerations
122169

123170
This ERC is designed to put metadata onchain, providing security benefits through onchain storage.
124171

172+
Implementations that choose to use the optional Diamond Storage pattern should consider the security considerations of ERC-8042.
173+
125174
## Copyright
126175

127176
Copyright and related rights waived via [CC0](../LICENSE.md).

0 commit comments

Comments
 (0)