Skip to content

Commit 0204942

Browse files
authored
Merge pull request #277 from InjectiveLabs/feat/create_api_components
Feat/create api components
2 parents c1c8089 + 7672c35 commit 0204942

File tree

204 files changed

+15123
-892
lines changed

Some content is hidden

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

204 files changed

+15123
-892
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

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

5-
## [1.0.1] - 2023-11-07
5+
## [1.0.1] - 2023-12-11
6+
### Added
7+
- Added low level API components for all modules (chain, exchain and explorer) to make the Python SDK compatible with the TypeScript SDK.
8+
69
### Changed
710
- Updated proto definitions to injective-core v1.12.2-testnet and injective-indexer v1.12.45-rc3
11+
- Added new functions in AsyncClient to interact with chain, exchange and explorer using the low level API components
12+
- Marked old function sin AsyncClient as deprecated (the functions will be removed in a future version)
13+
- Updated all API examples to use the new AsyncClient functions
814

915
## [1.0] - 2023-11-01
1016
### Added

examples/chain_client/0_LocalOrderHash.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def main() -> None:
2222
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2323
pub_key = priv_key.to_public_key()
2424
address = pub_key.to_address()
25-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2626
subaccount_id = address.get_subaccount_id(index=0)
2727
subaccount_id_2 = address.get_subaccount_id(index=1)
2828

@@ -118,7 +118,7 @@ async def main() -> None:
118118
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
119119

120120
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
121-
res = await client.send_tx_sync_mode(tx_raw_bytes)
121+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
122122
print(res)
123123
print("gas wanted: {}".format(gas_limit))
124124
print("gas fee: {} INJ".format(gas_fee))
@@ -155,7 +155,7 @@ async def main() -> None:
155155
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
156156

157157
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
158-
res = await client.send_tx_sync_mode(tx_raw_bytes)
158+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
159159
print(res)
160160
print("gas wanted: {}".format(gas_limit))
161161
print("gas fee: {} INJ".format(gas_fee))
@@ -245,7 +245,7 @@ async def main() -> None:
245245
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
246246

247247
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
248-
res = await client.send_tx_sync_mode(tx_raw_bytes)
248+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
249249
print(res)
250250
print("gas wanted: {}".format(gas_limit))
251251
print("gas fee: {} INJ".format(gas_fee))

examples/chain_client/13_MsgIncreasePositionMargin.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from grpc import RpcError
4+
35
from pyinjective.async_client import AsyncClient
46
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
57
from pyinjective.core.network import Network
@@ -20,7 +22,7 @@ async def main() -> None:
2022
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2123
pub_key = priv_key.to_public_key()
2224
address = pub_key.to_address()
23-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2426
subaccount_id = address.get_subaccount_id(index=0)
2527

2628
# prepare trade info
@@ -48,14 +50,15 @@ async def main() -> None:
4850
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
4951

5052
# simulate tx
51-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
52-
if not success:
53-
print(sim_res)
53+
try:
54+
sim_res = await client.simulate(sim_tx_raw_bytes)
55+
except RpcError as ex:
56+
print(ex)
5457
return
5558

