1717from sdk .reya_rest_api .constants .enums import ConditionalOrderStatus , OrdersGatewayOrderType
1818
1919
20+ DEFAULT_DEADLINE_MS = 5000
21+
2022class 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.
0 commit comments