Contract
The ContractPromise
interface allows you to interact with a deployed contract. In the previous Blueprint example this instance was created via createContract
. In general use, you can also create an instance via new
, i.e. when you are attaching to an existing contract on-chain -
Either via a create above or via a call to createContract
both instances are the same. The Contract
provides a wrapper around the Abi
and allows you to call either read
or exec
on a contract to interact with it.
#
Reading contract valuesIn the Blueprint
example we have instantiated an incrementer contract. In the following examples we will continue using it to read from and execute transactions into, since it is a well-known entity. To read a value from the contract, we can do the following -
Underlying the above .query.<messageName>
is using the api.rpc.contracts.call
API on the contracts palette to retrieve the value. For this interface, the format is always of the form messageName(<account address to use>, <value>, <gasLimit>, <...additional params>)
. An example of querying a balance of a specific account on an erc20 contract will therefore be -
In this example we have specified a gasLimit
of -1
, in a subsequent section we will expand on this. for now, just remember that is indicated to use max available, i.e. we don't explicitly want to specify a value.
When executing it encodes the message using the selector and the input values to allow execution in the contract environment. This can be executed on any contract message, unlike the examples that will follow below it will only read state, not actually execute and therefore not consume any real value from the account.
An alternative for reading would be via the lower-level .read
method, in this case
In cases where the ABI messages have conflicting names, instead of the 'get'
string the actual message index (or message from the Abi itself) can be passed-through.
#
Sending a transactionNow that we understand the underlying call/read interfaces where a message is executed, but not part of a block, we will loo into sending transaction messages in our next section.