更新文章
This commit is contained in:
@@ -35,7 +35,12 @@ const solidity = defineNoteConfig({
|
|||||||
{
|
{
|
||||||
text: "基础语法", prefix: "/basic-syntax", items: [
|
text: "基础语法", prefix: "/basic-syntax", items: [
|
||||||
{ text: "Solidity 基础语法与数据类型", link: "/programming/solidity/basic-syntax/" }
|
{ text: "Solidity 基础语法与数据类型", link: "/programming/solidity/basic-syntax/" }
|
||||||
]
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
text: "项目实例", prefix: "/basic-syntax", items: [
|
||||||
|
{ text: "Solidity 代码实例", link: "/programming/solidity/analysis/case-analysis/" }
|
||||||
|
],
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|||||||
359
docs/notes/programming/solidity/case-analysis.md
Normal file
359
docs/notes/programming/solidity/case-analysis.md
Normal file
@@ -0,0 +1,359 @@
|
|||||||
|
---
|
||||||
|
title: 十份代码实例
|
||||||
|
createTime: 2025/10/10 11:07:11
|
||||||
|
permalink: /programming/solidity/analysis/case-analysis/
|
||||||
|
password: simeng
|
||||||
|
---
|
||||||
|
|
||||||
|
## 题目一
|
||||||
|
## 题目二
|
||||||
|
## 题目三
|
||||||
|
## 题目四
|
||||||
|
原题:
|
||||||
|
1.编写原材料接口newMaterial,初始化原材料信息,返回合约地址,并实现原材料信息上链功能。
|
||||||
|
2.编写获取存原材料接口getMaterial,根据合约地址获取原材料信息
|
||||||
|
3.编写食品物流上链接口addLogistic,实现食品物流信息上链功能
|
||||||
|
4.编写获取食品物流信息的接口getLogistics,根据食品产品编号获取物流信息
|
||||||
|
::: code-tabs
|
||||||
|
@tab Logistics.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity 0.6.10;
|
||||||
|
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
contract Logistics {
|
||||||
|
|
||||||
|
struct LogisticsData {
|
||||||
|
address cargo; //货物合约地址
|
||||||
|
address orgin; //货物上级合约地址
|
||||||
|
address destination; //货物下级合约地址
|
||||||
|
string memo; //备注信息
|
||||||
|
uint createdAt; //创建时间
|
||||||
|
uint queryCount; //已查询次数
|
||||||
|
}
|
||||||
|
|
||||||
|
LogisticsData[] private _logisticsData; //全部物流信息数组
|
||||||
|
|
||||||
|
uint public recordCount;//所有记录条数
|
||||||
|
uint public queryCount;//所有查询次数
|
||||||
|
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function addLogistics(address _cargo,address _orgin,【请补充】) public {
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
}
|
||||||
|
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function getLogisticsInfo(address _cargo) public returns(LogisticsData[] memory _cargoLogisticsData) {
|
||||||
|
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@tab Material.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity 0.6.10;
|
||||||
|
|
||||||
|
contract Material {
|
||||||
|
struct Material {
|
||||||
|
address owner;
|
||||||
|
string name;
|
||||||
|
string id;
|
||||||
|
string memo;
|
||||||
|
uint createdAt;
|
||||||
|
bool exist;
|
||||||
|
}
|
||||||
|
|
||||||
|
mapping(string => Material) public materials;
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function newMaterial(string memory _name,string memory _id,【请补充】) public {
|
||||||
|
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function getMaterial(string memory id) public view returns(【请补充】) {
|
||||||
|
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
:::
|
||||||
|
|
||||||
|
参考答案:
|
||||||
|
::: code-tabs
|
||||||
|
@tab Logistics.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity 0.6.10;
|
||||||
|
|
||||||
|
pragma experimental ABIEncoderV2;
|
||||||
|
|
||||||
|
contract Logistics {
|
||||||
|
|
||||||
|
struct LogisticsData {
|
||||||
|
address cargo; //货物合约地址
|
||||||
|
address orgin; //货物上级合约地址
|
||||||
|
address destination; //货物下级合约地址
|
||||||
|
string memo; //备注信息
|
||||||
|
uint createdAt; //创建时间
|
||||||
|
uint queryCount; //已查询次数
|
||||||
|
}
|
||||||
|
|
||||||
|
LogisticsData[] private _logisticsData; //全部物流信息数组
|
||||||
|
|
||||||
|
uint public recordCount;//所有记录条数
|
||||||
|
uint public queryCount;//所有查询次数
|
||||||
|
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function addLogistics(address _cargo,address _orgin,【请补充】) public {
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
}
|
||||||
|
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function getLogisticsInfo(address _cargo) public returns(LogisticsData[] memory _cargoLogisticsData) {
|
||||||
|
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@tab Material.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity 0.6.10;
|
||||||
|
|
||||||
|
contract Material {
|
||||||
|
struct Material {
|
||||||
|
address owner;
|
||||||
|
string name;
|
||||||
|
string id;
|
||||||
|
string memo;
|
||||||
|
uint createdAt;
|
||||||
|
bool exist;
|
||||||
|
}
|
||||||
|
|
||||||
|
mapping(string => Material) public materials;
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function newMaterial(string memory _name,string memory _id,【请补充】) public {
|
||||||
|
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//可自行添加形参和返回值
|
||||||
|
function getMaterial(string memory id) public view returns(【请补充】) {
|
||||||
|
|
||||||
|
//TODO:”请补充缺失代码
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
:::
|
||||||
|
|
||||||
|
## 题目五
|
||||||
|
## 题目六
|
||||||
|
## 题目七
|
||||||
|
原题:
|
||||||
|
**子任务2-2-1:航班延误保险购买合约编码(5分)**
|
||||||
|
|
||||||
|
(1)编写航班保险购买上链接口,完成只有购买机票的用户可以购买保险、必须缴纳正确的保费金额、购买保险的时间不能超过购买机票后的0.5小时的功能,符合条件则用户可以购买保险,将用户购买保险状态上链;(3分)
|
||||||
|
|
||||||
|
(2)编写退保接口,完成保险公司预存赔偿金后,用户无法退保,反之用户可退保的功能,将用户退保状态上链,并退还用户保费功能。(2分)
|
||||||
|
|
||||||
|
**子任务2-2-2:航班信息存证上链合约编码**
|
||||||
|
|
||||||
|
(1)编写获取航班信息接口,实现依据航班号获得航班号对应的计划起飞时间、实际起飞时间、到达时间、是否延误状态的功能;
|
||||||
|
|
||||||
|
(2)编写判断航班是否延误接口,实现依据航班号获得航班号对应的航班是否延误,得到航班是否延误的结果功能。
|
||||||
|
|
||||||
|
**子任务2-2-3:航班延误险理赔合约编码**
|
||||||
|
|
||||||
|
(1)编写客户理赔接口,实现如果航班延误超过4小时,将赔偿金赔偿给乘客的功能;
|
||||||
|
|
||||||
|
(2)编写保险公司收取保费接口,实现如果航班没有延误或者延误时间少于4小时,将保费转账给保险公司,并退还赔偿金的功能。
|
||||||
|
|
||||||
|
::: code-tabs
|
||||||
|
@tab Claims.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity ^0.6.10;
|
||||||
|
|
||||||
|
import "./FlightDelayInsurance.sol";
|
||||||
|
|
||||||
|
contract Claims is FlightDelayInsurance{
|
||||||
|
|
||||||
|
// 定义航班信息结构体
|
||||||
|
struct Flight {
|
||||||
|
uint256 departureTime; // 航班出发时间
|
||||||
|
uint256 delayTime; // 航班延误时间
|
||||||
|
bool isDelayed; // 航班是否延误
|
||||||
|
bool isInsured; // 航班是否购买保险
|
||||||
|
}
|
||||||
|
|
||||||
|
// 定义保险公司地址
|
||||||
|
address public insuranceCompany;
|
||||||
|
|
||||||
|
// 定义航班信息映射
|
||||||
|
mapping(bytes32 => Flight) public flights;
|
||||||
|
|
||||||
|
// 定义购买保险事件
|
||||||
|
event BuyInsurance(bytes32 flightKey, address passenger, uint256 premium);
|
||||||
|
|
||||||
|
// 定义航班延误事件
|
||||||
|
event FlightDelay(bytes32 flightKey, uint256 delayTime);
|
||||||
|
|
||||||
|
// 定义理赔事件
|
||||||
|
event Claim(bytes32 flightKey, address passenger, uint256 amount);
|
||||||
|
|
||||||
|
// 定义保险公司预存的赔偿金
|
||||||
|
uint256 public compensationNew;
|
||||||
|
|
||||||
|
// 构造函数,初始化保险公司地址和赔偿金
|
||||||
|
constructor() FlightDelayInsurance(platformS,airlineV,insuranceCompanyC,premium,compensation) public {
|
||||||
|
insuranceCompany = msg.sender;
|
||||||
|
compensationNew = compensation;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新航班信息函数
|
||||||
|
function updateFlight(bytes32 flightKey, uint256 departureTime, uint256 delayTime, bool isDelayed) payable public {
|
||||||
|
// 判断调用者是否为保险公司
|
||||||
|
require(msg.sender == insuranceCompany, "Only insurance company can update flight information");
|
||||||
|
// 更新航班信息
|
||||||
|
flights[flightKey].departureTime = departureTime;
|
||||||
|
flights[flightKey].isDelayed = isDelayed;
|
||||||
|
|
||||||
|
/*********** 客户理赔接口 **********/
|
||||||
|
//任务2-2-3,请编写合约代码
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********** 客户理赔接口 ***********/
|
||||||
|
|
||||||
|
/*********** 保险公司收取保费接口开发 **********/
|
||||||
|
//任务2-2-3,请编写合约代码
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********** 保险公司收取保费接口开发 ***********/
|
||||||
|
// 触发航班延误事件
|
||||||
|
emit FlightDelay(flightKey, delayTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
@tab FlightDelayInsurance.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity ^0.6.10;
|
||||||
|
|
||||||
|
contract FlightDelayInsurance {
|
||||||
|
address public platformS; // 平台S的地址
|
||||||
|
address public airlineV; // 航空公司V的地址
|
||||||
|
address public insuranceCompanyC; // 保险公司C的地址
|
||||||
|
uint public premium; // 保险费
|
||||||
|
uint public compensation; // 赔偿金额
|
||||||
|
uint public purchaseTime; // 购买保险的时间
|
||||||
|
uint public depositTime; // 存入赔偿金额的时间
|
||||||
|
bool public purchased; // 是否购买了保险
|
||||||
|
bool public deposited; // 是否存入了赔偿金额
|
||||||
|
mapping(address => bool) public insured; // 已退保的用户
|
||||||
|
mapping(address => bool) public policy; // 已生成保单的用户
|
||||||
|
mapping(address => bool) public purchasedTicket; // 已购买机票的用户
|
||||||
|
|
||||||
|
constructor(address _platformS, address _airlineV, address _insuranceCompanyC, uint _premium, uint _compensation) public {
|
||||||
|
platformS = _platformS; // 初始化平台S的地址
|
||||||
|
airlineV = _airlineV; // 初始化航空公司V的地址
|
||||||
|
insuranceCompanyC = _insuranceCompanyC; // 初始化保险公司C的地址
|
||||||
|
premium = _premium; // 初始化保险费
|
||||||
|
compensation = _compensation; // 初始化赔偿金额
|
||||||
|
}
|
||||||
|
|
||||||
|
function purchaseTicket() public {
|
||||||
|
require(!purchasedTicket[msg.sender], "Ticket has already been purchased"); // 该用户已购买机票
|
||||||
|
purchasedTicket[msg.sender] = true; // 标记该用户已购买机票
|
||||||
|
purchaseTime = block.timestamp;
|
||||||
|
}
|
||||||
|
/*********** 航班保险购买上链接口开发 **********/
|
||||||
|
//任务2-2-1,请编写合约代码
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********** 航班保险购买上链接口开发 ***********/
|
||||||
|
function depositCompensation() public payable {
|
||||||
|
require(msg.sender == insuranceCompanyC, "Only insurance company C can deposit compensation"); // 只有保险公司C可以存入赔偿金额
|
||||||
|
require(msg.value == compensation, "Compensation amount is incorrect"); // 赔偿金额不正确
|
||||||
|
require(block.timestamp < depositTime + 2 hours, "Deposit time has expired"); // 存入赔偿金额的时间已过期
|
||||||
|
deposited = true; // 标记已存入赔偿金额
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********** 退保接口开发 **********/
|
||||||
|
//任务2-2-1,请编写合约代码
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********** 退保接口开发 ***********/
|
||||||
|
|
||||||
|
function generatePolicy() public {
|
||||||
|
require(deposited, "Compensation has not been deposited"); // 赔偿金额未存入,无法生成保单
|
||||||
|
require(msg.sender == platformS, "Only platform S can generate policy"); // 只有平台S可以生成保单
|
||||||
|
require(!policy[msg.sender], "Policy has already been generated"); // 该用户已生成保单
|
||||||
|
policy[msg.sender] = true; // 标记该用户已生成保单
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
@tab FlightManagement.sol
|
||||||
|
```solidity
|
||||||
|
pragma solidity ^0.6.10;
|
||||||
|
|
||||||
|
contract FlightManagement {
|
||||||
|
|
||||||
|
// 航班结构体
|
||||||
|
struct Flight {
|
||||||
|
string flightNumber; // 航班号
|
||||||
|
uint256 scheduledDepartureTime; // 计划起飞时间
|
||||||
|
uint256 actualDepartureTime; // 实际起飞时间
|
||||||
|
uint256 arrivalTime; // 到达时间
|
||||||
|
bool delayed; // 是否延误
|
||||||
|
}
|
||||||
|
|
||||||
|
// 航班号到航班信息的映射
|
||||||
|
mapping(string => Flight) flights;
|
||||||
|
|
||||||
|
// 添加航班
|
||||||
|
function addFlight(string memory _flightNumber, uint256 _scheduledDepartureTime, uint256 _arrivalTime) public {
|
||||||
|
flights[_flightNumber] = Flight(_flightNumber, _scheduledDepartureTime, 0, _arrivalTime, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 更新实际起飞时间
|
||||||
|
function updateActualDepartureTime(string memory _flightNumber, uint256 _actualDepartureTime) public {
|
||||||
|
flights[_flightNumber].actualDepartureTime = _actualDepartureTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*********** 获取航班信息接口开发 **********/
|
||||||
|
//任务2-2-2,请编写合约代码
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********** 获取航班信息接口开发 ***********/
|
||||||
|
|
||||||
|
/*********** 编写判断航班是否延误接口开发 **********/
|
||||||
|
//任务2-2-2,请编写合约代码
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/********** 编写判断航班是否延误接口开发 ***********/
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
:::
|
||||||
|
## 题目八
|
||||||
|
## 题目九
|
||||||
|
## 题目十
|
||||||
Reference in New Issue
Block a user