Interacting With Contracts
After initializing the SDK on a given chain, you can obtain instances of a target contract by their address, read state from it and execute transactions on it on behalf of the connected wallet.
Getting a Contract Instance
To get a contract instance, you only need the contract's address. The contract ABI will be automatically resolved from the onchain contract metadata.
You can still optionally specify the ABI as a second argument to skip the automatic resolution.
This will return an instance of SmartContract
which you can use to interact with the contract.
Reading State
Calling generic view
functions on your contract can be done via the call
function. The return type will match the return type of the contract function.
Executing Transactions
Executing transactions on your contract can also be done via the call
function. The return type will always be a TransactionResult
containing the transaction receipt.
By default, this will wait till the transaction is fully mined onchain.
You can also optionally specify transaction overrides as the third argument.
Preparing Transactions
You can also prepare a transaction without executing it. This will return a Transaction
object that you can use to estimate, simulate, sign, send and execute the transaction yourself.
Listening to Events
You can retrieve and listen to events emitted by your contract by using the contract.events
namespace.
Here's how to get past events emitted by your contract. Note that passing a block range is highly recommended.
And how to listen to new events emitted by your contract in real time: