ERC-7508 · Draft(草案) · ERC · 2023-08-15
ERC-7508: Dynamic On-Chain Token Attributes Repository
一个共享的链上仓库,任何 ERC-721 或 ERC-1155 合集(包括已有的)都可以在其中存储带类型的逐代币属性,其他合约可以直接读取。
它解决什么问题?
大多数 NFT 属性("strength: 12"、"rarity: gold")都存放在链下的 JSON 中。智能合约读不到它们,因此游戏离开预言机就无法信任角色的数值,而且一旦托管消失,属性也随之消失。
ERC-7508 提出一个单一的、无需许可的**属性仓库(attributes repository)**合约,任何 ERC-721 或 ERC-1155 合集都可以使用。由于它是独立的公共物品合约而不是扩展,即使是早在它出现之前部署的合集,也可以开始在链上存储属性。规范中列出的用例包括跨合集游戏、随所有者行为而演化的代币,以及状态从"pending"变为"shipped"再到"delivered"的供应链物品。
它如何运作
- 带类型的键/值存储。 属性按
(collection, tokenId, key)存储,有六种类型:address、bool、bytes、int256、string和uint256。每种类型都有 getter 和 setter(getUintAttribute、setStringAttribute等),还有批量版本以及组合的getAttributes/setAttributes。 - 合集级的值。 使用
tokenId = type(uint256).max可以存储整个合集共享的属性。 - 按键的访问控制。 合集首先通过
registerAccessControl注册(所有者可从Ownable读取,或手动设置)。然后所有者针对每个键选择谁可以写入:Owner、Collaborator、OwnerOrCollaborator、TokenOwner或SpecificAddress。 - 预签名写入。 有权限的设置者可以在链下签署属性更新(带有截止时间和特定于链的
DOMAIN_SEPARATOR),任何人都可以通过presignedSet…Attribute提交。 - 模式(Schema)。 合集可以发布一个描述其属性的元数据 URI(名称、类型、显示提示、最小/最大值等),通过
setAttributesMetadataURIForCollection设置。 - Gas。 键和值的字符串只存储一次并通过索引引用,因此之后使用相同字符串的人支付更少。
每次写入都会触发带类型的 …AttributeUpdated 事件。ERC-165 接口 ID 为 0x212206a8。
示例代码
一个在仓库中读取并更新角色等级的游戏合约:
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.21;
interface IERC7508Min {
function getUintAttribute(address collection, uint256 tokenId, string memory key)
external view returns (uint256);
function setUintAttribute(address collection, uint256 tokenId, string memory key, uint256 value)
external;
}
contract Arena {
IERC7508Min public immutable repo;
address public immutable heroes;
constructor(IERC7508Min _repo, address _heroes) { repo = _repo; heroes = _heroes; }
// Requires the collection owner to have granted this contract write access
// to the "level" key (e.g. as a Collaborator or SpecificAddress).
function win(uint256 heroId) external {
uint256 level = repo.getUintAttribute(heroes, heroId, "level");
repo.setUintAttribute(heroes, heroId, "level", level + 1);
}
}
需要注意
- 状态为 Draft。接口仍可能变化。
- 规范希望在每条链上的同一地址(以
0xA77B75开头)部署一份,但最终地址标注为 TBD。不要硬编码来自非官方来源的地址。 - 除非
TokenOwner访问类型需要查询所有者,否则仓库不会检查代币是否存在。 - 预签名消息在截止时间之前可以被重放。如果你的逻辑预期属性之后会改回去,请把截止时间设得短一些。
- 它与可嵌套 NFT(ERC-7401)出自同一批作者,可以与之组合使用。