wagmi v2 智能合约交互

问题描述 投票:0回答:1

我正在使用最新的Rainbowkit,以及wagmi hooks v2.5.7,找不到明显的问题,但我也无法与我的智能合约交互 基本上,我收到的错误是 write 不是一个函数 非常感谢任何帮助,谢谢

    import React, { useState } from "react"
    import { useAccount, useContractWrite } from "wagmi"
    import { Button, Input, Text } from "@nextui-org/react"
    import {
      presaleAdr as contractAddress,
      presale_ABI as contractABI,
    } from "@/constants/presaleAdr"

    function ModifyRate() {
      const [newRate, setNewRate] = useState("")
      const [error, setError] = useState("")
      const { address, isConnected } = useAccount()

      const {
        write,
        isLoading,
        error: writeError,
      } = useContractWrite({
        addressOrName: contractAddress,
        contractInterface: contractABI,
        functionName: "adjustRates",
        args: [newRate ? BigInt(newRate) : 0],
        onSettled(data, error) {
          if (error) {
            console.error("Transaction Error:", error)
            setError(error.message || "Adjustment failed.")
          } else {
            console.log("Transaction Success:", data)
            setError("")
          }
        },
      })

      const handleAdjustRates = async () => {
        setError("") 
        if (!isConnected) {
          setError("Please connect your wallet first.")
          return
        }

        if (!newRate) {
          setError("Please enter a new rate.")
          return
        }

        try {
          const tx = await write()
          console.log("Transaction initiated:", tx)
        } catch (err) {
          console.error("Adjustment failed:", err)
          setError(err.message || "An unexpected error occurred.")
        }
      }

      return (
        <div className="w-1/2 m-auto h-auto">
          <Input
            clearable
            bordered
            fullWidth
            color="primary"
            size="lg"
            placeholder="Enter the new rate"
            value={newRate}
            onChange={(e) => setNewRate(e.target.value)}
            disabled={isLoading}
          />
          <Button
            auto
            disabled={isLoading || !newRate || !isConnected}
            onClick={handleAdjustRates}
          >
            {isLoading ? "Processing..." : "Adjust Rate"}
          </Button>
          {writeError && <Text color="error">{writeError.message}</Text>}
        </div>
      )
    }

    export default ModifyRate



`    function ModifyRate() {
      const [newRate, setNewRate] = useState("")
      const [error, setError] = useState("")
      const { address, isConnected } = useAccount()

      const {
        write,
        isLoading,
        error: writeError,
      } = useContractWrite({
        addressOrName: contractAddress,
        contractInterface: contractABI,
        functionName: "adjustRates",
        args: [newRate ? BigInt(newRate) : 0],
        onSettled(data, error) {
          if (error) {
            console.error("Transaction Error:", error)
            setError(error.message || "Adjustment failed.")
          } else {
            console.log("Transaction Success:", data)
            setError("")
          }
        },
      })

      const handleAdjustRates = async () => {
        setError("") 
        if (!isConnected) {
          setError("Please connect your wallet first.")
          return
        }

        if (!newRate) {
          setError("Please enter a new rate.")
          return
        }

        try {
          const tx = await write()
          console.log("Transaction initiated:", tx)
        } catch (err) {
          console.error("Adjustment failed:", err)
          setError(err.message || "An unexpected error occurred.")
        }
      }`

` 主要是这个问题,我使用了旧的 useSginer 等,现在它只是不起作用,wagmi 的迁移指南太棒了

javascript ethereum wagmi
1个回答
0
投票
import { writeContract } from '@wagmi/core'
import { abi } from './abi'
import { config } from './config'

const result = await writeContract(config, {
  abi,
  address: '0x6b175474e89094c44da98b954eedeac495271d0f',
  functionName: 'transferFrom',
  args: [
    '0xd2135CfB216b74109775236E36d4b433F1DF507B',
    '0xA0Cf798816D4b9b9866b5330EEa46a18382f251e',
    123n,
  ],
})

config.ts 等等。

import { http, createConfig } from '@wagmi/core'
import { mainnet, sepolia } from '@wagmi/core/chains'

export const config = createConfig({
  chains: [mainnet, sepolia],
  transports: {
    [mainnet.id]: http(),
    [sepolia.id]: http(),
  },
})
© www.soinside.com 2019 - 2024. All rights reserved.