以太坊·食品溯源案例

简介: 背景 需求是通过区块链跟踪产品,实现产品产地,生产,流通等环节溯源。 需求归纳,需要实现下面几点: 产品具备通用的属性,例如名称,价格,重量,颜色,体积等等 生产销售链条跟踪

以太坊·食品溯源案例

本文节选自电子书《Netkiller Blockchain 手札》

Netkiller Blockchain 手札

本文作者最近在找工作,有意向致电 13113668890

Mr. Neo Chan, 陈景峯(BG7NYT)

中国广东省深圳市龙华新区民治街道溪山美地 518131

+86 13113668890 <netkiller@msn.com>

文档始创于2018-02-10

版权 © 2018 Netkiller(Neo Chan). All rights reserved.

版权声明

转载请与作者联系,转载时请务必标明文章原始出处和作者信息及本声明。

http://www.netkiller.cn

http://netkiller.github.io

http://netkiller.sourceforge.net

微信订阅号 netkiller-ebook (微信扫描二维码)

QQ:13721218 请注明“读者”

QQ群:128659835 请注明“读者”

内容摘要

这一部关于区块链开发及运维的电子书。

为什么会写区块链电子书?因为2018年是区块链年。

这本电子书是否会出版(纸质图书)? 不会,因为互联网技术更迭太快,纸质书籍的内容无法实时更新,一本书动辄百元,很快就成为垃圾,你会发现目前市面的上区块链书籍至少是一年前写的,内容已经过时,很多例子无法正确运行。所以我不会出版,电子书的内容会追逐技术发展,及时跟进软件版本的升级,做到内容最新,至少是主流。

这本电子书与其他区块链书籍有什么不同?市面上大部分区块链书籍都是用2/3去讲区块链原理,只要不到 1/3 的干货,干货不够理论来凑,通篇将理论或是大谈特谈区块链行业,这些内容更多是头脑风暴,展望区块链,均无法落地实施。本书与那些书籍完全不同,不讲理论和原理,面向应用落地,注重例子,均是干货。

电子书更新频率?每天都会有新内容加入,更新频率最迟不会超过一周,更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master

本文采用碎片化写作,原文会不定期更新,请尽量阅读原文。

http://www.netkiller.cn/blockchain/index.html

您的打赏是我的写作动力:http://www.netkiller.cn/blockchain/donations.html

-----------------------------------

食品溯源案例

背景

需求是通过区块链跟踪产品,实现产品产地,生产,流通等环节溯源。

需求归纳,需要实现下面几点:

产品具备通用的属性,例如名称,价格,重量,颜色,体积等等

生产销售链条跟踪

涉及环节,农产品的供应链是一个非常复杂的过程,涉及多方,农业局、卫生局、药监局、工商局、环保局等多个部门交织其中。

合约设计

我们设计一个简单的合约

pragma solidity ^0.4.20;

contract Trace {

    enum State { Origin, Factory, QA, Shipping, Received, Pending }
    
    string name;
    uint price;
    uint weight;
    bool lock = false;    //合约锁
    bool close = false;    //合约状态
    uint number = 1;
    uint attr_number = 1;
    
    mapping (address  => string) guestbook; //客户留言本    

    struct Attribute {
        address owner;    // 供应商
        string name;        // 属性的名字
        string date;        // 生产日期
        string desc;        // 描述信息
        
    }
    mapping (uint  => Attribute) attribute;

    struct Logistics {
        address owner;    // 中转站
        string date;    // 转运日期
        State status;   // 状态
        string message; // 留言信息
    }
    mapping (uint  => Logistics) stations;
    
    function Trace(string _name, uint _price, uint _weight) public {
        name = _name;
        price = _price;
        weight = _weight;
    }
    // 名称
    function getName() public view returns(string){
        return name;
    }
    // 价格
    function getPrice() public view returns(uint){
        return price;
    }
    // 重量
    function getWeight() public view returns(uint){
        return weight;
    }
    
     // 增加商品属性
    function putAttribute(address _owner,string _name, string _date, string _desc ) public{
        if(lock == false){
                Attribute memory item = Attribute(_owner, _name,_date,_desc);
                attribute[attr_number] = item;
                attr_number = attr_number + 1;
        }
    }

    // 获得属性
    function getAttribute(uint _attr_number) public view returns(address, string, string, string) {
        require(_attr_number < attr_number);
        Attribute memory item = attribute[_attr_number];
        
        return (item.owner, item.name, item.date, item.desc);
    }
    
    // 增加物流中转信息
    function putLogistics(address _owner,string _date, State _status, string _message ) public{
        if(close == false){
            Logistics memory node = Logistics(_owner,_date,_status,_message);
            stations[number] = node;
            number = number + 1;
            lock = true;
        }
        if (_status == State.Received) {
            close = true;
        }
    }

    // 获得中转信息
    function getLogistics(uint _number) public view returns(address, string, State, string) {
        require(_number < number);

        Logistics memory node = stations[_number];
        
        return (node.owner, node.date, node.status, node.message);
    }
    
    // 或者转中站数量
    function getLogisticsCount() public view returns(uint){
        return number;
    }
    
    // 客户留言
        function addGuestbook(address _owner, string message) public{
        guestbook[_owner] = message;
    }
}

怎样使用这个合约呢?

合约部署,需要输入三个参数,分别是名称,价格和装量

Trace(string _name, uint _price, uint _weight)     

应用场景一

调用合约案例一,这是没有经过深加工的原产品案例。例如 Trace("山羊肉", 25, 50)

var contract;
Trace.deployed().then(function(instance){contract=instance;});
contract.getName();
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","颜色", "", "黑色")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","产地", "", "内蒙古")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","出生", "2017-01-12", "XXX牧场")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","宰杀", "2018-02-12", "XXX宰杀")

contract.putLogistics("0x627306090abab3a6e1400e9345bc60c78a8bef57","2018-02-20",0,"XXX牧场");
contract.putLogistics("0x627306090abab3a6e1400e9345bc60c78a8bef57","2018-02-20",1,"XXX屠宰公司");
contract.putLogistics("0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef","2018-02-22",2,"XXX检验检疫");
contract.putLogistics("0xf17f52151ebef6c7334fad080c5704d77216b732","2018-02-21",3,"XXX一级经销商");
contract.putLogistics("0x821aea9a577a9b44299b9c15c88cf3087f3b5544","2018-02-23",3,"XXX二级经销商");
contract.putLogistics("0x821aea9a577a9b44299b9c15c88cf3087f3b5544","2018-02-24",3,"XXX批发中心");
contract.putLogistics("0x821aea9a577a9b44299b9c15c88cf3087f3b5544","2018-02-25",3,"XXX超市");
contract.putLogistics("0x0d1d4e623d10f9fba5db95830f7d3839406c6af2","2018-02-26",4,"用户包裹收到");

contract.getNode(); // 获得物流经过的转运站数量        
应用场景二

调用合约案例二,经过深加工的原产品案例。

例如 Trace("牦牛肉干", 80, 500)

var contract;
Trace.deployed().then(function(instance){contract=instance;});
contract.getName();
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","调和油", "2016-10-10", "银龙鱼牌")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","辣椒粉", "2016-10-30", "西藏XXX公司生产")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","生抽", "2016-01-12", "XXX生抽,XXX生产")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","山梨酸钾", "2017-02-12", "XXX生产")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","防腐剂", "2017-02-12", "XXX生产")
contract.putAttribute("0x627306090abab3a6e1400e9345bc60c78a8bef57","牦牛肉", "2017-02-12", "XXX牧场")

contract.putLogistics("0x627306090abab3a6e1400e9345bc60c78a8bef57","2018-02-20",0,"XXX牧场");
contract.putLogistics("0x627306090abab3a6e1400e9345bc60c78a8bef57","2018-02-20",1,"XXX公司生产");
contract.putLogistics("0xc5fdf4076b8f3a5357c5e395ab970b5b54098fef","2018-02-22",2,"XXX通过QA、QC");
contract.putLogistics("0xf17f52151ebef6c7334fad080c5704d77216b732","2018-02-21",3,"XXX一级经销商");
contract.putLogistics("0x821aea9a577a9b44299b9c15c88cf3087f3b5544","2018-02-23",3,"XXX二级经销商");
contract.putLogistics("0x821aea9a577a9b44299b9c15c88cf3087f3b5544","2018-02-24",3,"XXX批发中心");
contract.putLogistics("0x821aea9a577a9b44299b9c15c88cf3087f3b5544","2018-02-25",3,"XXX超市");
contract.putLogistics("0x0d1d4e623d10f9fba5db95830f7d3839406c6af2","2018-02-26",4,"用户包裹收到");

contract.getNode(); // 获得物流经过的转运站数量        

用户留言

contract.addGuestbook("0x0d1d423e623d10f9d10f9d10f9d10f9d10f9fba5","东西好吃,下次还买,给好评");

以上仅仅是作者的想法,这是一个学习和熟悉只能合约开发的例子,真实场景不太可行,因为以太坊无法处理高TPS的场景。

目录
相关文章
|
机器人 区块链
区块链数字货币量化交易系统机器人开发合约源码定制详情
event BuyOrderPlaced(address user, uint256 price, uint256 amount); event SellOrderPlaced(address user, uint256 price, uint256 amount);
|
区块链
经典智能合约之智能拍卖
经典智能合约之智能拍卖
147 0
经典智能合约之智能拍卖
|
Web App开发 JavaScript 前端开发
openseaNFT艺术品交易平台开发部署 | openseaNFT艺术品交易平台开发源码示例
OpenSea的主要特点是统一的平台和标准化的流程。用户可以在平台上创建和管理自己的NFT,如艺术品、虚拟房地产、数字收藏品等。同时,OpenSea还提供了一个拍卖市场,让用户可以出售自己的NFT并获得收益。此外,该平台还支持用户在线浏览和购买其他用户铸造的NFT。
|
安全 数据挖掘 区块链
区块链交易所开发运营版丨区块链交易所系统开发详情案例/源码功能/成熟技术
  Smart contracts are one of the core components of the blockchain public chain and an important carrier for public chain applications.Smart contracts are programmable scripts that can automatically execute protocols,rules,and conditions.Through smart contracts,various complex business functions on
|
JavaScript 前端开发 测试技术
区块链泰山众筹互助商城dapp系统开发合约定制(代币分红模式)技术分析
区块链泰山众筹互助商城dapp系统开发合约定制(代币分红模式)技术分析
|
供应链 区块链 数据安全/隐私保护
什么是智能合约?质押借贷理财挖矿dapp系统开发案例源代码详情
什么是智能合约?质押借贷理财挖矿dapp系统开发案例源代码详情
|
数据挖掘
借贷理财流动性挖矿开发详情丨借贷理财流动性挖矿系统开发(案例及源码)
 The"sharing"of the metauniverse ontology can be expressed in the form of standardization,which is the best order obtained by consensus within a certain range on the basis of scientific research and theoretical practice.The standards of the universe can be divided into five categories:basic,technolo
|
安全 5G
dapp借贷理财挖矿开发案例丨dapp借贷理财挖矿系统开发(开发逻辑及方案)丨dapp借贷理财挖矿系统源码
  Blockchain is a distributed ledger that is open to all.It uses blockchain data structure to verify and store data,and uses cryptography to ensure the security of data transmission and access.It has the characteristics of decentralization,tamper-proof,transparency and openness.
|
区块链
区块链交易所系统开发(参考版)丨区块链交易所系统开发(海外版)丨区块链交易所系统源码案例分析
  量化交易的主要特点:买卖双方不再是由人们的主观判断决定的,改为由定量模型决定的。建立定量交易的数学模型,即为交易设定触发条件。程序系统严格按照数学模型或市场交易条件,自动实现买卖的实际操作。
|
安全 区块链 数据安全/隐私保护
交易所源码购买数字货币量化交易平台 区块链项目源码 交易系统源码 币币交易源码 区块链游戏源码 火币交易所源码 交易系统源码
去中心化交易所开发 币严交易所搭建 数字货币交易所系统开发 开发一个交易所需要多久 交易所系统开发搭建 合约交易所系统搭建开发 区块链交易所如何搭建 交易所系统搭建 如何搭建一个区块链数字货币交易所系统 区块链搭建交易所 开发交易所交易系统 数字货币区块链交易所系统搭建方案  数字货币交易所源码 数字货币源码 合约交易所源码