Skip to content

Commit d2be7d3

Browse files
committed
feat: update sdk
1 parent a56bd48 commit d2be7d3

File tree

8 files changed

+52
-23
lines changed

8 files changed

+52
-23
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
TRADING_PASSWORD="xxxxxxxx"
12
DELTADEFI_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
23
BASE_URL="http://localhost:8080"

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "deltadefi"
3-
version = "0.0.10"
3+
version = "0.0.11"
44
classifiers = [
55
"Intended Audience :: Developers",
66
"License :: OSI Approved :: Apache Software License",

src/deltadefi/clients/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# flake8: noqa
2-
from .clients import *
2+
from .client import *

src/deltadefi/clients/accounts.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
from typing import List, Literal
2+
from typing import List
33

44
from sidan_gin import Asset, UTxO
55

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# flake8: noqa: E501
2-
from sidan_gin import Wallet
2+
from sidan_gin import Wallet, decrypt_with_cipher
33

44
from deltadefi.clients.accounts import Accounts
55
from 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
88
from deltadefi.models.models import OrderSide, OrderType
99
from 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}

tests/clients/test_order.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ class TestOrder(unittest.TestCase):
1212

1313
def setUp(self):
1414
api_key = os.getenv("DELTADEFI_API_KEY")
15+
password = os.getenv("TRADING_PASSWORD")
1516
base_url = os.getenv("BASE_URL", "http://localhost:8080")
16-
mnemonic = os.getenv("SEED_PHRASE")
17-
print(f"Mnemonic: {mnemonic}")
18-
print(f"Mnemonic words: {len(mnemonic.split()) if mnemonic else 0}")
19-
wallet = Wallet.new_mnemonic(mnemonic)
2017
if not api_key:
2118
self.skipTest("DELTADEFI_API_KEY not set in environment variables")
22-
self.api = ApiClient(api_key=api_key, base_url=base_url, wallet=wallet)
19+
api = ApiClient(api_key=api_key, base_url=base_url)
20+
api.load_operation_key(password)
21+
self.api = api
2322

2423
def test_post_order(self):
2524
response: PostOrderResponse = self.api.post_order(

0 commit comments

Comments
 (0)