|
| 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 | +import asyncio |
| 16 | +import logging |
| 17 | + |
| 18 | +from pyinjective.composer import Composer as ProtoMsgComposer |
| 19 | +from pyinjective.async_client import AsyncClient |
| 20 | +from pyinjective.transaction import Transaction |
| 21 | +from pyinjective.constant import Network |
| 22 | +from pyinjective.wallet import PrivateKey |
| 23 | + |
| 24 | + |
| 25 | +async def main() -> None: |
| 26 | + # select network: local, testnet, mainnet |
| 27 | + network = Network.testnet() |
| 28 | + composer = ProtoMsgComposer(network=network.string()) |
| 29 | + |
| 30 | + # initialize grpc client |
| 31 | + client = AsyncClient(network, insecure=False) |
| 32 | + await client.sync_timeout_height() |
| 33 | + |
| 34 | + # load account |
| 35 | + priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3") |
| 36 | + pub_key = priv_key.to_public_key() |
| 37 | + address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint) |
| 38 | + subaccount_id = address.get_subaccount_id(index=0) |
| 39 | + dest_subaccount_id = "0xaf79152ac5df276d9a8e1e2e22822f9713474902000000000000000000000000" |
| 40 | + |
| 41 | + # prepare tx msg |
| 42 | + msg = composer.MsgExternalTransfer( |
| 43 | + sender=address.to_acc_bech32(), |
| 44 | + source_subaccount_id=subaccount_id, |
| 45 | + destination_subaccount_id=dest_subaccount_id, |
| 46 | + amount=100, |
| 47 | + denom="inj" |
| 48 | + ) |
| 49 | + |
| 50 | + # build sim tx |
| 51 | + tx = ( |
| 52 | + Transaction() |
| 53 | + .with_messages(msg) |
| 54 | + .with_sequence(address.get_sequence()) |
| 55 | + .with_account_num(address.get_number()) |
| 56 | + .with_chain_id(network.chain_id) |
| 57 | + ) |
| 58 | + sim_sign_doc = tx.get_sign_doc(pub_key) |
| 59 | + sim_sig = priv_key.sign(sim_sign_doc.SerializeToString()) |
| 60 | + sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key) |
| 61 | + |
| 62 | + # simulate tx |
| 63 | + (sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes) |
| 64 | + if not success: |
| 65 | + print(sim_res) |
| 66 | + return |
| 67 | + |
| 68 | + # build tx |
| 69 | + gas_price = 500000000 |
| 70 | + gas_limit = sim_res.gas_info.gas_used + 20000 # add 20k for gas, fee computation |
| 71 | + gas_fee = '{:.18f}'.format((gas_price * gas_limit) / pow(10, 18)).rstrip('0') |
| 72 | + fee = [composer.Coin( |
| 73 | + amount=gas_price * gas_limit, |
| 74 | + denom=network.fee_denom, |
| 75 | + )] |
| 76 | + tx = tx.with_gas(gas_limit).with_fee(fee).with_memo('').with_timeout_height(client.timeout_height) |
| 77 | + sign_doc = tx.get_sign_doc(pub_key) |
| 78 | + sig = priv_key.sign(sign_doc.SerializeToString()) |
| 79 | + tx_raw_bytes = tx.get_tx_data(sig, pub_key) |
| 80 | + |
| 81 | + # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode |
| 82 | + res = await client.send_tx_sync_mode(tx_raw_bytes) |
| 83 | + print(res) |
| 84 | + print("gas wanted: {}".format(gas_limit)) |
| 85 | + print("gas fee: {} INJ".format(gas_fee)) |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + logging.basicConfig(level=logging.INFO) |
| 89 | + asyncio.get_event_loop().run_until_complete(main()) |
0 commit comments