智能合约练习

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract CollectionName {

    uint public totalNum;  //收集的信息总数

    uint contractCreationTime; //创建合约时间 构造函数初始化时获取时间

    address  public owner; //存储合约owner

    uint private deadline; //截至日期

    mapping (address => mapping(uint => CollectionSys)) forMessage;

    mapping (uint => address) projectIdInStore;

    mapping(uint256 => CollectionSys[])  getAllData;

   

    //收集中英文信息的结构体

    struct CollectionSys {

        uint id;

        string chineseName;

        string englishName;

        uint timestamp ; //时间戳

        address where;

        string description;

    }

    constructor() public {

        owner=msg.sender;

        totalNum = 0;  //对于填写信息的人进行计数

        contractCreationTime=block.timestamp;

    }

     function setDeadline(uint time) public {

        deadline = time;

    }

     modifier notExpired(){

        require(block.timestamp <= deadline);

        _;

    }

    function service(uint _projectId) public notExpired view returns (uint,uint,string memory,string memory,uint,address,string memory){

        CollectionSys memory CollectionSys = forMessage[projectIdInStore[_projectId]][_projectId];

        return (contractCreationTime,CollectionSys.id, CollectionSys.chineseName, CollectionSys.englishName, CollectionSys.timestamp, CollectionSys.where, CollectionSys.description);

    }

    function getBlockTimestamp() public view returns(uint) {

     // 获取当前块的时间戳

        return block.timestamp;

    }

   

    //添加收集的中英文名称信息上链

    function addProject(string memory _chineseName, string memory _englishName, string memory _description) public {

        totalNum += 1;

        CollectionSys memory CollectionSys = CollectionSys(totalNum, _chineseName, _englishName, getBlockTimestamp(), msg.sender, _description);

        forMessage[msg.sender][totalNum] = CollectionSys;

        projectIdInStore[totalNum] = msg.sender;

        getAllData[1].push(CollectionSys);

    }

    //根据id查询展示链上的数据

    function getProject(uint _projectId) view public returns (uint,uint, string memory, string memory, uint, address , string memory) {

        CollectionSys memory CollectionSys = forMessage[projectIdInStore[_projectId]][_projectId];

        return (contractCreationTime,CollectionSys.id, CollectionSys.chineseName, CollectionSys.englishName, CollectionSys.timestamp, CollectionSys.where, CollectionSys.description);

    }

    function getAll()view public returns(CollectionSys[] memory){

      CollectionSys[] memory allData =getAllData[1];

      return allData;

    }

    // function destoryContract() public {

    //    if (owner == msg.sender) {   // 检查谁在调用

    //       selfdestruct(owner);      // 销毁合约

    //    }

    // }

   

}




 

posted @ 2022-09-16 13:38  要买CT5的小曹  阅读(22)  评论(0)    收藏  举报  来源