useTx

A hook for sending a transaction for a contract and decoding successful responses or receiving errors. This hook is used in combination with the result of useContractarrow-up-right.

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

import { useTx, useContract, shouldDisable } from 'useink'
import { pickDecoded } from 'useink/utils'
import metadata from './metadata.json'

interface Result {
  color: string;
}

export const MyContractView: React.FC = () => {
  const contract = useContract('..address', metadata)
  const setColor = useTx<Result>(contract, 'setColor')
  const args = ['blue']

  return (
    <>
      <button onClick={() => setColor.signAndSend(args)} disable={shouldDisable(setColor)}>
        {shouldDisable(setColor) ? 'Changing Color...' : 'Change Color'}
      </button>

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

Return Valuearrow-up-right

Transaction Statusesarrow-up-right

useink extends transaction statuses defined in the Substrate transaction-pool pallet. These are used for pre-transaction senarios such as awaiting signature in a wallet, dry runs, or handling a JavaScript error.

Want to Learn More?arrow-up-right

Common Patterns With useTxarrow-up-right

See shouldDisablearrow-up-right and shouldDisableStrictarrow-up-right.

Last updated

Was this helpful?