Skip to content

Commit

Permalink
Merge pull request #277 from InjectiveLabs/feat/create_api_components
Browse files Browse the repository at this point in the history
Feat/create api components
  • Loading branch information
aarmoa committed Dec 4, 2023
2 parents c1c8089 + 7672c35 commit 0204942
Show file tree
Hide file tree
Showing 204 changed files with 15,123 additions and 892 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

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

## [1.0.1] - 2023-11-07
## [1.0.1] - 2023-12-11
### Added
- Added low level API components for all modules (chain, exchain and explorer) to make the Python SDK compatible with the TypeScript SDK.

### Changed
- Updated proto definitions to injective-core v1.12.2-testnet and injective-indexer v1.12.45-rc3
- Added new functions in AsyncClient to interact with chain, exchange and explorer using the low level API components
- Marked old function sin AsyncClient as deprecated (the functions will be removed in a future version)
- Updated all API examples to use the new AsyncClient functions

## [1.0] - 2023-11-01
### Added
Expand Down
8 changes: 4 additions & 4 deletions examples/chain_client/0_LocalOrderHash.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)
subaccount_id_2 = address.get_subaccount_id(index=1)

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

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

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
15 changes: 9 additions & 6 deletions examples/chain_client/13_MsgIncreasePositionMargin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -20,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
15 changes: 9 additions & 6 deletions examples/chain_client/15_MsgWithdraw.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -20,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
15 changes: 9 additions & 6 deletions examples/chain_client/16_MsgSubaccountTransfer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -20,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)
dest_subaccount_id = address.get_subaccount_id(index=1)

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
17 changes: 10 additions & 7 deletions examples/chain_client/17_MsgBatchUpdateOrders.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import asyncio
import uuid

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -21,7 +23,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())
subaccount_id = address.get_subaccount_id(index=0)

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

sim_res_msg = composer.MsgResponses(sim_res, simulation=True)
sim_res_msg = sim_res["result"]["msgResponses"]
print("---Simulation Response---")
print(sim_res_msg)

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print("---Transaction Response---")
print(res)
print("gas wanted: {}".format(gas_limit))
Expand Down
15 changes: 9 additions & 6 deletions examples/chain_client/18_MsgBid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -20,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
15 changes: 9 additions & 6 deletions examples/chain_client/19_MsgGrant.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -20,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())
# subaccount_id = address.get_subaccount_id(index=0)
# market_ids = ["0x0511ddc4e6586f3bfe1acb2dd905f8b8a82c97e1edaef654b12ca7e6031ca0fa"]

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
15 changes: 9 additions & 6 deletions examples/chain_client/1_MsgSend.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio

from grpc import RpcError

from pyinjective.async_client import AsyncClient
from pyinjective.constant import GAS_FEE_BUFFER_AMOUNT, GAS_PRICE
from pyinjective.core.network import Network
Expand All @@ -20,7 +22,7 @@ async def main() -> None:
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())
await client.fetch_account(address.to_acc_bech32())

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

# simulate tx
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
if not success:
print(sim_res)
try:
sim_res = await client.simulate(sim_tx_raw_bytes)
except RpcError as ex:
print(ex)
return

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

# broadcast tx: send_tx_async_mode, send_tx_sync_mode, send_tx_block_mode
res = await client.send_tx_sync_mode(tx_raw_bytes)
res = await client.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
Expand Down
Loading

0 comments on commit 0204942

Please sign in to comment.