This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogisticsForm.sol
168 lines (128 loc) · 6.68 KB
/
LogisticsForm.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.10;
pragma experimental ABIEncoderV2;
// Importing LogisticsInfo and CommodityInfo contracts
import "./LogisticsInfo.sol";
import "./CommodityInfo.sol";
contract LogisticsForm {
struct Form {
address logistics;
address[] transit;
string[] contact;
string[] transitAddrInfo;
}
mapping(address => Form) public forms;
address[] public formAddresses;
// Reference to LogisticsInfo contract
LogisticsInfo public logisticsInfo;
// Reference to CommodityInfo contract
CommodityInfo public commodityInfo;
constructor(address _logisticsInfoAddress, address _commodityInfoAddress) public {
logisticsInfo = LogisticsInfo(_logisticsInfoAddress);
commodityInfo = CommodityInfo(_commodityInfoAddress);
}
event FormCreated(address indexed formAddress, address indexed logisticsAddress);
function createLogisticsForm(address _logisticsAddress, address _transitAddress, string memory _transitContact, string memory _transitAddrInfo) public returns (address) {
require(_logisticsAddress != address(0), "物流表单头信息不得为空!");
require(_transitAddress != address(0), "中转方区块链地址不得为空!");
require(bytes(_transitContact).length > 0, "中转方联系方式不得为空!");
require(bytes(_transitAddrInfo).length > 0, "中转方地址信息不得为空!");
address formAddress = address(bytes20(keccak256(abi.encodePacked(msg.sender, block.timestamp))));
Form storage newForm = forms[formAddress];
newForm.logistics = _logisticsAddress;
newForm.transit.push(_transitAddress);
newForm.contact.push(_transitContact);
newForm.transitAddrInfo.push(_transitAddrInfo);
formAddresses.push(formAddress);
emit FormCreated(formAddress, _logisticsAddress);
return formAddress;
}
function getFormInfo(address _formAddress) public view returns (string memory, string memory, string memory, string memory, string memory) {
Form storage form = forms[_formAddress];
string memory logisticsInfoJson = logisticsInfo.getLogisticsInfo(form.logistics);
string memory formAddr = string(
abi.encodePacked(
'{',
'"formAddr":"', toString(_formAddress), '",',
'}'
)
);
uint256 length = form.transit.length;
string[] memory transitAddresses = new string[](length);
string[] memory transitContacts = new string[](length);
string[] memory transitAddrInfos = new string[](length);
for (uint256 i = 0; i < length; i++) {
transitAddresses[i] = toString(form.transit[i]);
transitContacts[i] = form.contact[i];
transitAddrInfos[i] = form.transitAddrInfo[i];
}
string memory transitAddressesJson = toJson(transitAddresses, "transitAddresses");
string memory transitContactsJson = toJson(transitContacts, "transitContacts");
string memory transitAddrInfosJson = toJson(transitAddrInfos, "transitAddrInfos");
return (logisticsInfoJson, formAddr ,transitAddressesJson, transitContactsJson, transitAddrInfosJson);
}
function getAllForms() public view returns (address[] memory) {
return formAddresses;
}
function updateLogisticsForm(address _formAddress, address _transitAddress, string memory _transitContact, string memory _transitAddrInfo) public {
Form storage form = forms[_formAddress];
require(form.logistics != address(0), "Form address does not exist");
form.transit.push(_transitAddress);
form.contact.push(_transitContact);
form.transitAddrInfo.push(_transitAddrInfo);
}
// Expose CommodityInfo functions
function storeProductInfo(string memory _name, string memory _productionDate, string memory _expiryDate, string memory _type, string memory _barcode) public returns (address) {
return commodityInfo.storeProduct(_name, _productionDate, _expiryDate, _type, _barcode);
}
function getProductInfo(address _productAddress) public view returns (string memory) {
return commodityInfo.getProductInfo(_productAddress);
}
function getAllProducts() public view returns (string[] memory, address[] memory) {
return commodityInfo.getAllProductsInfo();
}
// Expose LogisticsInfo functions
function createLogisticsInfo(address _senderAddress, string memory _logisticsCompanyName, string memory _senderAddressInfo, string memory _senderContact, address _receiverAddress, address _productAddress, string memory _receiverAddressInfo, string memory _receiverContact) public returns (address) {
return logisticsInfo.storeLogisticsInfo(_senderAddress, _logisticsCompanyName, _senderAddressInfo, _senderContact, _receiverAddress, _productAddress, _receiverAddressInfo, _receiverContact);
}
function getLogisticsInfo(address _logisticsAddress) public view returns (string memory) {
return logisticsInfo.getLogisticsInfo(_logisticsAddress);
}
function getAllLogisticsAddresses() public view returns (address[] memory) {
return logisticsInfo.getAllLogisticsAddresses();
}
function getAllLogisticsInfo() public view returns (string[] memory, address[] memory){
return logisticsInfo.getAllLogisticsInfo();
}
// Helper function to convert address to string
function toString(address _addr) internal pure returns(string memory) {
bytes32 value = bytes32(uint256(_addr));
bytes memory alphabet = "0123456789abcdef";
bytes memory str = new bytes(42);
str[0] = '0';
str[1] = 'x';
for (uint256 i = 0; i < 20; i++) {
str[2+i*2] = alphabet[uint8(value[i + 12] >> 4)];
str[3+i*2] = alphabet[uint8(value[i + 12] & 0x0f)];
}
return string(str);
}
function toJson(string[] memory _values, string memory _key) internal pure returns(string memory) {
bytes memory jsonStringBytes = abi.encodePacked(
'{"', _key, '": ['
);
for (uint256 i = 0; i < _values.length; i++) {
jsonStringBytes = abi.encodePacked(
jsonStringBytes,
'"', _values[i], '"'
);
if (i < _values.length - 1) {
jsonStringBytes = abi.encodePacked(jsonStringBytes, ',');
}
}
jsonStringBytes = abi.encodePacked(jsonStringBytes, "] }");
// Convert the resulting bytes to a string
string memory jsonString = string(jsonStringBytes);
return jsonString;
}
}