clawchain/wagmi/site/vue/api/composables/useWriteContract.md

2.8 KiB

title, description
title description
useWriteContract Composable for executing a write function on a contract.

useWriteContract

Composable for executing a write function on a contract.

A "write" function on a Solidity contract modifies the state of the blockchain. These types of functions require gas to be executed, hence a transaction is broadcasted in order to change the state.

Import

import { useWriteContract } from '@wagmi/vue'

Usage

::: code-group

<script setup lang="ts">
import { useWriteContract } from '@wagmi/vue'
import { abi } from './abi'

const writeContract = useWriteContract()
</script>

<template>
  <button @click="writeContract.mutate({ 
    abi,
    address: '0x6b175474e89094c44da98b954eedeac495271d0f',
    functionName: 'transferFrom',
    args: [
      '0xd2135CfB216b74109775236E36d4b433F1DF507B',
      '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
      123n,
    ],
  })">
    Transfer
  </button>
</template>

<<< @/snippets/abi-write.ts[abi.ts] <<< @/snippets/vue/config.ts[config.ts] :::

Parameters

import { type UseWriteContractParameters } from '@wagmi/vue'

config

Config | undefined

Config to use instead of retrieving from the WagmiPlugin.

::: code-group

<script setup lang="ts">
import { useWriteContract } from '@wagmi/vue'
import { config } from './config' // [!code focus]

const writeContract = useWriteContract({
  config, // [!code focus]
})
</script>

<<< @/snippets/vue/config.ts[config.ts] :::

Return Type

import { type UseWriteContractReturnType } from '@wagmi/vue'

The return type's data property is inferrable via the combination of abi, functionName, and args. Check out the TypeScript docs for more info.

Type Inference

With abi setup correctly, TypeScript will infer the correct types for functionName, args, and the value. See the Wagmi TypeScript docs for more information.

Action