|
| 1 | +# Copyright 2022 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 | + |
| 15 | + |
| 16 | +import asyncio |
| 17 | +import logging |
| 18 | + |
| 19 | +from pyinjective.composer import Composer as ProtoMsgComposer |
| 20 | +from pyinjective.async_client import AsyncClient |
| 21 | +from pyinjective.transaction import Transaction |
| 22 | +from pyinjective.constant import Network |
| 23 | +from pyinjective.wallet import PrivateKey |
| 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 = AsyncClient(network, insecure=False) |
| 33 | + await client.sync_timeout_height() |
| 34 | + |
| 35 | + # load account |
| 36 | + priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3") |
| 37 | + pub_key = priv_key.to_public_key() |
| 38 | + address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint) |
| 39 | + |
| 40 | + provider = "ufc" |
| 41 | + symbols = ["0x7ba77b6c69c15270bd9235f11a0068f3080017116aa3c57e17c16f49ea13f57f", "0x7ba77b6c69c15270bd9235f11a0068f3080017116aa3c57e17c16f49ea13f57f"] |
| 42 | + prices = [0.5, 0.8] |
| 43 | + |
| 44 | + # prepare tx msg |
| 45 | + msg = composer.MsgRelayProviderPrices( |
| 46 | + sender=address.to_acc_bech32(), |
| 47 | + provider=provider, |
| 48 | + symbols=symbols, |
| 49 | + prices=prices |
| 50 | + ) |
| 51 | + |
| 52 | + # build sim tx |
| 53 | + tx = ( |
| 54 | + Transaction() |
| 55 | + .with_messages(msg) |
| 56 | + .with_sequence(address.get_sequence()) |
| 57 | + .with_account_num(address.get_number()) |
| 58 | + .with_chain_id(network.chain_id) |
| 59 | + ) |
| 60 | + sim_sign_doc = tx.get_sign_doc(pub_key) |
| 61 | + sim_sig = priv_key.sign(sim_sign_doc.SerializeToString()) |
| 62 | + sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key) |
| 63 | + |
| 64 | + # simulate tx |
| 65 | + (sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes) |
| 66 | + if not success: |
| 67 | + print(sim_res) |
| 68 | + return |
| 69 | + |
| 70 | + sim_res_msg = ProtoMsgComposer.MsgResponses(sim_res.result.data, simulation=True) |
| 71 | + print("---Simulation Response---") |
| 72 | + print(sim_res_msg) |
| 73 | + |
| 74 | + # build tx |
| 75 | + gas_price = 500000000 |
| 76 | + gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation |
| 77 | + gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0') |
| 78 | + fee = [composer.Coin( |
| 79 | + amount=gas_price * gas_limit, |
| 80 | + denom=network.fee_denom, |
| 81 | + )] |
| 82 | + tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height) |
| 83 | + sign_doc = tx.get_sign_doc(pub_key) |
| 84 | + sig = priv_key.sign(sign_doc.SerializeToString()) |
| 85 | + tx_raw_bytes = tx.get_tx_data(sig, pub_key) |
| 86 | + |
| 87 | + # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode |
| 88 | + res = await client.send_tx_sync_mode(tx_raw_bytes) |
| 89 | + print("---Transaction Response---") |
| 90 | + print(res) |
| 91 | + print("gas wanted: {}".format(gas_limit)) |
| 92 | + print("gas fee: {} INJ".format(gas_fee)) |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + logging.basicConfig(level=logging.INFO) |
| 96 | + asyncio.get_event_loop().run_until_complete(main()) |
0 commit comments