Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article covers ERC6381, which allows users to express their impressions of NFTs using emoji.
The following proposal is translated, summarized, and supplemented in this article.
Overview
The Public Non-Fungible Token Emote Repository standard provides enhanced interactive utility for ERC721 and ERC1155, enabling users to express reactions to NFTs.
The proposal introduces the ability to react to NFTs using emoji defined by the Unicode standard. NFT holders can use emoji to express their emotions. This is implemented in a public, permissionless repository smart contract accessible at the same address across networks. It is a collection of smart contracts accessible at a particular address.
Motivation
As NFTs become widely used in the Ethereum ecosystem across various use cases, it is time to standardize additional utility for them. The ability to interact with NFTs adds an interactive element to holding them and opens the door to NFT mechanisms driven by feedback.
This ERC introduces new utility for ERC721-based tokens in the following areas.
- Interactivity
- Feedback-based evolution
- Evaluation
Interactivity
The ability to react to NFTs introduces an interactive aspect to NFT ownership. A reaction may reflect appreciation expressed by the person reacting to the NFT or may result from a particular action performed by the token owner. Accumulated reactions to a token may increase its uniqueness or value.
Feedback-based evolution
Standardized on-chain reactions to NFTs enable feedback-based evolution.
Existing solutions provide interactive features for NFTs through proprietary or off-chain mechanisms. However, these approaches may be vulnerable to manipulation and may be difficult to trust. The proposal introduces the ability to track NFT interactions on-chain. Interactions with NFTs are thereby recorded transparently on the blockchain, providing a trustworthy source of information. This approach reduces the risk of manipulation and fraud and enables fact-based evaluation.
The proposal also suggests mechanisms under which a token evolves when certain thresholds are met, such as a specified number of particular emotes. Increasing emotional interaction with an NFT could cause the token itself to evolve and acquire new characteristics or value. This encourages interaction with token collections and could increase their appeal.
Evaluation
Today's NFT markets rely heavily on historical sale prices, the lowest listed prices, and rarity data provided by marketplaces. There is no real-time indicator of appreciation or appeal for a specific token. Allowing users to react to tokens makes it possible to evaluate a token based on the impressions it has received.
Specification
pragma solidity ^0.8.16;
interface IERC6381 /*is IERC165*/ {
event Emoted(
address indexed emoter,
address indexed collection,
uint256 indexed tokenId,
bytes4 emoji,
bool on
);
function emoteCountOf(
address collection,
uint256 tokenId,
bytes4 emoji
) external view returns (uint256);
function bulkEmoteCountOf(
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis
) external view returns (uint256[] memory);
function hasEmoterUsedEmote(
address emoter,
address collection,
uint256 tokenId,
bytes4 emoji
) external view returns (bool);
function haveEmotersUsedEmotes(
address[] memory emoters,
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis
) external view returns (bool[] memory);
function prepareMessageToPresignEmote(
address collection,
uint256 tokenId,
bytes4 emoji,
bool state,
uint256 deadline
) external view returns (bytes32);
function bulkPrepareMessagesToPresignEmote(
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis,
bool[] memory states,
uint256[] memory deadlines
) external view returns (bytes32[] memory);
function emote(
address collection,
uint256 tokenId,
bytes4 emoji,
bool state
) external;
function bulkEmote(
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis,
bool[] memory states
) external;
function presignedEmote(
address emoter,
address collection,
uint256 tokenId,
bytes4 emoji,
bool state,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function bulkPresignedEmote(
address[] memory emoters,
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis,
bool[] memory states,
uint256[] memory deadlines,
uint8[] memory v,
bytes32[] memory r,
bytes32[] memory s
) external;
}Emoted
Event emitted when an emote is added to an NFT or a reaction is removed.
Parameters
emoter- Address of the account that added or removed the reaction.
collection- Smart contract address of the collection containing the NFT that received the emote or reaction.
tokenId- Token ID of the NFT that received the emote or reaction.
emoji- Identifier of the Unicode emoji used in the emote or reaction.
on- Indicates whether the emote is enabled (true) or removed (false).
emoteCountOf
Function that retrieves the number of reactions using a specified emoji for a particular NFT.
Parameters
collection- Smart contract address of the collection containing the NFT whose emote count is queried.
tokenId- Token ID of the NFT whose emote count is queried.
emoji- Unicode identifier of the emoji whose emote count is queried.
Return value
Number of reactions using the specified emoji for the specified NFT.
bulkEmoteCountOf
Function that retrieves emote counts for multiple NFTs and emoji in one call.
Parameters
collections- Array of smart contract addresses of the collections containing the NFTs whose emote counts are queried.
tokenIds- Array of token IDs of the NFTs whose emote counts are queried.
emojis- Array of Unicode identifiers of the emoji whose emote counts are queried.
Return value
Array of emote counts for the corresponding NFTs and emoji.
hasEmoterUsedEmote
Function that checks whether a specified address has used a particular emoji to react to a particular NFT.
emoter- Address of the account whose reaction is queried.
collection- Smart contract address of the collection containing the NFT whose reaction is queried.
tokenId- Token ID of the NFT whose reaction is queried.
emoji- Emoji code whose reaction is queried.
Return value
A bool indicating whether the specified address has used the specified emoji for the NFT.
haveEmotersUsedEmotes
Function that checks whether multiple addresses have used multiple emoji to react to multiple NFTs in one call.
emoters- Array of addresses of the accounts whose reactions are queried.
collections- Array of smart contract addresses of collections containing the NFTs whose reactions are queried.
tokenIds- Array of token IDs of the NFTs whose reactions are queried.
emojis- Array of emoji codes whose reactions are queried.
Return value
Array of bool values indicating whether each corresponding address has used the emoji for the NFT.
prepareMessageToPresignEmote
Function that retrieves a message for the emoter to sign so another party can submit the reaction.
collection- Smart contract address of the collection containing the NFT to receive the emote.
tokenId- Token ID of the NFT to receive the emote.
emoji- Unicode identifier of the emoji used for the emote.
state- Boolean value indicating whether to add (
true) or remove (false) the emote.
- Boolean value indicating whether to add (
deadline- UNIX timestamp specifying the deadline for submitting the signature.
Return value
Bytes of the message for the emoter to sign so another party can submit the reaction.
bulkPrepareMessagesToPresignEmote
Function that retrieves multiple messages for the emoter to sign in one call so reactions using multiple emoji can be submitted for multiple NFTs.
collections- Array of smart contract addresses of collections containing the NFTs to receive emotes.
tokenIds- Array of token IDs of the NFTs to receive emotes.
emojis- Array of Unicode identifiers of the emoji used for the emotes.
states- Array of boolean values indicating whether to add (
true) or remove (false) the emotes.
- Array of boolean values indicating whether to add (
deadlines- Array of UNIX timestamps specifying the deadlines for submitting the signatures.
Return value
Array of message byte sequences for the emoter to sign so another party can submit the reactions.
emote
Function that adds or removes an emote reaction to an NFT. It does nothing if the requested state is already the current state.
collection- Smart contract address of the collection containing the NFT to receive the emote.
tokenId- Token ID of the NFT to receive the emote.
emoji- Unicode identifier of the emoji used for the emote.
state- Boolean value indicating whether to add (
true) or remove (false) the emote.
- Boolean value indicating whether to add (
bulkEmote
Function that adds or removes emote reactions for multiple NFTs in one call. It does nothing for a reaction if the requested state is already the current state.
collections- Array of smart contract addresses of collections containing the NFTs to receive emotes.
tokenIds- Array of token IDs of the NFTs to receive emotes.
emojis- Array of Unicode identifiers of the emoji used for the emotes.
states- Array of boolean values indicating whether to add (
true) or remove (false) the emotes.
- Array of boolean values indicating whether to add (
presignedEmote
Function that adds or removes an emote reaction on behalf of another party. It performs the operation based on a signed message.
emoter- Address that signed the emote in advance.
collection- Smart contract address of the collection containing the NFT to receive the emote.
tokenId- Token ID of the NFT to receive the emote.
emoji- Unicode identifier of the emoji used for the emote.
state- Boolean value indicating whether to add (
true) or remove (false) the emote.
- Boolean value indicating whether to add (
deadline- UNIX timestamp specifying the deadline for submitting the signature.
v- The ECDSA signature's
vvalue.
- The ECDSA signature's
r- The ECDSA signature's
rvalue.
- The ECDSA signature's
s- The ECDSA signature's
svalue.
- The ECDSA signature's
bulkPresignedEmote
Function that adds or removes emote reactions for multiple NFTs on behalf of other parties in one call. It performs the operations based on signed messages.
emoters- Array of addresses that signed the emotes in advance.
collections- Array of smart contract addresses of collections containing the NFTs to receive emotes.
tokenIds- Array of token IDs of the NFTs to receive emotes.
emojis- Array of Unicode identifiers of the emoji used for the emotes.
states- Array of boolean values indicating whether to add (
true) or remove (false) the emotes.
- Array of boolean values indicating whether to add (
deadlines- Array of UNIX timestamps specifying the deadlines for submitting the signatures.
v- Array of ECDSA signature
vvalues.
- Array of ECDSA signature
r- Array of ECDSA signature
rvalues.
- Array of ECDSA signature
s- Array of ECDSA signature
svalues.
- Array of ECDSA signature
Message format for signed emotes
The message that the emoter signs so another party can submit a reaction has the following format.
keccak256(
abi.encode(
DOMAIN_SEPARATOR,
collection,
tokenId,
emoji,
state,
deadline
)
);DOMAIN_SEPARATOR- Generated domain separator.
collection- Smart contract address of the collection containing the NFT to receive the emote.
tokenId- Token ID of the NFT to receive the emote.
emoji- Unicode identifier of the emoji used for the emote.
state- A
boolindicating whether to add (true) or remove (false) the emote.
- A
deadline- UNIX timestamp specifying the deadline for submitting the signature.
These values are used to generate the message to be signed.
Following these steps generates the domain separator and the message to be signed; signing the message provides authentication and security.
The DOMAIN_SEPARATOR (domain separator) is generated as follows.
keccak256(
abi.encode(
"ERC-6381: Public Non-Fungible Token Emote Repository",
"1",
block.chainid,
address(this)
)
);The values are listed below in order.
"ERC-6381: Public Non-Fungible Token Emote Repository"- Identifier used in constructing the message hash.
"1"- Message version number.
block.chainid- Chain ID of the blockchain on which the message is used.
address(this)- Address of the
Emotablerepository smart contract.
- Address of the
Each chain on which the Emotable repository smart contract is deployed has a different DOMAIN_SEPARATOR because its chain ID differs.
Predetermined address of the Emotable repository smart contract
The address of the Emotable repository smart contract is designed to reflect its function.
It begins with 0x311073.
This is a stylized representation of EMOTE.
The full address is as follows.
0x31107354b61A0412E722455A771bC462901668eA
This address identifies where the Emotable repository smart contract is deployed. Its stylized EMOTE prefix indicates its purpose.
Users interact with this address to perform emote-related operations.
Rationale
1. Does the proposal support custom emotes or only Unicode emotes?
The proposal accepts only Unicode identifiers (bytes4 values).
Although standardized emoji are encouraged for reactions, values not included in the Unicode standard can be used as custom emotes.
In that case, however, the interface displaying reactions must know which image to render.
Custom emotes may therefore be added only within particular interfaces or marketplaces.
2. Should the proposal use emoji or another method to express impressions of NFTs?
We considered expressing impressions of NFTs through strings or numbers supplied by users, but chose emoji because they are widely recognized as a way to convey emotions and impressions.
3. Should the proposal establish an Emotable extension or a common repository?
We initially intended to create an Emotable extension usable with any ERC721-compliant token.
However, we concluded that a common repository allowing reactions to all NFT tokens, old and new, would be more useful.
This allows reactions to be added not only to new tokens but also to tokens that existed before the proposal.
4. Should the proposal include only single-action operations, only batch operations, or both?
We considered including only single-action operations but ultimately chose to include both single and batch operations. This allows users to choose whether to add or remove emotes for one token or multiple tokens. Based on network gas costs and the number of tokens in a collection, users can choose the most cost-effective way to react.
5. Should the proposal add the ability to emote on someone else's behalf?
We did not initially intend to include this in the proposal, but later realized that it would be useful. It allows users to react on behalf of another person. This can be useful, for example, when someone cannot perform the operation themselves or earns an emote through off-chain activity.
6. How can the proposal ensure that an emote submitted on someone else's behalf is legitimate?
Adding delegation to the proposal could allow users to delegate their right to emote to another party. However, this would introduce substantial complexity and additional logic. Using an ECDSA signature makes it possible to verify that the user authorized someone else to submit an emote. The user signs a message containing the emote parameters, and another party can submit that signature.
7. Should the chain ID be an additional parameter when reacting to a token?
During discussions of the proposal, it was suggested that the chain ID be added as a parameter when reacting to a token. This would allow users to react on one chain to tokens on another chain. However, we decided against this approach. The additional parameter would likely see little use and could add cost to reaction transactions. Moreover, if a collection's smart contract uses reactions to its tokens internally, those reactions must be recorded on the same chain. Marketplaces and wallets integrating the proposal are likewise expected to assume that reactions exist on the same chain.
Tests
The test file is shown below.
import { ethers } from "hardhat";
import { expect } from "chai";
import { BigNumber, Contract } from "ethers";
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { ERC721Mock, EmotableRepository } from "../typechain-types";
function bn(x: number): BigNumber {
return BigNumber.from(x);
}
async function tokenFixture() {
const factory = await ethers.getContractFactory("ERC721Mock");
const token = await factory.deploy("Chunky", "CHNK");
await token.deployed();
return token;
}
async function emotableRepositoryFixture() {
const factory = await ethers.getContractFactory("EmotableRepository");
const repository = await factory.deploy();
await repository.deployed();
return repository;
}
describe("RMRKEmotableRepositoryMock", async function () {
let token: ERC721Mock;
let repository: EmotableRepository;
let owner: SignerWithAddress;
let addrs: SignerWithAddress[];
const tokenId = bn(1);
const emoji1 = Buffer.from("😎");
const emoji2 = Buffer.from("😁");
beforeEach(async function () {
[owner, ...addrs] = await ethers.getSigners();
token = await loadFixture(tokenFixture);
repository = await loadFixture(emotableRepositoryFixture);
});
it("can support IERC6381", async function () {
expect(await repository.supportsInterface("0xd9fac55a")).to.equal(true);
});
it("can support IERC165", async function () {
expect(await repository.supportsInterface("0x01ffc9a7")).to.equal(true);
});
it("does not support other interfaces", async function () {
expect(await repository.supportsInterface("0xffffffff")).to.equal(false);
});
describe("With minted tokens", async function () {
beforeEach(async function () {
await token.mint(owner.address, tokenId);
});
it("can emote", async function () {
await expect(
repository.connect(addrs[0]).emote(token.address, tokenId, emoji1, true)
)
.to.emit(repository, "Emoted")
.withArgs(
addrs[0].address,
token.address,
tokenId.toNumber(),
emoji1,
true
);
expect(
await repository.emoteCountOf(token.address, tokenId, emoji1)
).to.equal(bn(1));
});
it("can undo emote", async function () {
await repository.emote(token.address, tokenId, emoji1, true);
await expect(repository.emote(token.address, tokenId, emoji1, false))
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
false
);
expect(
await repository.emoteCountOf(token.address, tokenId, emoji1)
).to.equal(bn(0));
});
it("can be emoted from different accounts", async function () {
await repository
.connect(addrs[0])
.emote(token.address, tokenId, emoji1, true);
await repository
.connect(addrs[1])
.emote(token.address, tokenId, emoji1, true);
await repository
.connect(addrs[2])
.emote(token.address, tokenId, emoji2, true);
expect(
await repository.emoteCountOf(token.address, tokenId, emoji1)
).to.equal(bn(2));
expect(
await repository.emoteCountOf(token.address, tokenId, emoji2)
).to.equal(bn(1));
});
it("can add multiple emojis to same NFT", async function () {
await repository.emote(token.address, tokenId, emoji1, true);
await repository.emote(token.address, tokenId, emoji2, true);
expect(
await repository.emoteCountOf(token.address, tokenId, emoji1)
).to.equal(bn(1));
expect(
await repository.emoteCountOf(token.address, tokenId, emoji2)
).to.equal(bn(1));
});
it("does nothing if new state is the same as old state", async function () {
await repository.emote(token.address, tokenId, emoji1, true);
await repository.emote(token.address, tokenId, emoji1, true);
expect(
await repository.emoteCountOf(token.address, tokenId, emoji1)
).to.equal(bn(1));
await repository.emote(token.address, tokenId, emoji2, false);
expect(
await repository.emoteCountOf(token.address, tokenId, emoji2)
).to.equal(bn(0));
});
it("can bulk emote", async function () {
expect(
await repository.bulkEmoteCountOf(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([bn(0), bn(0)]);
expect(
await repository.haveEmotersUsedEmotes(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([false, false]);
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true, true]
)
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
true
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji2,
true
);
expect(
await repository.bulkEmoteCountOf(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([bn(1), bn(1)]);
expect(
await repository.haveEmotersUsedEmotes(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([true, true]);
});
it("can bulk undo emote", async function () {
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true, true]
)
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
true
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji2,
true
);
expect(
await repository.bulkEmoteCountOf(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([bn(1), bn(1)]);
expect(
await repository.haveEmotersUsedEmotes(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([true, true]);
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[false, false]
)
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
false
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji2,
false
);
expect(
await repository.bulkEmoteCountOf(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([bn(0), bn(0)]);
expect(
await repository.haveEmotersUsedEmotes(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([false, false]);
});
it("can bulk emote and unemote at the same time", async function () {
await repository.emote(token.address, tokenId, emoji2, true);
expect(
await repository.bulkEmoteCountOf(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([bn(0), bn(1)]);
expect(
await repository.haveEmotersUsedEmotes(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([false, true]);
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true, false]
)
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
true
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji2,
false
);
expect(
await repository.bulkEmoteCountOf(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([bn(1), bn(0)]);
expect(
await repository.haveEmotersUsedEmotes(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2]
)
).to.eql([true, false]);
});
it("can not bulk emote if passing arrays of different length", async function () {
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true]
)
).to.be.revertedWithCustomError(
repository,
"BulkParametersOfUnequalLength"
);
await expect(
repository.bulkEmote(
[token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true, true]
)
).to.be.revertedWithCustomError(
repository,
"BulkParametersOfUnequalLength"
);
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId],
[emoji1, emoji2],
[true, true]
)
).to.be.revertedWithCustomError(
repository,
"BulkParametersOfUnequalLength"
);
await expect(
repository.bulkEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1],
[true, true]
)
).to.be.revertedWithCustomError(
repository,
"BulkParametersOfUnequalLength"
);
});
it("can use presigned emote to react to token", async function () {
const message = await repository.prepareMessageToPresignEmote(
token.address,
tokenId,
emoji1,
true,
bn(9999999999)
);
const signature = await owner.signMessage(ethers.utils.arrayify(message));
const r: string = signature.substring(0, 66);
const s: string = "0x" + signature.substring(66, 130);
const v: number = parseInt(signature.substring(130, 132), 16);
await expect(
repository
.connect(addrs[0])
.presignedEmote(
owner.address,
token.address,
tokenId,
emoji1,
true,
bn(9999999999),
v,
r,
s
)
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
true
);
});
it("can use presigned emotes to bulk react to token", async function () {
const messages = await repository.bulkPrepareMessagesToPresignEmote(
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true, true],
[bn(9999999999), bn(9999999999)]
);
const signature1 = await owner.signMessage(
ethers.utils.arrayify(messages[0])
);
const signature2 = await owner.signMessage(
ethers.utils.arrayify(messages[1])
);
const r1: string = signature1.substring(0, 66);
const s1: string = "0x" + signature1.substring(66, 130);
const v1: number = parseInt(signature1.substring(130, 132), 16);
const r2: string = signature2.substring(0, 66);
const s2: string = "0x" + signature2.substring(66, 130);
const v2: number = parseInt(signature2.substring(130, 132), 16);
await expect(
repository
.connect(addrs[0])
.bulkPresignedEmote(
[owner.address, owner.address],
[token.address, token.address],
[tokenId, tokenId],
[emoji1, emoji2],
[true, true],
[bn(9999999999), bn(9999999999)],
[v1, v2],
[r1, r2],
[s1, s2]
)
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji1,
true
)
.to.emit(repository, "Emoted")
.withArgs(
owner.address,
token.address,
tokenId.toNumber(),
emoji2,
true
);
});
});
});Run the tests with the following commands.
cd ../assets/eip-6381
npm install
npx hardhat testReference implementation
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.16;
import "./IERC6381.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
error BulkParametersOfUnequalLength();
error ExpiredPresignedEmote();
error InvalidSignature();
contract EmotableRepository is IERC6381 {
bytes32 public immutable DOMAIN_SEPARATOR = keccak256(
abi.encode(
"ERC-6381: Public Non-Fungible Token Emote Repository",
"1",
block.chainid,
address(this)
)
);
// Used to avoid double emoting and control undoing
mapping(address => mapping(address => mapping(uint256 => mapping(bytes4 => uint256))))
private _emotesUsedByEmoter; // Cheaper than using a bool
mapping(address => mapping(uint256 => mapping(bytes4 => uint256)))
private _emotesPerToken;
function emoteCountOf(
address collection,
uint256 tokenId,
bytes4 emoji
) public view returns (uint256) {
return _emotesPerToken[collection][tokenId][emoji];
}
function bulkEmoteCountOf(
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis
) public view returns (uint256[] memory) {
if(
collections.length != tokenIds.length ||
collections.length != emojis.length
){
revert BulkParametersOfUnequalLength();
}
uint256[] memory counts = new uint256[](collections.length);
for (uint256 i; i < collections.length; ) {
counts[i] = _emotesPerToken[collections[i]][tokenIds[i]][emojis[i]];
unchecked {
++i;
}
}
return counts;
}
function hasEmoterUsedEmote(
address emoter,
address collection,
uint256 tokenId,
bytes4 emoji
) public view returns (bool) {
return _emotesUsedByEmoter[emoter][collection][tokenId][emoji] == 1;
}
function haveEmotersUsedEmotes(
address[] memory emoters,
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis
) public view returns (bool[] memory) {
if(
emoters.length != collections.length ||
emoters.length != tokenIds.length ||
emoters.length != emojis.length
){
revert BulkParametersOfUnequalLength();
}
bool[] memory states = new bool[](collections.length);
for (uint256 i; i < collections.length; ) {
states[i] = _emotesUsedByEmoter[emoters[i]][collections[i]][tokenIds[i]][emojis[i]] == 1;
unchecked {
++i;
}
}
return states;
}
function emote(
address collection,
uint256 tokenId,
bytes4 emoji,
bool state
) public {
bool currentVal = _emotesUsedByEmoter[msg.sender][collection][tokenId][
emoji
] == 1;
if (currentVal != state) {
if (state) {
_emotesPerToken[collection][tokenId][emoji] += 1;
} else {
_emotesPerToken[collection][tokenId][emoji] -= 1;
}
_emotesUsedByEmoter[msg.sender][collection][tokenId][emoji] = state
? 1
: 0;
emit Emoted(msg.sender, collection, tokenId, emoji, state);
}
}
function bulkEmote(
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis,
bool[] memory states
) public {
if(
collections.length != tokenIds.length ||
collections.length != emojis.length ||
collections.length != states.length
){
revert BulkParametersOfUnequalLength();
}
bool currentVal;
for (uint256 i; i < collections.length; ) {
currentVal = _emotesUsedByEmoter[msg.sender][collections[i]][tokenIds[i]][
emojis[i]
] == 1;
if (currentVal != states[i]) {
if (states[i]) {
_emotesPerToken[collections[i]][tokenIds[i]][emojis[i]] += 1;
} else {
_emotesPerToken[collections[i]][tokenIds[i]][emojis[i]] -= 1;
}
_emotesUsedByEmoter[msg.sender][collections[i]][tokenIds[i]][emojis[i]] = states[i]
? 1
: 0;
emit Emoted(msg.sender, collections[i], tokenIds[i], emojis[i], states[i]);
}
unchecked {
++i;
}
}
}
function prepareMessageToPresignEmote(
address collection,
uint256 tokenId,
bytes4 emoji,
bool state,
uint256 deadline
) public view returns (bytes32) {
return keccak256(
abi.encode(
DOMAIN_SEPARATOR,
collection,
tokenId,
emoji,
state,
deadline
)
);
}
function bulkPrepareMessagesToPresignEmote(
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis,
bool[] memory states,
uint256[] memory deadlines
) public view returns (bytes32[] memory) {
if(
collections.length != tokenIds.length ||
collections.length != emojis.length ||
collections.length != states.length ||
collections.length != deadlines.length
){
revert BulkParametersOfUnequalLength();
}
bytes32[] memory messages = new bytes32[](collections.length);
for (uint256 i; i < collections.length; ) {
messages[i] = keccak256(
abi.encode(
DOMAIN_SEPARATOR,
collections[i],
tokenIds[i],
emojis[i],
states[i],
deadlines[i]
)
);
unchecked {
++i;
}
}
return messages;
}
function presignedEmote(
address emoter,
address collection,
uint256 tokenId,
bytes4 emoji,
bool state,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public {
if(block.timestamp > deadline){
revert ExpiredPresignedEmote();
}
bytes32 digest = keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
keccak256(
abi.encode(
DOMAIN_SEPARATOR,
collection,
tokenId,
emoji,
state,
deadline
)
)
)
);
address signer = ecrecover(digest, v, r, s);
if(signer != emoter){
revert InvalidSignature();
}
bool currentVal = _emotesUsedByEmoter[signer][collection][tokenId][
emoji
] == 1;
if (currentVal != state) {
if (state) {
_emotesPerToken[collection][tokenId][emoji] += 1;
} else {
_emotesPerToken[collection][tokenId][emoji] -= 1;
}
_emotesUsedByEmoter[signer][collection][tokenId][emoji] = state
? 1
: 0;
emit Emoted(signer, collection, tokenId, emoji, state);
}
}
function bulkPresignedEmote(
address[] memory emoters,
address[] memory collections,
uint256[] memory tokenIds,
bytes4[] memory emojis,
bool[] memory states,
uint256[] memory deadlines,
uint8[] memory v,
bytes32[] memory r,
bytes32[] memory s
) public {
if(
emoters.length != collections.length ||
emoters.length != tokenIds.length ||
emoters.length != emojis.length ||
emoters.length != states.length ||
emoters.length != deadlines.length ||
emoters.length != v.length ||
emoters.length != r.length ||
emoters.length != s.length
){
revert BulkParametersOfUnequalLength();
}
bytes32 digest;
address signer;
bool currentVal;
for (uint256 i; i < collections.length; ) {
if (block.timestamp > deadlines[i]){
revert ExpiredPresignedEmote();
}
digest = keccak256(
abi.encodePacked(
"\x19Ethereum Signed Message:\n32",
keccak256(
abi.encode(
DOMAIN_SEPARATOR,
collections[i],
tokenIds[i],
emojis[i],
states[i],
deadlines[i]
)
)
)
);
signer = ecrecover(digest, v[i], r[i], s[i]);
if(signer != emoters[i]){
revert InvalidSignature();
}
currentVal = _emotesUsedByEmoter[signer][collections[i]][tokenIds[i]][
emojis[i]
] == 1;
if (currentVal != states[i]) {
if (states[i]) {
_emotesPerToken[collections[i]][tokenIds[i]][emojis[i]] += 1;
} else {
_emotesPerToken[collections[i]][tokenIds[i]][emojis[i]] -= 1;
}
_emotesUsedByEmoter[signer][collections[i]][tokenIds[i]][emojis[i]] = states[i]
? 1
: 0;
emit Emoted(signer, collections[i], tokenIds[i], emojis[i], states[i]);
}
unchecked {
++i;
}
}
}
function supportsInterface(
bytes4 interfaceId
) public view virtual returns (bool) {
return
interfaceId == type(IERC6381).interfaceId ||
interfaceId == type(IERC165).interfaceId;
}
}Security considerations
The proposal is not intended to handle user assets, so assets are not exposed to risk by interaction with the Emote repository itself.
Using ECDSA signatures to emote on another person's behalf introduces a potential replay attack risk, which the format of the signed message addresses.
The DOMAIN_SEPARATOR used in the signed message is specific to the repository smart contract deployed on that chain.
The signature is therefore invalid on any other chain, and Emote repositories deployed on those chains should revert the operation if a replay is attempted.
Another consideration is the potential reuse of presigned messages. The message contains a signature expiry time, and the message may be reused any number of times before that deadline. Under the proposal, only a single reaction using a specific emoji is active for a particular token per reacting account, so presigned messages cannot be used to increase that token's reaction count repeatedly. However, if a service using the repository relies on removing a reaction after a particular action, a still-valid presigned message can be used to react to the token again. When using presigned messages, we recommend setting a reasonably short deadline so they become invalid quickly.
Exercise caution when interacting with unaudited contracts.
References
Bruno Škvorc (@Swader), Steven Pineda (@steven2308), Stevan Bogosavljevic (@stevyhacker), Jan Turk (@ThunderDeliverer), "ERC-6381: Public Non-Fungible Token Emote Repository," Ethereum Improvement Proposals, no. 6381, January 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6381.
Discussion
I find the ability to attach emoji to NFTs an interesting experiment in itself. However, I do not yet have a clear picture of its practical use cases. One idea that comes to mind is issuing an NFT in SBT form, then having other members of a community award emoji for activities within the community and offering rewards based on the number of reactions. 🤔 I have yet to identify a convincing reason to manage emoji such as likes on a blockchain. Adding an emoji requires a small gas fee, so people would presumably react to NFTs they genuinely appreciate. It might also be interesting to allow emoji reactions to articles and distribute rewards according to their reaction counts. There may still be gaps in my understanding, so I will continue researching this.
Conclusion
This article covered ERC6381, which allows people to express their impressions of NFTs using emoji reactions. I hope it was useful.


