商品溯源合约概念
合约设计
合约间的关系
1个商品种类----》n个商品,同时还可以创建多个商品种类(工厂合约的作用)
编写商品合约
pragma solidity^0.8.7;
contract Goods{
struct TraceData{
address operator; //操作者
uint8 status; //0 生产者,1 运输者,2-超市售卖者,3-消费者
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;//定义常量
//goodsid===>Good;
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.改变商品状态
文章来源:https://www.toymoban.com/news/detail-513216.html
8.查询商品溯源
文章来源地址https://www.toymoban.com/news/detail-513216.html
到了这里,关于Fisco-Bcos智能合约开发案例----商品溯源的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!