Skip to content

Instantly share code, notes, and snippets.

@himawari2021
Created February 12, 2022 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himawari2021/5fd345515132e713e33c91523f69ffee to your computer and use it in GitHub Desktop.
Save himawari2021/5fd345515132e713e33c91523f69ffee to your computer and use it in GitHub Desktop.
simple solidity BBS
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract BBS {
struct Post {
string message;
string name;
address sender;
uint blockNumber;
uint blockTimestamp;
}
//last three are possibly redandant
string public title;
Post[] public posts;
uint public post_limit = 100;
mapping(address => string) public names;
constructor() {
title = "bbs1";
}
function post(string memory _message, string memory _name) public {
require(posts.length <= post_limit, "exceed post limit");
posts.push(Post(_message, _name, msg.sender, block.number, block.timestamp));
if ( !_compareString(getName(),_name) ){
names[msg.sender] = _name;
}
}
function getPosts() view public returns(Post[] memory) {
return posts;
}
function getName() view public returns(string memory){
return names[msg.sender];
}
function _compareString(string memory s1, string memory s2) private pure returns(bool) {
//https://qiita.com/bc_yuuuuuki/items/bbc4938da0ae09a0c57c
return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment