{"id":4699,"library":"py-order-utils","title":"Polymarket Python Order Utilities","description":"Python utilities used to generate and sign orders for Polymarket's Exchange. The library facilitates interaction with Polymarket's Central Limit Order Book (CLOB) by providing tools for order building, signing, and data structuring. The current version is 0.3.2, with releases primarily driven by smart contract updates and critical bug fixes, indicating an active but demand-driven release cadence.","status":"active","version":"0.3.2","language":"en","source_language":"en","source_url":"https://github.com/polymarket/python-order-utils","tags":["polymarket","web3","ethereum","crypto","defi","orders","signature","blockchain"],"install":[{"cmd":"pip install py-order-utils","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Core dependency for Ethereum interaction, contract calls, and transaction signing.","package":"web3"},{"reason":"Used for managing Ethereum private keys and signing messages/transactions.","package":"eth-account"},{"reason":"Provides utility functions for common Ethereum operations.","package":"eth-utils"}],"imports":[{"symbol":"OrderBuilder","correct":"from py_order_utils.builders import OrderBuilder"},{"symbol":"Signer","correct":"from py_order_utils.signer import Signer"},{"note":"Assumed path based on common Python package structure for models/data classes. Not explicitly shown in quickstart snippet but necessary.","symbol":"OrderData","correct":"from py_order_utils.models import OrderData"}],"quickstart":{"code":"import os\nimport json\nfrom web3 import Web3\nfrom py_order_utils.builders import OrderBuilder\nfrom py_order_utils.signer import Signer\nfrom py_order_utils.models import OrderData, SignedOrder\n\n# Configuration (replace with your actual values or environment variables)\nPRIVATE_KEY = os.environ.get('POLYMARKET_PRIVATE_KEY', '0x...') # Your Ethereum private key\nRPC_URL = os.environ.get('POLYMARKET_RPC_URL', 'https://rpc-amoy.polygon.technology/') # e.g., Polygon Amoy RPC URL\nEXCHANGE_ADDRESS = os.environ.get('POLYMARKET_EXCHANGE_ADDRESS', '0x...') # Polymarket CLOB Exchange Contract Address\nCHAIN_ID = int(os.environ.get('POLYMARKET_CHAIN_ID', '80002')) # e.g., 80002 for Polygon Amoy\n\ndef main():\n    if PRIVATE_KEY == '0x...' or EXCHANGE_ADDRESS == '0x...':\n        print(\"Please configure POLYMARKET_PRIVATE_KEY, POLYMARKET_RPC_URL, EXCHANGE_ADDRESS, and CHAIN_ID environment variables.\")\n        return\n\n    w3 = Web3(Web3.HTTPProvider(RPC_URL))\n    if not w3.is_connected():\n        print(f\"Failed to connect to RPC at {RPC_URL}\")\n        return\n\n    print(f\"Connected to chain ID: {w3.eth.chain_id}\")\n    \n    signer_instance = Signer(PRIVATE_KEY)\n    builder = OrderBuilder(EXCHANGE_ADDRESS, CHAIN_ID, signer_instance)\n\n    # Example OrderData payload (replace with actual order parameters)\n    order_data = OrderData(\n        maker='0x' + signer_instance.address[2:], # Your wallet address\n        taker='0x0000000000000000000000000000000000000000', # Taker address (0x0 for open orders)\n        longToken='0x...', # Address of the long token for the market\n        shortToken='0x...', # Address of the short token for the market\n        longTokenAmount='1000000000000000000', # 1 Long Token (example: 1e18 wei)\n        shortTokenAmount='500000000000000000', # 0.5 Short Token (example: 0.5e18 wei)\n        salt=w3.eth.get_block('latest').number, # Unique salt for the order\n        expiry=w3.eth.get_block('latest').timestamp + 3600, # Expires in 1 hour\n        feeRate='0', # Example fee rate\n        isPostOnly=False,\n        market='0x...', # Market contract address\n        minAmountReceived='0' # Minimum amount to receive\n    )\n\n    try:\n        # Create and sign the order\n        signed_order: SignedOrder = builder.build_signed_order(order_data)\n\n        # Generate the Order and Signature JSON to be sent to the CLOB API\n        print(\"\\n--- Signed Order JSON ---\")\n        print(json.dumps(signed_order.dict(), indent=2))\n\n    except Exception as e:\n        print(f\"An error occurred: {e}\")\n\nif __name__ == '__main__':\n    main()","lang":"python","description":"This quickstart demonstrates how to initialize the `Signer` and `OrderBuilder` classes, construct a sample `OrderData` object with placeholder values, and generate a signed order suitable for submission to Polymarket's CLOB API. Ensure environment variables for `POLYMARKET_PRIVATE_KEY`, `POLYMARKET_RPC_URL`, `POLYMARKET_EXCHANGE_ADDRESS`, and `POLYMARKET_CHAIN_ID` are set for a runnable example."},"warnings":[{"fix":"Users upgrading from versions prior to 0.3.1 should ensure their environment is free of `pysha3` dependencies if experiencing import errors or unexpected behavior. The library now uses standard `web3.py` and `eth-account` cryptographic primitives internally, which should resolve compatibility issues.","message":"Breaking change in cryptographic dependencies. Version 0.3.1 replaced `pysha3` with compatible libraries.","severity":"breaking","affected_versions":"<0.3.1"},{"fix":"Always use the latest stable version of `py-order-utils`. Ensure that `EXCHANGE_ADDRESS`, `CHAIN_ID`, and any token/market addresses used in `OrderData` are up-to-date with the current Polymarket deployment. Refer to Polymarket's official documentation or contract repositories for the latest addresses.","message":"Major contract migrations (e.g., to Amoy testnet) frequently require updates to contract addresses and ABIs.","severity":"breaking","affected_versions":"All versions, specifically v0.3.0 and prior."},{"fix":"As of v0.3.2, the library explicitly forces the signature to include the '0x' prefix. If manually handling signatures or using older versions, ensure the signature string is prefixed with '0x' before sending to APIs or contracts.","message":"Signature formatting for submissions must include the '0x' prefix.","severity":"gotcha","affected_versions":"<0.3.2"},{"fix":"Always review the `OrderData` model definition in the library or the latest release notes when upgrading. Ensure all required fields are present and correctly formatted in your order payloads to avoid validation errors.","message":"Frequent updates to `OrderData` schema, including new required fields like `minAmountReceived` and `timeInForce`.","severity":"breaking","affected_versions":"v0.0.14, v0.0.23, v0.1.0, v0.1.1, and potentially future releases."}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}