Skip to content

Commit cb4e6f4

Browse files
committed
(feat) Added support for the new messages in exchange module, included in the chain upgrade v1.13. Added unit tests and example scripts
1 parent 5d9374e commit cb4e6f4

File tree

13 files changed

+734
-43
lines changed

13 files changed

+734
-43
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

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

5+
## [1.6.1] - 2024-08-07
6+
### Added
7+
- Added support for the following messages in the chain "exchange" module:
8+
- MsgDecreasePositionMargin
9+
- MsgUpdateSpotMarket
10+
- MsgUpdateDerivativeMarket
11+
- MsgAuthorizeStakeGrants
12+
- MsgActivateStakeGrant
13+
514
## [1.6.0] - 2024-07-30
615
### Added
716
- Support for all queries in the chain "tendermint" module
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import asyncio
2+
import os
3+
from decimal import Decimal
4+
5+
import dotenv
6+
7+
from pyinjective.async_client import AsyncClient
8+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
9+
from pyinjective.core.network import Network
10+
from pyinjective.wallet import PrivateKey
11+
12+
13+
async def main() -> None:
14+
dotenv.load_dotenv()
15+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
16+
17+
# select network: local, testnet, mainnet
18+
network = Network.testnet()
19+
20+
# initialize grpc client
21+
client = AsyncClient(network)
22+
composer = await client.composer()
23+
await client.sync_timeout_height()
24+
25+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
26+
network=network,
27+
private_key=configured_private_key,
28+
)
29+
30+
# load account
31+
priv_key = PrivateKey.from_hex(configured_private_key)
32+
pub_key = priv_key.to_public_key()
33+
address = pub_key.to_address()
34+
await client.fetch_account(address.to_acc_bech32())
35+
subaccount_id = address.get_subaccount_id(index=0)
36+
37+
# prepare trade info
38+
market_id = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
39+
40+
# prepare tx msg
41+
msg = composer.msg_decrease_position_margin(
42+
sender=address.to_acc_bech32(),
43+
market_id=market_id,
44+
source_subaccount_id=subaccount_id,
45+
destination_subaccount_id=subaccount_id,
46+
amount=Decimal(2),
47+
)
48+
49+
# broadcast the transaction
50+
result = await message_broadcaster.broadcast([msg])
51+
print("---Transaction Response---")
52+
print(result)
53+
54+
55+
if __name__ == "__main__":
56+
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+
import os
3+
from decimal import Decimal
4+
5+
import dotenv
6+
7+
from pyinjective.async_client import AsyncClient
8+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
9+
from pyinjective.core.network import Network
10+
from pyinjective.wallet import PrivateKey
11+
12+
13+
async def main() -> None:
14+
dotenv.load_dotenv()
15+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
16+
17+
# select network: local, testnet, mainnet
18+
network = Network.testnet()
19+
20+
# initialize grpc client
21+
client = AsyncClient(network)
22+
await client.initialize_tokens_from_chain_denoms()
23+
composer = await client.composer()
24+
await client.sync_timeout_height()
25+
26+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
27+
network=network,
28+
private_key=configured_private_key,
29+
)
30+
31+
# load account
32+
priv_key = PrivateKey.from_hex(configured_private_key)
33+
pub_key = priv_key.to_public_key()
34+
address = pub_key.to_address()
35+
await client.fetch_account(address.to_acc_bech32())
36+
37+
# prepare tx msg
38+
message = composer.msg_update_spot_market(
39+
admin=address.to_acc_bech32(),
40+
market_id="0x215970bfdea5c94d8e964a759d3ce6eae1d113900129cc8428267db5ccdb3d1a",
41+
new_ticker="INJ/USDC 2",
42+
new_min_price_tick_size=Decimal("0.01"),
43+
new_min_quantity_tick_size=Decimal("0.01"),
44+
new_min_notional=Decimal("2"),
45+
)
46+
47+
# broadcast the transaction
48+
result = await message_broadcaster.broadcast([message])
49+
print("---Transaction Response---")
50+
print(result)
51+
52+
53+
if __name__ == "__main__":
54+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import asyncio
2+
import os
3+
from decimal import Decimal
4+
5+
import dotenv
6+
7+
from pyinjective.async_client import AsyncClient
8+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
9+
from pyinjective.core.network import Network
10+
from pyinjective.wallet import PrivateKey
11+
12+
13+
async def main() -> None:
14+
dotenv.load_dotenv()
15+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
16+
17+
# select network: local, testnet, mainnet
18+
network = Network.testnet()
19+
20+
# initialize grpc client
21+
client = AsyncClient(network)
22+
await client.initialize_tokens_from_chain_denoms()
23+
composer = await client.composer()
24+
await client.sync_timeout_height()
25+
26+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
27+
network=network,
28+
private_key=configured_private_key,
29+
)
30+
31+
# load account
32+
priv_key = PrivateKey.from_hex(configured_private_key)
33+
pub_key = priv_key.to_public_key()
34+
address = pub_key.to_address()
35+
await client.fetch_account(address.to_acc_bech32())
36+
37+
# prepare tx msg
38+
message = composer.msg_update_derivative_market(
39+
admin=address.to_acc_bech32(),
40+
market_id="0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6",
41+
new_ticker="INJ/USDT PERP 2",
42+
new_min_price_tick_size=Decimal("1"),
43+
new_min_quantity_tick_size=Decimal("1"),
44+
new_min_notional=Decimal("2"),
45+
new_initial_margin_ratio=Decimal("0.40"),
46+
new_maintenance_margin_ratio=Decimal("0.085"),
47+
)
48+
49+
# broadcast the transaction
50+
result = await message_broadcaster.broadcast([message])
51+
print("---Transaction Response---")
52+
print(result)
53+
54+
55+
if __name__ == "__main__":
56+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import asyncio
2+
import os
3+
from decimal import Decimal
4+
5+
import dotenv
6+
7+
from pyinjective.async_client import AsyncClient
8+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
9+
from pyinjective.core.network import Network
10+
from pyinjective.wallet import PrivateKey
11+
12+
13+
async def main() -> None:
14+
dotenv.load_dotenv()
15+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
16+
17+
# select network: local, testnet, mainnet
18+
network = Network.testnet()
19+
20+
# initialize grpc client
21+
client = AsyncClient(network)
22+
await client.initialize_tokens_from_chain_denoms()
23+
composer = await client.composer()
24+
await client.sync_timeout_height()
25+
26+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
27+
network=network,
28+
private_key=configured_private_key,
29+
)
30+
31+
# load account
32+
priv_key = PrivateKey.from_hex(configured_private_key)
33+
pub_key = priv_key.to_public_key()
34+
address = pub_key.to_address()
35+
await client.fetch_account(address.to_acc_bech32())
36+
37+
# prepare tx msg
38+
grant_authorization = composer.create_grant_authorization(
39+
grantee="inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r",
40+
amount=Decimal("1"),
41+
)
42+
message = composer.msg_authorize_stake_grants(sender=address.to_acc_bech32(), grants=[grant_authorization])
43+
44+
# broadcast the transaction
45+
result = await message_broadcaster.broadcast([message])
46+
print("---Transaction Response---")
47+
print(result)
48+
49+
50+
if __name__ == "__main__":
51+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import asyncio
2+
import os
3+
4+
import dotenv
5+
6+
from pyinjective.async_client import AsyncClient
7+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
8+
from pyinjective.core.network import Network
9+
from pyinjective.wallet import PrivateKey
10+
11+
12+
async def main() -> None:
13+
dotenv.load_dotenv()
14+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
15+
16+
# select network: local, testnet, mainnet
17+
network = Network.testnet()
18+
19+
# initialize grpc client
20+
client = AsyncClient(network)
21+
await client.initialize_tokens_from_chain_denoms()
22+
composer = await client.composer()
23+
await client.sync_timeout_height()
24+
25+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
26+
network=network,
27+
private_key=configured_private_key,
28+
)
29+
30+
# load account
31+
priv_key = PrivateKey.from_hex(configured_private_key)
32+
pub_key = priv_key.to_public_key()
33+
address = pub_key.to_address()
34+
await client.fetch_account(address.to_acc_bech32())
35+
36+
# prepare tx msg
37+
message = composer.msg_activate_stake_grant(
38+
sender=address.to_acc_bech32(), granter="inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
39+
)
40+
41+
# broadcast the transaction
42+
result = await message_broadcaster.broadcast([message])
43+
print("---Transaction Response---")
44+
print(result)
45+
46+
47+
if __name__ == "__main__":
48+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
 (0)