Skip to content

Commit 6c47732

Browse files
authored
Merge pull request #296 from InjectiveLabs/fix/update_creation_msg_liquidate_position
fix/update_creation_msg_liquidate_position
2 parents 1ef9948 + 1606c4e commit 6c47732

File tree

4 files changed

+114
-3
lines changed

4 files changed

+114
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## [1.1.1] - 2024-01-18
6+
### Changed
7+
- Updated the logic to create a `MsgLiquidatePosition` message
8+
59
## [1.1.0] - 2024-01-15
610
### Added
711
- Added new functions in all Market classes to convert values from extended chain format (the ones provided by chain streams) into human-readable format
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import asyncio
2+
import uuid
3+
4+
from grpc import RpcError
5+
6+
from pyinjective.async_client import AsyncClient
7+
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
8+
from pyinjective.core.network import Network
9+
from pyinjective.transaction import Transaction
10+
from pyinjective.wallet import PrivateKey
11+
12+
13+
async def main() -> None:
14+
# select network: local, testnet, mainnet
15+
network = Network.testnet()
16+
17+
# initialize grpc client
18+
client = AsyncClient(network)
19+
composer = await client.composer()
20+
await client.sync_timeout_height()
21+
22+
# load account
23+
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
24+
pub_key = priv_key.to_public_key()
25+
address = pub_key.to_address()
26+
await client.fetch_account(address.to_acc_bech32())
27+
subaccount_id = address.get_subaccount_id(index=0)
28+
29+
# prepare trade info
30+
market_id = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
31+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
32+
cid = str(uuid.uuid4())
33+
34+
order = composer.DerivativeOrder(
35+
market_id=market_id,
36+
subaccount_id=subaccount_id,
37+
fee_recipient=fee_recipient,
38+
price=39.01, # This should be the liquidation price
39+
quantity=0.147,
40+
leverage=1,
41+
cid=cid,
42+
is_buy=False,
43+
)
44+
45+
# prepare tx msg
46+
msg = composer.MsgLiquidatePosition(
47+
sender=address.to_acc_bech32(),
48+
subaccount_id="0x156df4d5bc8e7dd9191433e54bd6a11eeb390921000000000000000000000000",
49+
market_id=market_id,
50+
order=order,
51+
)
52+
53+
# build sim tx
54+
tx = (
55+
Transaction()
56+
.with_messages(msg)
57+
.with_sequence(client.get_sequence())
58+
.with_account_num(client.get_number())
59+
.with_chain_id(network.chain_id)
60+
)
61+
sim_sign_doc = tx.get_sign_doc(pub_key)
62+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
63+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
64+
65+
# simulate tx
66+
try:
67+
sim_res = await client.simulate(sim_tx_raw_bytes)
68+
except RpcError as ex:
69+
print(ex)
70+
return
71+
72+
sim_res_msg = sim_res["result"]["msgResponses"]
73+
print("---Simulation Response---")
74+
print(sim_res_msg)
75+
76+
# build tx
77+
gas_price = GAS_PRICE
78+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
79+
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
80+
fee = [
81+
composer.Coin(
82+
amount=gas_price * gas_limit,
83+
denom=network.fee_denom,
84+
)
85+
]
86+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
87+
sign_doc = tx.get_sign_doc(pub_key)
88+
sig = priv_key.sign(sign_doc.SerializeToString())
89+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
90+
91+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
92+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
93+
print("---Transaction Response---")
94+
print(res)
95+
print("gas wanted: {}".format(gas_limit))
96+
print("gas fee: {} INJ".format(gas_fee))
97+
print(f"\n\ncid: {cid}")
98+
99+
100+
if __name__ == "__main__":
101+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/composer.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,15 @@ def MsgBatchUpdateOrders(self, sender: str, **kwargs):
671671
binary_options_orders_to_create=kwargs.get("binary_options_orders_to_create"),
672672
)
673673

674-
def MsgLiquidatePosition(self, sender: str, subaccount_id: str, market_id: str):
674+
def MsgLiquidatePosition(
675+
self,
676+
sender: str,
677+
subaccount_id: str,
678+
market_id: str,
679+
order: Optional[injective_dot_exchange_dot_v1beta1_dot_exchange__pb2.DerivativeOrder] = None,
680+
):
675681
return injective_exchange_tx_pb.MsgLiquidatePosition(
676-
sender=sender, subaccount_id=subaccount_id, market_id=market_id
682+
sender=sender, subaccount_id=subaccount_id, market_id=market_id, order=order
677683
)
678684

679685
def MsgIncreasePositionMargin(

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 = "injective-py"
3-
version = "1.1.0"
3+
version = "1.1.1"
44
description = "Injective Python SDK, with Exchange API Client"
55
authors = ["Injective Labs <contact@injectivelabs.org>"]
66
license = "Apache-2.0"

0 commit comments

Comments
 (0)