Skip to content

Commit a2c7adc

Browse files
authored
fix: Kman/update pyv2 conditional examples (#334)
1 parent 9e77056 commit a2c7adc

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

v4-client-js/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ git clone git@github.com:dydxprotocol/v4-clients.git
4545
cd v4-client-js/examples
4646
```
4747

48-
These examples by default use a test account with `DYDX_TEST_MNEMONIC` from the TS client library under `v4-client-js/examples/constants`.
48+
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.
4949

5050
### 3. Run the scripts with node
5151

v4-client-py-v2/documentation/intro.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
This guide will help you get started with the dYdX Python SDK, which allows for asynchronous programming and interaction with the dYdX protocol.
44

55
## Table of Contents
6+
67
1. [Asynchronous Programming](#asynchronous-programming)
78
2. [Node Client](#node-client)
89
3. [REST Indexer](#rest-indexer)
@@ -23,11 +24,13 @@ asyncio.run(main())
2324
```
2425

2526
### Node client
27+
2628
`NodeClient` allows to send transactions and fetch node state. E.g. you can deposit funds using the `deposit` method:
2729

2830
https://github.com/dydxprotocol/v4-clients/blob/3330f67752d430f9e0a20b419da4dc9daf7f7be0/v4-client-py-v2/examples/transfer_example_deposit.py#L1-L24
2931

3032
**Note:** It's possible to create a read only node client which doesn't allow to send transactions:
33+
3134
```python
3235
from dydx_v4_client import QueryNodeClient
3336
from dydx_v4_client.network import secure_channel
@@ -37,7 +40,9 @@ node = await QueryNodeClient(secure_channel("test-dydx-grpc.kingnodes.com"))
3740
```
3841

3942
### REST Indexer
43+
4044
`IndexerClient` allows to fetch data from indexer:
45+
Let's take the test address: dydx14zzueazeh0hj67cghhf9jypslcf9sh2n5k6art, but you can use any testnet address.
4146

4247
```python
4348
import asyncio
@@ -53,9 +58,10 @@ async def test_account():
5358
```
5459

5560
### Websockets
61+
5662
Websockets allow to subscribe to live updates from the indexer. Learn more in the [WebSocket Guide](./using_websockets.md).
5763

5864
### Faucet
65+
5966
Faucet allows to obtain usdc on testnet. To use it create `FaucetClient`:
6067
https://github.com/dydxprotocol/v4-clients/blob/3330f67752d430f9e0a20b419da4dc9daf7f7be0/v4-client-py-v2/examples/faucet_endpoint.py#L1-L15
61-

v4-client-py-v2/dydx_v4_client/node/market.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,17 @@ def order(
6767
good_til_block: int = None,
6868
good_til_block_time: int = None,
6969
execution: OrderExecution = OrderExecution.DEFAULT,
70+
condition_type=None,
7071
conditional_order_trigger_subticks: int = 0,
7172
) -> Order:
7273
order_time_in_force = OrderHelper.calculate_time_in_force(
7374
order_type, time_in_force, post_only, execution
7475
)
7576
client_metadata = OrderHelper.calculate_client_metadata(order_type)
76-
condition_type = OrderHelper.calculate_condition_type(order_type)
77+
78+
# Use the provided condition_type if given, otherwise calculate it
79+
if condition_type is None:
80+
condition_type = OrderHelper.calculate_condition_type(order_type)
7781

7882
return order(
7983
order_id=order_id,
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import asyncio
23

34
from dydx_v4_client import MAX_CLIENT_ID, OrderFlags
45
from v4_proto.dydxprotocol.clob.order_pb2 import Order
@@ -7,15 +8,15 @@
78
from dydx_v4_client.indexer.rest.indexer_client import IndexerClient
89
from dydx_v4_client.network import TESTNET
910
from dydx_v4_client.node.client import NodeClient
10-
from dydx_v4_client.node.market import Market
11+
from dydx_v4_client.node.market import Market, since_now
1112
from dydx_v4_client.wallet import Wallet
1213
from tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS
1314

1415
MARKET_ID = "ETH-USD"
1516

1617

1718
async def test_place_stop_limit_order():
18-
size, trigger_price = 0.001, 1800
19+
size, trigger_price, price = 0.001, 1800, 1799
1920
node = await NodeClient.connect(TESTNET.node)
2021
indexer = IndexerClient(TESTNET.rest_indexer)
2122

@@ -25,7 +26,7 @@ async def test_place_stop_limit_order():
2526
wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC, TEST_ADDRESS)
2627

2728
order_id = market.order_id(
28-
TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.SHORT_TERM
29+
TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.CONDITIONAL
2930
)
3031

3132
current_block = await node.latest_block_height()
@@ -35,11 +36,13 @@ async def test_place_stop_limit_order():
3536
order_type=OrderType.STOP_LIMIT,
3637
side=Order.Side.SIDE_SELL,
3738
size=size,
38-
price=trigger_price,
39-
time_in_force=Order.TimeInForce.TIME_IN_FORCE_IOC,
39+
price=price,
40+
time_in_force=Order.TimeInForce.TIME_IN_FORCE_UNSPECIFIED,
4041
reduce_only=False,
41-
execution=OrderExecution.IOC,
42-
good_til_block=current_block + 10,
42+
execution=OrderExecution.DEFAULT,
43+
good_til_block_time=since_now(seconds=3600),
44+
condition_type=Order.ConditionType.CONDITION_TYPE_STOP_LOSS,
45+
conditional_order_trigger_subticks=market.calculate_subticks(trigger_price),
4346
)
4447
print(new_order)
4548
transaction = await node.place_order(
@@ -49,3 +52,6 @@ async def test_place_stop_limit_order():
4952

5053
print(transaction)
5154
wallet.sequence += 1
55+
56+
57+
asyncio.run(test_place_stop_limit_order())

v4-client-py-v2/examples/stop_market_order_example.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import asyncio
23

34
from dydx_v4_client import MAX_CLIENT_ID, OrderFlags
45
from v4_proto.dydxprotocol.clob.order_pb2 import Order
@@ -7,7 +8,7 @@
78
from dydx_v4_client.indexer.rest.indexer_client import IndexerClient
89
from dydx_v4_client.network import TESTNET
910
from dydx_v4_client.node.client import NodeClient
10-
from dydx_v4_client.node.market import Market
11+
from dydx_v4_client.node.market import Market, since_now
1112
from dydx_v4_client.wallet import Wallet
1213
from tests.conftest import DYDX_TEST_MNEMONIC, TEST_ADDRESS
1314

@@ -25,7 +26,7 @@ async def test_place_stop_market_order():
2526
wallet = await Wallet.from_mnemonic(node, DYDX_TEST_MNEMONIC, TEST_ADDRESS)
2627

2728
order_id = market.order_id(
28-
TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.SHORT_TERM
29+
TEST_ADDRESS, 0, random.randint(0, MAX_CLIENT_ID), OrderFlags.CONDITIONAL
2930
)
3031

3132
current_block = await node.latest_block_height()
@@ -39,7 +40,9 @@ async def test_place_stop_market_order():
3940
time_in_force=Order.TimeInForce.TIME_IN_FORCE_IOC,
4041
reduce_only=False,
4142
execution=OrderExecution.IOC,
42-
good_til_block=current_block + 10,
43+
good_til_block_time=since_now(seconds=60),
44+
condition_type=Order.ConditionType.CONDITION_TYPE_STOP_LOSS,
45+
conditional_order_trigger_subticks=market.calculate_subticks(trigger_price),
4346
)
4447
print(new_order)
4548
transaction = await node.place_order(
@@ -49,3 +52,6 @@ async def test_place_stop_market_order():
4952

5053
print(transaction)
5154
wallet.sequence += 1
55+
56+
57+
asyncio.run(test_place_stop_market_order())

0 commit comments

Comments
 (0)