Created
February 12, 2022 23:19
-
-
Save himawari2021/5fd345515132e713e33c91523f69ffee to your computer and use it in GitHub Desktop.
simple solidity BBS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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