|
| 1 | +# Copyright 2021 Injective Labs |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Injective Chain Tx/Query client for Python. Example only.""" |
| 15 | + |
| 16 | +import asyncio |
| 17 | +import logging |
| 18 | + |
| 19 | +from pyinjective.composer import Composer as ProtoMsgComposer |
| 20 | +from pyinjective.client import Client |
| 21 | +from pyinjective.transaction import Transaction |
| 22 | +from pyinjective.constant import Network |
| 23 | +from pyinjective.wallet import PrivateKey, PublicKey, Address |
| 24 | + |
| 25 | + |
| 26 | +async def main() -> None: |
| 27 | + # select network: local, testnet, mainnet |
| 28 | + network = Network.testnet() |
| 29 | + composer = ProtoMsgComposer(network=network.string()) |
| 30 | + |
| 31 | + # initialize grpc client |
| 32 | + client = Client(network, insecure=True) |
| 33 | + |
| 34 | + # load account |
| 35 | + priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e") |
| 36 | + pub_key = priv_key.to_public_key() |
| 37 | + address = pub_key.to_address().init_num_seq(network.lcd_endpoint) |
| 38 | + subaccount_id = address.get_subaccount_id(index=0) |
| 39 | + |
| 40 | + # prepare trade info |
| 41 | + fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r" |
| 42 | + |
| 43 | + derivative_market_id_create = "0x7cc8b10d7deb61e744ef83bdec2bbcf4a056867e89b062c6a453020ca82bd4e4" |
| 44 | + spot_market_id_create = "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0" |
| 45 | + |
| 46 | + derivative_market_id_cancel = "0x1f73e21972972c69c03fb105a5864592ac2b47996ffea3c500d1ea2d20138717" |
| 47 | + derivative_market_id_cancel_2 = "0x8158e603fb80c4e417696b0e98765b4ca89dcf886d3b9b2b90dc15bfb1aebd51" |
| 48 | + spot_market_id_cancel = "0x74b17b0d6855feba39f1f7ab1e8bad0363bd510ee1dcc74e40c2adfe1502f781" |
| 49 | + spot_market_id_cancel_2 = "0x01edfab47f124748dc89998eb33144af734484ba07099014594321729a0ca16b" |
| 50 | + |
| 51 | + spot_market_ids_to_cancel_all =['0x28f3c9897e23750bf653889224f93390c467b83c86d736af79431958fff833d1', '0xe8bf0467208c24209c1cf0fd64833fa43eb6e8035869f9d043dbff815ab76d01'] |
| 52 | + derivative_market_ids_to_cancel_all = ['0x4ca0f92fc28be0c9761326016b5a1a2177dd6375558365116b5bdda9abc229ce', '0x979731deaaf17d26b2e256ad18fecd0ac742b3746b9ea5382bac9bd0b5e58f74'] |
| 53 | + |
| 54 | + derivative_orders_to_cancel = [ |
| 55 | + composer.OrderData( |
| 56 | + market_id=derivative_market_id_cancel, |
| 57 | + subaccount_id=subaccount_id, |
| 58 | + order_hash="0xd6edebb7ea4ce617c2ab30b42c8793260d4d28e1966403b5aca986d7c0349be1" |
| 59 | + ), |
| 60 | + composer.OrderData( |
| 61 | + market_id=derivative_market_id_cancel_2, |
| 62 | + subaccount_id=subaccount_id, |
| 63 | + order_hash="0x7ee76255d7ca763c56b0eab9828fca89fdd3739645501c8a80f58b62b4f76da5" |
| 64 | + ) |
| 65 | + ] |
| 66 | + |
| 67 | + spot_orders_to_cancel = [ |
| 68 | + composer.OrderData( |
| 69 | + market_id=spot_market_id_cancel, |
| 70 | + subaccount_id=subaccount_id, |
| 71 | + order_hash="0x3870fbdd91f07d54425147b1bb96404f4f043ba6335b422a6d494d285b387f2d" |
| 72 | + ), |
| 73 | + composer.OrderData( |
| 74 | + market_id=spot_market_id_cancel_2, |
| 75 | + subaccount_id=subaccount_id, |
| 76 | + order_hash="0x222daa22f60fe9f075ed0ca583459e121c23e64431c3fbffdedda04598ede0d2" |
| 77 | + ) |
| 78 | + ] |
| 79 | + |
| 80 | + derivative_orders_to_create = [ |
| 81 | + composer.DerivativeOrder( |
| 82 | + market_id=derivative_market_id_create, |
| 83 | + subaccount_id=subaccount_id, |
| 84 | + fee_recipient=fee_recipient, |
| 85 | + price=3, |
| 86 | + quantity=55, |
| 87 | + leverage=1, |
| 88 | + is_buy=True |
| 89 | + ), |
| 90 | + composer.DerivativeOrder( |
| 91 | + market_id=derivative_market_id_create, |
| 92 | + subaccount_id=subaccount_id, |
| 93 | + fee_recipient=fee_recipient, |
| 94 | + price=300, |
| 95 | + quantity=55, |
| 96 | + leverage=1, |
| 97 | + is_buy=False, |
| 98 | + ), |
| 99 | + ] |
| 100 | + |
| 101 | + spot_orders_to_create = [ |
| 102 | + composer.SpotOrder( |
| 103 | + market_id=spot_market_id_create, |
| 104 | + subaccount_id=subaccount_id, |
| 105 | + fee_recipient=fee_recipient, |
| 106 | + price=3, |
| 107 | + quantity=55, |
| 108 | + is_buy=True |
| 109 | + ), |
| 110 | + composer.SpotOrder( |
| 111 | + market_id=spot_market_id_create, |
| 112 | + subaccount_id=subaccount_id, |
| 113 | + fee_recipient=fee_recipient, |
| 114 | + price=300, |
| 115 | + quantity=55, |
| 116 | + is_buy=False |
| 117 | + ), |
| 118 | + ] |
| 119 | + |
| 120 | + # prepare tx msg |
| 121 | + msg = composer.MsgBatchUpdateOrders( |
| 122 | + sender=address.to_acc_bech32(), |
| 123 | + subaccount_id=subaccount_id, |
| 124 | + derivative_orders_to_create=derivative_orders_to_create, |
| 125 | + spot_orders_to_create=spot_orders_to_create, |
| 126 | + derivative_orders_to_cancel=derivative_orders_to_cancel, |
| 127 | + spot_orders_to_cancel=spot_orders_to_cancel, |
| 128 | + spot_market_ids_to_cancel_all=spot_market_ids_to_cancel_all, |
| 129 | + derivative_market_ids_to_cancel_all=derivative_market_ids_to_cancel_all, |
| 130 | + ) |
| 131 | + |
| 132 | + # build sim tx |
| 133 | + tx = ( |
| 134 | + Transaction() |
| 135 | + .with_messages(msg) |
| 136 | + .with_sequence(address.get_sequence()) |
| 137 | + .with_account_num(address.get_number()) |
| 138 | + .with_chain_id(network.chain_id) |
| 139 | + ) |
| 140 | + sim_sign_doc = tx.get_sign_doc(pub_key) |
| 141 | + sim_sig = priv_key.sign(sim_sign_doc.SerializeToString()) |
| 142 | + sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key) |
| 143 | + |
| 144 | + # simulate tx |
| 145 | + (sim_res, success) = client.simulate_tx(sim_tx_raw_bytes) |
| 146 | + if not success: |
| 147 | + print(sim_res) |
| 148 | + return |
| 149 | + |
| 150 | + sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True) |
| 151 | + print("simulation msg response") |
| 152 | + print(sim_res_msg) |
| 153 | + |
| 154 | + # build tx |
| 155 | + gas_price = 500000000 |
| 156 | + gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation |
| 157 | + fee = [composer.Coin( |
| 158 | + amount=gas_price * gas_limit, |
| 159 | + denom=network.fee_denom, |
| 160 | + )] |
| 161 | + current_height = client.get_latest_block().block.header.height |
| 162 | + tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(current_height+50) |
| 163 | + sign_doc = tx.get_sign_doc(pub_key) |
| 164 | + sig = priv_key.sign(sign_doc.SerializeToString()) |
| 165 | + tx_raw_bytes = tx.get_tx_data(sig, pub_key) |
| 166 | + |
| 167 | + # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode |
| 168 | + res = client.send_tx_block_mode(tx_raw_bytes) |
| 169 | + res_msg = ProtoMsgComposer.MsgResponses(res.data) |
| 170 | + print("tx response") |
| 171 | + print(res) |
| 172 | + print("tx msg response") |
| 173 | + print(res_msg) |
| 174 | + |
| 175 | +if __name__ == "__main__": |
| 176 | + logging.basicConfig(level=logging.INFO) |
| 177 | + asyncio.get_event_loop().run_until_complete(main()) |
0 commit comments