Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mainnet examples, readme and configs #97

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
32 changes: 32 additions & 0 deletions v4-client-py/examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# User guide to test examples

1. Go to your repository location for the Python client
```
cd ~/.../v4-clients/v4-client-py
```
2. Create a virtual environment for the DyDx client, activate it and install requirements
```
python3 -m venv venv
source venv/bin/activate
pip3 install -r requirements.txt
```
3. Export PYTHONPATH for your current location
```
export PYTHONPATH='~/.../v4-clients/v4-client-py'
```

Now you are ready to use the examples in this folder.

# Set up your configurations in constants.py
~/.../v4-clients/v4-client-py/v4_client_py/clients/constants.py

```
VALIDATOR_GRPC_ENDPOINT = <>
AERIAL_CONFIG_URL = <>
AERIAL_GRPC_OR_REST_PREFIX = <>
INDEXER_REST_ENDPOINT = <>
INDEXER_WS_ENDPOINT = <>
CHAIN_ID = <>
ENV = <>
```

88 changes: 43 additions & 45 deletions v4-client-py/examples/account_endpoints.py
Original file line number Diff line number Diff line change
@@ -1,122 +1,120 @@
'''Example for placing, replacing, and canceling orders.
"""Example for placing, replacing, and canceling orders.

Usage: python -m examples.private_endpoints
'''
"""

from v4_client_py.clients import IndexerClient, Subaccount
from v4_client_py.clients.constants import Network

from tests.constants import DYDX_TEST_MNEMONIC

client = IndexerClient(
config=Network.testnet().indexer_config,
config=Network.config_network().indexer_config,
)

try:
subaccount = Subaccount.from_mnemonic(DYDX_TEST_MNEMONIC)
address = subaccount.address


# Get subaccounts
try:
subaccounts_response = client.account.get_subaccounts(address)
print(f'{subaccounts_response.data}')
subaccounts = subaccounts_response.data['subaccounts']
print(f"{subaccounts_response.data}")
subaccounts = subaccounts_response.data["subaccounts"]
subaccount_0 = subaccounts[0]
print(f'{subaccount_0}')
subaccount_0_subaccountNumber = subaccount_0['subaccountNumber']
print(f"{subaccount_0}")
subaccount_0_subaccountNumber = subaccount_0["subaccountNumber"]
except:
print('failed to get subaccounts')
print("failed to get subaccounts")

try:
subaccount_response = client.account.get_subaccount(address, 0)
print(f'{subaccount_response.data}')
subaccount = subaccount_response.data['subaccount']
print(f'{subaccount}')
subaccount_subaccountNumber = subaccount['subaccountNumber']
print(f"{subaccount_response.data}")
subaccount = subaccount_response.data["subaccount"]
print(f"{subaccount}")
subaccount_subaccountNumber = subaccount["subaccountNumber"]
except:
print('failed to get subaccount')
print("failed to get subaccount")

# Get positions
try:
asset_positions_response = client.account.get_subaccount_asset_positions(address, 0)
print(f'{asset_positions_response.data}')
asset_positions = asset_positions_response.data['positions']
print(f"{asset_positions_response.data}")
asset_positions = asset_positions_response.data["positions"]
if len(asset_positions) > 0:
asset_positions_0 = asset_positions[0]
print(f'{asset_positions_0}')
print(f"{asset_positions_0}")
except:
print('failed to get asset positions')
print("failed to get asset positions")

try:
perpetual_positions_response = client.account.get_subaccount_perpetual_positions(address, 0)
print(f'{perpetual_positions_response.data}')
perpetual_positions = perpetual_positions_response.data['positions']
print(f"{perpetual_positions_response.data}")
perpetual_positions = perpetual_positions_response.data["positions"]
if len(perpetual_positions) > 0:
perpetual_positions_0 = perpetual_positions[0]
print(f'{perpetual_positions_0}')
print(f"{perpetual_positions_0}")
except:
print('failed to get perpetual positions')
print("failed to get perpetual positions")

# Get transfers
try:
transfers_response = client.account.get_subaccount_transfers(address, 0)
print(f'{transfers_response.data}')
transfers = transfers_response.data['transfers']
print(f"{transfers_response.data}")
transfers = transfers_response.data["transfers"]
if len(transfers) > 0:
transfers_0 = transfers[0]
print(f'{transfers_0}')
print(f"{transfers_0}")
except:
print('failed to get transfers')
print("failed to get transfers")

# Get orders
try:
orders_response = client.account.get_subaccount_orders(address, 0)
print(f'{orders_response.data}')
print(f"{orders_response.data}")
orders = orders_response.data
if len(orders) > 0:
order_0 = orders[0]
print(f'{order_0}')
order_0_id = order_0['id']
print(f"{order_0}")
order_0_id = order_0["id"]
order_response = client.account.get_order(order_id=order_0_id)
order = order_response.data
order_id = order['id']
order_id = order["id"]
except:
print('failed to get orders')

print("failed to get orders")

# Get fills
try:
fills_response = client.account.get_subaccount_fills(address, 0)
print(f'{fills_response.data}')
fills = fills_response.data['fills']
print(f"{fills_response.data}")
fills = fills_response.data["fills"]
if len(fills) > 0:
fill_0 = fills[0]
print(f'{fill_0}')
print(f"{fill_0}")
except:
print('failed to get fills')
print("failed to get fills")

# Get funding
try:
funding_response = client.account.get_subaccount_funding(address, 0)
print(f'{funding_response.data}')
funding = funding_response.data['fundingPayments']
print(f"{funding_response.data}")
funding = funding_response.data["fundingPayments"]
if len(funding) > 0:
funding_0 = funding[0]
print(f'{funding_0}')
print(f"{funding_0}")
except:
print('failed to get funding')
print("failed to get funding")

# Get historical pnl
try:
historical_pnl_response = client.account.get_subaccount_historical_pnls(address, 0)
print(f'{historical_pnl_response.data}')
historical_pnl = historical_pnl_response.data['historicalPnl']
print(f"{historical_pnl_response.data}")
historical_pnl = historical_pnl_response.data["historicalPnl"]
if len(historical_pnl) > 0:
historical_pnl_0 = historical_pnl[0]
print(f'{historical_pnl_0}')
print(f"{historical_pnl_0}")
except:
print('failed to get historical pnl')
print("failed to get historical pnl")

except:
print('from_mnemonic failed')
print("from_mnemonic failed")
21 changes: 0 additions & 21 deletions v4-client-py/examples/chain_client/Account.py

This file was deleted.

23 changes: 0 additions & 23 deletions v4-client-py/examples/chain_client/BankBalance.py

This file was deleted.

22 changes: 0 additions & 22 deletions v4-client-py/examples/chain_client/BankBalances.py

This file was deleted.

22 changes: 0 additions & 22 deletions v4-client-py/examples/chain_client/GetTx.py

This file was deleted.

1 change: 0 additions & 1 deletion v4-client-py/examples/chain_client/test.py

This file was deleted.

37 changes: 19 additions & 18 deletions v4-client-py/examples/composite_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'''Example for trading with human readable numbers
"""Example for trading with human readable numbers

