以太坊线上图书馆

  1. 概述:以太坊线上图书馆,所有的图书借阅信息全部存储在以太坊区块链上
  2. 项目架构 
  3. 后端逻辑 
       1  初始化web3
       2  初始化合约
       3  监听 
            1 获取所有账号
             2 获取所有书本
             3 按钮事件
             4 监听 
                 1 借出事件
                 2 归还事件
                
            5 获取图书
            6 初始化图书状态
            7 列出所有账号
            8 切换账号
            9 添加记录
            10 图书按钮点击事件
            11 添加借阅记录
            12 获取当前时间
  4. 实现
    
    pragma solidity ^0.4.18;
    
    contract Library {
    
        mapping(uint => address) bookStatus; //书籍状态 查询指定书籍是否已经被借出,如果没有借出 则地址为0x0,否则应该是借阅人的地址
    
        struct Record{
    
            address user; // 借阅人
    
            uint timestamp; // 借阅时间
    
            uint rtype; // 类型 1-借出,2-归还
    
        }
    
        mapping(uint=>Record[]) record; // 指定书籍的借阅记录
    
        event BorrowEvent(address indexed _from, uint _id, uint _cost); //借出事件
    
        event Reback(address indexed _from , uint _id); //归还
    
        // 借书
    
        function borrow(uint _id) payable returns(bool){
    
            ...
    
            return true;
    
        }
    
        // 归还
    
        function reback(uint _id) returns(bool) {
    
            ...;
    
            return true;
    
        }
    
        // 查找指定图书状态
    
        function getBookStatus(uint _id) returns (int) {
    
            int status = 0;
    
            ...       
    
            return status;
    
        }
    
        // 查找指定书籍的借阅人
    
        function getBookBorrower(uint _id) constant returns(address) {
    
            return bookStatus[_id];
    
        }
    
    }
    

猜你喜欢

转载自blog.csdn.net/chaishen10000/article/details/85316325