USDT 转账 – 合约函数 “transfer” 未返回数据 (“0x”)

huangapple go评论90阅读模式
英文:

USDT transfer - The contract function "transfer" returned no data ("0x")

问题

我正在使用wagmi和viem来调用usdt合约。我想将usdt从一个地址转移到另一个地址。我收到了以下错误信息:

App.jsx:51 ContractFunctionExecutionError: 合约函数"transfer"未返回任何数据("0x")。

这可能是由以下原因之一引起的:

  • 合约没有"transfer"函数,
  • 传递给合约函数的参数可能无效,或者
  • 地址不是一个合约。

合约调用:
地址:0xdAC17F958D2ee523a2206206994597C13D831ec7
函数:transfer(address recipient, uint256 amount)
参数:(0x04dAb01aA15Dc4e57910420f7464bFC68579b2d2, 10000000)
发送者:0x00d2ADa3365e40C05E8772e58B9aAcf356F3E474

文档:https://viem.sh/docs/contract/simulateContract.html
版本:viem@1.2.15
在getContractError.ts的getContractError函数处发生错误(getContractError.ts:55:10)
在simulateContract.ts的simulateContract函数处模拟合约(simulateContract.ts:159:11)
在async prepareWriteContract (chunk-LAFZBYO7.js:2078:31)处准备写入合约
在async writeContract (chunk-LAFZBYO7.js:2354:17)处写入合约

这是最小的重现,您需要npm install @web3modal/ethereum @web3modal/react wagmi viem

此外,以下代码段可以快速创建一个vite项目:npm create vite@latest viem --template react

import {
  EthereumClient,
  w3mConnectors,
  w3mProvider,
} from "@web3modal/ethereum";
import { Web3Modal } from "@web3modal/react";
import { configureChains, createConfig, WagmiConfig } from "wagmi";
import { mainnet } from "wagmi/chains";
import { Web3Button } from "@web3modal/react";
import { useContractWrite, erc20ABI } from "wagmi";
import { parseUnits } from "viem";
import { useState } from "react";

const chains = [mainnet];
const projectId = "ef4a2eaeff8be83c88ffd2dc3850a66c";

const { publicClient } = configureChains(chains, [w3mProvider({ projectId })]);
const wagmiConfig = createConfig({
  autoConnect: true,
  connectors: w3mConnectors({ projectId, chains }),
  publicClient,
});
const ethereumClient = new EthereumClient(wagmiConfig, chains);

function App() {
  return (
    <>
      <WagmiConfig config={wagmiConfig}>
        <Web3Button />
        <br />
        <br />
        <SendUsdt />
      </WagmiConfig>

      <Web3Modal projectId={projectId} ethereumClient={ethereumClient} />
    </>
  );
}

function SendUsdt() {
  const [amount, setAmount] = useState(0);

  const { write } = useContractWrite({
    // usdt contract address
    address: "0xC230abdFA22e563C7cD6435b24C15Fa9B913ccc2",
    abi: erc20ABI,
    functionName: "transfer",
    args: [
      "0x04dAb01aA15Dc4e57910420f7464bFC68579b2d2",
      parseUnits(`${amount}`, 6),
    ],
  });

  return (
    <>
      <input type="number" onChange={(e) => setAmount(e.target.value)} />
      <button onClick={() => write()}>Transfer</button>
    </>
  );
}

export default App;
英文:

I'm using wagmi and viem to call usdt contract. I want to transfer usdt from one address to another. I get this error:

App.jsx:51 ContractFunctionExecutionError: The contract function "transfer" returned no data ("0x").

This could be due to any of the following:

  • The contract does not have the function "transfer",
  • The parameters passed to the contract function may be invalid, or
  • The address is not a contract.

Contract Call:
address: 0xdAC17F958D2ee523a2206206994597C13D831ec7
function: transfer(address recipient, uint256 amount)
args: (0x04dAb01aA15Dc4e57910420f7464bFC68579b2d2, 10000000)
sender: 0x00d2ADa3365e40C05E8772e58B9aAcf356F3E474

