Skip to content

Commit 14aa9b8

Browse files
authored
Merge pull request #15 from Reya-Labs/feat/lstp-price-limit
Hardcode limit prices for SLTP orders
2 parents 21b9ac5 + 0539952 commit 14aa9b8

6 files changed

Lines changed: 96 additions & 143 deletions

File tree

examples/rest_api/order_entry.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ async def test_stop_loss_orders(client: ReyaTradingClient):
160160
market_id=1,
161161
is_buy=False,
162162
trigger_price="1000",
163-
price="1000",
164163
)
165164
long_sl_response = handle_order_response("Stop Loss (Long Position)", response)
166165

@@ -170,7 +169,6 @@ async def test_stop_loss_orders(client: ReyaTradingClient):
170169
market_id=1,
171170
is_buy=True,
172171
trigger_price="9000",
173-
price="9000",
174172
)
175173
short_sl_response = handle_order_response("Stop Loss (Short Position)", response)
176174

@@ -197,7 +195,6 @@ async def test_take_profit_orders(client: ReyaTradingClient):
197195
market_id=1,
198196
is_buy=False,
199197
trigger_price="10000",
200-
price="10000",
201198
)
202199
long_tp_response = handle_order_response("Take Profit (Long Position)", response)
203200

@@ -207,7 +204,6 @@ async def test_take_profit_orders(client: ReyaTradingClient):
207204
market_id=1,
208205
is_buy=True,
209206
trigger_price="1500",
210-
price="1500",
211207
)
212208
short_tp_response = handle_order_response("Take Profit (Short Position)", response)
213209

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 = "reya-python-sdk"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
description = "SDK for interacting with Reya Labs APIs"
55
authors = ["Reya Labs"]
66
readme = "README.md"

