|
| 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()) |
0 commit comments