# Case food traceability

## 1 Parallel Chain Environment Deployment

## 2 Contract Compiling

Use the Solidity compiler to compile contract, the following tools are available :

[Remix-ide](http://remix.ethereum.org/)

[Intellij-Solidity](https://plugins.jetbrains.com/plugin/9475-intellij-solidity)

#### 2.1 Remix-ide

Create a new solidity contract, and select the compiler version to compile.

![remix_prepare](../../../assets/en/dg39.png)

Once compiled, the ABI and Bytecode can be used to create contracts.

![remix_compare](../../../assets/en/dg40.png)

- ABI generated by remix has characters such as newline, whitespace, etc. Chain33 EVM cannot use it directly, and further formatting is required: remove line breaks and whitespace.
- Chrome does not support copying compiled abi or bin file, so try to use Edge or Internet explorer.

Considering these two points, it is recommended to use Intellij-Solidity plug-ins for compilation.

NOTE：

- When defining a struct in a contract：
- Do not define too many elements, or the compiler will declare exception Stack too deep.
- Do not use nested struct; currently the EVM virtual machine implemented internally by Chain33 does not support nested struct type.

#### 2.2 Intellij-Solidity

##### 2.2.1 Plug-In Installation

Open IntelliJ IDEA, in File->Settings->Plugins, find IntelliJ-Solidity to install。

![intelliJ_solidity](../../../assets/en/dg41.png)

##### 2.2.2 Solc Installation

If no geth node is installed locally, you can get the official compiled version of Chain33 directly or use code to compile locally.
Otherwise you can use node build-in Solc directly.

###### 2.2.2.1 Native Compilation

Get the latest code to compile, github address: [git@github.com](mailto:git@github.com):ethereum/solidity.git

Specific installation process refer to [Official Document](https://solidity.readthedocs.io/en/latest/installing-solidity.html#building-from-source)

###### 2.2.2.2 Get the Compiled Version of Chain33

windows：
https://bty.oss-ap-southeast-1.aliyuncs.com/solc/solc_0.5.5-windows.zip

linux：
https://bty.oss-ap-southeast-1.aliyuncs.com/solc/solc_0.5.5-linux.tar.gz

macOS：
https://bty.oss-ap-southeast-1.aliyuncs.com/solc/solc_0.5.5-macOS.tar.gz

##### 2.2.3 Contract Compilation

Use Build->Compile Solidity to compile contract, the compiled results can be seen in the project bar.

![compile_solidity](../../../assets/en/dg42.png)

Use the generated abi, bin files for contract creation.

### 3 Contrct Creation

For details see [HERE](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#3-合约的创建)

### 4 Case Implementation

To realize a food traceability process is to achieve the origin, production, circulation and other links of traceability.

The actual traceability is a very complicated process, which is reduced to the following steps: farm, food factory, supermarket, food quality inspection department, and user.
Properties of the product, take live pigs as an example.

#### 4.1 Information Input

##### 4.1.1 Add Pork Information

Cattle ranches selling

| Batch Number | Name   | Weight | Date     | Origin  |
| :----------- | :----- | :----- | :------- | :------ |
| 00001        | pig001 | 500    | 20190221 | NanJing |

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#411-猪肉信息录入)

##### 4.1.2 Add Food Information

Food factory production (for example, ham)

| Food number(such as QR code) | Name    | Weight | Production Date | Packing Date | Expiration Date | Pork Batch |
| :--------------------------- | :------ | :----- | :-------------- | :----------- | :-------------- | :--------- |
| 001                          | food001 | 500    | 20190215        | 20190220     | 20210215        | 00001      |

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#412-食品信息录入)

##### 4.1.3 Add Quality Control Information

Food quality inspection department sampling

| Food number(such as QR code) | Test Time | Test Results | Test Instructions |
| :--------------------------- | :-------- | :----------- | :---------------- |
| food001                      | 20190226  | Qualified    | The food is good. |

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#413-质检信息录入)

##### 4.1.4 Add Supermarket Shelf Information

| Food number(such as QR code) | Last Date |
| :--------------------------- | :-------- |
| food001                      | 20190304  |

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#414-上架信息录入)

##### 4.1.5 User Rating Update

| Food number(such as QR code) | User Rating |
| :--------------------------- | :---------- |
| food001                      | 90          |

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#415-评分信息录入)

#### 4.2 Information Inquiry

##### 4.2.1 Pork Information Inquiry

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#423-火腿原材料猪肉信息查询)

##### 4.2.2 Product Information Inquiry

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#421-火腿信息查询)

##### 4.2.3 Quality Testing Information Inquiry

[Call Contract](https://github.com/lynAzrael/L/blob/master/share/solidity/food_trace.md#422-质检信息查询)

