Contract Tx
In addition to using the .query.<messageName>
on a contract, the .tx.<messageName>
method is provides to send an actual encoded transaction to the contract, allow for execution and have this applied in a block. Expanding on our previous examples, we can now execute and then retrieve the subsequent value -
If we perform the same query.get
read on the value now, it would be 124
. For lower-level access, like we have in the Blueprint
via .createContract
you can also perform the execution via the .exec
function, which would yield equivalent results -
For the above interface we can specify the message as the string name, the index of the actual message as retrieved via the Abi.
#
Weight estimationTo estimate the gasLimit (which in the Substrate context refers to the weight used), we can use the .query
(read) interfaces with a sufficiently large value to retrieve the actual gas consumed. The API makes this easy - with a gasLimit
or -1
passed to the query it will use the maximum gas limit available to transactions and the return value will have the actual gas used.
To see this in practice -
We can use the gasConsumed
input (potentially with a buffer for various execution paths) in any calls to contract.tx.inc(...)
with the same input parameters specified on the query
where the estimation was done.
#
EventsOn current versions of the API, any events raised by the contract will be transparently decoded with the relevant ABI and will be made available on the result
(from .signAndSend(alicePair, (result) => {...}
) as contractEvents
.
Where no events were emitted this value would be undefined
, however should events be emitted, the array will contain all the decoded values.
#
That is it... for nowThis was a whirl-wind tour of what the API provides in terms of the @polkadot/api-contract
interface. It is not perfect yet, we would like to expand it to allow for greater type-checking on the contracts (instead of read/exec wit messages), but hopefully in the current state it already enhances the way you can interact with contracts.