Skip to content

Commit 20cd52b

Browse files
author
Qiu-INJ
committed
Add utils for decimal, functions related to sending and cancelling orders.
1 parent 1754629 commit 20cd52b

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

src/injective/chain_client/_transaction.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,84 @@ def add_exchange_msg_deposit(
7474
}
7575
self._msgs.append(msg)
7676

77+
def add_exchange_msg_cancel_spot_order(self, subaccount: str, market_id: int, order_hash: str) -> None:
78+
msg = {
79+
"type": "exchange/MsgCancelSpotOrder",
80+
"value": {
81+
"sender": privkey_to_address(self._privkey, hrp=self._hrp),
82+
"subaccount_id": subaccount,
83+
"market_id": market_id,
84+
"order_hash": order_hash,
85+
},
86+
}
87+
self._msgs.append(msg)
88+
89+
def add_exchange_msg_batch_cancel_spot_order(self, subaccount_id_list, market_id_list, order_hash_list) -> None:
90+
msg = {
91+
#INFO: ATTENTION, Order`s`
92+
"type": "exchange/MsgBatchCancelSpotOrders",
93+
"value": {
94+
"sender": privkey_to_address(self._privkey, hrp=self._hrp),
95+
"data": []
96+
},
97+
}
98+
99+
for i in range(len(subaccount_id_list)):
100+
msg["value"]["data"].append(
101+
{
102+
"subaccount_id": subaccount_id_list[i],
103+
"market_id": market_id_list[i],
104+
"order_hash": order_hash_list[i],
105+
}
106+
)
107+
108+
self._msgs.append(msg)
109+
110+
def add_exchange_msg_create_spot_limit_order(self, subaccount_id: str, market_id: str, fee_recipient: str, price, quantity, order_type, trigger_price) -> None:
111+
msg = {
112+
"type": "exchange/MsgCreateSpotLimitOrder",
113+
"value": {
114+
"sender": privkey_to_address(self._privkey, hrp=self._hrp),
115+
"order": {
116+
'market_id': market_id,
117+
'order_info': {
118+
'subaccount_id': subaccount_id,
119+
'fee_recipient': fee_recipient,
120+
'price': price,
121+
'quantity': quantity,
122+
},
123+
'order_type': order_type,
124+
"trigger_price": trigger_price,
125+
},
126+
}
127+
}
128+
self._msgs.append(msg)
129+
130+
def add_exchange_msg_batch_create_spot_limit_orders(self, subaccount_id_list, market_id_list, fee_recipient_list, price_list, quantity_list, order_type_list, trigger_price_list) -> None:
131+
msg = {
132+
"type": "exchange/MsgBatchCreateSpotLimitOrders",
133+
"value": {
134+
"sender": privkey_to_address(self._privkey, hrp=self._hrp),
135+
"orders": []
136+
}
137+
}
138+
139+
for i in range(len(subaccount_id_list)):
140+
msg["value"]["orders"].append({
141+
'market_id': market_id_list[i],
142+
'order_info': {
143+
'subaccount_id': subaccount_id_list[i],
144+
'fee_recipient': fee_recipient_list[i],
145+
'price': price_list[i],
146+
'quantity': quantity_list[i],
147+
},
148+
'order_type': order_type_list[i],
149+
"trigger_price": trigger_price_list[i],
150+
}
151+
)
152+
self._msgs.append(msg)
153+
154+
77155
def get_signed(self) -> str:
78156
pubkey = privkey_to_pubkey(self._privkey)
79157
base64_pubkey = base64.b64encode(pubkey).decode("utf-8")

src/injective/constant.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# from etherscan.io
2+
DECIMALS_DICT = {
3+
"INJ": 18, # uint8
4+
"USDT": 6, # uint256
5+
"BNB": 18, # uint8
6+
"LINK": 18, # uint8
7+
"UNI": 18, # uint8
8+
"AAVE": 18, # uint8 from https://etherscan.io/token/0x7fc66500c84a76ad7e9c93437bfc5ac33e2ddae9#readProxyContract
9+
"MATIC": 18, # uint8
10+
"ZRX": 18, # uint8
11+
"USDC": 6, # uint8 from https://etherscan.io/token/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48#readProxyContract
12+
}
13+
14+
15+
ORDERTYPE_DICT = {"UNSPECIFIED": 0, "BUY": 1, "SELL": 2,
16+
"STOP_BUY": 3, "STOP_SELL": 4, "TAKE_BUY": 5, "TAKE_SELL": 6}

src/injective/utils.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from decimal import Decimal
2+
3+
4+
def price_float_to_string(price, base_decimals, quote_decimals, precision=18) -> str:
5+
scale = Decimal(quote_decimals - base_decimals)
6+
exchange_price = Decimal(price) * pow(10, scale)
7+
price_string = ("{:."+str(precision)+"f}").format(exchange_price)
8+
print("price string :{}".format(price_string))
9+
return price_string
10+
11+
12+
def quantity_float_to_string(quantity, base_decimals, precision=18) -> str:
13+
scale = Decimal(base_decimals)
14+
exchange_quantity = Decimal(quantity) * pow(10, scale)
15+
quantity_string = ("{:."+str(precision)+"f}").format(exchange_quantity)
16+
print("quantity string:{}".format(quantity_string))
17+
return quantity_string
18+
19+
20+
def price_string_to_float(price_string, base_decimals, quote_decimals) -> float:
21+
scale = float(base_decimals - quote_decimals)
22+
return float(price_string) * pow(10, scale)
23+
24+
25+
def quantity_string_to_float(quantity_string, base_decimals) -> float:
26+
scale = float(0 - base_decimals)
27+
return float(quantity_string) * pow(10, scale)
28+
29+
30+
# test
31+
if __name__ == "__main__":
32+
assert "0.000000005000000000" == price_float_to_string(
33+
5000, 18, 6, 18), "result of get_price is wrong."
34+
35+
assert "1222000000000000000000.000000000000000000" == quantity_float_to_string(
36+
1222, 18, 18), "result of get_quantity is wrong."
37+
38+
assert 5000 == price_string_to_float(price_float_to_string(
39+
5000, 18, 6, 18), 18, 6), "something is wrong."
40+
41+
assert 1222 == quantity_string_to_float(
42+
quantity_float_to_string(1222, 18, 18), 18), "something is wrong."

0 commit comments

Comments
 (0)