3

I'm looking for a way to encode smart contract calls in javascript code with polkadot-js, the same way I can do it from the web UI, but I cannot find a description of the available API in the documentation

  1. is there a better documentation somewhere describing the methods and their arguments available somewhere?
  2. How should I use the ABI in my .contract file to encode a function call to a smart contract?

The contract is written in ink and the chain uses the contracts pallet.

Parts of the functionally will use some code written in Rust and executed as wasm in the browser so if it is easier to do in no_std rust that is also an option for me

1 Answer 1

4

To my knowledge, this is not documented anywhere unfortunately. You don't use the .contract file directly, but create an Abi instance from it.

Taking flipper as an example, the steps would be like so:

  1. Create an Abi object either directly or by creating a ContractPromise
const abi = new Abi(flipper.json, api.registry.getChainProperties())
const contract = new ContractPromise(api, flipper.json, address)
const abi = contract.abi
  1. Encode your call input
const inputData = abi.messages["flip"].toU8a([true])

You can use this encoded data for runtime calls where the arguments are not encoded automatically.

If you use api-contract and do contract calls via the ContractPromise interface the data is encoded for you. Just need to pass an array with values for the message params.

contract.tx["flip"]({gasLimit, storageDepositLimit}, true).signAndSend(...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.