Skip to content

Commit 26cf36a

Browse files
committed
feat: upgraded Indexer proto to version v1.16.0-rc2. Updated gas estimator using heuristics include GTB orders
1 parent 0d50fc2 commit 26cf36a

File tree

50 files changed

+2151
-1647
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2151
-1647
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ clean-all:
3131
$(call clean_repos)
3232

3333
clone-injective-indexer:
34-
git clone https://github.com/InjectiveLabs/injective-indexer.git -b v1.15.6 --depth 1 --single-branch
34+
git clone https://github.com/InjectiveLabs/injective-indexer.git -b v1.16.0-rc2 --depth 1 --single-branch
3535

3636
clone-all: clone-injective-indexer
3737

buf.gen.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ inputs:
2121
tag: v0.50.13-evm-comet1-inj.2
2222
# - git_repo: https://github.com/InjectiveLabs/wasmd
2323
# branch: v0.51.x-inj
24-
# subdir: proto
25-
# - git_repo: https://github.com/InjectiveLabs/injective-core
26-
# tag: v1.15.0
2724
# subdir: proto
2825
- git_repo: https://github.com/InjectiveLabs/injective-core
29-
branch: master
26+
tag: v1.16.0-beta.2
3027
subdir: proto
28+
# - git_repo: https://github.com/InjectiveLabs/injective-core
29+
# branch: master
30+
# subdir: proto
3131
- directory: proto

examples/chain_client/1_LocalOrderHash.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ async def main() -> None:
178178
print("gas fee: {} INJ".format(gas_fee))
179179

