-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(feat) Added support for the new messages in exchange module, include…
…d in the chain upgrade v1.13. Added unit tests and example scripts
- Loading branch information
Showing
13 changed files
with
734 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
examples/chain_client/exchange/23_MsgDecreasePositionMargin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
|
||
# 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) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.get_event_loop().run_until_complete(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
56 changes: 56 additions & 0 deletions
56
examples/chain_client/exchange/25_MsgUpdateDerivativeMarket.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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_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) | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.get_event_loop().run_until_complete(main()) |
51 changes: 51 additions & 0 deletions
51
examples/chain_client/exchange/26_MsgAuthorizeStakeGrants.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
48 changes: 48 additions & 0 deletions
48
examples/chain_client/exchange/27_MsgActivateStakeGrant.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
Oops, something went wrong.