Skip to content

Ramyromel/wagmi

 
 

Repository files navigation


wagmi logo

Reactive primitives for Ethereum apps

Version MIT License Downloads per month Best of JS Code coverage


Documentation

For documentation and guides, visit wagmi.sh.

Community

For help, discussion about best practices, or any other conversation that would benefit from being searchable:

Discuss Wagmi on GitHub

For casual chit-chat with others using the framework:

Join the Wagmi Discord

Contributing

Contributions to Wagmi are greatly appreciated! If you're interested in contributing to Wagmi, please read the Contributing Guide before submitting a pull request.

Examples and Use Cases

Here are some examples and use cases to help you understand how to use Wagmi:

Example 1: Fetching Account Balance

import { useAccount, useBalance } from 'wagmi'

function AccountBalance() {
  const { address } = useAccount()
  const { data, isError, isLoading } = useBalance({
    address,
  })

  if (isLoading) return <div>Fetching balance...</div>
  if (isError) return <div>Error fetching balance</div>
  return <div>Balance: {data?.formatted} {data?.symbol}</div>
}

Example 2: Sending a Transaction

import { useAccount, useSendTransaction } from 'wagmi'
import { parseEther } from 'ethers/lib/utils'

function SendTransaction() {
  const { address } = useAccount()
  const { sendTransaction } = useSendTransaction()

  const handleSend = async () => {
    try {
      await sendTransaction({
        to: '0xRecipientAddress',
        value: parseEther('0.01'),
      })
      alert('Transaction sent!')
    } catch (error) {
      alert('Error sending transaction')
    }
  }

  return (
    <div>
      <button onClick={handleSend}>Send 0.01 ETH</button>
    </div>
  )
}

Example 3: Reading from a Smart Contract

import { useContractRead } from 'wagmi'
import { abi } from './MyContract.json'

function ReadContract() {
  const { data, isError, isLoading } = useContractRead({
    address: '0xContractAddress',
    abi,
    functionName: 'myFunction',
  })

  if (isLoading) return <div>Loading...</div>
  if (isError) return <div>Error reading contract</div>
  return <div>Data: {data}</div>
}

Sponsors

If you find Wagmi useful or use it for work, please consider sponsoring Wagmi. Thank you 🙏

paradigm logo

family logo context logo WalletConnect logo PartyDAO logo Dynamic logo Sushi logo Stripe logo BitKeep logo Privy logo Spruce logo rollup.id logo pancake logo celo logo rainbow logo pimlico logo zora logo lattice logo supa logo zksync logo

Sponsor Wagmi



Powered by Vercel

About

React Hooks for Ethereum

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • TypeScript 99.0%
  • Other 1.0%