forked from fulldecent/live-testing-with-estimateGas
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator-example.sol
More file actions
210 lines (193 loc) · 4.23 KB
/
Copy pathvalidator-example.sol
File metadata and controls
210 lines (193 loc) · 4.23 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
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
205
206
207
208
209
210
pragma solidity 0.5.6;
import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/erc721.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/erc721-token-receiver.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/tokens/erc721-enumerable.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/utils/erc165.sol";
import "https://github.com/0xcert/ethereum-erc721/src/contracts/utils/address-utils.sol";
contract BasicValidator
{
using AddressUtils for address;
bytes4 constant ERC165ID = 0x01ffc9a7;
bytes4 constant ERC721ID = 0x80ac58cd;
bytes4 constant ERC721EnumerableID = 0x780e9d63;
constructor(
uint256 _caseId,
address _target
)
public
{
if (_caseId == 1) {
sanityCheck(_target);
return;
} else if (_caseId == 2) {
checkERC165Interface(_target);
return;
} else if (_caseId == 3) {
checkERC721Interface(_target);
return;
} else if (_caseId == 4) {
checkERC721EnumerableInterface(_target);
return;
}
assert(false);
}
/**
* @dev Sanity checks
* Find the amount of value (ether) assigned to CONTRACT_ADDRESS, it should be greater than or
* equal to zero. Find the code_size of CONTRACT_ADDRESS, it should be greater than zero.
*/
function sanityCheck(
address _target
)
internal
view
{
require(_target.balance >= 0);
assert(_target.isContract());
}
/**
* @dev Check interface 165.
*/
function checkERC165Interface(
address _target
)
internal
view
{
bool result = ERC165(_target).supportsInterface(ERC165ID);
assert(result);
}
/**
* @dev Check interface ERC721.
*/
function checkERC721Interface(
address _target
)
internal
view
{
bool result = ERC165(_target).supportsInterface(ERC721ID);
assert(result);
}
/**
* @dev Check interface ERC721Enumerable.
*/
function checkERC721EnumerableInterface(
address _target
)
internal
view
{
bool result = ERC165(_target).supportsInterface(ERC721EnumerableID);
assert(result);
}
}
contract Stub1 is
ERC721TokenReceiver
{
bytes4 constant MAGIC_ON_ERC721_RECEIVED = 0x150b7a02;
/**
* @dev Receive token and map id to contract address (which is parsed from data).
*/
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes calldata _data
)
external
returns(bytes4)
{
require(StringUtils.compare(_data, "") == 0);
return MAGIC_ON_ERC721_RECEIVED;
}
function transferToken(
address _contract,
uint256 _tokenId,
address _receiver
)
external
{
ERC721(_contract).transferFrom(ERC721(_contract).ownerOf(_tokenId), _receiver, _tokenId);
}
}
contract Stub2 is
ERC721TokenReceiver
{
bytes4 constant MAGIC_ON_ERC721_RECEIVED = 0x150b7a02;
/**
* @dev Receive token and map id to contract address (which is parsed from data).
*/
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes calldata _data
)
external
returns(bytes4)
{
require(StringUtils.compare(_data, "ffff") == 0);
return MAGIC_ON_ERC721_RECEIVED;
}
}
contract Stub3 is
ERC721TokenReceiver
{
bytes4 constant MAGIC_ON_ERC721_RECEIVED_FALSE = 0x150b7a0b;
/**
* @dev Receive token and map id to contract address (which is parsed from data).
*/
function onERC721Received(
address _operator,
address _from,
uint256 _tokenId,
bytes calldata _data
)
external
returns(bytes4)
{
return MAGIC_ON_ERC721_RECEIVED_FALSE;
}
}
library StringUtils {
function compare(
bytes memory _a,
string memory _b
)
internal
pure
returns (int)
{
bytes memory a = _a;
bytes memory b = bytes(_b);
uint minLength = a.length;
if (b.length < minLength)
{
minLength = b.length;
}
for (uint i = 0; i < minLength; i ++)
{
if (a[i] < b[i])
{
return -1;
}
else if (a[i] > b[i])
{
return 1;
}
}
if (a.length < b.length)
{
return -1;
}
else if (a.length > b.length)
{
return 1;
}
else
{
return 0;
}
}
}