编译器
0.8.24+commit.e11b9ed9
文件 1 的 2:IProtocol.sol
pragma solidity ^0.8.24;
interface IProtocol {
struct Echo {
address sender;
uint64 timestamp;
uint32 dictIndex;
}
struct EchoResult {
address sender;
uint64 timestamp;
string word;
}
function dictionary() external view returns (string[] memory);
function echoesCount() external view returns (uint);
function echoes(uint index, uint offset) external view returns (EchoResult[] memory);
function echoesAll() external view returns (EchoResult[] memory);
function latestEchoes(uint offset) external view returns (EchoResult[] memory);
function echo(string memory word) external;
event Echoed(address indexed sender, uint64 timestamp, uint32 dictIndex);
}
文件 2 的 2:Protocol.sol
pragma solidity ^0.8.24;
import "./IProtocol.sol";
contract Protocol is IProtocol {
Echo[] private _echoes;
string[] private _dictionary;
mapping(string => uint32) private dictionaryIndexMap;
constructor(string[] memory dictionary_) {
require(dictionary_.length > 0, "Protocol: Dictionary is empty");
require(dictionary_.length < 2 ** 32 - 1, "Protocol: Dictionary is too large");
_dictionary = dictionary_;
for (uint32 i = 0; i < _dictionary.length; i++) {
dictionaryIndexMap[_dictionary[i]] = i + 1;
}
}
function dictionary() external view override returns (string[] memory) {
return _dictionary;
}
function echoesCount() external view override returns (uint256) {
return _echoes.length;
}
function echoes(uint index, uint offset) public view override returns (EchoResult[] memory) {
if (index >= _echoes.length) {
return new EchoResult[](0);
}
if (index + offset > _echoes.length) {
offset = _echoes.length - index;
}
EchoResult[] memory result = new EchoResult[](offset);
for (uint i = 0; i < offset; i++) {
result[i].sender = _echoes[index + i].sender;
result[i].timestamp = _echoes[index + i].timestamp;
result[i].word = _dictionary[_echoes[index + i].dictIndex];
}
return result;
}
function echoesAll() external view override returns (EchoResult[] memory) {
return echoes(0, _echoes.length);
}
function latestEchoes(uint offset) external view override returns (EchoResult[] memory) {
if (offset > _echoes.length) {
offset = _echoes.length;
}
return echoes(_echoes.length - offset, offset);
}
function echo(string memory word) public override {
require(bytes(word).length > 0, "Protocol: Word is empty");
require(dictionaryIndexMap[word] > 0, "Protocol: Word is not in dictionary");
_echoes.push(Echo(msg.sender, uint64(block.timestamp), dictionaryIndexMap[word] - 1));
emit Echoed(msg.sender, uint64(block.timestamp), dictionaryIndexMap[word] - 1);
}
fallback() external {
echo(string(msg.data));
}
}
{
"compilationTarget": {
"contracts/Protocol.sol": "Protocol"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [],
"viaIR": true
}
[{"inputs":[{"internalType":"string[]","name":"dictionary_","type":"string[]"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"sender","type":"address"},{"indexed":false,"internalType":"uint64","name":"timestamp","type":"uint64"},{"indexed":false,"internalType":"uint32","name":"dictIndex","type":"uint32"}],"name":"Echoed","type":"event"},{"stateMutability":"nonpayable","type":"fallback"},{"inputs":[],"name":"dictionary","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"string","name":"word","type":"string"}],"name":"echo","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"},{"internalType":"uint256","name":"offset","type":"uint256"}],"name":"echoes","outputs":[{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint64","name":"timestamp","type":"uint64"},{"internalType":"string","name":"word","type":"string"}],"internalType":"struct IProtocol.EchoResult[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"echoesAll","outputs":[{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint64","name":"timestamp","type":"uint64"},{"internalType":"string","name":"word","type":"string"}],"internalType":"struct IProtocol.EchoResult[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"echoesCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"offset","type":"uint256"}],"name":"latestEchoes","outputs":[{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"uint64","name":"timestamp","type":"uint64"},{"internalType":"string","name":"word","type":"string"}],"internalType":"struct IProtocol.EchoResult[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"}]