Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
0x-sra-client.py: Fix bug in config_order, and other small improvemen…
Browse files Browse the repository at this point in the history
…ts (#2399)

* Bug fix: unescape backslashes in regexes

Root problem is that there are too many backslashes in the SRA spec
itself.  See #1727

This was previously fixed for heavily-tested endpoints (get and post
order, etc), but was only recently discovered for the get-order-config
endpoint.

* Demonstrate get_order_config()

* Rename DefaultApi to RelayerApi

* Simplify RelayerApi instantiation

* Document paylod and response schemas

* Stop caring which contracts are wrapped

* Increase platform agnosticism

* Update CHANGELOG

* Remove unnecessary f-string
  • Loading branch information
feuGeneA authored Dec 20, 2019
1 parent 4c21a69 commit 551a65c
Show file tree
Hide file tree
Showing 23 changed files with 267 additions and 319 deletions.
28 changes: 1 addition & 27 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,33 +160,7 @@ contracts/exchange-forwarder/generated-wrappers/
contracts/exchange-forwarder/test/generated-wrappers/
contracts/dev-utils/generated-wrappers/
contracts/dev-utils/test/generated-wrappers/
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/dev_utils/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc20_token/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/exchange/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/asset_proxy_owner/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/coordinator/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/coordinator_registry/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/dummy_erc20_token/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/dummy_erc721_token/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/dutch_auction/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc1155_mintable/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc1155_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc20_bridge_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc20_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc721_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/erc721_token/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/forwarder/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/i_asset_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/i_validator/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/i_wallet/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/multi_asset_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/order_validator/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/staking/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/staking_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/static_call_proxy/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/weth9/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/zrx_token/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/zrx_vault/__init__.py
python-packages/contract_wrappers/src/zero_ex/contract_wrappers/*/__init__.py

# solc-bin in sol-compiler
packages/sol-compiler/solc_bin/
Expand Down
47 changes: 20 additions & 27 deletions python-packages/contract_wrappers/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# we import things outside of top-level because 3rd party libs may not yet be
# installed when you invoke this script

from glob import glob
import subprocess # nosec
from shutil import rmtree
from os import environ, path, remove
Expand All @@ -18,30 +19,6 @@
from setuptools.command.test import test as TestCommand


CONTRACTS_TO_BE_WRAPPED = [
"asset_proxy_owner",
"coordinator",
"coordinator_registry",
"dev_utils",
"dummy_erc20_token",
"dummy_erc721_token",
"dutch_auction",
"erc20_proxy",
"erc20_token",
"erc721_proxy",
"erc721_token",
"exchange",
"forwarder",
"i_asset_proxy",
"i_validator",
"i_wallet",
"multi_asset_proxy",
"order_validator",
"weth9",
"zrx_token",
]


class PreInstallCommand(distutils.command.build_py.build_py):
"""Custom setuptools command class for pulling in generated code."""

Expand Down Expand Up @@ -128,11 +105,27 @@ def run(self):
rmtree(".mypy_cache", ignore_errors=True)
rmtree(".tox", ignore_errors=True)
rmtree(".pytest_cache", ignore_errors=True)
rmtree("src/0x_contract_wrappers.egg-info", ignore_errors=True)
rmtree(
path.join("src", "0x_contract_wrappers.egg-info"),
ignore_errors=True,
)
# generated files:
for contract in CONTRACTS_TO_BE_WRAPPED:
print("Removing src/zero_ex/contract_wrappers/*/__init__.py...")
for contract in glob(
path.join(
".", "src", "zero_ex", "contract_wrappers", "*", "__init__.py"
)
):
try:
remove(f"src/zero_ex/contract_wrappers/{contract}/__init__.py")
remove(
path.join(
"src",
"zero_ex",
"contract_wrappers",
contract,
"__init__.py",
)
)
except FileNotFoundError:
pass

Expand Down
6 changes: 6 additions & 0 deletions python-packages/sra_client/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 5.0.0 - TBD

- Renamed class DefaultApi to RelayerApi, and changed its construction parameters.
- Updated documentation to include schemas for request payloads and responses, and to demonstrate the RelayerApi.get_order_config() method.
- Fixed bug with numeric types not being handled properly for asset data trade info and order config methods.

## 4.0.0 - 2019-12-03

- Migrated from v2 to v3 of the 0x protocol.
Expand Down
49 changes: 47 additions & 2 deletions python-packages/sra_client/src/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,55 @@ Python zero_ex.sra_client

.. automodule:: zero_ex.sra_client

zero_ex.sra_client.DefaultApi
zero_ex.sra_client.RelayerApi
=============================

.. autoclass:: zero_ex.sra_client.DefaultApi
.. autoclass:: zero_ex.sra_client.RelayerApi
:members:

zero_ex.sra_client.models
=========================

.. autoclass:: zero_ex.sra_client.models.relayer_api_asset_data_pairs_response_schema.RelayerApiAssetDataPairsResponseSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_asset_data_trade_info_schema.RelayerApiAssetDataTradeInfoSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_error_response_schema.RelayerApiErrorResponseSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_error_response_schema_validation_errors.RelayerApiErrorResponseSchemaValidationErrors
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_fee_recipients_response_schema.RelayerApiFeeRecipientsResponseSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_orderbook_response_schema.RelayerApiOrderbookResponseSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_order_config_payload_schema.RelayerApiOrderConfigPayloadSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_order_config_response_schema.RelayerApiOrderConfigResponseSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_orders_channel_subscribe_payload_schema.RelayerApiOrdersChannelSubscribePayloadSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_orders_channel_subscribe_schema.RelayerApiOrdersChannelSubscribeSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_orders_channel_update_schema.RelayerApiOrdersChannelUpdateSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_order_schema.RelayerApiOrderSchema
:members:

.. autoclass:: zero_ex.sra_client.models.relayer_api_orders_response_schema.RelayerApiOrdersResponseSchema
:members:

.. autoclass:: zero_ex.sra_client.models.signed_order_schema.SignedOrderSchema
:members:

Indices and tables
Expand Down
45 changes: 36 additions & 9 deletions python-packages/sra_client/src/zero_ex/sra_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@
Configure and create an API client instance
-------------------------------------------
>>> from zero_ex.sra_client import ApiClient, Configuration, DefaultApi
>>> config = Configuration()
>>> config.host = "http://localhost:3000"
>>> relayer = DefaultApi(ApiClient(config))
>>> from zero_ex.sra_client import RelayerApi
>>> relayer = RelayerApi("http://localhost:3000")
Preparing to trade
------------------
Expand Down Expand Up @@ -121,10 +119,39 @@
... )
... )
Before hashing and submitting our order, it's a good idea to ask the relayer
how to configure the order, so that the submission won't be rejected:
>>> order_config = relayer.get_order_config(
... relayer_api_order_config_payload_schema={
... "makerAddress": order["makerAddress"],
... "takerAddress": order["takerAddress"],
... "makerAssetAmount": order["makerAssetAmount"],
... "takerAssetAmount": order["takerAssetAmount"],
... "expirationTimeSeconds": order["expirationTimeSeconds"],
... "makerAssetData": '0x' + order["makerAssetData"].hex(),
... "takerAssetData": '0x' + order["takerAssetData"].hex(),
... "exchangeAddress": contract_addresses.exchange,
... }
... )
>>> order_config
{'fee_recipient_address': '0x0000000000000000000000000000000000000001',
'maker_fee': '0',
'sender_address': '0x0000000000000000000000000000000000000000',
'taker_fee': '0'}
Now we'll apply that configuration to our order before proceeding:
>>> order["feeRecipientAddress"] = order_config.fee_recipient_address
>>> order["makerFee"] = int(order_config.maker_fee)
>>> order["takerFee"] = int(order_config.taker_fee)
>>> order["senderAddress"] = order_config.sender_address
>>> from zero_ex.order_utils import generate_order_hash_hex
>>> order_hash_hex = generate_order_hash_hex(
... order, contract_addresses.exchange, Web3(eth_node).eth.chainId
... )
>>> relayer.post_order_with_http_info(
... signed_order_schema=order_to_jsdict(
... order=order,
Expand Down Expand Up @@ -155,7 +182,7 @@
'order': {'chainId': 1337,
'exchangeAddress': '0x...',
'expirationTimeSeconds': '...',
'feeRecipientAddress': '0x0000000000000000000000000000000000000000',
'feeRecipientAddress': '0x0000000000000000000000000000000000000001',
'makerAddress': '0x...',
'makerAssetAmount': '2',
'makerAssetData': '0xf47261b0000000000000000000000000...',
Expand All @@ -182,7 +209,7 @@
'order': {'chainId': 1337,
'exchangeAddress': '0x...',
'expirationTimeSeconds': '...',
'feeRecipientAddress': '0x0000000000000000000000000000000000000000',
'feeRecipientAddress': '0x0000000000000000000000000000000000000001',
'makerAddress': '0x...',
'makerAssetAmount': '2',
'makerAssetData': '0xf47261b000000000000000000000000...',
Expand Down Expand Up @@ -242,7 +269,7 @@
'order': {'chainId': 1337,
'exchangeAddress': '0x...',
'expirationTimeSeconds': '...',
'feeRecipientAddress': '0x0000000000000000000000000000000000000000',
'feeRecipientAddress': '0x0000000000000000000000000000000000000001',
'makerAddress': '0x...',
'makerAssetAmount': '2',
'makerAssetData': '0xf47261b0000000000000000000000000...',
Expand Down Expand Up @@ -278,7 +305,7 @@
>>> pprint(order)
{'chainId': 1337,
'expirationTimeSeconds': ...,
'feeRecipientAddress': '0x0000000000000000000000000000000000000000',
'feeRecipientAddress': '0x0000000000000000000000000000000000000001',
'makerAddress': '0x...',
'makerAssetAmount': 2,
'makerAssetData': b...
Expand Down Expand Up @@ -403,7 +430,7 @@
__version__ = "1.0.0"

# import apis into sdk package
from .api.default_api import DefaultApi
from .api.relayer_api import RelayerApi

# import ApiClient
from .api_client import ApiClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
# flake8: noqa

# import apis into api package
from zero_ex.sra_client.api.default_api import DefaultApi
from zero_ex.sra_client.api.relayer_api import RelayerApi
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,24 @@
import six

from zero_ex.sra_client.api_client import ApiClient
from zero_ex.sra_client.configuration import Configuration
from zero_ex.sra_client.models.relayer_api_order_config_payload_schema import (
RelayerApiOrderConfigPayloadSchema,
)


class DefaultApi(object):
"""Default API for SRA compliant 0x relayers."""
class RelayerApi(object):
"""API for SRA compliant 0x relayers."""

# NOTE: This class is auto generated by OpenAPI Generator
# Ref: https://openapi-generator.tech

# Do not edit the class manually.

def __init__(self, api_client=None):
if api_client is None:
api_client = ApiClient()
self.api_client = api_client
def __init__(self, url: str):
config = Configuration()
config.host = url
self.api_client = ApiClient(config)

def get_asset_pairs(self, **kwargs):
"""get_asset_pairs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@


class OrderSchema(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Ref: https://openapi-generator.tech
Do not edit the class manually.
"""

"""
Attributes:
openapi_types (dict): The key is attribute name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@


class PaginatedCollectionSchema(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Ref: https://openapi-generator.tech
Do not edit the class manually.
"""

"""
Attributes:
openapi_types (dict): The key is attribute name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@


class RelayerApiAssetDataPairsResponseSchema(object):
"""NOTE: This class is auto generated by OpenAPI Generator.
Ref: https://openapi-generator.tech
Do not edit the class manually.
"""

"""
Attributes:
openapi_types (dict): The key is attribute name
Expand All @@ -35,10 +29,9 @@ def __init__(self, records=None): # noqa: E501

@property
def records(self):
"""Gets the records of this RelayerApiAssetDataPairsResponseSchema. # noqa: E501
"""Gets the records of this RelayerApiAssetDataPairsResponseSchema.
:return: The records of this RelayerApiAssetDataPairsResponseSchema. # noqa: E501
:return: The records of this RelayerApiAssetDataPairsResponseSchema.
:rtype: list[object]
"""
return self._records
Expand All @@ -47,8 +40,7 @@ def records(self):
def records(self, records):
"""Sets the records of this RelayerApiAssetDataPairsResponseSchema.
:param records: The records of this RelayerApiAssetDataPairsResponseSchema. # noqa: E501
:param records: The records of this RelayerApiAssetDataPairsResponseSchema.
:type: list[object]
"""
if records is None:
Expand Down
Loading

0 comments on commit 551a65c

Please sign in to comment.