LoginSignup
0
0

More than 3 years have passed since last update.

[ETH-MEETUP] Solidityで入退室管理をしよう

Last updated at Posted at 2019-07-06

Ethereumのミートアップの課題「入退室管理をしょう」

2019/07/06 SingularityHIVE 開催
Ethereum Solidity入門課題

仕様

  • addUser(address, string): 送信者はオーナーのみ許される.ユーザの名前を含めた基本情報を登録する.
  • enter(): 入室処理
  • exit(): 退室処理
  • get~~~(address): 各パラメータのゲッター

アドレス帳のコード例

Cas.sol
pragma solidity >=0.4.22 <0.6.0;
contract Cas {
    // ユーザ情報
    struct Info {
        string name;
        uint total;
        bool status;
        uint last;
    }
    // アドレスと紐付け
    mapping(address => Info) data;
    // このコントラクトを管理するアドレス
    address owner;
    // コントラクトをデプロイしたアドレスをオーナーとする
    constructor() public {
        owner = msg.sender;
    }
    // ユーザを追加(オーナー権限)
    function addUser(address user, string name) public {
        _checkOwner();
        Info storage info = data[user];
        require(info.last == 0, "Already_Exist");
        info.name = name;
        info.total = 0;
        info.status = false;
        info.last = now;
    }
    // ユーザの入室処理
    function enter() public {
        Info storage info = data[msg.sender];
        require(info.last > 0, "Not_Registered");
        require(!info.status, "Already_in");
        info.status = true;
        info.last = now;
    }
    // ユーザの退室処理
    function exit() public returns(uint stay){
        Info storage info = data[msg.sender];
        require(info.last > 0, "Not_Registered");
        require(info.status, "Already_out");
        info.status = false;
        stay = now - info.last;
        info.total += stay ;
        info.last = now;
    }
    // ユーザの名前を得る
    function getName(address target) view public returns (string name) {
        name =  data[target].name;
    }
    // ユーザの今までの滞在時間を得る
    function getTotal(address target) view public returns (uint total) {
        total =  data[target].total;
    }
    // ユーザの入室状態を得る
    function getStatus(address target) view public returns (bool status) {
        status =  data[target].status;
    }
    // ユーザが最後に行動した時間を得る
    function getLast(address target) view public returns (uint last) {
        last = data[target].last;
    }
    // トランザクション送信者がオーナーかどうかのチェックを行う
    function _checkOwner() view private {
        require(msg.sender == owner, "Permission_Denyed");
    }
}

今後追加してもいいと思う機能

  • オーナーの移行機能
  • ERC20などのトークンを用いたインセンティブ設計
  • ランキング機能
  • 鍵管理機能
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0