Fisco-Bcos智能合约开发案例----商品溯源

该博客介绍了如何使用智能合约实现商品的溯源管理。通过商品合约、商品种类合约和工厂合约的交互,实现了从生产到消费的全程跟踪。商品合约记录了每个状态变更,包括操作者、状态、时间戳和备注。商品种类合约则用于创建和管理不同种类的商品,而工厂合约作为顶层合约,负责创建商品种类和商品,并进行状态查询和修改。测试部分涵盖了合约的部署、商品创建、状态查询和变更等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

商品溯源合约概念

在这里插入图片描述

合约设计

在这里插入图片描述

合约间的关系

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.改变商品状态

在这里插入图片描述

8.查询商品溯源

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜鸟逆袭之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值