Skip to content

Commit 880ba4b

Browse files
authored
Merge pull request #226 from InjectiveLabs/feat/message_broadcaster_without_simulation
Feat/message broadcaster without simulation
2 parents 2b87432 + f1633ed commit 880ba4b

File tree

9 files changed

+1312
-29
lines changed

9 files changed

+1312
-29
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,17 @@ make tests
8787
```
8888

8989
### Changelogs
90+
**0.7.2**
91+
* Added a new gas limit calculation for the TransactionBroadcaster that estimates the value based on the messages in the transaction (without running the transaction simulation).
92+
9093
**0.7.1.2**
9194
* Add NBLA
9295

9396
**0.7.1.1**
9497
* Fixed Testnet network URLs
9598

9699
**0.7.1**
97-
* Include implementation of the MessageBroadcaster, to simplify the transaction creation and broadcasting process.
100+
* Include implementation of the TransactionBroadcaster, to simplify the transaction creation and broadcasting process.
98101

99102
**0.7.0.6**
100103
* ADD SEI/USDT in metadata
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import asyncio
2+
3+
from pyinjective.composer import Composer as ProtoMsgComposer
4+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
5+
from pyinjective.constant import Network
6+
from pyinjective.wallet import PrivateKey
7+
8+
9+
async def main() -> None:
10+
# select network: local, testnet, mainnet
11+
network = Network.testnet()
12+
composer = ProtoMsgComposer(network=network.string())
13+
private_key_in_hexa = "f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3"
14+
15+
message_broadcaster = MsgBroadcasterWithPk.new_without_simulation(
16+
network=network,
17+
private_key=private_key_in_hexa,
18+
use_secure_connection=True
19+
)
20+
21+
priv_key = PrivateKey.from_hex(private_key_in_hexa)
22+
pub_key = priv_key.to_public_key()
23+
address = pub_key.to_address()
24+
subaccount_id = address.get_subaccount_id(index=0)
25+
26+
# prepare trade info
27+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
28+
29+
spot_market_id_create = "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
30+
31+
spot_orders_to_create = [
32+
composer.SpotOrder(
33+
market_id=spot_market_id_create,
34+
subaccount_id=subaccount_id,
35+
fee_recipient=fee_recipient,
36+
price=3,
37+
quantity=55,
38+
is_buy=True,
39+
is_po=False
40+
),
41+
composer.SpotOrder(
42+
market_id=spot_market_id_create,
43+
subaccount_id=subaccount_id,
44+
fee_recipient=fee_recipient,
45+
price=300,
46+
quantity=55,
47+
is_buy=False,
48+
is_po=False
49+
),
50+
]
51+
52+
# prepare tx msg
53+
msg = composer.MsgBatchUpdateOrders(
54+
sender=address.to_acc_bech32(),
55+
spot_orders_to_create=spot_orders_to_create,
56+
)
57+
58+
# broadcast the transaction
59+
result = await message_broadcaster.broadcast([msg])
60+
print("---Transaction Response---")
61+
print(result)
62+
63+
if __name__ == "__main__":
64+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import asyncio
2+
3+
from pyinjective.composer import Composer as ProtoMsgComposer
4+
from pyinjective.async_client import AsyncClient
5+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
6+
from pyinjective.constant import Network
7+
from pyinjective.wallet import PrivateKey, Address
8+
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 = AsyncClient(network, insecure=False)
17+
await client.sync_timeout_height()
18+
19+
# load account
20+
private_key_in_hexa = "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e"
21+
priv_key = PrivateKey.from_hex(private_key_in_hexa)
22+
pub_key = priv_key.to_public_key()
23+
address = pub_key.to_address()
24+
25+
message_broadcaster = MsgBroadcasterWithPk.new_for_grantee_account_without_simulation(
26+
network=network,
27+
grantee_private_key=private_key_in_hexa,
28+
use_secure_connection=True
29+
)
30+
31+
# prepare tx msg
32+
market_id = "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
33+
granter_inj_address = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
34+
granter_address = Address.from_acc_bech32(granter_inj_address)
35+
granter_subaccount_id = granter_address.get_subaccount_id(index=0)
36+
37+
msg = composer.MsgCreateSpotLimitOrder(
38+
sender=granter_inj_address,
39+
market_id=market_id,
40+
subaccount_id=granter_subaccount_id,
41+
fee_recipient=address.to_acc_bech32(),
42+
price=7.523,
43+
quantity=0.01,
44+
is_buy=True,
45+
is_po=False
46+
)
47+
48+
# broadcast the transaction
49+
result = await message_broadcaster.broadcast([msg])
50+
print("---Transaction Response---")
51+
print(result)
52+
53+
if __name__ == "__main__":
54+
asyncio.get_event_loop().run_until_complete(main())

pyinjective/async_client.py

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ async def get_account(self, address: str) -> Optional[account_pb2.EthAccount]:
302302
try:
303303
metadata = await self.load_cookie(type="chain")
304304
account_any = (await self.stubAuth.Account(
305-
auth_query.QueryAccountRequest.__call__(address=address), metadata=metadata
305+
auth_query.QueryAccountRequest(address=address), metadata=metadata
306306
)).account
307307
account = account_pb2.EthAccount()
308308
if account_any.Is(account.DESCRIPTOR):
@@ -339,7 +339,7 @@ async def simulate_tx(
339339
try:
340340
req = tx_service.SimulateRequest(tx_bytes=tx_byte)
341341
metadata = await self.load_cookie(type="chain")
342-
return await self.stubTx.Simulate.__call__(req, metadata=metadata), True
342+
return await self.stubTx.Simulate(request=req, metadata=metadata), True
343343
except grpc.RpcError as err:
344344
return err, False
345345

@@ -348,23 +348,23 @@ async def send_tx_sync_mode(self, tx_byte: bytes) -> abci_type.TxResponse:
348348
tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_SYNC
349349
)
350350
metadata = await self.load_cookie(type="chain")
351-
result = await self.stubTx.BroadcastTx.__call__(req, metadata=metadata)
351+
result = await self.stubTx.BroadcastTx(request=req, metadata=metadata)
352352
return result.tx_response
353353

354354
async def send_tx_async_mode(self, tx_byte: bytes) -> abci_type.TxResponse:
355355
req = tx_service.BroadcastTxRequest(
356356
tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_ASYNC
357357
)
358358
metadata = await self.load_cookie(type="chain")
359-
result = await self.stubTx.BroadcastTx.__call__(req, metadata=metadata)
359+
result = await self.stubTx.BroadcastTx(request=req, metadata=metadata)
360360
return result.tx_response
361361

362362
async def send_tx_block_mode(self, tx_byte: bytes) -> abci_type.TxResponse:
363363
req = tx_service.BroadcastTxRequest(
364364
tx_bytes=tx_byte, mode=tx_service.BroadcastMode.BROADCAST_MODE_BLOCK
365365
)
366366
metadata = await self.load_cookie(type="chain")
367-
result = await self.stubTx.BroadcastTx.__call__(req, metadata=metadata)
367+
result = await self.stubTx.BroadcastTx(request=req, metadata=metadata)
368368
return result.tx_response
369369

370370
async def get_chain_id(self) -> str:
@@ -632,7 +632,7 @@ async def stream_spot_markets(self, **kwargs):
632632
market_ids=kwargs.get("market_ids")
633633
)
634634
metadata = await self.load_cookie(type="exchange")
635-
return self.stubSpotExchange.StreamMarkets.__call__(req, metadata=metadata)
635+
return self.stubSpotExchange.StreamMarkets(request=req, metadata=metadata)
636636

637637
async def get_spot_orderbookV2(self, market_id: str):
638638
req = spot_exchange_rpc_pb.OrderbookV2Request(market_id=market_id)
@@ -689,12 +689,12 @@ async def get_spot_trades(self, **kwargs):
689689
async def stream_spot_orderbook_snapshot(self, market_ids: List[str]):
690690
req = spot_exchange_rpc_pb.StreamOrderbookV2Request(market_ids=market_ids)
691691
metadata = await self.load_cookie(type="exchange")
692-
return self.stubSpotExchange.StreamOrderbookV2.__call__(req, metadata=metadata)
692+
return self.stubSpotExchange.StreamOrderbookV2(request=req, metadata=metadata)
693693

694694
async def stream_spot_orderbook_update(self, market_ids: List[str]):
695695
req = spot_exchange_rpc_pb.StreamOrderbookUpdateRequest(market_ids=market_ids)
696696
metadata = await self.load_cookie(type="exchange")
697-
return self.stubSpotExchange.StreamOrderbookUpdate.__call__(req, metadata=metadata)
697+
return self.stubSpotExchange.StreamOrderbookUpdate(request=req, metadata=metadata)
698698

699699
async def stream_spot_orders(self, market_id: str, **kwargs):
700700
req = spot_exchange_rpc_pb.StreamOrdersRequest(
@@ -703,7 +703,7 @@ async def stream_spot_orders(self, market_id: str, **kwargs):
703703
subaccount_id=kwargs.get("subaccount_id"),
704704
)
705705
metadata = await self.load_cookie(type="exchange")
706-
return self.stubSpotExchange.StreamOrders.__call__(req, metadata=metadata)
706+
return self.stubSpotExchange.StreamOrders(request=req, metadata=metadata)
707707

708708
async def stream_historical_spot_orders(self, market_id: str, **kwargs):
709709
req = spot_exchange_rpc_pb.StreamOrdersHistoryRequest(
@@ -715,7 +715,7 @@ async def stream_historical_spot_orders(self, market_id: str, **kwargs):
715715
execution_types=kwargs.get("execution_types")
716716
)
717717
metadata = await self.load_cookie(type="exchange")
718-
return self.stubSpotExchange.StreamOrdersHistory.__call__(req, metadata=metadata)
718+
return self.stubSpotExchange.StreamOrdersHistory(request=req, metadata=metadata)
719719

720720
async def stream_historical_derivative_orders(self, market_id: str, **kwargs):
721721
req = derivative_exchange_rpc_pb.StreamOrdersHistoryRequest(
@@ -727,7 +727,7 @@ async def stream_historical_derivative_orders(self, market_id: str, **kwargs):
727727
execution_types=kwargs.get("execution_types")
728728
)
729729
metadata = await self.load_cookie(type="exchange")
730-
return self.stubDerivativeExchange.StreamOrdersHistory.__call__(req, metadata=metadata)
730+
return self.stubDerivativeExchange.StreamOrdersHistory(request=req, metadata=metadata)
731731

732732
async def stream_spot_trades(self, **kwargs):
733733
req = spot_exchange_rpc_pb.StreamTradesRequest(
@@ -740,7 +740,7 @@ async def stream_spot_trades(self, **kwargs):
740740
execution_types=kwargs.get("execution_types"),
741741
)
742742
metadata = await self.load_cookie(type="exchange")
743-
return self.stubSpotExchange.StreamTrades.__call__(req, metadata=metadata)
743+
return self.stubSpotExchange.StreamTrades(request=req, metadata=metadata)
744744

745745
async def get_spot_subaccount_orders(self, subaccount_id: str, **kwargs):
746746
req = spot_exchange_rpc_pb.SubaccountOrdersListRequest(
@@ -780,7 +780,7 @@ async def stream_derivative_markets(self, **kwargs):
780780
market_ids=kwargs.get("market_ids")
781781
)
782782
metadata = await self.load_cookie(type="exchange")
783-
return self.stubDerivativeExchange.StreamMarket.__call__(req, metadata=metadata)
783+
return self.stubDerivativeExchange.StreamMarket(request=req, metadata=metadata)
784784

785785
async def get_derivative_orderbook(self, market_id: str):
786786
req = derivative_exchange_rpc_pb.OrderbookV2Request(market_id=market_id)
@@ -846,16 +846,12 @@ async def get_derivative_trades(self, **kwargs):
846846
async def stream_derivative_orderbook_snapshot(self, market_ids: List[str]):
847847
req = derivative_exchange_rpc_pb.StreamOrderbookV2Request(market_ids=market_ids)
848848
metadata = await self.load_cookie(type="exchange")
849-
return self.stubDerivativeExchange.StreamOrderbookV2.__call__(
850-
req, metadata=metadata
851-
)
849+
return self.stubDerivativeExchange.StreamOrderbookV2(request=req, metadata=metadata)
852850

853851
async def stream_derivative_orderbook_update(self, market_ids: List[str]):
854852
req = derivative_exchange_rpc_pb.StreamOrderbookUpdateRequest(market_ids=market_ids)
855853
metadata = await self.load_cookie(type="exchange")
856-
return self.stubDerivativeExchange.StreamOrderbookUpdate.__call__(
857-
req, metadata=metadata
858-
)
854+
return self.stubDerivativeExchange.StreamOrderbookUpdate(request=req, metadata=metadata)
859855

860856
async def stream_derivative_orders(self, market_id: str, **kwargs):
861857
req = derivative_exchange_rpc_pb.StreamOrdersRequest(
@@ -864,7 +860,7 @@ async def stream_derivative_orders(self, market_id: str, **kwargs):
864860
subaccount_id=kwargs.get("subaccount_id"),
865861
)
866862
metadata = await self.load_cookie(type="exchange")
867-
return self.stubDerivativeExchange.StreamOrders.__call__(req, metadata=metadata)
863+
return self.stubDerivativeExchange.StreamOrders(request=req, metadata=metadata)
868864

869865
async def stream_derivative_trades(self, **kwargs):
870866
req = derivative_exchange_rpc_pb.StreamTradesRequest(
@@ -879,7 +875,7 @@ async def stream_derivative_trades(self, **kwargs):
879875
execution_types=kwargs.get("execution_types"),
880876
)
881877
metadata = await self.load_cookie(type="exchange")
882-
return self.stubDerivativeExchange.StreamTrades.__call__(req, metadata=metadata)
878+
return self.stubDerivativeExchange.StreamTrades(request=req, metadata=metadata)
883879

884880
async def get_derivative_positions(self, **kwargs):
885881
req = derivative_exchange_rpc_pb.PositionsRequest(
@@ -901,9 +897,7 @@ async def stream_derivative_positions(self, **kwargs):
901897
subaccount_ids=kwargs.get("subaccount_ids"),
902898
)
903899
metadata = await self.load_cookie(type="exchange")
904-
return self.stubDerivativeExchange.StreamPositions.__call__(
905-
req, metadata=metadata
906-
)
900+
return self.stubDerivativeExchange.StreamPositions(request=req, metadata=metadata)
907901

908902
async def get_derivative_liquidable_positions(self, **kwargs):
909903
req = derivative_exchange_rpc_pb.LiquidablePositionsRequest(
@@ -979,4 +973,4 @@ async def stream_account_portfolio(self, account_address: str, **kwargs):
979973
type=kwargs.get("type")
980974
)
981975
metadata = await self.load_cookie(type="exchange")
982-
return self.stubPortfolio.StreamAccountPortfolio.__call__(req, metadata=metadata)
976+
return self.stubPortfolio.StreamAccountPortfolio(request=req, metadata=metadata)

0 commit comments

Comments
 (0)