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
|
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity 0.4.24;
pragma experimental ABIEncoderV2;
import "../../protocol/Exchange/interfaces/IExchange.sol";
import "../../tokens/ERC721Token/IERC721Token.sol";
import "../../utils/LibBytes/LibBytes.sol";
import "../../utils/ExchangeSelectors/ExchangeSelectors.sol";
contract CompliantForwarder is ExchangeSelectors{
using LibBytes for bytes;
IExchange internal EXCHANGE;
IERC721Token internal COMPLIANCE_TOKEN;
event ValidatedAddresses (
bytes32 selector,
address one,
address[] addresses
);
constructor(address exchange, address complianceToken)
public
{
EXCHANGE = IExchange(exchange);
COMPLIANCE_TOKEN = IERC721Token(complianceToken);
}
function executeTransaction(
uint256 salt,
address signerAddress,
bytes signedExchangeTransaction,
bytes signature
)
external
{
// Validate `signedFillOrderTransaction`
address[] memory validatedAddresses;
bytes32 selectorS;
address one;
assembly {
// Adds address to validate
function addAddressToValidate(addressToValidate) {
// Compute `addressesToValidate` memory location
let addressesToValidate_ := mload(0x40)
let nAddressesToValidate_ := mload(addressesToValidate_)
// Increment length
nAddressesToValidate_ := add(mload(addressesToValidate_), 1)
mstore(addressesToValidate_, nAddressesToValidate_)
// Append address to validate
let offset := mul(32, nAddressesToValidate_)
mstore(add(addressesToValidate_, offset), addressToValidate)
}
function appendMakerAddressFromOrder(paramIndex) {
let exchangeTxPtr := calldataload(0x44)
// Add 0x20 for length offset and 0x04 for selector offset
let orderPtrRelativeToExchangeTx := calldataload(add(0x4, add(exchangeTxPtr, 0x24))) // 0x60
let orderPtr := add(0x4,add(exchangeTxPtr, add(0x24, orderPtrRelativeToExchangeTx)))
let makerAddress := calldataload(orderPtr)
addAddressToValidate(makerAddress)
}
// Extract addresses to validate
let exchangeTxPtr1 := calldataload(0x44)
let selector := and(calldataload(add(0x4, add(0x20, exchangeTxPtr1))), 0xffffffff00000000000000000000000000000000000000000000000000000000)
switch selector
case 0x097bb70b00000000000000000000000000000000000000000000000000000000 /* batchFillOrders */
{
}
case 0x3c28d86100000000000000000000000000000000000000000000000000000000 /* matchOrders */
{
}
case 0xb4be83d500000000000000000000000000000000000000000000000000000000 /* fillOrder */
{
appendMakerAddressFromOrder(0)
addAddressToValidate(signerAddress)
}
case 0xd46b02c300000000000000000000000000000000000000000000000000000000 /* cancelOrder */ {}
default {
revert(0, 100)
}
let addressesToValidate := mload(0x40)
let nAddressesToValidate := mload(addressesToValidate)
let newMemFreePtr := add(addressesToValidate, add(0x20, mul(mload(addressesToValidate), 0x20)))
mstore(0x40, newMemFreePtr)
// Validate addresses
let complianceTokenAddress := sload(COMPLIANCE_TOKEN_slot)
for {let i := add(0x20, addressesToValidate)} lt(i, add(addressesToValidate, add(32, mul(nAddressesToValidate, 32)))) {i := add(i, 32)} {
// Construct calldata for `COMPLIANCE_TOKEN.balanceOf`
mstore(newMemFreePtr, 0x70a0823100000000000000000000000000000000000000000000000000000000)
mstore(add(4, newMemFreePtr), mload(i))
// call `COMPLIANCE_TOKEN.balanceOf`
let success := call(
gas, // forward all gas
complianceTokenAddress, // call address of asset proxy
0, // don't send any ETH
newMemFreePtr, // pointer to start of input
0x24, // length of input (one padded address)
newMemFreePtr, // write output to next free memory offset
0x20 // reserve space for return balance (0x20 bytes)
)
if eq(success, 0) {
// Revert with `Error("BALANCE_CHECK_FAILED")`
mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
mstore(32, 0x0000002000000000000000000000000000000000000000000000000000000000)
mstore(64, 0x0000001453454e4445525f4e4f545f415554484f52495a454400000000000000)
mstore(96, 0)
revert(0, 100)
}
// Revert if balance not held
let addressBalance := mload(newMemFreePtr)
if eq(addressBalance, 0) {
// Revert with `Error("AT_LEAST_ONE_ADDRESS_HAS_ZERO_BALANCE")`
mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
mstore(32, 0x0000002000000000000000000000000000000000000000000000000000000000)
mstore(64, 0x0000002541545f4c454153545f4f4e455f414444524553535f4841535f5a4552)
mstore(96, 0x4f5f42414c414e43450000000000000000000000000000000000000000000000)
revert(0, 109)
}
}
validatedAddresses := addressesToValidate
selectorS := selector
}
emit ValidatedAddresses(selectorS, one, validatedAddresses);
// All entities are verified. Execute fillOrder.
EXCHANGE.executeTransaction(
salt,
signerAddress,
signedExchangeTransaction,
signature
);
}
}
|