InjectiveLabs / sdk-python

Injective Exchange API for Python clients
Apache License 2.0
41 stars 27 forks source link

{TypeError}TypeError("Parameter to MergeFrom() must be instance of same class: expected <class 'cosmos.base.v1beta1.coin_pb2.Coin'> got <class 'str'>.") #285

Closed sherlock-tez closed 9 months ago

sherlock-tez commented 10 months ago

This is my code:

`async def main(contract: str, amount_to_buy: float) -> None:

select network: local, testnet, mainnet

network = Network.mainnet()

# initialize grpc client
# set custom cookie location (optional) - defaults to current dir
client = AsyncClient(network)
composer = await client.composer()
await client.sync_timeout_height()

# load account
priv_key = PrivateKey.from_hex(PRIVATE_KEY)
pub_key = priv_key.to_public_key()
address = pub_key.to_address()
await client.get_account(address.to_acc_bech32())

# prepare tx msg
amount = int(amount_to_buy * 10**18)
msg = composer.MsgExecuteContract(
    sender=address.to_acc_bech32(),
    contract=contract,
    msg=json.dumps({"swap": {"offer_asset": {"info": {"native_token": {"denom": "inj"}}, "amount": amount},
                             "max_spread": "0.5", "belief_price": "1002409.016161624728221610"}}),
    funds=[composer.Coin(amount=amount, denom=network.fee_denom)]
)

# 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
(sim_res, success) = await client.simulate_tx(sim_tx_raw_bytes)
# if not success:
#     print(sim_res)
#     return

# build tx
gas_price = 500000000
gas_limit = 5000000 + 20000  # add 20k 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.send_tx_sync_mode(tx_raw_bytes)
print(res)
print("gas wanted: {}".format(gas_limit))
print("gas fee: {} INJ".format(gas_fee))
)`

I am trying to simulate this transaction: https://explorer.injective.network/transaction/090A5FD7E2BE63EA79EF6021F24F7C86EE3F80A12C247BBB9C35375CF402318A/

Error here: https://explorer.injective.network/transaction/F61A61D714AA4E337CCAEBC81554D01B36799686DF16DDC16C62370ABA81B033/

How to create "MsgExecuteContractCompat"?

aarmoa commented 9 months ago

The project has an example in the examples module on how to use the MsgExecuteContractCompat