This repository has been archived by the owner on Oct 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEVTExample.sol
204 lines (184 loc) · 10.7 KB
/
EVTExample.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "./EVT.sol";
import "./IEVTExample.sol";
import "./extensions/EVTVariable.sol";
import "../libraries/HexStrings.sol";
import "../libraries/NewtonAddress.sol";
import "../libraries/base64.sol";
contract EVTExample is ERC165, EVT, IEVTExample {
enum Types {Bool, Int256, Uint256, Bytes32, Address, Bytes, String, NewtonAddress}
mapping(bytes32 => string) private _properties;
mapping(bytes32 => Types) private _propertiesTypes;
constructor(string memory name_, string memory symbol_) EVT(name_, symbol_) {}
/**
* @dev See {IERC165-supportsInterface}.
*/
function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165, EVT) returns (bool) {
return interfaceId == type(IEVTExample).interfaceId || super.supportsInterface(interfaceId);
}
function getPropertyId(string memory propertyName) public view virtual override returns (bytes32 propertyId) {
return keccak256(abi.encode(propertyName));
}
function addDynamicProperty(string memory propertyName, Types propertyType) public virtual returns (bool) {
bytes32 propertyId = getPropertyId(propertyName);
_propertiesTypes[propertyId] = propertyType;
// EVTVariable.addDynamicProperty(propertyId); // TODO:
return true;
}
function setBytesDynamicProperty(uint256 tokenId, string memory propertyName, bytes memory propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Bytes, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, propertyValue);
}
function setUint256DynamicProperty(uint256 tokenId, string memory propertyName, uint256 propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Uint256, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, abi.encode(propertyValue));
}
function setBytes32DynamicProperty(uint256 tokenId, string memory propertyName, bytes32 propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Bytes32, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, abi.encode(propertyValue));
}
function setBoolDynamicProperty(uint256 tokenId, string memory propertyName, bool propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Bool, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, abi.encode(propertyValue));
}
function setStringDynamicProperty(uint256 tokenId, string memory propertyName, string memory propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.String, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, abi.encode(propertyValue));
}
function setAddressDynamicProperty(uint256 tokenId, string memory propertyName, address propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Address, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, abi.encode(propertyValue));
}
function setNewtonAddressDynamicProperty(uint256 tokenId, string memory propertyName, address propertyValue) public virtual override payable {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.NewtonAddress, "property type error");
_properties[propertyId] = propertyName;
EVTVariable.setDynamicProperty(tokenId, propertyId, abi.encode(propertyValue));
}
function setDynamicProperties(uint256 tokenId, bytes memory message) public virtual override(IEVTExample, EVTVariable) payable {
// TODO:
}
function getBytesDynamicProperty(uint256 tokenId, string memory propertyName) public view virtual override returns (bytes memory) {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Bytes, "property type error");
return EVTVariable.getProperty(tokenId, propertyId);
}
function getUint256DynamicProperty(uint256 tokenId, string memory propertyName) public view virtual override returns (uint256 propertyValue) {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Uint256, "property type error");
propertyValue = abi.decode(EVTVariable.getProperty(tokenId, propertyId), (uint256));
}
function getBytes32DynamicProperty(uint256 tokenId, string memory propertyName) public view virtual override returns (bytes32 propertyValue) {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Bytes32, "property type error");
propertyValue = abi.decode(EVTVariable.getProperty(tokenId, propertyId), (bytes32));
}
function getBoolDynamicProperty(uint256 tokenId, string memory propertyName) public view virtual override returns (bool propertyValue) {
bytes32 propertyId = getPropertyId(propertyName);
require(_propertiesTypes[propertyId] == Types.Bool, "property type error");
propertyValue = abi.decode(EVTVariable.getProperty(tokenId, propertyId), (bool));
}
function getProperties(uint256 tokenId) public view virtual override(IEVTExample, EVTVariable) returns (bytes memory message) {
// TODO:
}
function supportsProperty(string memory propertyName) public view virtual override returns (bool) {
bytes32 propertyId = getPropertyId(propertyName);
return EVTVariable.supportsProperty(propertyId);
}
function getDynamicPropertiesArray(uint256 tokenId) public view virtual returns(string[] memory properties) {
(bytes32[] memory ids, bytes[] memory values) = EVTVariable.getAll(tokenId);
require(ids.length == values.length, "length error");
properties = new string[](ids.length);
for (uint256 i = 0; i < ids.length; i++) {
bytes32 id = ids[i];
string memory args = string(abi.encodePacked('{"trait_type":"', _properties[id], '","value":'));
if (_propertiesTypes[id] == Types.Bool) {
(bool value) = abi.decode(values[i], (bool));
if (value) {
args = string(abi.encodePacked(args, '"true"'));
} else {
args = string(abi.encodePacked(args, '"false"'));
}
} else if (_propertiesTypes[id] == Types.Int256) {
(int256 value) = abi.decode(values[i], (int256));
if (value >= 0) {
args = string(abi.encodePacked(args, Strings.toString(uint256(value))));
} else {
args = string(abi.encodePacked(args, '-', Strings.toString(uint256(-value))));
}
} else if (_propertiesTypes[id] == Types.Uint256) {
(uint256 value) = abi.decode(values[i], (uint256));
args = string(abi.encodePacked(args, Strings.toString(value)));
} else if (_propertiesTypes[id] == Types.Bytes32) {
(bytes32 value) = abi.decode(values[i], (bytes32));
args = string(abi.encodePacked(args, '"', HexStrings.toHexStringNoPrefix(uint256(value), 32), '"'));
} else if (_propertiesTypes[id] == Types.Address) {
(address value) = abi.decode(values[i], (address));
args = string(abi.encodePacked(args, '"', HexStrings.toHexString(uint256(uint160(value)), 20), '"'));
} else if (_propertiesTypes[id] == Types.Bytes) {
(bytes memory value) = abi.decode(values[i], (bytes));
args = string(abi.encodePacked(args, '"', string(value), '"'));
} else if (_propertiesTypes[id] == Types.String) {
(string memory value) = abi.decode(values[i], (string));
args = string(abi.encodePacked(args, '"', value, '"'));
} else if (_propertiesTypes[id] == Types.NewtonAddress) {
(address value) = abi.decode(values[i], (address));
args = string(abi.encodePacked(args, '"', NewtonAddress.hex2New(value), '"'));
} else {
args = string(abi.encodePacked(args, '"', string(values[i]), '"'));
}
args = string(abi.encodePacked(args, '}'));
properties[i] = string(args);
}
}
function getDynamicPropertiesAsString(uint256 tokenId) public view virtual returns(string memory properties) {
string[] memory args = getDynamicPropertiesArray(tokenId);
properties = '[';
for (uint256 i = 0; i < args.length; i++) {
if (i + 1 == args.length) {
properties = string(abi.encodePacked(properties, args[i]));
} else {
properties = string(abi.encodePacked(properties, args[i], ','));
}
}
properties = string(abi.encodePacked(properties, ']'));
}
/**
* {"name":"SYMBOL #1", "description": "Name #1", "image": "ipfs://hash",
"attributes": [
]}
*/
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
string[17] memory args;
args[0] = '"name": "';
args[1] = symbol();
args[2] = ' # ';
args[3] = Strings.toString(tokenId);
args[4] = '", "description": " The EVT of ';
args[5] = name();
args[6] = ' by Newton.", "image": "ipfs://QmRHrWJxfc1ahV1pQ5wczQ6jEBuabfKKTBWC1RvoZXoy8W", "attributes":';
args[7] = getDynamicPropertiesAsString(tokenId);
args[8] = '}';
string memory json = Base64.encode(bytes(string(abi.encodePacked(
args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8]
))));
return string(abi.encodePacked('data:application/json;base64,', json));
}
}