Skip to content

Commit ea64265

Browse files
committed
add MsgBid example
1 parent cc6384e commit ea64265

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
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 = Client(network, insecure=True)
32+
33+
# load account
34+
priv_key = PrivateKey.from_hex("5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e")
35+
pub_key = priv_key.to_public_key()
36+
address = pub_key.to_address().init_num_seq(network.lcd_endpoint)
37+
38+
# prepare tx msg
39+
msg = composer.MsgBid(
40+
sender=address.to_acc_bech32(),
41+
round=17,
42+
bid_amount=1000
43+
)
44+
45+
# build sim tx
46+
tx = (
47+
Transaction()
48+
.with_messages(msg)
49+
.with_sequence(address.get_sequence())
50+
.with_account_num(address.get_number())
51+
.with_chain_id(network.chain_id)
52+
)
53+
sim_sign_doc = tx.get_sign_doc(pub_key)
54+
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
55+
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
56+
57+
# simulate tx
58+
(sim_res, success) = client.simulate_tx(sim_tx_raw_bytes)
59+
if not success:
60+
print(sim_res)
61+
return
62+
63+
# build tx
64+
gas_price = 500000000
65+
gas_limit = sim_res.gas_info.gas_used + 15000 # add 15k for gas, fee computation
66+
fee = [composer.Coin(
67+
amount=gas_price * gas_limit,
68+
denom=network.fee_denom,
69+
)]
70+
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(0)
71+
sign_doc = tx.get_sign_doc(pub_key)
72+
sig = priv_key.sign(sign_doc.SerializeToString())
73+
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
74+
75+
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
76+
res = client.send_tx_block_mode(tx_raw_bytes)
77+
78+
# print tx response
79+
print(res)
80+
81+
if __name__ == "__main__":
82+
logging.basicConfig(level=logging.INFO)
83+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)