11# flake8: noqa: E501
2- from sidan_gin import Wallet
2+ from sidan_gin import Wallet , decrypt_with_cipher
33
44from deltadefi .clients .accounts import Accounts
55from deltadefi .clients .app import App
6- from deltadefi .clients .market import Market
7- from deltadefi .clients .order import Order
6+ from deltadefi .clients .markets import Market
7+ from deltadefi .clients .orders import Order
88from deltadefi .models .models import OrderSide , OrderType
99from deltadefi .responses import PostOrderResponse
1010
@@ -18,8 +18,8 @@ def __init__(
1818 self ,
1919 network : str = "preprod" ,
2020 api_key : str = None ,
21- wallet : Wallet = None ,
2221 base_url : str = None ,
22+ master_wallet : Wallet = None ,
2323 ):
2424 """
2525 Initialize the ApiClient.
@@ -40,12 +40,26 @@ def __init__(
4040 self .base_url = base_url
4141
4242 self .api_key = api_key
43- self .wallet = wallet
43+ self .master_wallet = master_wallet
4444
4545 self .accounts = Accounts (base_url = self .base_url , api_key = api_key )
4646 self .app = App (base_url = self .base_url , api_key = api_key )
47- self .order = Order (base_url = self .base_url , api_key = api_key )
48- self .market = Market (base_url = self .base_url , api_key = api_key )
47+ self .orders = Order (base_url = self .base_url , api_key = api_key )
48+ self .markets = Market (base_url = self .base_url , api_key = api_key )
49+
50+ def load_operation_key (self , password : str ):
51+ """
52+ Load the operation key from the wallet using the provided password.
53+
54+ Args:
55+ password: The password to decrypt the operation key.
56+
57+ Returns:
58+ The decrypted operation key.
59+ """
60+ res = self .accounts .get_operation_key ()
61+ operation_key = decrypt_with_cipher (res ["encrypted_operation_key" ], password )
62+ self .operation_wallet = Wallet .new_root_key (operation_key )
4963
5064 def post_order (
5165 self , symbol : str , side : OrderSide , type : OrderType , quantity : int , ** kwargs
@@ -58,24 +72,39 @@ def post_order(
5872 side: The side of the order (e.g., "buy" or "sell").
5973 type: The type of the order (e.g., "limit" or "market").
6074 quantity: The quantity of the asset to be traded.
61- **kwargs: Additional parameters for the order, such as price, limit_slippage, etc.
75+ price: Required for limit order; The price for limit orders.
76+ limit_slippage: Optional; Whether to apply slippage for market orders. Defaults to False.
77+ max_slippage_basis_point: Optional; The maximum slippage in basis points for market orders. Defaults to null.
6278
6379 Returns:
6480 A PostOrderResponse object containing the response from the API.
6581
6682 Raises:
6783 ValueError: If the wallet is not initialized.
6884 """
69- if not hasattr (self , "wallet " ) or self .wallet is None :
70- raise ValueError ("Wallet is not initialized" )
85+ if not hasattr (self , "operation_wallet " ) or self .operation_wallet is None :
86+ raise ValueError ("Operation wallet is not initialized" )
7187
72- build_res = self .order .build_place_order_transaction (
88+ build_res = self .orders .build_place_order_transaction (
7389 symbol , side , type , quantity , ** kwargs
7490 )
75- print (f"build_res: { build_res } " )
76- signed_tx = self .wallet .sign_tx (build_res ["tx_hex" ])
77- submit_res = self .order .submit_place_order_transaction (
91+ signed_tx = self .operation_wallet .sign_tx (build_res ["tx_hex" ])
92+ submit_res = self .orders .submit_place_order_transaction (
7893 build_res ["order_id" ], signed_tx , ** kwargs
7994 )
80- print (f"submit_res: { submit_res } " )
8195 return submit_res
96+
97+ def cancel_order (self , order_id : str , ** kwargs ):
98+ """
99+ Cancel an order by its ID.
100+
101+ Args:
102+ order_id: The ID of the order to be canceled.
103+ """
104+ if not hasattr (self , "operation_wallet" ) or self .operation_wallet is None :
105+ raise ValueError ("Operation wallet is not initialized" )
106+
107+ build_res = self .orders .build_cancel_order_transaction (order_id )
108+ signed_tx = self .operation_wallet .sign_tx (build_res ["tx_hex" ])
109+ self .orders .submit_cancel_order_transaction (signed_tx , ** kwargs )
110+ return {"message" : "Order cancelled successfully" , "order_id" : order_id }
0 commit comments