aboutsummaryrefslogtreecommitdiffstats
path: root/packages/contracts/src/contracts/current/protocol/AssetProxyDispatcher/proxies/ERC721Proxy.sol
blob: 404d1ca6e009f7e08228e60eebeb782b16003e60 (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
/*

  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.21;

import "../IAssetProxy.sol";
import "../../../utils/LibBytes/LibBytes.sol";
import "../../../utils/Authorizable/Authorizable.sol";
import "/zeppelin/contracts/token/ERC721/ERC721Token.sol";

contract ERC721Proxy is
    LibBytes,
    Authorizable,
    IAssetProxy
{

    /// @dev Transfers ERC721 tokens. Either succeeds or throws.
    /// @param assetMetadata ERC721-encoded byte array
    /// @param from Address to transfer token from.
    /// @param to Address to transfer token to.
    /// @param amount Amount of token to transfer.
    function transferFrom(
        bytes assetMetadata,
        address from,
        address to,
        uint256 amount)
        external
        onlyAuthorized
    {
        // There exists only 1 of each token.
        require(amount == 1);

        // Decode metadata
        address token;
        uint256 tokenId;
        (token, tokenId) = decodeMetadata(assetMetadata);

        // Either succeeds or throws.
        // @TODO: Call safeTransferFrom if there is additional
        //        data stored in `assetMetadata`.
        ERC721Token(token).transferFrom(from, to, tokenId);
    }

    /// @dev Decodes ERC721-encoded byte array.
    /// @param assetMetadata ERC721-encoded byte array.
    /// @return tokenAddress Address of ERC721 token.
    /// @return tokenId Id of ERC721 token.
    function decodeMetadata(bytes memory assetMetadata)
        public pure
        returns (address tokenAddress, uint256 tokenId)
    {
        require(assetMetadata.length == 53);
        tokenAddress = readAddress(assetMetadata, 1);
        tokenId = readUint256(assetMetadata, 21);
        return (tokenAddress, tokenId);
    }

    /// @dev Encodes ERC721 byte array.
    /// @param assetProxyId Id of the asset proxy.
    /// @param tokenAddress Address of the asset.
    /// @param tokenId Id of ERC721 token.
    /// @return assetMetadata ERC721-encoded byte array.
    function encodeMetadata(
        uint8 assetProxyId,
        address tokenAddress,
        uint256 tokenId)
        public pure
        returns (bytes memory assetMetadata)
    {
        // 0 is reserved as invalid proxy id
        require(assetProxyId != 0);

        // Encode fields into a byte array
        assetMetadata = new bytes(53);
        assetMetadata[0] = byte(assetProxyId);
        writeAddress(assetMetadata, 1, tokenAddress);
        writeUint256(assetMetadata, 21, tokenId);
        return assetMetadata;
    }
}