# Contract Hooks

### Install dependencies

```shell
npm install wagmi ethers
```

## useContractWrite&#x20;

```typescript
// import contract information and hook
import { useContractWrite } from 'wagmi'
import { DEPLOYED_CONTRACT_ADDRESS, CONTRACT_ABI, CONTRACT_CHAIN_ID } from '../constants'

// instantiate contract function 
const { write } = useContractWrite({
    addressOrName: DEPLOYED_CONTRACT_ADDRESS,
    chainId: CONTRACT_CHAIN_ID,
    contractInterface: CONTRACT_ABI,
    functionName: 'writeSomeStuff',
    args: [firstParam, secondParam],
    overrides: {
      from: address,
      value: amountInWei,
    },
    onMutate({ args, overrides }) {
      console.log('Mutate', { args, overrides })
    },
    onError(error) {
      console.log('Error', error)
    },
    onSuccess(data) {
      console.log('Success', data)
    },
})

// call function 
  <button onClick={() => write()}>
      Call Function
  </button>

```

## useContractReads

```typescript
// import contract information and hook
import { useContractReads } from 'wagmi'
import { DEPLOYED_CONTRACT_ADDRESS, CONTRACT_ABI, CONTRACT_CHAIN_ID } from '../constants'

// create contract instance 
  const contract = {
    addressOrName: DEPLOYED_CONTRACT_ADDRESS,
    contractInterface: CONTRACT_ABI,
  }

// instantiate read function 
  const { data, isError, isLoading } = useContractReads({
    contracts: [
      {
        ...contract,
        functionName: 'firstGetterFunction',
        args: [firstParam, secondParam],
        chainId: CONTRACT_CHAIN_ID,
      },
      {
        ...contract,
        functionName: 'secondGetterFunction',
        args: addr,
        chainId: CONTRACT_CHAIN_ID,
      },
    ],
    onSuccess(data) {
      console.log('first read result', data[0])
      console.log('second read result', data[1])
    },
    onError(error) {
      console.log('Error', error)
    },
  })
```

References: \
<https://wagmi.sh/>

##


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://thryec.gitbook.io/dev-compedium/ethereum/frontend/contract-hooks.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
