🌱
Dev Compendium
  • Ethereum
    • Solidity
      • EVM
      • Architecture
      • Execution Context
      • Transactions
      • Gas
      • Calldata, Memory & Storage
      • Gas Optimisation
      • Function Declarations
      • receive() & fallback()
      • CALL vs. DELEGATE CALL
    • Yul
      • Introduction
      • Types
      • Basic Operations
      • Storage
      • Memory
        • Arrays
        • Structs
        • Tuples, revert, keccak256
        • Logs and Events
        • Gotchas
        • abi.encode
      • Calldata
        • External Calls
        • Dynamic Length Inputs
        • Transferring Value
        • Receiving Contract Calls
      • Contracts in Yul
      • Other Yul Functions
    • Foundry
    • Security
      • Common Vulnerabilities
      • Best Practices
      • Development Workflow
      • Contract Migration
    • Auditing Tools
      • Slither
      • Mythril
      • Fuzzing
    • Upgradable Contracts
      • Upgrade Patterns
      • ERC-1967 Implementation
      • Deployment
    • MEV
    • Tooling
      • Chainlink
      • IPFS
      • Radicle
    • Frontend
      • Contract Hooks
      • Wallet Connection
        • wagmi.sh
        • Rainbow Kit
      • thirdweb
    • Protocol Research
      • Uniswap v2
      • Uniswap v3
      • Curve
      • GMX
  • Starkware
    • Fundamentals
    • Account Abstraction
    • Universal Deployer
    • Cairo 1.0
    • starknet.js
    • Security Model
  • Zero Knowledge
    • Group Theory
    • ECDSA
  • Rust
    • Basic Operations
    • Set up
    • Primitives
    • Control Flow
    • Mutability & Shadowing
    • Adding Behavior
    • Lifetimes
    • Std Library
  • SUI
    • Architecture
    • Consensus Mechanism
    • Local Node Setup
    • Sui Client CLI
    • Move Contracts
      • Move
      • Move.toml
      • Move.lock
      • Accessing Time in Sui Move
      • Set up Development Framework
      • Debug & Publish
      • Package Upgrades
      • Sui Move Library
      • Difference from Core Move
    • Object Programming
      • Object Basics
      • Using Objects
      • Immutable Objects
      • Object Wrapping
      • Dynamic Fields
      • Collections
      • Unit Testing
      • Deployment with CLI
  • NEAR
    • Architecture
    • Contract Standards
      • Fungible Token (NEP-141)
      • Non-Fungible Token (NEP-171)
      • Storage Management (NEP-145)
      • Events (NEP-297)
      • Meta-Transactions
    • Rust Contracts
      • Development Workflow
      • Smart Contract Layout
      • Storage Management
      • Events & Meta-transactions
      • Method Types
      • Upgrading Contracts
      • Unit Testing
    • NEAR Libraries
    • Environment Variables
    • Serialisation
    • Security Concepts
    • Collections
    • JS SDK
Powered by GitBook
On this page
  • Install dependencies
  • useContractWrite
  • useContractReads
  1. Ethereum
  2. Frontend

Contract Hooks

Contract interactions based on wagmi.sh (v0.5.9)

Install dependencies

npm install wagmi ethers

useContractWrite

// 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

// 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)
    },
  })

PreviousFrontendNextWallet Connection

Last updated 2 years ago

References:

https://wagmi.sh/