useCall
A hook for calling a contract message and decoding a successful response or receiving an error. See useink/utils helpers for compatible functions that work well with this hook.
Usage
import { useCall } from 'useink'
import { pickDecoded } from 'useink/utils'
import metadata from 'contract/metadata.json'
const CONTRACT_ADDRESS = '...'
// We define a response type so that `get.result.value.decoded` is of type SuccessfulResponse
interface SuccessfulResponse {
foo: 'bar'
}
export const MyContractView: React.FC = () => {
const contract = useContract(CONTRACT_ADDRESS, metadata, 'astar');
const get = useCall<SuccessfulResponse>(contract, 'get');
const args = ['arg-1', 2];
return (
<>
<h1>Get the Result the hard way: {get.result?.ok ? get.result.value.decoded.foo : '--'}</h1>
<h1>Or the easy way: {pickDecoded(get.result)?.foo || '--'}</h1>
<button disabled={get.isSubmitting} onClick={() => get.send(args)}>
Get Result
</button>
</>
);
}Calling with a default caller address
You must first define a default caller in configuration, then call the contract with options:
Handling Result<T, E> responses from an ink! contract
Result<T, E> responses from an ink! contractOne of the benefits of using ink! is ability to return meaningful errors with Result<T, E> (since ink! v4.0.0). In this example we will distinguish between two kinds of errors and a successful result. Let's say that you have the following ink! code in your contract.
In this example, when you call mood(2), you will get an Ok response. If you call mood(1) you will get an Err. If you call mood(5) you will get another type of Err.
Here is how we could handle the view using useink.
Return Value
Last updated
Was this helpful?