智能合约练习

这个智能合约示例展示了如何在以太坊平台上创建一个用于收集和管理中英文信息的系统。合约包括结构体`CollectionSys`来存储信息,以及相关函数如`addProject`用于添加信息,`getProject`用于按ID查询信息,`getBlockTimestamp`获取当前区块时间戳,且具备截至日期的管理功能。
摘要由CSDN通过智能技术生成

// 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);      // 销毁合约

    //    }

    // }

   

}




 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想买CT5的小曹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值