Skip to content

Commit

Permalink
(feat) Added support for the new messages in exchange module, include…
Browse files Browse the repository at this point in the history
…d in the chain upgrade v1.13. Added unit tests and example scripts
  • Loading branch information
aarmoa committed Aug 6, 2024
1 parent 5d9374e commit cb4e6f4
Show file tree
Hide file tree
Showing 13 changed files with 734 additions and 43 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

All notable changes to this project will be documented in this file.

## [1.6.1] - 2024-08-07
### Added
- Added support for the following messages in the chain "exchange" module:
- MsgDecreasePositionMargin
- MsgUpdateSpotMarket
- MsgUpdateDerivativeMarket
- MsgAuthorizeStakeGrants
- MsgActivateStakeGrant

## [1.6.0] - 2024-07-30
### Added
- Support for all queries in the chain "tendermint" module
Expand Down
56 changes: 56 additions & 0 deletions examples/chain_client/exchange/23_MsgDecreasePositionMargin.py
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())
54 changes: 54 additions & 0 deletions examples/chain_client/exchange/24_MsgUpdateSpotMarket.py
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 examples/chain_client/exchange/25_MsgUpdateDerivativeMarket.py
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 examples/chain_client/exchange/26_MsgAuthorizeStakeGrants.py
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 examples/chain_client/exchange/27_MsgActivateStakeGrant.py
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())
Loading

0 comments on commit cb4e6f4

Please sign in to comment.