예제 #1
0
def encode_erc20(token_address: str) -> bytes:
    """Encode an ERC20 token address into asset data bytes.

    :param token_address: the ERC20 token's contract address.
    :returns: hex encoded asset data string, usable in the makerAssetData or
        takerAssetData fields in a 0x order.

    >>> encode_erc20('0x1dc4c1cefef38a777b15aa20260a54e584b16c48').hex()
    'f47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48'
    """
    assert_is_string(token_address, "token_address")

    return abi_utils.simple_encode("ERC20Token(address)", token_address)
예제 #2
0
def encode_erc721(token_address: str, token_id: int) -> bytes:
    """Encode an ERC721 token address into asset data bytes.

    :param token_address: the ERC721 token's contract address.
    :param token_id: the identifier of the asset's instance of the token.
    :returns: hex encoded asset data string, usable in the makerAssetData or
        takerAssetData fields in a 0x order.

    >>> encode_erc721('0x1dc4c1cefef38a777b15aa20260a54e584b16c48', 1).hex()
    '025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001'
    """  # noqa: E501 (line too long)
    assert_is_string(token_address, "token_address")
    assert_is_int(token_id, "token_id")

    return abi_utils.simple_encode("ERC721Token(address,uint256)",
                                   token_address, token_id)
예제 #3
0
def encode_erc721_asset_data(token_address: str, token_id: int) -> str:
    # docstring considered all one line by pylint: disable=line-too-long
    """Encode an ERC721 asset data hex string.

    :param token_address: the ERC721 token's contract address.
    :param token_id: the identifier of the asset's instance of the token.
    :rtype: hex encoded asset data string, usable in the makerAssetData or
        takerAssetData fields in a 0x order.

    >>> encode_erc721_asset_data('0x1dc4c1cefef38a777b15aa20260a54e584b16c48', 1)
    '0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001'
    """  # noqa: E501 (line too long)
    assert_is_string(token_address, "token_address")
    assert_is_int(token_id, "token_id")

    return (
        "0x"
        + abi_utils.simple_encode(
            "ERC721Token(address,uint256)", token_address, token_id
        ).hex()
    )
def test_simple_encode_type_error():
    """Test that passing in wrong types raises TypeError."""
    with pytest.raises(TypeError):
        simple_encode(123)