商品溯源合约概念

合约设计

合约间的关系
1个商品种类----》n个商品,同时还可以创建多个商品种类(工厂合约的作用)
编写商品合约
pragma solidity^0.8.7;
contract Goods{
struct TraceData{
address operator;
uint8 status;
uint256 timestamp;
string remark;
}
uint8 constant STATUS_CREATE=0;
uint256 goodsID;
uint8 currentStatus;
TraceData[] traceDatas;
event NewStatus(address _operator,uint8 _status,uint256 _time,string remark);
constructor(uint256 _goodsID){
goodsID=_goodsID;
traceDatas.push(TraceData(msg.sender,STATUS_CREATE,block.timestamp,"create"));
currentStatus=STATUS_CREATE;
emit NewStatus(msg.sender,STATUS_CREATE,block.timestamp,"create");
}
function changeStatus(uint8 _status,string memory _remark)public returns(bool){
currentStatus=_status;
traceDatas.push(TraceData(msg.sender,_status,block.timestamp,_remark));
emit NewStatus(msg.sender,_status,block.timestamp,_remark);
return true;
}
function getStatus()public view returns(uint8){
return currentStatus;
}
function getTraceInfo()public view returns(TraceData[]memory){
return traceDatas;
}
}
编写商品种类合约
pragma solidity^0.8.7;
import "./goods.sol";
contract Category{
struct GoodsData{
Goods traceData;
bool isExists;
}
bytes32 currentCategory;
uint8 constant STATUS_INVALID=255;
mapping(uint256=>GoodsData) goods;
event NewGoods(address _operator,uint256 _goodID);
constructor(bytes32 _category){
currentCategory=_category;
}
function createGoods(uint256 _goodsID)public returns(Goods){
require(!goods[_goodsID].isExists,"goodsID already exists");
goods[_goodsID].isExists=true;
Goods _goods = new Goods(_goodsID);
goods[_goodsID].traceData=_goods;
emit NewGoods(msg.sender,_goodsID);
return _goods;
}
function getStatus(uint256 _goodsID)public view returns(uint8){
if(!goods[_goodsID].isExists){
return STATUS_INVALID ;
}
return goods[_goodsID].traceData.getStatus();
}
function changeStatus(uint256 _goodsID,uint8 _status,string memory _remark)public returns(bool){
return goods[_goodsID].traceData.changeStatus(_status,_remark);
}
function getGoods(uint256 _goodsID) public view returns(bool,Goods){
return (goods[_goodsID].isExists,goods[_goodsID],goods[_goodsID].traceData);
}
}
编写工厂合约
pragma solidity^0.8.7;
import "/category.sol";
contract TraceFactory{
struct GoodsCategory{
Category categoryData;
bool isExists;
}
mapping(bytes32=>GoodsCategory) goodsCategorys;
event NewCateGory(address _operator,bytes32 _category);
function NewCateGory(bytes32 _category) public returns(Category){
require(!goodsCategorys[_category].isExists,"category already exists");
Category category = new Category(_category);
goodsCategorys[_category].isExists=true;
goodsCategorys[_category].categoryData=category;
emit NewCateGory(msg.sender,_category);
return category;
}
function newGoods(bytes32 _category,uint256 _goodsID)public returns(Goods){
Category category = getCategory(_category);
return category.createGoods(_goodsID);
}
function getCategory(bytes32 _category)public view returns(Category){
return goodsCategorys[_category].categoryData;
}
function getStatus(bytes32 _category,uint256 _goodsID)public view returns(uint8){
Category category = getCategory(_category);
return category.getStatus(_goodsID);
}
function changeStatus(bytes32 _category,uint256 _goodsID,uint8 _status,string memory _remark)public returns(bool){
Category category = getCategory(_category);
return category.changeStatus(_goodsID,_status,_remark);
}
function calCategory(string memory _name)public view returns(bytes32){
return keccak256(abi.encode(_name));
}
}
测试
1.部署工厂合约

2.创建商品种类

3. 创建对应的商品

4.查询商品种类

5. 查询商品状态
0–生产者,1—运输者,2—超市售卖者,3—消费者

6. 查询商品溯源信息

7.改变商品状态

8.查询商品溯源

|