diff --git a/py_clob_client/client.py b/py_clob_client/client.py index b971c84..1de5a10 100644 --- a/py_clob_client/client.py +++ b/py_clob_client/client.py @@ -1,8 +1,6 @@ import logging from .order_builder.builder import OrderBuilder -from .clob_types import ApiCreds, OrderType - from .headers.headers import create_level_1_headers, create_level_2_headers from .signer import Signer @@ -47,7 +45,10 @@ BalanceAllowanceParams, OrderScoringParams, TickSize, + CreateOrderOptions, OrdersScoringParams, + OrderType, + PartialCreateOrderOptions, ) from .exceptions import PolyException from .http_helpers.helpers import ( @@ -60,7 +61,7 @@ add_order_scoring_params_to_url, add_orders_scoring_params_to_url, ) -from py_order_utils.config import get_contract_config + from .constants import L0, L1, L1_AUTH_UNAVAILABLE, L2, L2_AUTH_UNAVAILABLE from .utilities import ( parse_raw_orderbook_summary, @@ -96,8 +97,7 @@ def __init__( self.signer = Signer(key, chain_id) if key else None self.creds = creds self.mode = self._get_client_mode() - if chain_id: - self.contract_config = get_contract_config(chain_id) + if self.signer: self.builder = OrderBuilder( self.signer, sig_type=signature_type, funder=funder @@ -263,15 +263,29 @@ def __resolve_tick_size( tick_size = min_tick_size return tick_size - def create_order(self, order_args: OrderArgs, tick_size: TickSize = None): + def create_order( + self, order_args: OrderArgs, options: PartialCreateOrderOptions = None + ): """ Creates and signs an order - Level 2 Auth required + Level 1 Auth required """ - self.assert_level_2_auth() + self.assert_level_1_auth() - tick_size = self.__resolve_tick_size(order_args.token_id, tick_size) - return self.builder.create_order(order_args, tick_size) + # add resolve_order_options, or similar + tick_size = self.__resolve_tick_size( + order_args.token_id, + options.tick_size if options else None, + ) + neg_risk = options.neg_risk if options else False + + return self.builder.create_order( + order_args, + CreateOrderOptions( + tick_size=tick_size, + neg_risk=neg_risk, + ), + ) def post_order(self, order, orderType: OrderType = OrderType.GTC): """ @@ -286,11 +300,13 @@ def post_order(self, order, orderType: OrderType = OrderType.GTC): ) return post("{}{}".format(self.host, POST_ORDER), headers=headers, data=body) - def create_and_post_order(self, order_args: OrderArgs): + def create_and_post_order( + self, order_args: OrderArgs, options: PartialCreateOrderOptions = None + ): """ Utility function to create and publish an order """ - ord = self.create_order(order_args) + ord = self.create_order(order_args, options) return self.post_order(ord) def cancel(self, order_id): diff --git a/py_clob_client/clob_types.py b/py_clob_client/clob_types.py index bbcb563..3617818 100644 --- a/py_clob_client/clob_types.py +++ b/py_clob_client/clob_types.py @@ -2,7 +2,7 @@ from typing import Any from dataclasses import dataclass, asdict from json import dumps -from typing import Literal +from typing import Literal, Optional from .constants import ZERO_ADDRESS @@ -143,6 +143,18 @@ class OrdersScoringParams: TickSize = Literal["0.1", "0.01", "0.001", "0.0001"] +@dataclass +class CreateOrderOptions: + tick_size: TickSize + neg_risk: bool = False + + +@dataclass +class PartialCreateOrderOptions: + tick_size: Optional[TickSize] = None + neg_risk: bool = False + + @dataclass class RoundConfig: price: float @@ -150,4 +162,23 @@ class RoundConfig: amount: float -TickSizes: dict[str, TickSize] +@dataclass +class ContractConfig: + """ + Contract Configuration + """ + + exchange: str + """ + The exchange contract responsible for matching orders + """ + + collateral: str + """ + The ERC20 token used as collateral for the exchange's markets + """ + + conditional_tokens: str + """ + The ERC1155 conditional tokens contract + """ diff --git a/py_clob_client/config.py b/py_clob_client/config.py new file mode 100644 index 0000000..a5243d9 --- /dev/null +++ b/py_clob_client/config.py @@ -0,0 +1,42 @@ +from .clob_types import ContractConfig + + +def get_contract_config(chainID: int, neg_risk: bool = False) -> ContractConfig: + """ + Get the contract configuration for the chain + """ + + CONFIG = { + 137: ContractConfig( + exchange="0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", + collateral="0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174", + conditional_tokens="0x4D97DCd97eC945f40cF65F87097ACe5EA0476045", + ), + 80001: ContractConfig( + exchange="0x4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E", + collateral="0x2E8DCfE708D44ae2e406a1c02DFE2Fa13012f961", + conditional_tokens="0x7D8610E9567d2a6C9FBf66a5A13E9Ba8bb120d43", + ), + } + + NEG_RISK_CONFIG = { + 137: ContractConfig( + exchange="0xC5d563A36AE78145C45a50134d48A1215220f80a", + collateral="0x2791bca1f2de4661ed88a30c99a7a9449aa84174", + conditional_tokens="0x4D97DCd97eC945f40cF65F87097ACe5EA0476045", + ), + 80001: ContractConfig( + exchange="0x87d1A0DdB4C63a6301916F02090A51a7241571e4", + collateral="0x2e8dcfe708d44ae2e406a1c02dfe2fa13012f961", + conditional_tokens="0x7D8610E9567d2a6C9FBf66a5A13E9Ba8bb120d43", + ), + } + + if neg_risk: + config = NEG_RISK_CONFIG.get(chainID) + else: + config = CONFIG.get(chainID) + if config is None: + raise Exception("Invalid chainID: ${}".format(chainID)) + + return config diff --git a/py_clob_client/order_builder/builder.py b/py_clob_client/order_builder/builder.py index e161894..371dd4e 100644 --- a/py_clob_client/order_builder/builder.py +++ b/py_clob_client/order_builder/builder.py @@ -1,4 +1,5 @@ -from py_order_utils.builders import OrderBuilder as UtilsOrderBuild +from py_order_utils.builders import OrderBuilder as UtilsOrderBuilder +from py_order_utils.signer import Signer as UtilsSigner from py_order_utils.model import ( EOA, OrderData, @@ -6,7 +7,6 @@ BUY as UtilsBuy, SELL as UtilsSell, ) -from py_order_utils.config import get_contract_config from .helpers import ( to_token_decimals, @@ -16,12 +16,11 @@ round_up, ) from .constants import BUY, SELL - +from ..config import get_contract_config from ..signer import Signer -from ..clob_types import OrderArgs, TickSize, RoundConfig -from typing import Tuple +from ..clob_types import OrderArgs, CreateOrderOptions, TickSize, RoundConfig -ROUNDING_CONFIG: Tuple[TickSize, RoundConfig] = { +ROUNDING_CONFIG: dict[TickSize, RoundConfig] = { "0.1": RoundConfig(price=1, size=2, amount=3), "0.01": RoundConfig(price=2, size=2, amount=4), "0.001": RoundConfig(price=3, size=2, amount=5), @@ -40,13 +39,6 @@ def __init__(self, signer: Signer, sig_type=None, funder=None): # Used for Polymarket proxy wallets and other smart contract wallets # Defaults to the address of the signer self.funder = funder if funder is not None else self.signer.address() - self.contract_config = self._get_contract_config(self.signer.get_chain_id()) - self.order_builder = UtilsOrderBuild( - self.contract_config.exchange, self.signer.get_chain_id(), self.signer - ) - - def _get_contract_config(self, chain_id: int): - return get_contract_config(chain_id) def get_order_amounts( self, side: str, size: float, price: float, round_config: RoundConfig @@ -82,7 +74,9 @@ def get_order_amounts( else: raise ValueError(f"order_args.side must be '{BUY}' or '{SELL}'") - def create_order(self, order_args: OrderArgs, tick_size: TickSize) -> SignedOrder: + def create_order( + self, order_args: OrderArgs, options: CreateOrderOptions + ) -> SignedOrder: """ Creates and signs an order """ @@ -90,15 +84,15 @@ def create_order(self, order_args: OrderArgs, tick_size: TickSize) -> SignedOrde order_args.side, order_args.size, order_args.price, - ROUNDING_CONFIG[tick_size], + ROUNDING_CONFIG[options.tick_size], ) data = OrderData( maker=self.funder, taker=order_args.taker, tokenId=order_args.token_id, - makerAmount=maker_amount, - takerAmount=taker_amount, + makerAmount=str(maker_amount), + takerAmount=str(taker_amount), side=side, feeRateBps=str(order_args.fee_rate_bps), nonce=str(order_args.nonce), @@ -107,4 +101,14 @@ def create_order(self, order_args: OrderArgs, tick_size: TickSize) -> SignedOrde signatureType=self.sig_type, ) - return self.order_builder.build_signed_order(data) + contract_config = get_contract_config( + self.signer.get_chain_id(), options.neg_risk + ) + + order_builder = UtilsOrderBuilder( + contract_config.exchange, + self.signer.get_chain_id(), + UtilsSigner(key=self.signer.private_key), + ) + + return order_builder.build_signed_order(data) diff --git a/requirements.txt b/requirements.txt index 8b2372f..26ba7cb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -31,7 +31,7 @@ parsimonious==0.8.1 pluggy==1.0.0 protobuf==3.19.4 py==1.11.0 -py-order-utils>=0.1.3 +py-order-utils>=0.2.0 pycryptodome==3.14.0 pyparsing==3.0.7 pyrsistent==0.18.1 diff --git a/setup.py b/setup.py index 96cbdc4..443e403 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="py_clob_client", - version="0.11.0", + version="0.12.0", author="Polymarket Engineering", author_email="engineering@polymarket.com", maintainer="Polymarket Engineering", diff --git a/tests/order_builder/test_builder.py b/tests/order_builder/test_builder.py index 6fcb675..d5f06be 100644 --- a/tests/order_builder/test_builder.py +++ b/tests/order_builder/test_builder.py @@ -1,6 +1,6 @@ from unittest import TestCase -from py_clob_client.clob_types import OrderArgs +from py_clob_client.clob_types import OrderArgs, CreateOrderOptions from py_clob_client.constants import MUMBAI from py_clob_client.order_builder.constants import BUY, SELL @@ -203,7 +203,7 @@ def test_create_order_decimal_accuracy(self): size=15, side=BUY, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -222,7 +222,7 @@ def test_create_order_decimal_accuracy(self): size=15, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -241,7 +241,7 @@ def test_create_order_decimal_accuracy(self): size=101, side=BUY, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -260,7 +260,7 @@ def test_create_order_decimal_accuracy(self): size=101, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -279,7 +279,7 @@ def test_create_order_decimal_accuracy(self): size=12.8205, side=BUY, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -298,7 +298,7 @@ def test_create_order_decimal_accuracy(self): size=12.8205, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -317,7 +317,7 @@ def test_create_order_decimal_accuracy(self): size=2435.89, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -336,7 +336,7 @@ def test_create_order_decimal_accuracy(self): size=19.1, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -355,7 +355,7 @@ def test_create_order_decimal_accuracy(self): size=18233.33, side=BUY, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertEqual( signed_order.order["makerAmount"], @@ -382,7 +382,7 @@ def test_create_order_buy_0_1(self): nonce=123, expiration=50000, ), - tick_size="0.1", + options=CreateOrderOptions(tick_size="0.1"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -451,7 +451,7 @@ def test_create_order_buy_0_01(self): nonce=123, expiration=50000, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -520,7 +520,7 @@ def test_create_order_buy_0_001(self): nonce=123, expiration=50000, ), - tick_size="0.001", + options=CreateOrderOptions(tick_size="0.001"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -576,7 +576,7 @@ def test_create_order_buy_0_001(self): 0.056, ) - def test_create_order_buy_0_001(self): + def test_create_order_buy_0_0001(self): builder = OrderBuilder(signer) signed_order = builder.create_order( @@ -589,7 +589,7 @@ def test_create_order_buy_0_001(self): nonce=123, expiration=50000, ), - tick_size="0.0001", + options=CreateOrderOptions(tick_size="0.0001"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -658,7 +658,7 @@ def test_create_order_sell_0_1(self): nonce=123, expiration=50000, ), - tick_size="0.1", + options=CreateOrderOptions(tick_size="0.1"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -727,7 +727,7 @@ def test_create_order_sell_0_01(self): nonce=123, expiration=50000, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -796,7 +796,7 @@ def test_create_order_sell_0_001(self): nonce=123, expiration=50000, ), - tick_size="0.001", + options=CreateOrderOptions(tick_size="0.001"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -865,7 +865,7 @@ def test_create_order_sell_0_0001(self): nonce=123, expiration=50000, ), - tick_size="0.0001", + options=CreateOrderOptions(tick_size="0.0001"), ) self.assertTrue(isinstance(signed_order.order["salt"], int)) @@ -921,6 +921,871 @@ def test_create_order_sell_0_0001(self): 0.0056, ) + def test_create_order_decimal_accuracy_neg_risk(self): + builder = OrderBuilder(signer) + + # BUY + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.24, + size=15, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 3600000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 15000000, + ) + + # SELL + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.24, + size=15, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 15000000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 3600000, + ) + + # BUY + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.82, + size=101, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 82820000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 101000000, + ) + + # SELL + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.82, + size=101, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 101000000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 82820000, + ) + + # BUY + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.78, + size=12.8205, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 9999600, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 12820000, + ) + + # SELL + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.78, + size=12.8205, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 12820000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 9999600, + ) + + # SELL + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.39, + size=2435.89, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 2435890000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 949997100, + ) + + # SELL + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.43, + size=19.1, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 19100000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 8213000, + ) + + # BUY + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.58, + size=18233.33, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + self.assertEqual( + signed_order.order["makerAmount"], + 10575331400, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 18233330000, + ) + self.assertEqual( + signed_order.order["makerAmount"] / signed_order.order["takerAmount"], 0.58 + ) + + def test_create_order_buy_0_1_neg_risk(self): + builder = OrderBuilder(signer) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.5, + size=21.04, + side=BUY, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.1", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 10520000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["side"], + 0, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + EOA, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["makerAmount"]) + / float(signed_order.order["takerAmount"]), + 0.5, + ) + + def test_create_order_buy_0_01_neg_risk(self): + builder = OrderBuilder(signer) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.56, + size=21.04, + side=BUY, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 11782400, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["side"], + 0, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + EOA, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["makerAmount"]) + / float(signed_order.order["takerAmount"]), + 0.56, + ) + + def test_create_order_buy_0_001_neg_risk(self): + builder = OrderBuilder(signer) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.056, + size=21.04, + side=BUY, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.001", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 1178240, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["side"], + 0, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + EOA, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["makerAmount"]) + / float(signed_order.order["takerAmount"]), + 0.056, + ) + + def test_create_order_buy_0_0001_neg_risk(self): + builder = OrderBuilder(signer) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.0056, + size=21.04, + side=BUY, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.0001", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 117824, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["side"], + 0, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + EOA, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["makerAmount"]) + / float(signed_order.order["takerAmount"]), + 0.0056, + ) + + def test_create_order_sell_0_1_neg_risk(self): + builder = OrderBuilder(signer, sig_type=POLY_GNOSIS_SAFE) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.5, + size=21.04, + side=SELL, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.1", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 10520000, + ) + self.assertEqual( + signed_order.order["side"], + 1, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + POLY_GNOSIS_SAFE, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["takerAmount"]) + / float(signed_order.order["makerAmount"]), + 0.5, + ) + + def test_create_order_sell_0_01_neg_risk(self): + builder = OrderBuilder(signer, sig_type=POLY_GNOSIS_SAFE) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.56, + size=21.04, + side=SELL, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 11782400, + ) + self.assertEqual( + signed_order.order["side"], + 1, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + POLY_GNOSIS_SAFE, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["takerAmount"]) + / float(signed_order.order["makerAmount"]), + 0.56, + ) + + def test_create_order_sell_0_001_neg_risk(self): + builder = OrderBuilder(signer, sig_type=POLY_GNOSIS_SAFE) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.056, + size=21.04, + side=SELL, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.001", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 1178240, + ) + self.assertEqual( + signed_order.order["side"], + 1, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + POLY_GNOSIS_SAFE, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["takerAmount"]) + / float(signed_order.order["makerAmount"]), + 0.056, + ) + + def test_create_order_sell_0_0001_neg_risk(self): + builder = OrderBuilder(signer, sig_type=POLY_GNOSIS_SAFE) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.0056, + size=21.04, + side=SELL, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.0001", neg_risk=True), + ) + + self.assertTrue(isinstance(signed_order.order["salt"], int)) + self.assertIsNotNone(signed_order) + self.assertEqual( + signed_order.order["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order.order["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order.order["tokenId"], + 123, + ) + self.assertEqual( + signed_order.order["makerAmount"], + 21040000, + ) + self.assertEqual( + signed_order.order["takerAmount"], + 117824, + ) + self.assertEqual( + signed_order.order["side"], + 1, + ) + self.assertEqual( + signed_order.order["expiration"], + 50000, + ) + self.assertEqual( + signed_order.order["nonce"], + 123, + ) + self.assertEqual( + signed_order.order["feeRateBps"], + 111, + ) + self.assertEqual( + signed_order.order["signatureType"], + POLY_GNOSIS_SAFE, + ) + self.assertIsNotNone(signed_order.signature) + self.assertEqual( + float(signed_order.order["takerAmount"]) + / float(signed_order.order["makerAmount"]), + 0.0056, + ) + + def test_dict_order_buy_neg_risk(self): + builder = OrderBuilder(signer) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.56, + size=21.04, + side=BUY, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + + self.assertIsNotNone(signed_order) + + signed_order_dict = signed_order.dict() + + self.assertIsNotNone(signed_order_dict) + self.assertTrue(isinstance(signed_order_dict["salt"], int)) + self.assertEqual( + signed_order_dict["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order_dict["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order_dict["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order_dict["tokenId"], + "123", + ) + self.assertEqual( + signed_order_dict["makerAmount"], + "11782400", + ) + self.assertEqual( + signed_order_dict["takerAmount"], + "21040000", + ) + self.assertEqual( + signed_order_dict["side"], + BUY, + ) + self.assertEqual( + signed_order_dict["expiration"], + "50000", + ) + self.assertEqual( + signed_order_dict["nonce"], + "123", + ) + self.assertEqual( + signed_order_dict["feeRateBps"], + "111", + ) + self.assertEqual( + signed_order_dict["signatureType"], + EOA, + ) + self.assertIsNotNone(signed_order_dict["signature"]) + + def test_dict_order_sell_neg_risk(self): + builder = OrderBuilder(signer, sig_type=POLY_GNOSIS_SAFE) + + signed_order = builder.create_order( + order_args=OrderArgs( + token_id="123", + price=0.56, + size=21.04, + side=SELL, + fee_rate_bps=111, + nonce=123, + expiration=50000, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ) + + self.assertIsNotNone(signed_order) + + signed_order_dict = signed_order.dict() + + self.assertIsNotNone(signed_order_dict) + self.assertTrue(isinstance(signed_order_dict["salt"], int)) + self.assertEqual( + signed_order_dict["maker"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order_dict["signer"], + "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", + ) + self.assertEqual( + signed_order_dict["taker"], + "0x0000000000000000000000000000000000000000", + ) + self.assertEqual( + signed_order_dict["tokenId"], + "123", + ) + self.assertEqual( + signed_order_dict["makerAmount"], + "21040000", + ) + self.assertEqual( + signed_order_dict["takerAmount"], + "11782400", + ) + self.assertEqual( + signed_order_dict["side"], + SELL, + ) + self.assertEqual( + signed_order_dict["expiration"], + "50000", + ) + self.assertEqual( + signed_order_dict["nonce"], + "123", + ) + self.assertEqual( + signed_order_dict["feeRateBps"], + "111", + ) + self.assertEqual( + signed_order_dict["signatureType"], + POLY_GNOSIS_SAFE, + ) + self.assertIsNotNone(signed_order_dict["signature"]) + def test_dict_order_buy(self): builder = OrderBuilder(signer) @@ -934,7 +1799,7 @@ def test_dict_order_buy(self): nonce=123, expiration=50000, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertIsNotNone(signed_order) @@ -1002,7 +1867,7 @@ def test_dict_order_sell(self): nonce=123, expiration=50000, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ) self.assertIsNotNone(signed_order) diff --git a/tests/test_utilities.py b/tests/test_utilities.py index a6240ad..4c0f95a 100644 --- a/tests/test_utilities.py +++ b/tests/test_utilities.py @@ -1,6 +1,10 @@ from unittest import TestCase -from py_clob_client.clob_types import OrderArgs, OrderType +from py_clob_client.clob_types import ( + OrderArgs, + OrderType, + CreateOrderOptions, +) from py_clob_client.constants import MUMBAI from py_clob_client.order_builder.constants import BUY, SELL from py_clob_client.signer import Signer @@ -171,7 +175,7 @@ def test_order_to_json_0_1(self): size=100, side=BUY, ), - tick_size="0.1", + options=CreateOrderOptions(tick_size="0.1"), ), owner=owner, orderType=OrderType.GTC, @@ -210,7 +214,7 @@ def test_order_to_json_0_1(self): size=100, side=SELL, ), - tick_size="0.1", + options=CreateOrderOptions(tick_size="0.1"), ), owner=owner, orderType=OrderType.GTC, @@ -249,7 +253,7 @@ def test_order_to_json_0_1(self): size=100, side=BUY, ), - tick_size="0.1", + options=CreateOrderOptions(tick_size="0.1"), ), owner=owner, orderType=OrderType.GTD, @@ -288,7 +292,7 @@ def test_order_to_json_0_1(self): size=100, side=SELL, ), - tick_size="0.1", + options=CreateOrderOptions(tick_size="0.1"), ), owner=owner, orderType=OrderType.GTD, @@ -337,7 +341,7 @@ def test_order_to_json_0_01(self): size=100, side=BUY, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ), owner=owner, orderType=OrderType.GTC, @@ -376,7 +380,7 @@ def test_order_to_json_0_01(self): size=100, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ), owner=owner, orderType=OrderType.GTC, @@ -415,7 +419,7 @@ def test_order_to_json_0_01(self): size=100, side=BUY, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ), owner=owner, orderType=OrderType.GTD, @@ -454,7 +458,7 @@ def test_order_to_json_0_01(self): size=100, side=SELL, ), - tick_size="0.01", + options=CreateOrderOptions(tick_size="0.01"), ), owner=owner, orderType=OrderType.GTD, @@ -503,7 +507,7 @@ def test_order_to_json_0_001(self): size=100, side=BUY, ), - tick_size="0.001", + options=CreateOrderOptions(tick_size="0.001"), ), owner=owner, orderType=OrderType.GTC, @@ -542,7 +546,7 @@ def test_order_to_json_0_001(self): size=100, side=SELL, ), - tick_size="0.001", + options=CreateOrderOptions(tick_size="0.001"), ), owner=owner, orderType=OrderType.GTC, @@ -581,7 +585,7 @@ def test_order_to_json_0_001(self): size=100, side=BUY, ), - tick_size="0.001", + options=CreateOrderOptions(tick_size="0.001"), ), owner=owner, orderType=OrderType.GTD, @@ -620,7 +624,7 @@ def test_order_to_json_0_001(self): size=100, side=SELL, ), - tick_size="0.001", + options=CreateOrderOptions(tick_size="0.001"), ), owner=owner, orderType=OrderType.GTD, @@ -669,7 +673,671 @@ def test_order_to_json_0_0001(self): size=100, side=BUY, ), - tick_size="0.0001", + options=CreateOrderOptions(tick_size="0.0001"), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "50000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTC SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.0005, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.0001"), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "50000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.0005, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.0001"), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "50000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.0005, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.0001"), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "50000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + def test_order_to_json_0_1_neg_risk(self): + # publicly known private key + private_key = ( + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ) + chain_id = MUMBAI + signer = Signer(private_key=private_key, chain_id=chain_id) + owner = "aaa-bbb-ccc" + builder = OrderBuilder(signer) + + # GTC BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.5, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.1", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "50000000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTC SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.5, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.1", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "50000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.5, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.1", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "50000000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.5, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.1", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "50000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + def test_order_to_json_0_01_neg_risk(self): + # publicly known private key + private_key = ( + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ) + chain_id = MUMBAI + signer = Signer(private_key=private_key, chain_id=chain_id) + owner = "aaa-bbb-ccc" + builder = OrderBuilder(signer) + + # GTC BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.05, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "5000000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTC SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.05, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "5000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.05, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "5000000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.05, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.01", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "5000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + def test_order_to_json_0_001_neg_risk(self): + # publicly known private key + private_key = ( + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ) + chain_id = MUMBAI + signer = Signer(private_key=private_key, chain_id=chain_id) + owner = "aaa-bbb-ccc" + builder = OrderBuilder(signer) + + # GTC BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.005, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.001", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "500000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTC SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.005, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.001", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTC, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTC") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "500000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.005, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.001", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "500000") + self.assertEqual(json_order["order"]["takerAmount"], "100000000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "BUY") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + # GTD SELL + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.005, + size=100, + side=SELL, + ), + options=CreateOrderOptions(tick_size="0.001", neg_risk=True), + ), + owner=owner, + orderType=OrderType.GTD, + ) + + self.assertIsNotNone(json_order) + self.assertEqual(json_order["orderType"], "GTD") + self.assertEqual(json_order["owner"], owner) + self.assertIsNotNone(json_order["order"]) + self.assertIsNotNone(json_order["order"]["salt"]) + self.assertEqual( + json_order["order"]["maker"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["signer"], "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266" + ) + self.assertEqual( + json_order["order"]["taker"], "0x0000000000000000000000000000000000000000" + ) + self.assertEqual(json_order["order"]["tokenId"], "100") + self.assertEqual(json_order["order"]["makerAmount"], "100000000") + self.assertEqual(json_order["order"]["takerAmount"], "500000") + self.assertEqual(json_order["order"]["expiration"], "0") + self.assertEqual(json_order["order"]["nonce"], "0") + self.assertEqual(json_order["order"]["feeRateBps"], "0") + self.assertEqual(json_order["order"]["side"], "SELL") + self.assertEqual(json_order["order"]["signatureType"], 0) + self.assertIsNotNone(json_order["order"]["signature"]) + + def test_order_to_json_0_0001_neg_risk(self): + # publicly known private key + private_key = ( + "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80" + ) + chain_id = MUMBAI + signer = Signer(private_key=private_key, chain_id=chain_id) + owner = "aaa-bbb-ccc" + builder = OrderBuilder(signer) + + # GTC BUY + json_order = order_to_json( + order=builder.create_order( + order_args=OrderArgs( + token_id="100", + price=0.0005, + size=100, + side=BUY, + ), + options=CreateOrderOptions(tick_size="0.0001", neg_risk=True), ), owner=owner, orderType=OrderType.GTC, @@ -708,7 +1376,7 @@ def test_order_to_json_0_0001(self): size=100, side=SELL, ), - tick_size="0.0001", + options=CreateOrderOptions(tick_size="0.0001", neg_risk=True), ), owner=owner, orderType=OrderType.GTC, @@ -747,7 +1415,7 @@ def test_order_to_json_0_0001(self): size=100, side=BUY, ), - tick_size="0.0001", + options=CreateOrderOptions(tick_size="0.0001", neg_risk=True), ), owner=owner, orderType=OrderType.GTD, @@ -786,7 +1454,7 @@ def test_order_to_json_0_0001(self): size=100, side=SELL, ), - tick_size="0.0001", + options=CreateOrderOptions(tick_size="0.0001", neg_risk=True), ), owner=owner, orderType=OrderType.GTD,