Usage: python -m examples.composite_example
'''
"""
import asyncio
import logging
from random import randrange
Expand All @@ -10,9 +10,9 @@
from v4_client_py.clients.constants import BECH32_PREFIX, Network

from v4_client_py.clients.helpers.chain_helpers import (
OrderType,
OrderSide,
OrderTimeInForce,
OrderType,
OrderSide,
OrderTimeInForce,
OrderExecution,
)
from examples.utils import loadJson
Expand All @@ -22,12 +22,12 @@

async def main() -> None:
wallet = LocalWallet.from_mnemonic(DYDX_TEST_MNEMONIC, BECH32_PREFIX)
network = Network.testnet()
network = Network.config_network()
client = CompositeClient(
network,
)
subaccount = Subaccount(wallet, 0)
ordersParams = loadJson('human_readable_orders.json')
ordersParams = loadJson("human_readable_orders.json")
for orderParams in ordersParams:
type = OrderType[orderParams["type"]]
side = OrderSide[orderParams["side"]]
Expand All @@ -48,7 +48,7 @@ async def main() -> None:
try:
tx = client.place_order(
subaccount,
market='ETH-USD',
market="ETH-USD",
type=type,
side=side,
price=price,
Expand All @@ -59,39 +59,40 @@ async def main() -> None:
good_til_time_in_seconds=time_in_force_seconds,
execution=OrderExecution.DEFAULT,
post_only=post_only,
reduce_only=False
reduce_only=False,
)
print('**Order Tx**')
print("**Order Tx**")
print(tx)
except Exception as error:
print('**Order Failed**')
print("**Order Failed**")
print(str(error))

await asyncio.sleep(5) # wait for placeOrder to complete


try:
tx = client.place_order(
subaccount,
market='ETH-USD',
market="ETH-USD",
type=OrderType.STOP_MARKET,
side=OrderSide.SELL,
price=900.0,
size=0.01,
client_id=randrange(0, 100000000),
time_in_force=OrderTimeInForce.GTT,
good_til_block=0, # long term orders use GTBT
good_til_block=0, # long term orders use GTBT
good_til_time_in_seconds=1000,
execution=OrderExecution.IOC,
post_only=False,
reduce_only=False,
trigger_price=1000,
)
print('**Order Tx**')
print("**Order Tx**")
print(tx)
except Exception as error:
print('**Order Failed**')
print("**Order Failed**")
print(str(error))
if __name__ == '__main__':


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
asyncio.get_event_loop().run_until_complete(main())
asyncio.get_event_loop().run_until_complete(main())
Loading