Python zero_ex.order_utils¶
Order utilities for 0x applications.
Setup¶
Install the package with pip:
pip install 0x-order-utils
Some methods require the caller to pass in a Web3.BaseProvider
object.
For local testing one may construct such a provider pointing at an instance of
ganache-cli which has the 0x
contracts deployed on it. For convenience, a docker container is provided for
just this purpose. To start it:
docker run -d -p 8545:8545 0xorg/ganache-cli
-
zero_ex.order_utils.
generate_order_hash_hex
(order, exchange_address, chain_id)[source]¶ Calculate the hash of the given order as a hexadecimal string.
- Parameters
order (
Order
) – The order to be hashed. Must conform to the 0x order JSON schema.exchange_address (
str
) – The address to which the 0x Exchange smart contract has been deployed.
- Return type
- Returns
A string, of ASCII hex digits, representing the order hash.
Inputs and expected result below were copied from @0x/order-utils/test/order_hash_test.ts
>>> generate_order_hash_hex( ... Order( ... makerAddress="0x0000000000000000000000000000000000000000", ... takerAddress="0x0000000000000000000000000000000000000000", ... feeRecipientAddress="0x0000000000000000000000000000000000000000", ... senderAddress="0x0000000000000000000000000000000000000000", ... makerAssetAmount="0", ... takerAssetAmount="0", ... makerFee="0", ... takerFee="0", ... expirationTimeSeconds="0", ... salt="0", ... makerAssetData=((0).to_bytes(1, byteorder='big') * 20), ... takerAssetData=((0).to_bytes(1, byteorder='big') * 20), ... makerFeeAssetData=((0).to_bytes(1, byteorder='big') * 20), ... takerFeeAssetData=((0).to_bytes(1, byteorder='big') * 20), ... ), ... exchange_address="0x1dc4c1cefef38a777b15aa20260a54e584b16c48", ... chain_id=1337 ... ) 'cb36e4fedb36508fb707e2c05e21bffc7a72766ccae93f8ff096693fff7f1714'
-
zero_ex.order_utils.
is_valid_signature
(provider, data, signature, signer_address)[source]¶ Check the validity of the supplied signature.
Check if the supplied
signature
corresponds to signingdata
with the private key corresponding tosigner_address
.- Parameters
- Return type
- Returns
Tuple consisting of a boolean and a string. Boolean is true if valid, false otherwise. If false, the string describes the reason.
>>> is_valid_signature( ... Web3.HTTPProvider("http://127.0.0.1:8545"), ... '0x6927e990021d23b1eb7b8789f6a6feaf98fe104bb0cf8259421b79f9a34222b0', ... '0x1B61a3ed31b43c8780e905a260a35faefcc527be7516aa11c0256729b5b351bc3340349190569279751135161d22529dc25add4f6069af05be04cacbda2ace225403', ... '0x5409ed021d9299bf6814279a6a1411a7e866a631', ... ) True
-
zero_ex.order_utils.
sign_hash
(web3_or_provider, signer_address, hash_hex)[source]¶ Sign a message with the given hash, and return the signature.
- Parameters
- Return type
- Returns
A string, of ASCII hex digits, representing the signature.
>>> provider = Web3.HTTPProvider("http://127.0.0.1:8545") >>> sign_hash( ... provider, ... Web3(provider).geth.personal.listAccounts()[0], ... '0x34decbedc118904df65f379a175bb39ca18209d6ce41d5ed549d54e6e0a95004', ... ) '0x1b117902c86dfb95fe0d1badd983ee166ad259b27acb220174cbb4460d872871137feabdfe76e05924b484789f79af4ee7fa29ec006cedce1bbf369320d034e10b03'
-
zero_ex.order_utils.
sign_hash_to_bytes
(web3_or_provider, signer_address, hash_hex)[source]¶ Sign a message with the given hash, and return the signature.
>>> provider = Web3.HTTPProvider("http://127.0.0.1:8545") >>> sign_hash_to_bytes( ... provider, ... Web3(provider).geth.personal.listAccounts()[0], ... '0x34decbedc118904df65f379a175bb39ca18209d6ce41d5ed549d54e6e0a95004', ... ).decode(encoding='utf_8') '1b117902c86dfb95fe0d1badd983ee166ad259b27acb220174cbb4460d872871137feabdfe76e05924b484789f79af4ee7fa29ec006cedce1bbf369320d034e10b03'
- Return type
zero_ex.order_utils.asset_data_utils¶
Asset data encoding and decoding utilities.
-
class
zero_ex.order_utils.asset_data_utils.
ERC20AssetData
[source]¶ Object interface to ERC20 asset data.
-
property
asset_proxy_id
¶ Asset proxy identifier.
-
property
token_address
¶ Token address
-
property
-
class
zero_ex.order_utils.asset_data_utils.
ERC721AssetData
[source]¶ Object interface to ERC721 asset data.
-
property
asset_proxy_id
¶ Asset proxy identifier.
-
property
token_address
¶ Token address
-
property
token_id
¶ Token identifier.
-
property
-
zero_ex.order_utils.asset_data_utils.
decode_erc20_asset_data
(asset_data)[source]¶ Decode an ERC20 asset data hex string.
- Parameters
asset_data (
str
) – String produced by prior call to encode_erc20_asset_data()
>>> decode_erc20_asset_data("0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48") ERC20AssetData(asset_proxy_id='0xf47261b0', token_address='0x1dc4c1cefef38a777b15aa20260a54e584b16c48')
- Return type
-
zero_ex.order_utils.asset_data_utils.
decode_erc721_asset_data
(asset_data)[source]¶ Decode an ERC721 asset data hex string.
>>> decode_erc721_asset_data('0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001') ERC721AssetData(asset_proxy_id='0x02571792', token_address='0x1dc4c1cefef38a777b15aa20260a54e584b16c48', token_id=1)
- Return type
-
zero_ex.order_utils.asset_data_utils.
encode_erc20
(token_address)[source]¶ Encode an ERC20 token address into asset data bytes.
- Parameters
token_address (
str
) – the ERC20 token’s contract address.- Return type
- Returns
hex encoded asset data string, usable in the makerAssetData or takerAssetData fields in a 0x order.
>>> encode_erc20('0x1dc4c1cefef38a777b15aa20260a54e584b16c48').hex() 'f47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48'
-
zero_ex.order_utils.asset_data_utils.
encode_erc20_asset_data
(token_address)[source]¶ Encode an ERC20 token address into an asset data string.
- 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_asset_data('0x1dc4c1cefef38a777b15aa20260a54e584b16c48') '0xf47261b00000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c48'
Deprecated since version use: “0x”+encode_erc20().hex() instead
- Return type
-
zero_ex.order_utils.asset_data_utils.
encode_erc721
(token_address, token_id)[source]¶ Encode an ERC721 token address into asset data bytes.
- Parameters
- Return type
- Returns
hex encoded asset data string, usable in the makerAssetData or takerAssetData fields in a 0x order.
>>> encode_erc721('0x1dc4c1cefef38a777b15aa20260a54e584b16c48', 1).hex() '025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001'
-
zero_ex.order_utils.asset_data_utils.
encode_erc721_asset_data
(token_address, token_id)[source]¶ 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.
- returns
hex encoded asset data string, usable in the makerAssetData or takerAssetData fields in a 0x order.
>>> encode_erc721_asset_data('0x1dc4c1cefef38a777b15aa20260a54e584b16c48', 1) '0x025717920000000000000000000000001dc4c1cefef38a777b15aa20260a54e584b16c480000000000000000000000000000000000000000000000000000000000000001'
Deprecated since version use: “0x”+encode_erc721().hex() instead
- Return type