Front-end app: smart contract interaction
Learn how to query a contract's state and send signed transactions in your front-end application.
Intro
In this tutorial, you will learn how to make the front-end of your App interact with smart contracts using @polkadot/api-contract
, more specifically:
how to read values stored in your smart contract
how to send signed transactions to your smart contract
Connect to a deployed smart contract
The first step will be to create an API instance to connect to a running node. For that, we need a provider: the default instance of WsProvider
connects to "ws://127.0.0.1:9944"
which usually is your local node's endpoint. For Aleph Zero Testnet use "wss://ws.test.azero.dev"
and for Aleph Zero Mainnet: "wss://ws.azero.dev"
.
The @polkadot/api-contract
comes with 4 general helpers (see the docs). Here, we are using ContractPromise
which allows us to interact with already deployed contracts.
Query a contract state
Under the hood, querying a contract is an extrinsic dry run. It is not submitted on the chain, however, it requires to specify the gasLimit
which refers to the maximum resources used by a contract call. Because we are not submitting an extrinsic to the chain (we're only performing a dry-run), it is safe to use sufficiently high values to ensure that the call won’t be reverted. For contract calls, Substrate uses a 2-dimensional weight (gas) system which consists of:
refTime
: the amount of computational time used for execution, in picoseconds;proofSize
: the amount of storage in bytes that a transaction is allowed to read.
In this example, we call the contract method getByAccount
which takes one argument accountId
. For simplicity, we are using the contract address as the caller.
Contract query results in ContractCallOutcome
. If the query is successful, you can extract the return value from the output object. See how it can be done here.
Send signed transaction to a contract
Signing account
To sign a transaction, we need a wallet. To keep this example simple, we will use @polkadot/extension-dapp
to retrieve wallet providers added to the app page and assume that there’s at least one account. See the Aleph Zero Signer Integration tutorial for more information.
Gas estimation
To sign and send a transaction to a contract, we should estimate values for gasLimit
. Although the unused gas is refunded after the call, it is good practice to specify a reasonable gasLimit
for transactions. As shown here, this (gasRequired
) can be estimated with contract.query.[method]
. Here is an alternative way of how to do it using api.call.contractsApi.call<ContractExecResult>
.
In practice, when estimating gasLimit
using gasRequired
you may want to add some margin to ensure the transaction won't be reverted due to insufficient gasLimit
.
The first step is to get AbiMessage
for the contract method. The function below searches for the method in the contract ABI and returns it if found.
The getGasLimit(...)
function below estimates the gasRequired
for this contract call which should be sufficient to cover the gas fees when submitting the actual extrinsic. There are also other contract call options which can be adjusted:
storageDepositLimit
- The maximum amount of balance that can be charged/reserved for the storage consumed.value
- The balance (in native currency,AZERO
in our case) to transfer from the caller to the contract. Non-zero values here apply only to methods marked with the#[ink(message, payable)]
macro.
Sign and send the transaction
Function sendPost(...)
puts all the pieces together. It calls the payable method on the contract post(expiresAfter: u32, postText: String)
and handles the result.
To learn more about how to handle transaction events see Transaction Subscriptions. Also, see how it is done in the Bulletin Board Example.
When running substrate-contract-node, the blocks are never finalized since it does not use a finality mechanism by default. On a live network such as Aleph Zero Testnet, you should expect a ‘Finalized’ transaction status soon after the ‘InBlock’ status.
If you want to run a local chain that will work exactly as the Testnet, you can use the ./scripts/run_nodes.sh
script, which will bootstrap a small chain locally.
Closing remarks
We hope that this tutorial helped you learn more about how to interact with smart contracts in the front-end application. You shouldn't stop here! Check out other resources that may help you extend your dApp:
Nightly-connect: A permissionless, open-source solution that serves as both a wallet adapter and a bridge wallet, enabling connections through QR codes or deep links.
Typechain: Generate Typescript wrappers around your smart contract!
ink!athon: A full-stack dApp boilerplate for ink! smart contracts with an integrated front-end.
Last updated
Was this helpful?