-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/add exchange messages from v1.13 #341
Changes from 6 commits
734b7e9
5d9374e
cb4e6f4
857c11f
e5095d5
dedef04
1bffc7c
10f28fd
8c70ac7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||
import asyncio | ||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||
from decimal import Decimal | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
import dotenv | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
from pyinjective.async_client import AsyncClient | ||||||||||||||||||||||||||
from pyinjective.core.broadcaster import MsgBroadcasterWithPk | ||||||||||||||||||||||||||
from pyinjective.core.network import Network | ||||||||||||||||||||||||||
from pyinjective.wallet import PrivateKey | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
async def main() -> None: | ||||||||||||||||||||||||||
dotenv.load_dotenv() | ||||||||||||||||||||||||||
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY") | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# select network: local, testnet, mainnet | ||||||||||||||||||||||||||
network = Network.testnet() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# initialize grpc client | ||||||||||||||||||||||||||
client = AsyncClient(network) | ||||||||||||||||||||||||||
composer = await client.composer() | ||||||||||||||||||||||||||
await client.sync_timeout_height() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation( | ||||||||||||||||||||||||||
network=network, | ||||||||||||||||||||||||||
private_key=configured_private_key, | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
Comment on lines
+25
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors in transaction broadcasting. Add error handling for the transaction broadcasting process to ensure robustness. try:
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
network=network,
private_key=configured_private_key,
)
except Exception as e:
print(f"Error initializing message broadcaster: {e}")
return Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# load account | ||||||||||||||||||||||||||
priv_key = PrivateKey.from_hex(configured_private_key) | ||||||||||||||||||||||||||
pub_key = priv_key.to_public_key() | ||||||||||||||||||||||||||
address = pub_key.to_address() | ||||||||||||||||||||||||||
await client.fetch_account(address.to_acc_bech32()) | ||||||||||||||||||||||||||
subaccount_id = address.get_subaccount_id(index=0) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# prepare trade info | ||||||||||||||||||||||||||
market_id = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6" | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# prepare tx msg | ||||||||||||||||||||||||||
msg = composer.msg_decrease_position_margin( | ||||||||||||||||||||||||||
sender=address.to_acc_bech32(), | ||||||||||||||||||||||||||
market_id=market_id, | ||||||||||||||||||||||||||
source_subaccount_id=subaccount_id, | ||||||||||||||||||||||||||
destination_subaccount_id=subaccount_id, | ||||||||||||||||||||||||||
amount=Decimal(2), | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# broadcast the transaction | ||||||||||||||||||||||||||
result = await message_broadcaster.broadcast([msg]) | ||||||||||||||||||||||||||
print("---Transaction Response---") | ||||||||||||||||||||||||||
print(result) | ||||||||||||||||||||||||||
Comment on lines
+50
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors in transaction response. Add error handling for the transaction response to ensure robustness. try:
result = await message_broadcaster.broadcast([msg])
print("---Transaction Response---")
print(result)
except Exception as e:
print(f"Error broadcasting transaction: {e}") Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||||||
asyncio.get_event_loop().run_until_complete(main()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import os | ||
from decimal import Decimal | ||
|
||
import dotenv | ||
|
||
from pyinjective.async_client import AsyncClient | ||
from pyinjective.core.broadcaster import MsgBroadcasterWithPk | ||
from pyinjective.core.network import Network | ||
from pyinjective.wallet import PrivateKey | ||
|
||
|
||
async def main() -> None: | ||
dotenv.load_dotenv() | ||
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY") | ||
|
||
# select network: local, testnet, mainnet | ||
network = Network.testnet() | ||
|
||
# initialize grpc client | ||
client = AsyncClient(network) | ||
await client.initialize_tokens_from_chain_denoms() | ||
composer = await client.composer() | ||
await client.sync_timeout_height() | ||
|
||
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation( | ||
network=network, | ||
private_key=configured_private_key, | ||
) | ||
|
||
# load account | ||
priv_key = PrivateKey.from_hex(configured_private_key) | ||
pub_key = priv_key.to_public_key() | ||
address = pub_key.to_address() | ||
await client.fetch_account(address.to_acc_bech32()) | ||
|
||
# prepare tx msg | ||
message = composer.msg_update_spot_market( | ||
admin=address.to_acc_bech32(), | ||
market_id="0x215970bfdea5c94d8e964a759d3ce6eae1d113900129cc8428267db5ccdb3d1a", | ||
new_ticker="INJ/USDC 2", | ||
new_min_price_tick_size=Decimal("0.01"), | ||
new_min_quantity_tick_size=Decimal("0.01"), | ||
new_min_notional=Decimal("2"), | ||
) | ||
|
||
# broadcast the transaction | ||
result = await message_broadcaster.broadcast([message]) | ||
print("---Transaction Response---") | ||
print(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.get_event_loop().run_until_complete(main()) |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||
import asyncio | ||||||||||||||||||||||||||
import os | ||||||||||||||||||||||||||
from decimal import Decimal | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
import dotenv | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
from pyinjective.async_client import AsyncClient | ||||||||||||||||||||||||||
from pyinjective.core.broadcaster import MsgBroadcasterWithPk | ||||||||||||||||||||||||||
from pyinjective.core.network import Network | ||||||||||||||||||||||||||
from pyinjective.wallet import PrivateKey | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
async def main() -> None: | ||||||||||||||||||||||||||
dotenv.load_dotenv() | ||||||||||||||||||||||||||
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY") | ||||||||||||||||||||||||||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle missing environment variable. Add error handling for the case when dotenv.load_dotenv()
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
if configured_private_key is None:
raise EnvironmentError("INJECTIVE_PRIVATE_KEY is not set in the environment.") Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# select network: local, testnet, mainnet | ||||||||||||||||||||||||||
network = Network.testnet() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# initialize grpc client | ||||||||||||||||||||||||||
client = AsyncClient(network) | ||||||||||||||||||||||||||
await client.initialize_tokens_from_chain_denoms() | ||||||||||||||||||||||||||
composer = await client.composer() | ||||||||||||||||||||||||||
await client.sync_timeout_height() | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation( | ||||||||||||||||||||||||||
network=network, | ||||||||||||||||||||||||||
private_key=configured_private_key, | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
Comment on lines
+26
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors in transaction broadcasting. Add error handling for the transaction broadcasting process to ensure robustness. try:
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
network=network,
private_key=configured_private_key,
)
except Exception as e:
print(f"Error initializing message broadcaster: {e}")
return Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# load account | ||||||||||||||||||||||||||
priv_key = PrivateKey.from_hex(configured_private_key) | ||||||||||||||||||||||||||
pub_key = priv_key.to_public_key() | ||||||||||||||||||||||||||
address = pub_key.to_address() | ||||||||||||||||||||||||||
await client.fetch_account(address.to_acc_bech32()) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# prepare tx msg | ||||||||||||||||||||||||||
message = composer.msg_update_derivative_market( | ||||||||||||||||||||||||||
admin=address.to_acc_bech32(), | ||||||||||||||||||||||||||
market_id="0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6", | ||||||||||||||||||||||||||
new_ticker="INJ/USDT PERP 2", | ||||||||||||||||||||||||||
new_min_price_tick_size=Decimal("1"), | ||||||||||||||||||||||||||
new_min_quantity_tick_size=Decimal("1"), | ||||||||||||||||||||||||||
new_min_notional=Decimal("2"), | ||||||||||||||||||||||||||
new_initial_margin_ratio=Decimal("0.40"), | ||||||||||||||||||||||||||
new_maintenance_margin_ratio=Decimal("0.085"), | ||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
# broadcast the transaction | ||||||||||||||||||||||||||
result = await message_broadcaster.broadcast([message]) | ||||||||||||||||||||||||||
print("---Transaction Response---") | ||||||||||||||||||||||||||
print(result) | ||||||||||||||||||||||||||
Comment on lines
+50
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle potential errors in transaction response. Add error handling for the transaction response to ensure robustness. try:
result = await message_broadcaster.broadcast([message])
print("---Transaction Response---")
print(result)
except Exception as e:
print(f"Error broadcasting transaction: {e}") Committable suggestion
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if __name__ == "__main__": | ||||||||||||||||||||||||||
asyncio.get_event_loop().run_until_complete(main()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import asyncio | ||
import os | ||
from decimal import Decimal | ||
|
||
import dotenv | ||
|
||
from pyinjective.async_client import AsyncClient | ||
from pyinjective.core.broadcaster import MsgBroadcasterWithPk | ||
from pyinjective.core.network import Network | ||
from pyinjective.wallet import PrivateKey | ||
|
||
|
||
async def main() -> None: | ||
dotenv.load_dotenv() | ||
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY") | ||
|
||
# select network: local, testnet, mainnet | ||
network = Network.testnet() | ||
|
||
# initialize grpc client | ||
client = AsyncClient(network) | ||
await client.initialize_tokens_from_chain_denoms() | ||
composer = await client.composer() | ||
await client.sync_timeout_height() | ||
|
||
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation( | ||
network=network, | ||
private_key=configured_private_key, | ||
) | ||
|
||
# load account | ||
priv_key = PrivateKey.from_hex(configured_private_key) | ||
pub_key = priv_key.to_public_key() | ||
address = pub_key.to_address() | ||
await client.fetch_account(address.to_acc_bech32()) | ||
|
||
# prepare tx msg | ||
grant_authorization = composer.create_grant_authorization( | ||
grantee="inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r", | ||
amount=Decimal("1"), | ||
) | ||
message = composer.msg_authorize_stake_grants(sender=address.to_acc_bech32(), grants=[grant_authorization]) | ||
|
||
# broadcast the transaction | ||
result = await message_broadcaster.broadcast([message]) | ||
print("---Transaction Response---") | ||
print(result) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.get_event_loop().run_until_complete(main()) |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,48 @@ | ||||||||||
import asyncio | ||||||||||
import os | ||||||||||
|
||||||||||
import dotenv | ||||||||||
|
||||||||||
from pyinjective.async_client import AsyncClient | ||||||||||
from pyinjective.core.broadcaster import MsgBroadcasterWithPk | ||||||||||
from pyinjective.core.network import Network | ||||||||||
from pyinjective.wallet import PrivateKey | ||||||||||
|
||||||||||
|
||||||||||
async def main() -> None: | ||||||||||
dotenv.load_dotenv() | ||||||||||
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY") | ||||||||||
|
||||||||||
# select network: local, testnet, mainnet | ||||||||||
network = Network.testnet() | ||||||||||
|
||||||||||
# initialize grpc client | ||||||||||
client = AsyncClient(network) | ||||||||||
await client.initialize_tokens_from_chain_denoms() | ||||||||||
composer = await client.composer() | ||||||||||
await client.sync_timeout_height() | ||||||||||
|
||||||||||
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation( | ||||||||||
network=network, | ||||||||||
private_key=configured_private_key, | ||||||||||
) | ||||||||||
|
||||||||||
# load account | ||||||||||
priv_key = PrivateKey.from_hex(configured_private_key) | ||||||||||
pub_key = priv_key.to_public_key() | ||||||||||
address = pub_key.to_address() | ||||||||||
await client.fetch_account(address.to_acc_bech32()) | ||||||||||
|
||||||||||
# prepare tx msg | ||||||||||
message = composer.msg_activate_stake_grant( | ||||||||||
sender=address.to_acc_bech32(), granter="inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r" | ||||||||||
) | ||||||||||
|
||||||||||
# broadcast the transaction | ||||||||||
result = await message_broadcaster.broadcast([message]) | ||||||||||
print("---Transaction Response---") | ||||||||||
print(result) | ||||||||||
|
||||||||||
|
||||||||||
if __name__ == "__main__": | ||||||||||
asyncio.get_event_loop().run_until_complete(main()) | ||||||||||
Comment on lines
+47
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Using - asyncio.get_event_loop().run_until_complete(main())
+ asyncio.run(main()) Committable suggestion
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Handle missing environment variable.
Add error handling for the case when
INJECTIVE_PRIVATE_KEY
is not set.Committable suggestion