|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +import os |
| 4 | + |
| 5 | +import dotenv |
| 6 | + |
| 7 | +from pyinjective.async_client import AsyncClient |
| 8 | +from pyinjective.core.broadcaster import MsgBroadcasterWithPk |
| 9 | +from pyinjective.core.network import Network |
| 10 | +from pyinjective.wallet import PrivateKey |
| 11 | + |
| 12 | + |
| 13 | +async def main() -> None: |
| 14 | + dotenv.load_dotenv() |
| 15 | + configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY") |
| 16 | + |
| 17 | + # select network: local, testnet, mainnet |
| 18 | + network = Network.testnet() |
| 19 | + |
| 20 | + # initialize grpc client |
| 21 | + client = AsyncClient(network) |
| 22 | + composer = await client.composer() |
| 23 | + |
| 24 | + gas_price = await client.current_chain_gas_price() |
| 25 | + # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted |
| 26 | + gas_price = int(gas_price * 1.1) |
| 27 | + |
| 28 | + message_broadcaster = MsgBroadcasterWithPk.new_using_gas_heuristics( |
| 29 | + network=network, |
| 30 | + private_key=configured_private_key, |
| 31 | + gas_price=gas_price, |
| 32 | + client=client, |
| 33 | + composer=composer, |
| 34 | + ) |
| 35 | + |
| 36 | + # load account |
| 37 | + priv_key = PrivateKey.from_hex(configured_private_key) |
| 38 | + pub_key = priv_key.to_public_key() |
| 39 | + address = pub_key.to_address() |
| 40 | + await client.fetch_account(address.to_acc_bech32()) |
| 41 | + |
| 42 | + usdt_denom = "factory/inj10vkkttgxdeqcgeppu20x9qtyvuaxxev8qh0awq/usdt" |
| 43 | + usdt_erc20 = "0xdAC17F958D2ee523a2206206994597C13D831ec7" |
| 44 | + |
| 45 | + # prepare tx msg |
| 46 | + msg = composer.msg_create_token_pair( |
| 47 | + sender=address.to_acc_bech32(), |
| 48 | + bank_denom=usdt_denom, |
| 49 | + erc20_address=usdt_erc20, |
| 50 | + ) |
| 51 | + |
| 52 | + # broadcast the transaction |
| 53 | + result = await message_broadcaster.broadcast([msg]) |
| 54 | + print("---Transaction Response---") |
| 55 | + print(json.dumps(result, indent=2)) |
| 56 | + |
| 57 | + gas_price = await client.current_chain_gas_price() |
| 58 | + # adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted |
| 59 | + gas_price = int(gas_price * 1.1) |
| 60 | + message_broadcaster.update_gas_price(gas_price=gas_price) |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + asyncio.get_event_loop().run_until_complete(main()) |
0 commit comments