Docs: https://viem.sh/docs/contract/simulateContract.html
Version: viem@1.2.15
at getContractError (getContractError.ts:55:10)
at simulateContract (simulateContract.ts:159:11)
at async prepareWriteContract (chunk-LAFZBYO7.js:2078:31)
at async writeContract (chunk-LAFZBYO7.js:2354:17)

This is the minimum repro, you will need to npm install @web3modal/ethereum @web3modal/react wagmi viem.

Here is this snippet too to create a vite project quickly: npm create vite@latest viem --template react

import {
  EthereumClient,
  w3mConnectors,
  w3mProvider,
} from &quot;@web3modal/ethereum&quot;;
import { Web3Modal } from &quot;@web3modal/react&quot;;
import { configureChains, createConfig, WagmiConfig } from &quot;wagmi&quot;;
import { mainnet } from &quot;wagmi/chains&quot;;
import { Web3Button } from &quot;@web3modal/react&quot;;
import { useContractWrite, erc20ABI } from &quot;wagmi&quot;;
import { parseUnits } from &quot;viem&quot;;
import { useState } from &quot;react&quot;;

const chains = [mainnet];
const projectId = &quot;ef4a2eaeff8be83c88ffd2dc3850a66c&quot;;

const { publicClient } = configureChains(chains, [w3mProvider({ projectId })]);
const wagmiConfig = createConfig({
  autoConnect: true,
  connectors: w3mConnectors({ projectId, chains }),
  publicClient,
});
const ethereumClient = new EthereumClient(wagmiConfig, chains);

function App() {
  return (
    &lt;&gt;
      &lt;WagmiConfig config={wagmiConfig}&gt;
        &lt;Web3Button /&gt;
        &lt;br /&gt;
        &lt;br /&gt;
        &lt;SendUsdt /&gt;
      &lt;/WagmiConfig&gt;

      &lt;Web3Modal projectId={projectId} ethereumClient={ethereumClient} /&gt;
    &lt;/&gt;
  );
}

function SendUsdt() {
  const [amount, setAmount] = useState(0);

  const { write } = useContractWrite({
    // usdt contract address
    address: &quot;0xC230abdFA22e563C7cD6435b24C15Fa9B913ccc2&quot;,
    abi: erc20ABI,
    functionName: &quot;transfer&quot;,
    args: [
      &quot;0x04dAb01aA15Dc4e57910420f7464bFC68579b2d2&quot;,
      parseUnits(`${amount}`, 6),
    ],
  });

  return (
    &lt;&gt;
      &lt;input type=&quot;number&quot; onChange={(e) =&gt; setAmount(e.target.value)} /&gt;
      &lt;button onClick={() =&gt; write()}&gt;Transfer&lt;/button&gt;
    &lt;/&gt;
  );
}

export default App;

答案1

得分: 0

"USDT的ABI并不完全遵循erc20ABI(见下文)。请确保您使用正确的ABI。"

[
    {
        "constant": false,
        "inputs": [
            {
                "name": "_to",
                "type": "address"
            },
            {
                "name": "_value",
                "type": "uint256"
            }
        ],
        "name": "transfer",
        "outputs": [
-            {
-                "name": "",
-                "type": "bool"
-            }
        ],
        "payable": false,
        "stateMutability": "nonpayable",
        "type": "function"
    },
]
英文:

From https://github.com/wagmi-dev/wagmi/issues/2749#issuecomment-1638200817

"USDTs ABI does not exactly follow the erc20ABI (see below). Make sure you either use the correct ABI."

[
    {
        &quot;constant&quot;: false,
        &quot;inputs&quot;: [
            {
                &quot;name&quot;: &quot;_to&quot;,
                &quot;type&quot;: &quot;address&quot;
            },
            {
                &quot;name&quot;: &quot;_value&quot;,
                &quot;type&quot;: &quot;uint256&quot;
            }
        ],
        &quot;name&quot;: &quot;transfer&quot;,
        &quot;outputs&quot;: [
-            {
-                &quot;name&quot;: &quot;&quot;,
-                &quot;type&quot;: &quot;bool&quot;
-            }
        ],
        &quot;payable&quot;: false,
        &quot;stateMutability&quot;: &quot;nonpayable&quot;,
        &quot;type&quot;: &quot;function&quot;
    },
]

huangapple
  • 本文由 发表于 2023年7月17日 16:53:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定