|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | + |
| 4 | +from pyinjective.composer import Composer as ProtoMsgComposer |
| 5 | +from pyinjective.client import Client |
| 6 | +from pyinjective.transaction import Transaction |
| 7 | +from pyinjective.constant import Network |
| 8 | +from pyinjective.wallet import PrivateKey, PublicKey, Address |
| 9 | + |
| 10 | +async def main() -> None: |
| 11 | + # select network: local, testnet, mainnet |
| 12 | + network = Network.testnet() |
| 13 | + composer = ProtoMsgComposer(network=network.string()) |
| 14 | + |
| 15 | + # initialize grpc client |
| 16 | + client = Client(network.grpc_endpoint, insecure=True) |
| 17 | + |
| 18 | + # load account |
| 19 | + priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3") |
| 20 | + pub_key = priv_key.to_public_key() |
| 21 | + address = pub_key.to_address() |
| 22 | + subaccount_id = address.get_subaccount_id(index=0) |
| 23 | + dest_subaccount_id = address.get_subaccount_id(index=1) |
| 24 | + |
| 25 | + # prepare tx msg |
| 26 | + msg = composer.MsgSubaccountTransfer( |
| 27 | + sender=address.to_acc_bech32(), |
| 28 | + source_subaccount_id=subaccount_id, |
| 29 | + destination_subaccount_id=dest_subaccount_id, |
| 30 | + amount=1000000000000000000, |
| 31 | + denom="inj" |
| 32 | + ) |
| 33 | + |
| 34 | + acc_num, acc_seq = await address.get_num_seq(network.lcd_endpoint) |
| 35 | + gas_price = 500000000 |
| 36 | + gas_limit = 200000 |
| 37 | + fee = [composer.Coin( |
| 38 | + amount=str(gas_price * gas_limit), |
| 39 | + denom=network.fee_denom, |
| 40 | + )] |
| 41 | + |
| 42 | + # build tx |
| 43 | + tx = ( |
| 44 | + Transaction() |
| 45 | + .with_messages(msg) |
| 46 | + .with_sequence(acc_seq) |
| 47 | + .with_account_num(acc_num) |
| 48 | + .with_chain_id(network.chain_id) |
| 49 | + .with_gas(gas_limit) |
| 50 | + .with_fee(fee) |
| 51 | + .with_memo("") |
| 52 | + .with_timeout_height(0) |
| 53 | + ) |
| 54 | + |
| 55 | + # build signed tx |
| 56 | + sign_doc = tx.get_sign_doc(pub_key) |
| 57 | + sig = priv_key.sign(sign_doc.SerializeToString()) |
| 58 | + tx_raw_bytes = tx.get_tx_data(sig, pub_key) |
| 59 | + |
| 60 | + # broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode |
| 61 | + res = client.send_tx_block_mode(tx_raw_bytes) |
| 62 | + |
| 63 | + # print tx response |
| 64 | + print(res) |
| 65 | + |
| 66 | +if __name__ == "__main__": |
| 67 | + logging.basicConfig(level=logging.INFO) |
| 68 | + asyncio.get_event_loop().run_until_complete(main()) |
0 commit comments