Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion v4-client-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion v4-client-py-v2/documentation/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

6 changes: 5 additions & 1 deletion v4-client-py-v2/dydx_v4_client/node/market.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 13 additions & 7 deletions v4-client-py-v2/examples/stop_limit_order_example.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -7,15 +8,15 @@
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

MARKET_ID = "ETH-USD"


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)

Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -49,3 +52,6 @@ async def test_place_stop_limit_order():

print(transaction)
wallet.sequence += 1


asyncio.run(test_place_stop_limit_order())
12 changes: 9 additions & 3 deletions v4-client-py-v2/examples/stop_market_order_example.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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(
Expand All @@ -49,3 +52,6 @@ async def test_place_stop_market_order():

print(transaction)
wallet.sequence += 1


asyncio.run(test_place_stop_market_order())
Loading