180180
spot_orders = [
181-
composer.spot_order(
181+
composer.create_spot_order_v2(
182182
market_id=spot_market_id,
183183
subaccount_id=subaccount_id_2,
184184
fee_recipient=fee_recipient,
@@ -187,7 +187,7 @@ async def main() -> None:
187187
order_type="BUY_PO",
188188
cid=str(uuid.uuid4()),
189189
),
190-
composer.spot_order(
190+
composer.create_spot_order_v2(
191191
market_id=spot_market_id,
192192
subaccount_id=subaccount_id_2,
193193
fee_recipient=fee_recipient,
@@ -199,7 +199,7 @@ async def main() -> None:
199199
]
200200

201201
derivative_orders = [
202-
composer.derivative_order(
202+
composer.create_derivative_order_v2(
203203
market_id=deriv_market_id,
204204
subaccount_id=subaccount_id_2,
205205
fee_recipient=fee_recipient,
@@ -211,7 +211,7 @@ async def main() -> None:
211211
order_type="BUY",
212212
cid=str(uuid.uuid4()),
213213
),
214-
composer.derivative_order(
214+
composer.create_derivative_order_v2(
215215
market_id=deriv_market_id,
216216
subaccount_id=subaccount_id_2,
217217
fee_recipient=fee_recipient,

examples/chain_client/exchange/25_MsgUpdateDerivativeMarket.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ async def main() -> None:
5151
new_min_notional=Decimal("2"),
5252
new_initial_margin_ratio=Decimal("0.40"),
5353
new_maintenance_margin_ratio=Decimal("0.085"),
54+
new_reduce_margin_ratio=Decimal("3.5"),
5455
)
5556

5657
# broadcast the transaction
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import asyncio
2+
import json
3+
import os
4+
import uuid
5+
from decimal import Decimal
6+
7+
import dotenv
8+
9+
from pyinjective.async_client import AsyncClient
10+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
11+
from pyinjective.core.network import Network
12+
from pyinjective.wallet import PrivateKey
13+
14+
15+
async def main() -> None:
16+
dotenv.load_dotenv()
17+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
18+
19+
# select network: local, testnet, mainnet
20+
network = Network.testnet()
21+
22+
# initialize grpc client
23+
client = AsyncClient(network)
24+
composer = await client.composer()
25+
26+
gas_price = await client.current_chain_gas_price()
27+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
28+
gas_price = int(gas_price * 1.1)
29+
30+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
31+
network=network,
32+
private_key=configured_private_key,
33+
gas_price=gas_price,
34+
client=client,
35+
composer=composer,
36+
)
37+
38+
# load account
39+
priv_key = PrivateKey.from_hex(configured_private_key)
40+
pub_key = priv_key.to_public_key()
41+
address = pub_key.to_address()
42+
await client.fetch_account(address.to_acc_bech32())
43+
subaccount_id = address.get_subaccount_id(index=0)
44+
45+
latest_block = await client.fetch_latest_block()
46+
latest_height = int(latest_block["block"]["header"]["height"])
47+
48+
# prepare trade info
49+
market_id = "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
50+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
51+
cid = str(uuid.uuid4())
52+
53+
# prepare tx msg
54+
msg = composer.msg_create_spot_limit_order_v2(
55+
sender=address.to_acc_bech32(),
56+
market_id=market_id,
57+
subaccount_id=subaccount_id,
58+
fee_recipient=fee_recipient,
59+
price=Decimal("7.523"),
60+
quantity=Decimal("0.01"),
61+
order_type="BUY",
62+
cid=cid,
63+
expiration_block=latest_height + 10,
64+
)
65+
66+
# broadcast the transaction
67+
result = await message_broadcaster.broadcast([msg])
68+
print("---Transaction Response---")
69+
print(json.dumps(result, indent=2))
70+
71+
gas_price = await client.current_chain_gas_price()
72+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
73+
gas_price = int(gas_price * 1.1)
74+
message_broadcaster.update_gas_price(gas_price=gas_price)
75+
76+
77+
if __name__ == "__main__":
78+
asyncio.get_event_loop().run_until_complete(main())
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import asyncio
2+
import json
3+
import os
4+
import uuid
5+
from decimal import Decimal
6+
7+
import dotenv
8+
9+
from pyinjective.async_client import AsyncClient
10+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
11+
from pyinjective.core.network import Network
12+
from pyinjective.wallet import PrivateKey
13+
14+
15+
async def main() -> None:
16+
dotenv.load_dotenv()
17+
configured_private_key = os.getenv("INJECTIVE_PRIVATE_KEY")
18+
19+
# select network: local, testnet, mainnet
20+
network = Network.testnet()
21+
22+
# initialize grpc client
23+
client = AsyncClient(network)
24+
composer = await client.composer()
25+
26+
gas_price = await client.current_chain_gas_price()
27+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
28+
gas_price = int(gas_price * 1.1)
29+
30+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
31+
network=network,
32+
private_key=configured_private_key,
33+
gas_price=gas_price,
34+
client=client,
35+
composer=composer,
36+
)
37+
38+
# load account
39+
priv_key = PrivateKey.from_hex(configured_private_key)
40+
pub_key = priv_key.to_public_key()
41+
address = pub_key.to_address()
42+
await client.fetch_account(address.to_acc_bech32())
43+
subaccount_id = address.get_subaccount_id(index=0)
44+
45+
latest_block = await client.fetch_latest_block()
46+
latest_height = int(latest_block["block"]["header"]["height"])
47+
48+
# prepare trade info
49+
market_id = "0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6"
50+
fee_recipient = "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
51+
52+
# prepare tx msg
53+
msg = composer.msg_create_derivative_limit_order_v2(
54+
sender=address.to_acc_bech32(),
55+
market_id=market_id,
56+
subaccount_id=subaccount_id,
57+
fee_recipient=fee_recipient,
58+
price=Decimal(50000),
59+
quantity=Decimal(0.1),
60+
margin=composer.calculate_margin(
61+
quantity=Decimal(0.1), price=Decimal(50000), leverage=Decimal(1), is_reduce_only=False
62+
),
63+
order_type="SELL",
64+
cid=str(uuid.uuid4()),
65+
expiration_block=latest_height + 10,
66+
)
67+
68+
# broadcast the transaction
69+
result = await message_broadcaster.broadcast([msg])
70+
print("---Transaction Response---")
71+
print(json.dumps(result, indent=2))
72+
73+
gas_price = await client.current_chain_gas_price()
74+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
75+
gas_price = int(gas_price * 1.1)
76+
message_broadcaster.update_gas_price(gas_price=gas_price)
77+
78+
79+
if __name__ == "__main__":
80+
asyncio.get_event_loop().run_until_complete(main())

examples/chain_client/exchange/4_MsgInstantPerpetualMarketLaunch.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async def main() -> None:
5454
taker_fee_rate=Decimal("0.001"),
5555
initial_margin_ratio=Decimal("0.33"),
5656
maintenance_margin_ratio=Decimal("0.095"),
57+
reduce_margin_ratio=Decimal("3"),
5758
min_price_tick_size=Decimal("0.001"),
5859
min_quantity_tick_size=Decimal("0.01"),
5960
min_notional=Decimal("1"),

examples/chain_client/exchange/6_MsgCreateSpotLimitOrder.py

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import asyncio
2+
import json
23
import os
34
import uuid
45
from decimal import Decimal
56

67
import dotenv
7-
from grpc import RpcError
88

99
from pyinjective.async_client import AsyncClient
10-
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT
10+
from pyinjective.core.broadcaster import MsgBroadcasterWithPk
1111
from pyinjective.core.network import Network
12-
from pyinjective.transaction import Transaction
1312
from pyinjective.wallet import PrivateKey
1413

1514

@@ -23,7 +22,18 @@ async def main() -> None:
2322
# initialize grpc client
2423
client = AsyncClient(network)
2524
composer = await client.composer()
26-
await client.sync_timeout_height()
25+
26+
gas_price = await client.current_chain_gas_price()
27+
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
28+
gas_price = int(gas_price * 1.1)
29+
30+
message_broadcaster = MsgBroadcasterWithPk.new_using_simulation(
31+
network=network,
32+
private_key=configured_private_key,
33+
gas_price=gas_price,
34+
client=client,
35+
composer=composer,
36+
)
2737

2838
# load account
2939
priv_key = PrivateKey.from_hex(configured_private_key)
@@ -49,54 +59,15 @@ async def main() -> None:
4959
cid=cid,
5060
)
5161

52-
# build sim tx
53-
tx = (
54-
Transaction()
55-
.with_messages(msg)
56-
.with_sequence(client.get_sequence())
57-
.with_account_num(client.get_number())
58-
.with_chain_id(network.chain_id)
59-
)
60-
sim_sign_doc = tx.get_sign_doc(pub_key)
61-
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
62-
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
63-
64-
# simulate tx
65-
try:
66-
sim_res = await client.simulate(sim_tx_raw_bytes)
67-
except RpcError as ex:
68-
print(ex)
69-
return
70-
71-
sim_res_msg = sim_res["result"]["msgResponses"]
72-
print("---Simulation Response---")
73-
print(sim_res_msg)
62+
# broadcast the transaction
63+
result = await message_broadcaster.broadcast([msg])
64+
print("---Transaction Response---")
65+
print(json.dumps(result, indent=2))
7466

75-
# build tx
7667
gas_price = await client.current_chain_gas_price()
7768
# adjust gas price to make it valid even if it changes between the time it is requested and the TX is broadcasted
7869
gas_price = int(gas_price * 1.1)
79-
80-
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
81-
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
82-
fee = [
83-
composer.coin(
84-
amount=gas_price * gas_limit,
85-
denom=network.fee_denom,
86-
)
87-
]
88-
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
89-
sign_doc = tx.get_sign_doc(pub_key)
90-
sig = priv_key.sign(sign_doc.SerializeToString())
91-
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
92-
93-
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
94-
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
95-
print("---Transaction Response---")
96-
print(res)
97-
print("gas wanted: {}".format(gas_limit))
98-
print("gas fee: {} INJ".format(gas_fee))
99-
print(f"\n\ncid: {cid}")
70+
message_broadcaster.update_gas_price(gas_price=gas_price)
10071

10172

10273
if __name__ == "__main__":

examples/chain_client/tendermint/query/3_GetLatestBlock.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23

34
from pyinjective.async_client import AsyncClient
45
from pyinjective.core.network import Network
@@ -9,7 +10,7 @@ async def main() -> None:
910
client = AsyncClient(network)
1011

1112
latest_block = await client.fetch_latest_block()
12-
print(latest_block)
13+
print(json.dumps(latest_block, indent=2))
1314

1415

1516
if __name__ == "__main__":

examples/exchange_client/derivative_exchange_rpc/22_OrderbooksV2.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import json
23

34
from pyinjective.async_client import AsyncClient
45
from pyinjective.core.network import Network
@@ -12,8 +13,9 @@ async def main() -> None:
1213
"0x17ef48032cb24375ba7c2e39f384e56433bcab20cbee9a7357e4cba2eb00abe6",
1314
"0xd5e4b12b19ecf176e4e14b42944731c27677819d2ed93be4104ad7025529c7ff",
1415
]
15-
orderbooks = await client.fetch_derivative_orderbooks_v2(market_ids=market_ids)
16-
print(orderbooks)
16+
depth = 1
17+
orderbooks = await client.fetch_derivative_orderbooks_v2(market_ids=market_ids, depth=depth)
18+
print(json.dumps(orderbooks, indent=2))
1719

1820

1921
if __name__ == "__main__":

0 commit comments

Comments
 (0)