useCallSubscription

A React hook for calling a contract message on each new block and decoding a successful response or receiving an error. This is similar to useCallarrow-up-right, except that there is no send() function in the response. The contract message will automatically be called on each new block. See useCallarrow-up-right to learn about more shared features.

See useink/utils helpersarrow-up-right for compatible functions that work well with this hook.

import { useCallSubscription } 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 CallGetOnNewBlocks: React.FC = () => {
  const contract = useContract(CONTRACT_ADDRESS, metadata, 'aleph') 
  const args = ['arg-1', 2]
  const get = useCallSubscription<SuccessfulResponse>(contract, 'get', args)

  return (
    <>
      <h2>Get the Result the hard way: {get.result?.ok ? get.result.value.decoded.foo : '--'}</h2>
      <h2>Or the easy way: {pickDecoded(get.result)?.foo || '--'}</h2>
    </>
  )
}

Calling with a default caller addressarrow-up-right

You must first define a default caller in configurationarrow-up-right, then call the contract with options:

Return Valuearrow-up-right

Last updated