Skip to content

Commit

Permalink
Merge pull request #288 from InjectiveLabs/feat/support_msg_execute_c…
Browse files Browse the repository at this point in the history
…ontract_compat

Feat/support msg execute contract compat
  • Loading branch information
aarmoa committed Jan 8, 2024
2 parents 2b33a3b + d6910b6 commit 8e673ba
Show file tree
Hide file tree
Showing 27 changed files with 1,284 additions and 281 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ clean-all:
$(call clean_repos)

clone-injective-core:
git clone https://github.com/InjectiveLabs/injective-core.git -b v1.12.8-testnet --depth 1 --single-branch
git clone https://github.com/InjectiveLabs/injective-core.git -b v1.12.9-testnet --depth 1 --single-branch

clone-injective-indexer:
git clone https://github.com/InjectiveLabs/injective-indexer.git -b v1.12.67 --depth 1 --single-branch
git clone https://github.com/InjectiveLabs/injective-indexer.git -b v1.12.72 --depth 1 --single-branch

clone-cometbft:
git clone https://github.com/cometbft/cometbft.git -b v0.37.2 --depth 1 --single-branch
git clone https://github.com/InjectiveLabs/cometbft.git -b v0.37.2-inj --depth 1 --single-branch

clone-wasmd:
git clone https://github.com/InjectiveLabs/wasmd.git -b v0.40.2-inj --depth 1 --single-branch
git clone https://github.com/InjectiveLabs/wasmd.git -b v0.45.0-inj --depth 1 --single-branch

clone-cosmos-sdk:
git clone https://github.com/InjectiveLabs/cosmos-sdk.git -b v0.47.3-inj-6 --depth 1 --single-branch
git clone https://github.com/InjectiveLabs/cosmos-sdk.git -b v0.47.3-inj-9 --depth 1 --single-branch

clone-ibc-go:
git clone https://github.com/InjectiveLabs/ibc-go.git -b v7.2.0-inj --depth 1 --single-branch
Expand Down
84 changes: 84 additions & 0 deletions examples/chain_client/76_MsgExecuteContractCompat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import asyncio
import json

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
from pyinjective.transaction import Transaction
from pyinjective.wallet import PrivateKey


async def main() -> None:
# select network: local, testnet, mainnet
network = Network.testnet()

client = AsyncClient(network)
composer = await client.composer()
await client.sync_timeout_height()

# load account
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.fetch_account(address.to_acc_bech32())

# prepare tx msg
# NOTE: COIN MUST BE SORTED IN ALPHABETICAL ORDER BY DENOMS
funds = (
"69factory/inj1hdvy6tl89llqy3ze8lv6mz5qh66sx9enn0jxg6/inj12ngevx045zpvacus9s6anr258gkwpmthnz80e9,"
"420peggy0x44C21afAaF20c270EBbF5914Cfc3b5022173FEB7,"
"1peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5"
)

msg = composer.msg_execute_contract_compat(
sender=address.to_acc_bech32(),
contract="inj1ady3s7whq30l4fx8sj3x6muv5mx4dfdlcpv8n7",
msg=json.dumps({"increment": {}}),
funds=funds,
)

# build sim tx
tx = (
Transaction()
.with_messages(msg)
.with_sequence(client.get_sequence())
.with_account_num(client.get_number())
.with_chain_id(network.chain_id)
)
sim_sign_doc = tx.get_sign_doc(pub_key)
sim_sig = priv_key.sign(sim_sign_doc.SerializeToString())
sim_tx_raw_bytes = tx.get_tx_data(sim_sig, pub_key)

# simulate tx
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 = 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(
amount=gas_price * gas_limit,
denom=network.fee_denom,
)
]
tx = tx.with_gas(gas_limit).with_fee(fee).with_memo("").with_timeout_height(client.timeout_height)
sign_doc = tx.get_sign_doc(pub_key)
sig = priv_key.sign(sign_doc.SerializeToString())
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.broadcast_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))


if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())
9 changes: 9 additions & 0 deletions pyinjective/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pyinjective.proto.injective.peggy.v1 import msgs_pb2 as injective_peggy_tx_pb
from pyinjective.proto.injective.stream.v1beta1 import query_pb2 as chain_stream_query
from pyinjective.proto.injective.tokenfactory.v1beta1 import tx_pb2 as token_factory_tx_pb
from pyinjective.proto.injective.wasmx.v1 import tx_pb2 as wasmx_tx_pb

REQUEST_TO_RESPONSE_TYPE_MAP = {
"MsgCreateSpotLimitOrder": injective_exchange_tx_pb.MsgCreateSpotLimitOrderResponse,
Expand Down Expand Up @@ -1032,6 +1033,14 @@ def msg_change_admin(
new_admin=new_admin,
)

def msg_execute_contract_compat(self, sender: str, contract: str, msg: str, funds: str):
return wasmx_tx_pb.MsgExecuteContractCompat(
sender=sender,
contract=contract,
msg=msg,
funds=funds,
)

def chain_stream_bank_balances_filter(
self, accounts: Optional[List[str]] = None
) -> chain_stream_query.BankBalancesFilter:
Expand Down
47 changes: 28 additions & 19 deletions pyinjective/proto/cosmwasm/wasm/v1/authz_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8e673ba

Please sign in to comment.