sdk/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
- reya_rpc: For making RPC calls to Reya's blockchain services
88
"""
99

10-
SDK_VERSION = "1.0.0" # Keep in sync with pyproject.toml
10+
SDK_VERSION = "1.0.1" # Keep in sync with pyproject.toml
1111

1212
from sdk.reya_rest_api import ReyaTradingClient, TradingConfig
1313
from sdk.reya_websocket import ReyaSocket

sdk/reya_rest_api/auth/signatures.py

Lines changed: 23 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
from sdk.reya_rest_api.constants.enums import ConditionalOrderStatus, OrdersGatewayOrderType
1818

1919

20+
DEFAULT_DEADLINE_MS = 5000
21+
2022
class SignatureGenerator:
2123
"""Generate signatures for Reya Trading API requests."""
2224

@@ -37,15 +39,11 @@ def __init__(self, config: TradingConfig):
3739
# Calculate public address from private key
3840
self._public_address = Account.from_key(self._private_key).address
3941

40-
def get_signature_deadline(self) -> int:
41-
"""Get signature deadline."""
42-
return 10 ** 18
43-
44-
def get_deadline(self, expires_after: Optional[int] = None) -> int:
42+
def get_default_expires_after(self, expires_after: Optional[int] = None) -> int:
4543
"""
4644
Returns expires_after if given, otherwise now (ms) + 5s.
4745
"""
48-
return expires_after if expires_after is not None else int(time.time() * 1000) + 5000
46+
return expires_after if expires_after is not None else int(time.time() * 1000) + DEFAULT_DEADLINE_MS
4947

5048
def scale(self, decimals: int):
5149
"""Returns a function that scales a number (str, int, float, or Decimal) to an integer."""
@@ -54,43 +52,34 @@ def _scale(value):
5452
return int(Decimal(value) * factor)
5553
return _scale
5654

57-
def encode_inputs(
55+
def encode_inputs_limit_order(
5856
self,
59-
order_type: OrdersGatewayOrderType,
60-
is_buy: Optional[bool] = None,
61-
trigger_price: Optional[Decimal] = None,
62-
order_base: Optional[Decimal] = None,
63-
order_price_limit: Optional[Decimal] = None
57+
is_buy: bool,
58+
limit_price: Decimal,
59+
order_base: Decimal,
6460
) -> str:
65-
"""
66-
Encode order inputs for signature based on the order type.
67-
68-
- LIMIT_ORDER / MARKET_ORDER / REDUCE_ONLY_MARKET_ORDER: ['int256', 'uint256'] → order_base (size), trigger_price (price)
69-
- STOP_LOSS / TAKE_PROFIT: ['bool', 'uint256', 'uint256'] → is_buy, trigger_price, order_price_limit
70-
"""
71-
7261
scaler = self.scale(18)
7362

74-
# STOP_LOSS / TAKE_PROFIT Orders
75-
if order_type in (OrdersGatewayOrderType.STOP_LOSS, OrdersGatewayOrderType.TAKE_PROFIT):
76-
if is_buy is None or trigger_price is None or order_price_limit is None:
77-
raise ValueError("STOP_LOSS / TAKE_PROFIT require is_buy, trigger_price, and order_price_limit")
78-
encoded = encode(
79-
['bool', 'uint256', 'uint256'],
80-
[bool(is_buy), scaler(trigger_price), scaler(order_price_limit)]
81-
)
82-
return encoded.hex() if encoded.hex().startswith("0x") else f"0x{encoded.hex()}"
83-
84-
# LIMIT_ORDER / MARKET_ORDER / REDUCE_ONLY_MARKET_ORDER
85-
if order_base is None or trigger_price is None or is_buy is None:
86-
raise ValueError("LIMIT_ORDER / MARKET_ORDER / REDUCE_ONLY_MARKET_ORDER require is_buy, order_base and trigger_price")
87-
8863
# Negate order_base if it's a sell order
8964
signed_order_base = order_base if is_buy else -order_base
9065

9166
encoded = encode(
9267
['int256', 'uint256'],
93-
[scaler(signed_order_base), scaler(trigger_price)]
68+
[scaler(signed_order_base), scaler(limit_price)]
69+
)
70+
return encoded.hex() if encoded.hex().startswith("0x") else f"0x{encoded.hex()}"
71+
72+
def encode_inputs_trigger_order(
73+
self,
74+
is_buy: bool,
75+
trigger_price: Decimal,
76+
limit_price: Decimal
77+
) -> str:
78+
scaler = self.scale(18)
79+
80+
encoded = encode(
81+
['bool', 'uint256', 'uint256'],
82+
[bool(is_buy), scaler(trigger_price), scaler(limit_price)]
9483
)
9584
return encoded.hex() if encoded.hex().startswith("0x") else f"0x{encoded.hex()}"
9685

@@ -196,54 +185,6 @@ def sign_raw_order(
196185

197186
return signed_message.signature.hex() if signed_message.signature.hex().startswith("0x") else f"0x{signed_message.signature.hex()}"
198187

199-
def sign_order(
200-
self,
201-
market_id: int,
202-
order_type: OrdersGatewayOrderType,
203-
nonce: int,
204-
deadline: int,
205-
is_buy: Optional[bool] = None,
206-
price: Optional[Decimal] = None,
207-
size: Optional[Decimal] = None,
208-
order_price_limit: Optional[Decimal] = None,
209-
) -> str:
210-
"""
211-
Sign an order (market, reduce-only market, limit, take profit, stop loss) using the Orders Gateway signature format.
212-
213-
Args:
214-
market_id: The market ID for this order.
215-
order_type: The type of the order (market, limit, stop loss, take profit, etc.).
216-
nonce: Random nonce for signature.
217-
is_buy: Whether this is a buy order.
218-
price: Price for the order (used for market and limit orders).
219-
size: Order size (positive for buy, negative for sell).
220-
order_price_limit: Limit price for conditional orders.
221-
222-
Returns:
223-
Hex-encoded signature.
224-
"""
225-
226-
# Encode inputs based on order type
227-
inputs = self.encode_inputs(
228-
order_type=order_type,
229-
is_buy=is_buy,
230-
trigger_price=price,
231-
order_base=size,
232-
order_price_limit=order_price_limit,
233-
)
234-
235-
# Generate signature
236-
return self.sign_raw_order(
237-
account_id=self.config.account_id,
238-
market_id=market_id,
239-
exchange_id=self.config.dex_id,
240-
counterparty_account_ids=[self.config.pool_account_id],
241-
order_type=order_type,
242-
inputs=inputs,
243-
deadline=deadline,
244-
nonce=nonce,
245-
)
246-
247188
def sign_cancel_order(self, order_id: str) -> str:
248189
"""
249190
Sign an order cancellation message using personal_sign.

sdk/reya_rest_api/client.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ async def create_take_profit_order(
177177
market_id: int,
178178
is_buy: bool,
179179
trigger_price: str,
180-
price: str,
181180
) -> CreateOrderResponse:
182181
"""
183182
Create a take profit order asynchronously.
@@ -192,12 +191,11 @@ async def create_take_profit_order(
192191
API response for the order creation
193192
"""
194193

195-
self.validate_inputs(trigger_price=trigger_price, price=price)
194+
self.validate_inputs(trigger_price=trigger_price)
196195
response = await self.orders.create_trigger_order(
197196
market_id=market_id,
198197
is_buy=is_buy,
199198
trigger_price=Decimal(trigger_price),
200-
price=Decimal(price),
201199
trigger_type=TpslType.TP
202200
)
203201

@@ -208,7 +206,6 @@ async def create_stop_loss_order(
208206
market_id: int,
209207
is_buy: bool,
210208
trigger_price: str,
211-
price: str,
212209
) -> CreateOrderResponse:
213210
"""
214211
Create a stop loss order asynchronously.
@@ -223,12 +220,11 @@ async def create_stop_loss_order(
223220
API response for the order creation
224221
"""
225222

226-
self.validate_inputs(trigger_price=trigger_price, price=price)
223+
self.validate_inputs(trigger_price=trigger_price)
227224
response = await self.orders.create_trigger_order(
228225
market_id=market_id,
229226
is_buy=is_buy,
230227
trigger_price=Decimal(trigger_price),
231-
price=Decimal(price),
232228
trigger_type=TpslType.SL
233229
)
234230

0 commit comments

Comments
 (0)