如何为智能合约添加创建时间?
How can I add a created at for smart contract?
我正在为患者记录编写智能合约。但数据将采用时间序列数据格式。我想我应该为此添加 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;
}
您可以使用 block.timestamp
关键字将当前块时间戳设置为自包含您的交易的 unix 纪元以来的秒数。
有关block.timestamphere的更多信息。
您必须将 createdAt
变量设置为结构:
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];
}
}
我正在为患者记录编写智能合约。但数据将采用时间序列数据格式。我想我应该为此添加 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;
}
您可以使用 block.timestamp
关键字将当前块时间戳设置为自包含您的交易的 unix 纪元以来的秒数。
有关block.timestamphere的更多信息。
您必须将 createdAt
变量设置为结构:
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];
}
}