aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/contracts/current/protocol/AssetProxy/MixinERC721Transfer.sol
blob: 561c2a625ac96a417e725e3b91478db96e3ceec6 (plain) (blame)
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*

  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 "./mixins/MAuthorizable.sol";
import "../../utils/LibBytes/LibBytes.sol";
import "./libs/LibTransferErrors.sol";

contract MixinERC721Transfer is
    LibTransferErrors,
    MAuthorizable
{
    using LibBytes for bytes;
    
    bytes4 constant SAFE_TRANSFER_FROM_SELECTOR = bytes4(keccak256("safeTransferFrom(address,address,uint256,bytes)"));
    /// @dev Internal version of `transferFrom`.
    /// @param assetData Encoded byte array.
    /// @param from Address to transfer asset from.
    /// @param to Address to transfer asset to.
    /// @param amount Amount of asset to transfer.
    function transferFrom(
        bytes assetData,
        address from,
        address to,
        uint256 amount
    )
        external
        onlyAuthorized()
    {
        // `transferFrom`.
        // The function is marked `external`, so no abi decodeding is done for
        // us. Instead, we expect the `calldata` memory to contain the
        // following:
        //
        // | Area     | Offset | Length  | Contents                            |
        // |----------|--------|---------|-------------------------------------|
        // | Header   | 0      | 4       | function selector                   |
        // | Params   |        | 4 * 32  | function parameters:                |
        // |          | 4      |         |   1. offset to assetData (*)        |
        // |          | 36     |         |   2. from                           |
        // |          | 68     |         |   3. to                             |
        // |          | 100    |         |   4. amount                         |
        // | Data     |        |         | assetData:                          |
        // |          | 132    | 32      | assetData Length                    |
        // |          | 164    | **      | assetData Contents                  |
        //
        // (*): offset is computed from start of function parameters, so offset
        //      by an additional 4 bytes in the calldata.
        //
        // WARNING: The ABIv2 specification allows additional padding between
        //          the Params and Data section. This will result in a larger
        //          offset to assetData.
        
        // Asset data itself is encoded as follows:
        //
        // | Area     | Offset | Length  | Contents                            |
        // |----------|--------|---------|-------------------------------------|
        // | Header   | 0      | 4       | function selector                   |
        // | Params   |        | 3 * 32  | function parameters:                |
        // |          | 4      | 12 + 20 |   1. token address                  |
        // |          | 36     |         |   2. tokenId                        |
        // |          | 68     |         |   3. offset to receiverData (*)     |
        // | Data     |        |         | receiverData:                       |
        // |          | 100    | 32      | receiverData Length                 |
        // |          | 132    | **      | receiverData Contents               |
        
        // We construct calldata for the `token.safeTransferFrom` ABI.
        // The layout of this calldata is in the table below.
        // 
        // | Area     | Offset | Length  | Contents                            |
        // |----------|--------|---------|-------------------------------------|
        // | Header   | 0      | 4       | function selector                   |
        // | Params   |        | 4 * 32  | function parameters:                |
        // |          | 4      |         |   1. from                           |
        // |          | 36     |         |   2. to                             |
        // |          | 68     |         |   3. tokenId                        |
        // |          | 100    |         |   4. offset to receiverData (*)     |
        // | Data     |        |         | receiverData:                       |
        // |          | 132    | 32      | receiverData Length                 |
        // |          | 164    | **      | receiverData Contents               |

        // bytes4 safeTransferFromSelector = SAFE_TRANSFER_FROM_SELECTOR;
        // bool success;
        assembly {
            // There exists only 1 of each token.
            // require(amount == 1, "INVALID_AMOUNT")
            if sub(calldataload(100), 1) {
                // Revert with `Error("INVALID_AMOUNT")`
                mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
                mstore(32, 0x0000002000000000000000000000000000000000000000000000000000000000)
                mstore(64, 0x0000000e494e56414c49445f414d4f554e540000000000000000000000000000)
                mstore(96, 0)
                revert(0, 100)
            }
            
            // Require assetData to be at least 132 bytes
            let offset := calldataload(4)
            if lt(calldataload(add(offset, 4)), 132) {
                // Revert with `Error("ASSET_DATA_TO_SHORT")`
                mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
                mstore(32, 0x0000002000000000000000000000000000000000000000000000000000000000)
                mstore(64, 0x0000001341535345545f444154415f544f5f53484f5254000000000000000000)
                mstore(96, 0)
                revert(0, 100)
            }
            
            /////// Setup State ///////
            // `cdStart` is the start of the calldata for
            // `token.safeTransferFrom` (equal to free memory ptr).
            let cdStart := mload(64)
            // `dataAreaLength` is the total number of words
            // needed to store `receiverData`
            // As-per the ABI spec, this value is padded up to
            // the nearest multiple of 32,
            // and includes 32-bytes for length.
            // It's calculated as folows:
            //      - Unpadded length in bytes = `mload(receiverData) + 32`
            //      - Add 31 to convert rounding down to rounding up.
            //        Combined with the previous and this is `63`.
            //      - Round down to nearest multiple of 32 by clearing
            //        bits 0x1F. This is done with `and` and a mask.

            /////// Setup Header Area ///////
            // This area holds the 4-byte `transferFromSelector`.
            // Any trailing data in transferFromSelector will be
            // overwritten in the next `mstore` call.
            mstore(cdStart, 0xb88d4fde00000000000000000000000000000000000000000000000000000000)
            
            /////// Setup Params Area ///////
            // Each parameter is padded to 32-bytes.
            // The entire Params Area is 128 bytes.
            // Notes:
            //   1. A 20-byte mask is applied to addresses
            //      to zero-out the unused bytes.
            //   2. The offset to `receiverData` is the length
            //      of the Params Area (128 bytes).
            
            let length := calldataload(add(offset, 136))
            let token := calldataload(add(offset, 40))
            
            // Round length up to multiple of 32
            length := and(add(length, 31), 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0)
            
            // Copy `from` and `to`
            calldatacopy(add(cdStart, 4), 36, 64)
            
            // TokenId
            mstore(add(cdStart, 68), calldataload(add(offset, 72)))
            
            // Offset to receiverData
            mstore(add(cdStart, 100), 128)
            
            // receiverData (including length)
            calldatacopy(add(cdStart, 132), add(offset, 136), add(length, 32))
            
            /////// Call `token.safeTransferFrom` using the calldata ///////
            let success := call(
                gas,                    // forward all gas
                token,                  // call address of token contract
                0,                      // don't send any ETH
                cdStart,                // pointer to start of input
                add(length, 164),       // length of input
                0,                      // write output to null
                0                       // output size is 0 bytes
            )
            if success {
                return(0, 0)
            }
            
            // Revert with `Error("TRANSFER_FAILED")`
            mstore(0, 0x08c379a000000000000000000000000000000000000000000000000000000000)
            mstore(32, 0x0000002000000000000000000000000000000000000000000000000000000000)
            mstore(64, 0x0000000f5452414e534645525f4641494c454400000000000000000000000000)
            mstore(96, 0)
            revert(0, 100)
        }
    }

    /// @dev Internal version of `transferFrom`.
    /// @param assetData Encoded byte array.
    /// @param from Address to transfer asset from.
    /// @param to Address to transfer asset to.
    /// @param amount Amount of asset to transfer.
    function transferFromInternal(
        bytes memory assetData,
        address from,
        address to,
        uint256 amount
    )
        internal
    {
        // There exists only 1 of each token.
        require(
            amount == 1,
            INVALID_AMOUNT
        );
    
        // Decode asset data.
        (
            address token,
            uint256 tokenId,
            bytes memory receiverData
        ) = decodeERC721AssetData(assetData);

        // We construct calldata for the `token.safeTransferFrom` ABI.
        // The layout of this calldata is in the table below.
        // 
        // | Area     | Offset | Length  | Contents                            |
        // |----------|--------|---------|-------------------------------------|
        // | Header   | 0      | 4       | function selector                   |
        // | Params   |        | 4 * 32  | function parameters:                |
        // |          | 4      |         |   1. from                           |
        // |          | 36     |         |   2. to                             |
        // |          | 68     |         |   3. tokenId                        |
        // |          | 100    |         |   4. offset to receiverData (*)     |
        // | Data     |        |         | receiverData:                       |
        // |          | 132    | 32      | receiverData Length                 |
        // |          | 164    | **      | receiverData Contents               |

        bytes4 safeTransferFromSelector = SAFE_TRANSFER_FROM_SELECTOR;
        bool success;
        assembly {
            /////// Setup State ///////
            // `cdStart` is the start of the calldata for
            // `token.safeTransferFrom` (equal to free memory ptr).
            let cdStart := mload(64)
            // `dataAreaLength` is the total number of words
            // needed to store `receiverData`
            // As-per the ABI spec, this value is padded up to
            // the nearest multiple of 32,
            // and includes 32-bytes for length.
            // It's calculated as folows:
            //      - Unpadded length in bytes = `mload(receiverData) + 32`
            //      - Add 31 to convert rounding down to rounding up.
            //        Combined with the previous and this is `63`.
            //      - Round down to nearest multiple of 32 by clearing
            //        bits 0x1F. This is done with `and` and a mask.
            let dataAreaLength := and(add(mload(receiverData), 63), 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE0)
            // `cdEnd` is the end of the calldata for `token.safeTransferFrom`.
            let cdEnd := add(cdStart, add(132, dataAreaLength))

            /////// Setup Header Area ///////
            // This area holds the 4-byte `transferFromSelector`.
            // Any trailing data in transferFromSelector will be
            // overwritten in the next `mstore` call.
            mstore(cdStart, safeTransferFromSelector)
            
            /////// Setup Params Area ///////
            // Each parameter is padded to 32-bytes.
            // The entire Params Area is 128 bytes.
            // Notes:
            //   1. A 20-byte mask is applied to addresses
            //      to zero-out the unused bytes.
            //   2. The offset to `receiverData` is the length
            //      of the Params Area (128 bytes).
            mstore(add(cdStart, 4), and(from, 0xffffffffffffffffffffffffffffffffffffffff))
            mstore(add(cdStart, 36), and(to, 0xffffffffffffffffffffffffffffffffffffffff))
            mstore(add(cdStart, 68), tokenId)
            mstore(add(cdStart, 100), 128)

            /////// Setup Data Area ///////
            // This area holds `receiverData`.
            let dataArea := add(cdStart, 132)
            for {} lt(dataArea, cdEnd) {} {
                mstore(dataArea, mload(receiverData))
                dataArea := add(dataArea, 32)
                receiverData := add(receiverData, 32)
            }
            
            /////// Call `token.safeTransferFrom` using the calldata ///////
            success := call(
                gas,                    // forward all gas
                token,                  // call address of token contract
                0,                      // don't send any ETH
                cdStart,                // pointer to start of input
                sub(cdEnd, cdStart),    // length of input
                cdStart,                // write output over input
                0                       // output size is 0 bytes
            )
        }
        require(
            success,
            TRANSFER_FAILED
        );
    }

    /// @dev Decodes ERC721 Asset data.
    /// @param assetData Encoded byte array.
    /// @return proxyId Intended ERC721 proxy id.
    /// @return token ERC721 token address.
    /// @return tokenId ERC721 token id.
    /// @return receiverData Additional data with no specific format, which
    ///                      is passed to the receiving contract's onERC721Received.
    function decodeERC721AssetData(bytes memory assetData)
        internal
        pure
        returns (
            address token,
            uint256 tokenId,
            bytes memory receiverData
        )
    {
        // Decode asset data.
        token = assetData.readAddress(16);
        tokenId = assetData.readUint256(36);
        receiverData = assetData.readBytesWithLength(100);

        return (
            token,
            tokenId,
            receiverData
        );
    }
}