ERC-3722 · Stagnant(停滞) · ERC · 2021-07-31
ERC-3722: Poster
一个刻意做得极小的合约,只有一个函数 post(content, tag),仅仅触发一个事件,为去中心化社交应用提供一个共享的、可索引的地方,在任何 EVM 链上发布帖子。
它解决什么问题?
构建去中心化社交网络通常意味着编写一个带有自己的存储、ID 和规则的定制合约,结果每个应用都互不兼容。Poster 采取相反的做法:链上只记录谁说了什么,其余一切(讨论串、点赞、关注、编辑)都由索引器在链下重建。
由于该合约非常简单且无状态,可以在每个网络上以相同地址只部署一次,并由所有想读写帖子的应用共享。
它如何运作
整个合约只有一个函数和一个事件:
contract Poster {
event NewPost(address indexed user, string content, string indexed tag);
function post(string calldata content, string calldata tag) public {
emit NewPost(msg.sender, content, tag);
}
}
content是一个自由格式的字符串。规范为类似 Twitter 的应用提出了一种 JSON 格式,一次post()可以在content数组中携带多个条目:microblog帖子(可选replyTo、image、edit),以及delete、like、repost、follow、unfollow、block、report和permissions等操作。tag是带索引的,因此索引器可以按应用或话题过滤。由于它是带索引的string,日志里只存储它的 keccak256 哈希,而不是文本本身。- 不会触发帖子 ID。索引器从
transactionHash+logIndex推导出一个。 - 作者选择
string而不是bytes,使帖子在区块浏览器上仍可供人阅读;并选择把内容通过事件触发出来(而不是只放在 calldata 中),这样索引器只靠日志就够了。
规范的参考部署使用 EIP-2470 的单例工厂,使该合约在任何 EVM 网络上都落在相同地址;地址、初始化代码和 salt 都列在规范中。
示例代码
一份发布两条消息讨论串的 JSON 载荷,以及用 ethers v6 发送它:
{
"content": [
{ "type": "microblog", "text": "gm, first post in a thread" },
{ "type": "microblog", "text": "second post", "replyTo": "this[0]" }
]
}
import { Contract, Wallet, JsonRpcProvider, id } from "ethers";
const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);
const poster = new Contract(
process.env.POSTER_ADDRESS!, // the deployment on your network
[
"function post(string content, string tag)",
"event NewPost(address indexed user, string content, string indexed tag)",
],
wallet,
);
const payload = JSON.stringify({ content: [{ type: "microblog", text: "hello" }] });
await (await poster.post(payload, "microblog")).wait();
// Read posts with a given tag (topic holds keccak256 of the tag string)
const logs = await poster.queryFilter(poster.filters.NewPost(null, null, id("microblog")));
for (const l of logs) console.log((l as any).args.user, (l as any).args.content);
需要注意
- 状态为 Stagnant:该提案已不再积极推进到最终确定。
- 所有含义都在链下。合约不校验 JSON,不强制执行删除,也不检查权限;这些必须由索引器和客户端来做。
- 如果帖子中的
"from"与msg.sender不同,客户端只有在"from"此前通过permissions条目授予了该发送者权限时才应认可它;否则应视为无效,或视为msg.sender发的帖子。 - 客户端在渲染帖子内容之前必须对其进行清理(sanitize)。
- 发布的所有内容都是公开的,并永久保存在链的日志中;
delete只是请求索引器把它隐藏起来。