如何为智能合约添加创建的一个?

发布于 2025-01-20 19:20:56 字数 447 浏览 0 评论 0原文

我正在为患者记录编写智能合约。但数据将采用时间序列数据格式。我想我应该为此添加created_at 字段。但我不知道具体该怎么做。

我对这份工作还很陌生。你能帮助我吗?

您可以看到结构体的一部分:

    struct Patient {
        string name;
        uint16 age;
        //max of uint16 is 4096
        //if we use uint8 the max is uint8
        string telephone;
        string homeAddress;
        uint64 birthday; //unix time
        string disease; //disease can be enum
        Gender gender;
    }

I'm writing a smart contract for patient records. But the data will be in time-series data format. And I guess I should add created_at field for that. But I don't know exactly how to do this.

I'm pretty new at this job. Can you help me?

You can see part of the struct:

    struct Patient {
        string name;
        uint16 age;
        //max of uint16 is 4096
        //if we use uint8 the max is uint8
        string telephone;
        string homeAddress;
        uint64 birthday; //unix time
        string disease; //disease can be enum
        Gender gender;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

找个人就嫁了吧 2025-01-27 19:20:56

您可以使用block.timestamp为具有当前块时间戳作为秒的关键字,因为unix epoch包括您的事务。

有关 block.timestamp 在这里

您必须将Createat变量设置为结构:

struct Patient {
  string name;
  uint16 age;
  //max of uint16 is 4096
  //if we use uint8 the max is uint8
  string telephone;
  string homeAddress;
  uint64 birthday; //unix time
  string disease; //disease can be enum
  Gender gender;
  // NOTE: createdAt variable
  uint createdAt
}

然后您必须将此语句用于设置此变量:

[your_struct_variable] = block.timestamp;

智能合约代码的示例:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Hospital {

    enum Gender { MALE, FEMALE }

    struct Patient {
        string name;
        uint16 age;
        //max of uint16 is 4096
        //if we use uint8 the max is uint8
        string telephone;
        string homeAddress;
        uint64 birthday; //unix time
        string disease; //disease can be enum
        Gender gender;
        uint256 createdAt;
    }

    mapping(address => Patient) _patients;

    function setPatients() public {
        Patient memory _patient = Patient({
            name: "test",
            age: 50,
            telephone: "test",
            homeAddress: "test",
            birthday: 1010101010,
            disease: "test",
            gender: Gender.MALE,
            createdAt: block.timestamp            
        });
        _patients[msg.sender] = _patient;
    }

    function getPatient() external view returns(Patient memory) {
        return _patients[msg.sender];
    }

}

You can use the block.timestamp keyword for have current block timestamp as seconds since unix epoch which your transaction is included.

More information about block.timestamp here.

You must to set your createdAt variable into a struct:

struct Patient {
  string name;
  uint16 age;
  //max of uint16 is 4096
  //if we use uint8 the max is uint8
  string telephone;
  string homeAddress;
  uint64 birthday; //unix time
  string disease; //disease can be enum
  Gender gender;
  // NOTE: createdAt variable
  uint createdAt
}

And then you must use this statement for set this variable:

[your_struct_variable] = block.timestamp;

Example of smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Hospital {

    enum Gender { MALE, FEMALE }

    struct Patient {
        string name;
        uint16 age;
        //max of uint16 is 4096
        //if we use uint8 the max is uint8
        string telephone;
        string homeAddress;
        uint64 birthday; //unix time
        string disease; //disease can be enum
        Gender gender;
        uint256 createdAt;
    }

    mapping(address => Patient) _patients;

    function setPatients() public {
        Patient memory _patient = Patient({
            name: "test",
            age: 50,
            telephone: "test",
            homeAddress: "test",
            birthday: 1010101010,
            disease: "test",
            gender: Gender.MALE,
            createdAt: block.timestamp            
        });
        _patients[msg.sender] = _patient;
    }

    function getPatient() external view returns(Patient memory) {
        return _patients[msg.sender];
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文