Skip to content

Commit 3e7979e

Browse files
committed
feat: release post order
1 parent 2611146 commit 3e7979e

File tree

9 files changed

+682
-534
lines changed

9 files changed

+682
-534
lines changed

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cov-html: cov ## check code coverage and generate an html report
77
$(BROWSER) cov_html/index.html
88

99
test: ## runs tests
10-
poetry run pytest -vv
10+
poetry run dotenv run -- pytest -vv
1111

1212
clean-test: ## remove test and coverage artifacts
1313
rm -f .coverage
@@ -24,7 +24,6 @@ format: ## runs code style and formatter
2424
poetry run isort .
2525
poetry run black .
2626

27-
2827
deps:
2928
poetry lock
3029
poetry install

poetry.lock

Lines changed: 565 additions & 503 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,14 @@ license = "Apache-2.0"
1818

1919
[tool.poetry.dependencies]
2020
python = ">3.11,<4.0.0"
21-
sidan-gin = "0.1.2"
21+
sidan-gin = "0.1.6"
2222
requests = "^2.25"
2323
certifi = "2024.8.30"
2424
charset-normalizer = "3.4.0"
2525
idna = "3.10"
2626
urllib3 = "2.2.3"
2727
pycardano = "^0.12.3"
28+
dotenv = "^0.9.9"
2829

2930
[tool.poetry.group.dev.dependencies]
3031
pytest = "^8.2.0"

src/deltadefi/clients/clients.py

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

44
from deltadefi.clients.accounts import Accounts
55
from deltadefi.clients.app import App
66
from deltadefi.clients.market import Market
77
from deltadefi.clients.order import Order
8+
from deltadefi.models.models import OrderSide, OrderType
89
from deltadefi.responses import PostOrderResponse
910

1011

