英文:
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 "@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;
答案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."
[
{
"constant": false,
"inputs": [
{
"name": "_to",
"type": "address"
},
{
"name": "_value",
"type": "uint256"
}
],
"name": "transfer",
"outputs": [
- {
- "name": "",
- "type": "bool"
- }
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论