diff --git a/v4-client-js/README.md b/v4-client-js/README.md index d45a3262..e8b4a60a 100644 --- a/v4-client-js/README.md +++ b/v4-client-js/README.md @@ -45,7 +45,7 @@ git clone git@github.com:dydxprotocol/v4-clients.git cd v4-client-js/examples ``` -These examples by default use a test account with `DYDX_TEST_MNEMONIC` from the TS client library under `v4-client-js/examples/constants`. +These examples by default use a test account with `DYDX_TEST_MNEMONIC` from the TS client library under `v4-client-js/examples/constants`, but you can use any test address that you own. ### 3. Run the scripts with node diff --git a/v4-client-py-v2/documentation/intro.md b/v4-client-py-v2/documentation/intro.md index c3497562..6fd5f713 100644 --- a/v4-client-py-v2/documentation/intro.md +++ b/v4-client-py-v2/documentation/intro.md @@ -3,6 +3,7 @@ This guide will help you get started with the dYdX Python SDK, which allows for asynchronous programming and interaction with the dYdX protocol. ## Table of Contents + 1. [Asynchronous Programming](#asynchronous-programming) 2. [Node Client](#node-client) 3. [REST Indexer](#rest-indexer) @@ -23,11 +24,13 @@ asyncio.run(main()) ``` ### Node client + `NodeClient` allows to send transactions and fetch node state. E.g. you can deposit funds using the `deposit` method: https://github.com/dydxprotocol/v4-clients/blob/3330f67752d430f9e0a20b419da4dc9daf7f7be0/v4-client-py-v2/examples/transfer_example_deposit.py#L1-L24 **Note:** It's possible to create a read only node client which doesn't allow to send transactions: + ```python from dydx_v4_client import QueryNodeClient from dydx_v4_client.network import secure_channel @@ -37,7 +40,9 @@ node = await QueryNodeClient(secure_channel("test-dydx-grpc.kingnodes.com")) ``` ### REST Indexer + `IndexerClient` allows to fetch data from indexer: +Let's take the test address: dydx14zzueazeh0hj67cghhf9jypslcf9sh2n5k6art, but you can use any testnet address. ```python import asyncio @@ -53,9 +58,10 @@ async def test_account(): ``` ### Websockets + Websockets allow to subscribe to live updates from the indexer. Learn more in the [WebSocket Guide](./using_websockets.md). ### Faucet + Faucet allows to obtain usdc on testnet. To use it create `FaucetClient`: https://github.com/dydxprotocol/v4-clients/blob/3330f67752d430f9e0a20b419da4dc9daf7f7be0/v4-client-py-v2/examples/faucet_endpoint.py#L1-L15 - diff --git a/v4-client-py-v2/dydx_v4_client/node/market.py b/v4-client-py-v2/dydx_v4_client/node/market.py index 7cfaaf6d..968289bd 100644 --- a/v4-client-py-v2/dydx_v4_client/node/market.py +++ b/v4-client-py-v2/dydx_v4_client/node/market.py @@ -67,13 +67,17 @@ def order( good_til_block: int = None, good_til_block_time: int = None, execution: OrderExecution = OrderExecution.DEFAULT, + condition_type=None, conditional_order_trigger_subticks: int = 0, ) -> Order: order_time_in_force = OrderHelper.calculate_time_in_force( order_type, time_in_force, post_only, execution ) client_metadata = OrderHelper.calculate_client_metadata(order_type) - condition_type = OrderHelper.calculate_condition_type(order_type) + + # Use the provided condition_type if given, otherwise calculate it + if condition_type is None: + condition_type = OrderHelper.calculate_condition_type(order_type) return order( order_id=order_id, diff --git a/v4-client-py-v2/examples/stop_limit_order_example.py b/v4-client-py-v2/examples/stop_limit_order_example.py index 08e53644..b486f42e 100644 --- a/v4-client-py-v2/examples/stop_limit_order_example.py +++ b/v4-client-py-v2/examples/stop_limit_order_example.py @@ -1,4 +1,5 @@ import random +import asyncio from dydx_v4_client import MAX_CLIENT_ID, OrderFlags from v4_proto.dydxprotocol.clob.order_pb2 import Order @@ -7,7 +8,7 @@ from dydx_v4_client.indexer.rest.indexer_client import IndexerClient from dydx_v4_client.network import TESTNET from dydx_v4_client.node.client import NodeClient -from dydx_v4_client.node.market import Market +from dydx_v4_client.node.market import Market, since_now from dydx_v4_client.wallet import Wallet from tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS @@ -15,7 +16,7 @@ async def test_place_stop_limit_order(): - size, trigger_price = 0.001, 1800 + size, trigger_price, price = 0.001, 1800, 1799 node = await NodeClient.connect(TESTNET.node) indexer = IndexerClient(TESTNET.rest_indexer) @@ -25,7 +26,7 @@ async def test_place_stop_limit_order(): wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC, TEST_ADDRESS) order_id = market.order_id( - TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.SHORT_TERM + TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.CONDITIONAL ) current_block = await node.latest_block_height() @@ -35,11 +36,13 @@ async def test_place_stop_limit_order(): order_type=OrderType.STOP_LIMIT, side=Order.Side.SIDE_SELL, size=size, - price=trigger_price, - time_in_force=Order.TimeInForce.TIME_IN_FORCE_IOC, + price=price, + time_in_force=Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED, reduce_only=False, - execution=OrderExecution.IOC, - good_til_block=current_block + 10, + execution=OrderExecution.DEFAULT, + good_til_block_time=since_now(seconds=3600), + condition_type=Order.ConditionType.CONDITION_TYPE_STOP_LOSS, + conditional_order_trigger_subticks=market.calculate_subticks(trigger_price), ) print(new_order) transaction = await node.place_order( @@ -49,3 +52,6 @@ async def test_place_stop_limit_order(): print(transaction) wallet.sequence += 1 + + +asyncio.run(test_place_stop_limit_order()) diff --git a/v4-client-py-v2/examples/stop_market_order_example.py b/v4-client-py-v2/examples/stop_market_order_example.py index 917c1e24..19e95494 100644 --- a/v4-client-py-v2/examples/stop_market_order_example.py +++ b/v4-client-py-v2/examples/stop_market_order_example.py @@ -1,4 +1,5 @@ import random +import asyncio from dydx_v4_client import MAX_CLIENT_ID, OrderFlags from v4_proto.dydxprotocol.clob.order_pb2 import Order @@ -7,7 +8,7 @@ from dydx_v4_client.indexer.rest.indexer_client import IndexerClient from dydx_v4_client.network import TESTNET from dydx_v4_client.node.client import NodeClient -from dydx_v4_client.node.market import Market +from dydx_v4_client.node.market import Market, since_now from dydx_v4_client.wallet import Wallet from tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS @@ -25,7 +26,7 @@ async def test_place_stop_market_order(): wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC, TEST_ADDRESS) order_id = market.order_id( - TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.SHORT_TERM + TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.CONDITIONAL ) current_block = await node.latest_block_height() @@ -39,7 +40,9 @@ async def test_place_stop_market_order(): time_in_force=Order.TimeInForce.TIME_IN_FORCE_IOC, reduce_only=False, execution=OrderExecution.IOC, - good_til_block=current_block + 10, + good_til_block_time=since_now(seconds=60), + condition_type=Order.ConditionType.CONDITION_TYPE_STOP_LOSS, + conditional_order_trigger_subticks=market.calculate_subticks(trigger_price), ) print(new_order) transaction = await node.place_order( @@ -49,3 +52,6 @@ async def test_place_stop_market_order(): print(transaction) wallet.sequence += 1 + + +asyncio.run(test_place_stop_market_order())