-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathorderhash.py
More file actions
155 lines (125 loc) · 4.94 KB
/
orderhash.py
File metadata and controls
155 lines (125 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from decimal import Decimal
import requests
from eip712.messages import EIP712Domain, EIP712Message, EIP712Type
from eth_account.messages import _hash_eip191_message as hash_eip191_message
from eth_pydantic_types import abi
from hexbytes import HexBytes
from pyinjective.core.token import Token
class OrderInfo(EIP712Type):
SubaccountId: abi.string
FeeRecipient: abi.string
Price: abi.string
Quantity: abi.string
class SpotOrder(EIP712Message):
eip712_domain = EIP712Domain(
name="Injective Protocol",
version="2.0.0",
chainId=888,
verifyingContract="0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
salt=HexBytes("0x0000000000000000000000000000000000000000000000000000000000000000"),
)
MarketId: abi.string
OrderInfo: OrderInfo
Salt: abi.string
OrderType: abi.string
TriggerPrice: abi.string
class DerivativeOrder(EIP712Message):
eip712_domain = EIP712Domain(
name="Injective Protocol",
version="2.0.0",
chainId=888,
verifyingContract="0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
salt=HexBytes("0x0000000000000000000000000000000000000000000000000000000000000000"),
)
MarketId: abi.string
OrderInfo: OrderInfo
OrderType: abi.string
Margin: abi.string
TriggerPrice: abi.string
Salt: abi.string
# domain_separator = EIP712_domain.hash_struct()
order_type_dict = {0: "\x00", 1: "\x01", 2: "\x02", 3: "\x03", 4: "\x04", 5: "\x05", 6: "\x06", 7: "\x07", 8: "\x08"}
class OrderHashResponse:
def __init__(
self,
spot: [str] = None,
derivative: [str] = None,
):
self.spot = spot
self.derivative = derivative
class OrderHashManager:
def __init__(
self,
address,
network,
subaccount_indexes: [int] = None,
):
self.address = address
self.subacc_nonces = dict()
for i in subaccount_indexes:
subaccount_id = address.get_subaccount_id(index=i)
url = network.lcd_endpoint + "/injective/exchange/v1beta1/exchange/" + subaccount_id
res = requests.get(url=url)
nonce = res.json()["nonce"]
self.subacc_nonces[i] = [subaccount_id, nonce + 1]
def compute_order_hashes(self, spot_orders, derivative_orders, subaccount_index) -> [str]:
if len(spot_orders) + len(derivative_orders) == 0:
return []
order_hashes = OrderHashResponse(spot=[], derivative=[])
for o in spot_orders:
msg = build_eip712_msg(o, self.subacc_nonces[subaccount_index][1])
order_hash = hash_order(msg)
order_hashes.spot.append(order_hash)
self.subacc_nonces[subaccount_index][1] += 1
for o in derivative_orders:
msg = build_eip712_msg(o, self.subacc_nonces[subaccount_index][1])
order_hash = hash_order(msg)
order_hashes.derivative.append(order_hash)
self.subacc_nonces[subaccount_index][1] += 1
return order_hashes
def param_to_backend_go(param: str) -> str:
go_param = Token.convert_value_from_extended_decimal_format(value=Decimal(param))
return f"{go_param.normalize():.18f}"
def parse_order_type(order):
return order_type_dict[order.order_type]
def build_eip712_msg(order, nonce):
if order.__class__.__name__ == "SpotOrder":
go_price = param_to_backend_go(order.order_info.price)
go_trigger_price = param_to_backend_go(order.trigger_price)
go_quantity = param_to_backend_go(order.order_info.quantity)
go_order_type = parse_order_type(order)
return SpotOrder(
MarketId=order.market_id,
OrderInfo=OrderInfo(
SubaccountId=order.order_info.subaccount_id,
FeeRecipient=order.order_info.fee_recipient,
Price=go_price,
Quantity=go_quantity,
),
Salt=str(nonce),
OrderType=go_order_type,
TriggerPrice=go_trigger_price,
)
if order.__class__.__name__ == "DerivativeOrder":
go_price = param_to_backend_go(order.order_info.price)
go_trigger_price = param_to_backend_go(order.trigger_price)
go_quantity = param_to_backend_go(order.order_info.quantity)
go_margin = param_to_backend_go(order.margin)
go_order_type = parse_order_type(order)
return DerivativeOrder(
MarketId=order.market_id,
OrderInfo=OrderInfo(
SubaccountId=order.order_info.subaccount_id,
FeeRecipient=order.order_info.fee_recipient,
Price=go_price,
Quantity=go_quantity,
),
Salt=str(nonce),
OrderType=go_order_type,
TriggerPrice=go_trigger_price,
Margin=go_margin,
)
def hash_order(msg):
signable_message = msg.signable_message
hex_digest = hash_eip191_message(signable_message=signable_message).hex()
return "0x" + hex_digest