-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExample.sol
More file actions
158 lines (146 loc) · 4.6 KB
/
Example.sol
File metadata and controls
158 lines (146 loc) · 4.6 KB
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
// SPDX-License-Identifier: CC0-1.0
pragma solidity >=0.8.0;
import "./Types.sol";
import "./IComponent.sol";
contract Component is IComponent {
//@dev Types contains all data types in solidity
mapping(string => Types.Type[]) methodRequests;
mapping(string => Types.Type[]) methodResponses;
mapping(MethodTypes => string[]) methods;
mapping(string => string) instructions;
//@dev define the data type of this component
struct Profiles {
string name;
uint256 age;
}
mapping(address => Profiles) users;
constructor() {
Types.Type[] memory getReqArray = new Types.Type[](1);
getReqArray[0] = Types.Type.ADDRESS;
Types.Type[] memory dataTypeArray = new Types.Type[](2);
dataTypeArray[0] = Types.Type.STRING;
dataTypeArray[1] = Types.Type.UINT256;
Types.Type[] memory putReqArray = new Types.Type[](2);
putReqArray[0] = Types.Type.ADDRESS;
putReqArray[1] = Types.Type.STRING;
// @dev initialize get, post, put request parameter data types and response data types
setMethod(
"getUser",
MethodTypes.GET,
getReqArray,
dataTypeArray,
"get user profiles"
);
setMethod(
"createUser",
MethodTypes.POST,
dataTypeArray,
dataTypeArray,
"Create user profiles"
);
setMethod(
"updateUserName",
MethodTypes.PUT,
putReqArray,
new Types.Type[](0),
"Update user information"
);
}
function get(string memory _methodName, bytes memory _methodReq)
public
view
returns (bytes memory)
{
if (compareStrings(_methodName, "getUser")) {
address user = abi.decode(_methodReq, (address));
bytes memory userData = abi.encode(
users[user].name,
users[user].age
);
return userData;
} else {
return abi.encode("");
}
}
function post(string memory _methodName, bytes memory _methodReq)
public
payable
returns (bytes memory)
{
if (compareStrings(_methodName, "createUser")) {
(string memory name, uint256 age) = abi.decode(
_methodReq,
(string, uint256)
);
users[msg.sender] = Profiles(name, age);
bytes memory resBytes = abi.encode(name, age);
emit Response(resBytes);
return resBytes;
}
return abi.encode("");
}
function put(string memory _methodName, bytes memory _methodReq)
public
payable
returns (bytes memory)
{
if (compareStrings(_methodName, "updateUserName")) {
(address userAddress, string memory name) = abi.decode(
_methodReq,
(address, string)
);
require(userAddress == msg.sender);
users[userAddress].name = name;
}
return abi.encode("");
}
function options() public pure returns (MethodTypes[] memory) {
MethodTypes[] memory methodTypes = new MethodTypes[](3);
methodTypes[0] = MethodTypes.GET;
methodTypes[1] = MethodTypes.POST;
methodTypes[2] = MethodTypes.PUT;
return methodTypes;
}
function setMethod(
string memory _methodName,
MethodTypes _methodType,
Types.Type[] memory _methodReq,
Types.Type[] memory _methodRes,
string memory _instruction
) private {
methods[_methodType].push(_methodName);
methodRequests[_methodName] = _methodReq;
methodResponses[_methodName] = _methodRes;
instructions[_methodName] = _instruction;
}
function getMethodReqAndRes(string memory _methodName)
public
view
returns (Types.Type[] memory, Types.Type[] memory)
{
return (methodRequests[_methodName], methodResponses[_methodName]);
}
function getMethods(MethodTypes _methodTypes)
public
view
returns (string[] memory)
{
return methods[_methodTypes];
}
function getMethodInstruction(string memory _methodName)
public
view
returns (string memory)
{
return instructions[_methodName];
}
//@dev compares two strings for equality
function compareStrings(string memory _a, string memory _b)
private
pure
returns (bool)
{
return
keccak256(abi.encodePacked(_a)) == keccak256(abi.encodePacked(_b));
}
}