// SPDX-License-Identifier: MIT
pragma solidity ^0.5.2;
pragma experimental ABIEncoderV2;
contract Energy {
enum Role { Consumer, EnergyCompany }
enum Status { Trading, Closed ,ReSell}
struct EnergyInfo {
string id; // id 现在是 string 类型
string energyType; // 能源类型
uint256 price; // 能源单价
uint256 capacity; // 能源剩余容量
Role role; // 发布者角色
string publisherName; // 发布者名称
string deliveryTime; // 交割时间
uint8 status; // 状态
uint256 allCapacity; //所有容量
address publisher; // 发布者地址
string code;
string createTime;
}
struct Trade {
uint256 tradeId; // 交易ID
address buyer; // 买方
address seller; // 卖方
uint256 amount; // 交易量
uint256 totalPrice; // 总价
uint256 timestamp; // 交易时间戳
string name;
}
mapping(string => EnergyInfo) private energyInfos; // 使用 string ID 存储能源信息
mapping(uint256 => Trade) public trades; // 存储交易记录
uint256 public tradeCount; // 交易计数
string[] private recordKeys;
event EnergyInfoCreated(string id, string energyType, uint256 price, uint256 capacity, Role role, string publisherName, string deliveryTime, uint8 status, uint256 allCapacity,address publisher);
event TradeMatched(uint256 tradeId, address buyer, address seller, uint256 amount, uint256 totalPrice, uint256 timestamp,string name);
// 发布能源信息
function createEnergyInfo(string memory id, string memory energyType, uint256 price, uint256 capacity, Role role, string memory publisherName, string memory deliveryTime,string memory _code,string memory _createTime) public {
require(price > 0, "价格必须大于0");
// require(capacity > 0, "容量必须大于0");
energyInfos[id] = EnergyInfo(
id,
energyType,
price,
capacity,
role,
publisherName,
deliveryTime,
0,
capacity,
msg.sender,
_code,_createTime
);
emit EnergyInfoCreated(id, energyType, price, capacity, role, publisherName, deliveryTime,0, capacity,msg.sender);
recordKeys.push(id);
}
function updateStateAsReSell(string memory _id) public {
// 确保_id存在
//require(bytes(energyInfos[_id].id).length != 0, "EnergyInfo does not exist");
// 直接修改状态
EnergyInfo storage energyInfo = energyInfos[_id];
energyInfo.status = 2;
}
function updateObject(string memory id, string memory energyType, uint256 price, uint256 capacity, Role role, string memory publisherName, string memory deliveryTime,uint8 status,uint256 allCapacity) public {
require(price > 0, "价格必须大于0");
energyInfos[id] = EnergyInfo(
id,
energyType,
price,
capacity,
role,
publisherName,
deliveryTime,
status,
allCapacity,
msg.sender,
'',''
);
emit EnergyInfoCreated(id, energyType, price, capacity, role, publisherName, deliveryTime, status, capacity,msg.sender);
// recordKeys.push(id);
}
function buyById(string memory id,string memory buyId,uint256 capacity) public {
EnergyInfo storage matchingEnergyInfo = energyInfos[id];
EnergyInfo storage energyInfo = energyInfos[buyId];
// 计算交易量(取两者容量的较小值)
uint256 tradeAmount = (energyInfo.allCapacity < matchingEnergyInfo.capacity) ? energyInfo.allCapacity : matchingEnergyInfo.capacity;
// 更新能源信息容量
energyInfo.capacity -= tradeAmount;
matchingEnergyInfo.capacity -= tradeAmount;
// 如果容量为 0,关闭交易
if (energyInfo.capacity == 0) {
energyInfo.status = 1;
}
if (matchingEnergyInfo.capacity == 0) {
matchingEnergyInfo.status = 1;
}
// 记录交易
tradeCount++;
trades[tradeCount] = Trade(
tradeCount,
msg.sender,
matchingEnergyInfo.publisher,
tradeAmount,
tradeAmount * energyInfo.price,
block.timestamp,
matchingEnergyInfo.energyType
);
emit TradeMatched(tradeCount, msg.sender, matchingEnergyInfo.publisher, tradeAmount, tradeAmount * energyInfo.price, block.timestamp, matchingEnergyInfo.energyType);
}
// 获取能源信息
function getEnergyInfo(string memory energyInfoId) public view returns (EnergyInfo memory) {
return energyInfos[energyInfoId];
}
// 获取交易记录
function getTrade(uint256 tradeId) public view returns (Trade memory) {
return trades[tradeId];
}
// 查询所有记录
function getAllObjects() public view returns (EnergyInfo[] memory) {
EnergyInfo[] memory allObjects = new EnergyInfo[](recordKeys.length);
for (uint256 i = 0; i < recordKeys.length; i++) {
allObjects[i] = energyInfos[recordKeys[i]];
}
return allObjects;
}
}智能合约的函数及功能