@@ -17,15 +18,15 @@ def __init__(
1718
self,
1819
network: str = "preprod",
1920
api_key: str = None,
20-
wallet: HDWallet = None,
21+
wallet: Wallet = None,
2122
base_url: str = None,
2223
):
2324
"""
2425
Initialize the ApiClient.
2526
2627
Args:
2728
config: An instance of ApiConfig containing the API configuration.
28-
wallet: An instance of HDWallet for signing transactions.
29+
wallet: An instance of Wallet for signing transactions.
2930
base_url: Optional; The base URL for the API. Defaults to "https://api-dev.deltadefi.io".
3031
"""
3132
if network == "mainnet":
@@ -46,23 +47,38 @@ def __init__(
4647
self.order = Order(base_url=base_url, api_key=api_key)
4748
self.market = Market(base_url=base_url, api_key=api_key)
4849

49-
async def post_order(self, **kwargs) -> PostOrderResponse:
50+
def post_order(
51+
self, symbol: str, side: OrderSide, type: OrderType, quantity: int, **kwargs
52+
) -> PostOrderResponse:
5053
"""
51-
Post an order to the DeltaDeFi API.
54+
Post an order to the DeltaDeFi API. It includes building the transaction, signing it with the wallet, and submitting it.
5255
5356
Args:
54-
data: A PostOrderRequest object containing the order details.
57+
symbol: The trading pair symbol (e.g., "BTC-USD").
58+
side: The side of the order (e.g., "buy" or "sell").
59+
type: The type of the order (e.g., "limit" or "market").
60+
quantity: The quantity of the asset to be traded.
61+
**kwargs: Additional parameters for the order, such as price, limit_slippage, etc.
5562
5663
Returns:
5764
A PostOrderResponse object containing the response from the API.
5865
5966
Raises:
6067
ValueError: If the wallet is not initialized.
6168
"""
69+
print(
70+
f"post_order: symbol={symbol}, side={side}, type={type}, quantity={quantity}, kwargs={kwargs}"
71+
)
6272
if not hasattr(self, "wallet") or self.wallet is None:
6373
raise ValueError("Wallet is not initialized")
6474

65-
build_res = "" # TODO: import wallet build order
75+
build_res = self.order.build_place_order_transaction(
76+
symbol, side, type, quantity, **kwargs
77+
)
78+
print(f"build_res: {build_res}")
6679
signed_tx = self.wallet.sign_tx(build_res["tx_hex"])
67-
submit_res = signed_tx + "" # TODO: import wallet submit tx
80+
submit_res = self.order.submit_place_order_transaction(
81+
build_res["order_id"], signed_tx, **kwargs
82+
)
83+
print(f"submit_res: {submit_res}")
6884
return submit_res

src/deltadefi/clients/order.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ def build_place_order_transaction(
3131
Build a place order transaction.
3232
3333
Args:
34-
data: A BuildPlaceOrderTransactionRequest object containing the order details.
34+
symbol: The trading pair symbol (e.g., "BTC-USD").
35+
side: The side of the order (e.g., "buy" or "sell").
36+
type: The type of the order (e.g., "limit" or "market").
37+
quantity: The quantity of the asset to be traded.
38+
**kwargs: Additional parameters for the order, such as price, limit_slippage, etc.
3539
3640
Returns:
3741
A BuildPlaceOrderTransactionResponse object containing the built order transaction.

src/deltadefi/responses/accounts.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import List
2+
from typing import List, TypedDict
33

44
from deltadefi.models.models import (
55
AssetBalance,
@@ -10,23 +10,23 @@
1010

1111

1212
@dataclass
13-
class CreateNewAPIKeyResponse:
13+
class CreateNewAPIKeyResponse(TypedDict):
1414
api_key: str
1515

1616

1717
@dataclass
18-
class GetOperationKeyResponse:
18+
class GetOperationKeyResponse(TypedDict):
1919
encrypted_operation_key: str
2020
operation_key_hash: str
2121

2222

2323
@dataclass
24-
class BuildDepositTransactionResponse:
24+
class BuildDepositTransactionResponse(TypedDict):
2525
tx_hex: str
2626

2727

2828
@dataclass
29-
class SubmitDepositTransactionResponse:
29+
class SubmitDepositTransactionResponse(TypedDict):
3030
tx_hash: str
3131

3232

@@ -41,22 +41,22 @@ class GetWithdrawalRecordsResponse(List[WithdrawalRecord]):
4141

4242

4343
@dataclass
44-
class GetOrderRecordResponse:
44+
class GetOrderRecordResponse(TypedDict):
4545
orders: List[OrderJSON]
4646

4747

4848
@dataclass
49-
class BuildWithdrawalTransactionResponse:
49+
class BuildWithdrawalTransactionResponse(TypedDict):
5050
tx_hex: str
5151

5252

5353
@dataclass
54-
class SubmitWithdrawalTransactionResponse:
54+
class SubmitWithdrawalTransactionResponse(TypedDict):
5555
tx_hash: str
5656

5757

5858
@dataclass
59-
class GetAccountInfoResponse:
59+
class GetAccountInfoResponse(TypedDict):
6060
api_key: str
6161
api_limit: int
6262
created_at: str
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
from dataclasses import dataclass
2-
from typing import List
2+
from typing import List, TypedDict
33

44
from deltadefi.models import OrderJSON
55

66

77
@dataclass
8-
class GetTermsAndConditionResponse:
8+
class GetTermsAndConditionResponse(TypedDict):
99
value: str
1010

1111

1212
@dataclass
13-
class MarketDepth:
13+
class MarketDepth(TypedDict):
1414
price: float
1515
quantity: float
1616

1717

1818
@dataclass
19-
class GetMarketDepthResponse:
19+
class GetMarketDepthResponse(TypedDict):
2020
bids: List[MarketDepth]
2121
asks: List[MarketDepth]
2222

2323

2424
@dataclass
25-
class GetMarketPriceResponse:
25+
class GetMarketPriceResponse(TypedDict):
2626
price: float
2727

2828

2929
@dataclass
30-
class Trade:
30+
class Trade(TypedDict):
3131
time: str
3232
symbol: str
3333
open: float
@@ -43,13 +43,13 @@ class GetAggregatedPriceResponse(List[Trade]):
4343

4444

4545
@dataclass
46-
class BuildPlaceOrderTransactionResponse:
46+
class BuildPlaceOrderTransactionResponse(TypedDict):
4747
order_id: str
4848
tx_hex: str
4949

5050

5151
@dataclass
52-
class SubmitPlaceOrderTransactionResponse:
52+
class SubmitPlaceOrderTransactionResponse(TypedDict):
5353
order: OrderJSON
5454

5555

@@ -59,10 +59,10 @@ class PostOrderResponse(SubmitPlaceOrderTransactionResponse):
5959

6060

6161
@dataclass
62-
class BuildCancelOrderTransactionResponse:
62+
class BuildCancelOrderTransactionResponse(TypedDict):
6363
tx_hex: str
6464

6565

6666
@dataclass
67-
class SubmitCancelOrderTransactionResponse:
67+
class SubmitCancelOrderTransactionResponse(TypedDict):
6868
tx_hash: str

tests/clients/test_order.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# flake8: noqa
2+
import os
3+
import unittest
4+
5+
from sidan_gin import Wallet
6+
7+
from deltadefi.clients import ApiClient
8+
from deltadefi.responses import PostOrderResponse
9+
10+
11+
class TestOrder(unittest.TestCase):
12+
13+
def setUp(self):
14+
api_key = os.getenv("DELTADEFI_API_KEY")
15+
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)
20+
if not api_key:
21+
self.skipTest("DELTADEFI_API_KEY not set in environment variables")
22+
self.api = ApiClient(api_key=api_key, base_url=base_url, wallet=wallet)
23+
24+
def test_post_order(self):
25+
response: PostOrderResponse = self.api.post_order(
26+
symbol="ADAUSDM",
27+
side="sell",
28+
type="limit",
29+
quantity=51,
30+
price=15,
31+
)
32+
33+
# Assert
34+
print(f"response: {response}")
35+
self.assertIn("order", response)
36+
37+
38+
if __name__ == "__main__":
39+
unittest.main()

tests/clients/test_sign.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# flake8: noqa
2+
import unittest
3+
4+
from sidan_gin import Wallet
5+
6+
7+
class TestSign(unittest.TestCase):
8+
9+
def setUp(self):
10+
self.mnemonic = "summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer summer"
11+
self.root_key = "xprv1cqa46gk29plgkg98upclnjv5t425fcpl4rgf9mq2txdxuga7jfq5shk7np6l55nj00sl3m4syzna3uwgrwppdm0azgy9d8zahyf32s62klfyhe0ayyxkc7x92nv4s77fa0v25tufk9tnv7x6dgexe9kdz5gpeqgu"
12+
self.cli_key = (
13+
"51022b7e38be01d1cc581230e18030e6e1a3e949a1fdd2aeae5f5412154fe82b"
14+
)
15+
16+
def test_sign_tx_mnemonic(self):
17+
self.wallet = Wallet.new_mnemonic(self.mnemonic)
18+
tx_hex = "84a4008182582004509185eb98edd8e2420c1ceea914d6a7a3142041039b2f12b4d4f03162d56f04018282581d605867c3b8e27840f556ac268b781578b14c5661fc63ee720dbeab663f1a000f42408258390004845038ee499ee8bc0afe56f688f27b2dd76f230d3698a9afcc1b66e0464447c1f51adaefe1ebfb0dd485a349a70479ced1d198cbdf7fe71a15d35396021a0002917d075820bdaa99eb158414dea0a91d6c727e2268574b23efe6e08ab3b841abe8059a030ca0f5d90103a0"
19+
signature = self.wallet.sign_tx(tx_hex)
20+
self.assertEqual(
21+
signature,
22+
"84a4008182582004509185eb98edd8e2420c1ceea914d6a7a3142041039b2f12b4d4f03162d56f04018282581d605867c3b8e27840f556ac268b781578b14c5661fc63ee720dbeab663f1a000f42408258390004845038ee499ee8bc0afe56f688f27b2dd76f230d3698a9afcc1b66e0464447c1f51adaefe1ebfb0dd485a349a70479ced1d198cbdf7fe71a15d35396021a0002917d075820bdaa99eb158414dea0a91d6c727e2268574b23efe6e08ab3b841abe8059a030ca1008182582089f4b576f05f5aad99bce0bdd51afe48529772f7561bb2ac9d84a4afbda1ecd658404cd1466fcc4579fa9c89656dbbd25ca659cccf2d2783417ef13a1b060bf836fbe8383c10e25c6fa323c1c81a0799e87e6cf3eaa25990113b27953a9836635a01f5d90103a0",
23+
)
24+
25+
26+
if __name__ == "__main__":
27+
unittest.main()

0 commit comments

Comments
 (0)