Skip to content

Commit 6219ca4

Browse files
Merge pull request #45 from InjectiveLabs/auctions
add MsgBid
2 parents a270d47 + ea64265 commit 6219ca4

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
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())

pyinjective/composer.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,21 @@ def MsgWithdraw(
344344
amount=self.Coin(amount=be_amount, denom=peggy_denom)
345345
)
346346

347+
def MsgBid(
348+
self,
349+
sender: str,
350+
bid_amount: float,
351+
round: float
352+
):
353+
354+
be_amount = amount_to_backend(bid_amount, 18)
355+
356+
return injective_auction_tx_pb.MsgBid(
357+
sender=sender,
358+
round=round,
359+
bid_amount=self.Coin(amount=be_amount, denom="inj")
360+
)
361+
347362
# data field format: [request-msg-header][raw-byte-msg-response]
348363
# you need to figure out this magic prefix number to trim request-msg-header off the data
349364
# this method handles only exchange responses
@@ -365,7 +380,8 @@ def MsgResponses(data, simulation=False):
365380
"/injective.exchange.v1beta1.MsgWithdraw": injective_exchange_tx_pb.MsgWithdrawResponse,
366381
"/injective.exchange.v1beta1.MsgSubaccountTransfer": injective_exchange_tx_pb.MsgSubaccountTransferResponse,
367382
"/injective.exchange.v1beta1.MsgLiquidatePosition": injective_exchange_tx_pb.MsgLiquidatePosition,
368-
"/injective.exchange.v1beta1.MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMargin
383+
"/injective.exchange.v1beta1.MsgIncreasePositionMargin": injective_exchange_tx_pb.MsgIncreasePositionMargin,
384+
"/injective.auction.v1beta1.MsgBid": injective_auction_tx_pb.MsgBidResponse,
369385
}
370386

371387
response = tx_response_pb.TxResponseData.FromString(data)

0 commit comments

Comments
 (0)