5659
# build tx
5760
gas_price = GAS_PRICE
58-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
61+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
5962
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
6063
fee = [
6164
composer.Coin(
@@ -69,7 +72,7 @@ async def main() -> None:
6972
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
7073

7174
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
72-
res = await client.send_tx_sync_mode(tx_raw_bytes)
75+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
7376
print(res)
7477
print("gas wanted: {}".format(gas_limit))
7578
print("gas fee: {} INJ".format(gas_fee))

examples/chain_client/15_MsgWithdraw.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from grpc import RpcError
4+
35
from pyinjective.async_client import AsyncClient
46
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
57
from pyinjective.core.network import Network
@@ -20,7 +22,7 @@ async def main() -> None:
2022
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2123
pub_key = priv_key.to_public_key()
2224
address = pub_key.to_address()
23-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2426
subaccount_id = address.get_subaccount_id(index=0)
2527

2628
# prepare tx msg
@@ -39,14 +41,15 @@ async def main() -> None:
3941
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
4042

4143
# simulate tx
42-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
43-
if not success:
44-
print(sim_res)
44+
try:
45+
sim_res = await client.simulate(sim_tx_raw_bytes)
46+
except RpcError as ex:
47+
print(ex)
4548
return
4649

4750
# build tx
4851
gas_price = GAS_PRICE
49-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
52+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
5053
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
5154
fee = [
5255
composer.Coin(
@@ -60,7 +63,7 @@ async def main() -> None:
6063
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
6164

6265
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
63-
res = await client.send_tx_sync_mode(tx_raw_bytes)
66+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
6467
print(res)
6568
print("gas wanted: {}".format(gas_limit))
6669
print("gas fee: {} INJ".format(gas_fee))

examples/chain_client/16_MsgSubaccountTransfer.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from grpc import RpcError
4+
35
from pyinjective.async_client import AsyncClient
46
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
57
from pyinjective.core.network import Network
@@ -20,7 +22,7 @@ async def main() -> None:
2022
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2123
pub_key = priv_key.to_public_key()
2224
address = pub_key.to_address()
23-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2426
subaccount_id = address.get_subaccount_id(index=0)
2527
dest_subaccount_id = address.get_subaccount_id(index=1)
2628

@@ -46,14 +48,15 @@ async def main() -> None:
4648
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
4749

4850
# simulate tx
49-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
50-
if not success:
51-
print(sim_res)
51+
try:
52+
sim_res = await client.simulate(sim_tx_raw_bytes)
53+
except RpcError as ex:
54+
print(ex)
5255
return
5356

5457
# build tx
5558
gas_price = GAS_PRICE
56-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
59+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
5760
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
5861
fee = [
5962
composer.Coin(
@@ -67,7 +70,7 @@ async def main() -> None:
6770
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
6871

6972
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
70-
res = await client.send_tx_sync_mode(tx_raw_bytes)
73+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
7174
print(res)
7275
print("gas wanted: {}".format(gas_limit))
7376
print("gas fee: {} INJ".format(gas_fee))

examples/chain_client/17_MsgBatchUpdateOrders.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import asyncio
22
import uuid
33

4+
from grpc import RpcError
5+
46
from pyinjective.async_client import AsyncClient
57
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
68
from pyinjective.core.network import Network
@@ -21,7 +23,7 @@ async def main() -> None:
2123
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2224
pub_key = priv_key.to_public_key()
2325
address = pub_key.to_address()
24-
await client.get_account(address.to_acc_bech32())
26+
await client.fetch_account(address.to_acc_bech32())
2527
subaccount_id = address.get_subaccount_id(index=0)
2628

2729
# prepare trade info
@@ -131,18 +133,19 @@ async def main() -> None:
131133
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
132134

133135
# simulate tx
134-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
135-
if not success:
136-
print(sim_res)
136+
try:
137+
sim_res = await client.simulate(sim_tx_raw_bytes)
138+
except RpcError as ex:
139+
print(ex)
137140
return
138141

139-
sim_res_msg = composer.MsgResponses(sim_res, simulation=True)
142+
sim_res_msg = sim_res["result"]["msgResponses"]
140143
print("---Simulation Response---")
141144
print(sim_res_msg)
142145

143146
# build tx
144147
gas_price = GAS_PRICE
145-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
148+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
146149
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
147150
fee = [
148151
composer.Coin(
@@ -156,7 +159,7 @@ async def main() -> None:
156159
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
157160

158161
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
159-
res = await client.send_tx_sync_mode(tx_raw_bytes)
162+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
160163
print("---Transaction Response---")
161164
print(res)
162165
print("gas wanted: {}".format(gas_limit))

examples/chain_client/18_MsgBid.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from grpc import RpcError
4+
35
from pyinjective.async_client import AsyncClient
46
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
57
from pyinjective.core.network import Network
@@ -20,7 +22,7 @@ async def main() -> None:
2022
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2123
pub_key = priv_key.to_public_key()
2224
address = pub_key.to_address()
23-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2426

2527
# prepare tx msg
2628
msg = composer.MsgBid(sender=address.to_acc_bech32(), round=16250, bid_amount=1)
@@ -38,14 +40,15 @@ async def main() -> None:
3840
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
3941

4042
# simulate tx
41-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
42-
if not success:
43-
print(sim_res)
43+
try:
44+
sim_res = await client.simulate(sim_tx_raw_bytes)
45+
except RpcError as ex:
46+
print(ex)
4447
return
4548

4649
# build tx
4750
gas_price = GAS_PRICE
48-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
51+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
4952
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
5053
fee = [
5154
composer.Coin(
@@ -59,7 +62,7 @@ async def main() -> None:
5962
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
6063

6164
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
62-
res = await client.send_tx_sync_mode(tx_raw_bytes)
65+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
6366
print(res)
6467
print("gas wanted: {}".format(gas_limit))
6568
print("gas fee: {} INJ".format(gas_fee))

examples/chain_client/19_MsgGrant.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from grpc import RpcError
4+
35
from pyinjective.async_client import AsyncClient
46
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
57
from pyinjective.core.network import Network
@@ -20,7 +22,7 @@ async def main() -> None:
2022
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2123
pub_key = priv_key.to_public_key()
2224
address = pub_key.to_address()
23-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2426
# subaccount_id = address.get_subaccount_id(index=0)
2527
# market_ids = ["0x0511ddc4e6586f3bfe1acb2dd905f8b8a82c97e1edaef654b12ca7e6031ca0fa"]
2628

@@ -57,14 +59,15 @@ async def main() -> None:
5759
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
5860

5961
# simulate tx
60-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
61-
if not success:
62-
print(sim_res)
62+
try:
63+
sim_res = await client.simulate(sim_tx_raw_bytes)
64+
except RpcError as ex:
65+
print(ex)
6366
return
6467

6568
# build tx
6669
gas_price = GAS_PRICE
67-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
70+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
6871
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
6972
fee = [
7073
composer.Coin(
@@ -78,7 +81,7 @@ async def main() -> None:
7881
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
7982

8083
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
81-
res = await client.send_tx_sync_mode(tx_raw_bytes)
84+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
8285
print(res)
8386
print("gas wanted: {}".format(gas_limit))
8487
print("gas fee: {} INJ".format(gas_fee))

examples/chain_client/1_MsgSend.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22

3+
from grpc import RpcError
4+
35
from pyinjective.async_client import AsyncClient
46
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
57
from pyinjective.core.network import Network
@@ -20,7 +22,7 @@ async def main() -> None:
2022
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
2123
pub_key = priv_key.to_public_key()
2224
address = pub_key.to_address()
23-
await client.get_account(address.to_acc_bech32())
25+
await client.fetch_account(address.to_acc_bech32())
2426

2527
# prepare tx msg
2628
msg = composer.MsgSend(
@@ -43,14 +45,15 @@ async def main() -> None:
4345
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)
4446

4547
# simulate tx
46-
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
47-
if not success:
48-
print(sim_res)
48+
try:
49+
sim_res = await client.simulate(sim_tx_raw_bytes)
50+
except RpcError as ex:
51+
print(ex)
4952
return
5053

5154
# build tx
5255
gas_price = GAS_PRICE
53-
gas_limit = sim_res.gas_info.gas_used + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
56+
gas_limit = int(sim_res["gasInfo"]["gasUsed"]) + GAS_FEE_BUFFER_AMOUNT # add buffer for gas fee computation
5457
gas_fee = "{:.18f}".format((gas_price * gas_limit) / pow(10, 18)).rstrip("0")
5558
fee = [
5659
composer.Coin(
@@ -64,7 +67,7 @@ async def main() -> None:
6467
tx_raw_bytes = tx.get_tx_data(sig, pub_key)
6568

6669
# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
67-
res = await client.send_tx_sync_mode(tx_raw_bytes)
70+
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
6871
print(res)
6972
print("gas wanted: {}".format(gas_limit))
7073
print("gas fee: {} INJ".format(gas_fee))

0 commit comments

Comments
 (0)