You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用web3 v0.20.2转账自定义ERC20代币时遇无效地址错误求助

Fixing "invalid address" Error When Transferring ERC20 Tokens with web3.js 0.20.2

I’ve run into this exact issue with web3.js 0.20.x before—even though web3.isAddress() says your target address is valid, the underlying formatter still throws an error. Let’s break down the most likely causes and fixes:

1. Checksum Address Mismatch

The web3.isAddress() method in 0.20.x only validates the basic address format (42 hex characters starting with 0x), but the inputAddressFormatter used when sending transactions enforces strict checksum validation. So even if your address is syntactically correct, if it’s not in the proper checksum case format, you’ll get the error.

Fix:
Convert your target address to a checksum address using web3.toChecksumAddress():

var to = "0x20....";
var checksumTo = web3.toChecksumAddress(to);

2. Missing Explicit from Parameter

When calling write methods like transfer(), web3.js 0.20.x tries to use the node’s default account if you don’t specify a from parameter. If that default account is missing or invalid, it will throw an "invalid address" error that looks like it’s related to your target address.

Fix:
Explicitly pass the sender’s address (your owner variable) in the options object, and make sure it’s also a checksum address:

var checksumOwner = web3.toChecksumAddress(owner);
myContractInstance.transfer(checksumTo, value, { from: checksumOwner }, (err, res) => {
  console.log(err, res);
});

3. Verify Your ABI is Correct

Even though you can read totalSupply() and balanceOf(), a misdefined ABI for the transfer function can cause parameter formatting issues that trigger address errors. Double-check that your ABI matches the contract’s transfer method exactly.

Example Correct ABI Snippet:

{
  "constant": false,
  "inputs": [
    { "name": "_to", "type": "address" },
    { "name": "_value", "type": "uint256" }
  ],
  "name": "transfer",
  "outputs": [{ "name": "success", "type": "bool" }],
  "payable": false,
  "type": "function"
}

Bonus: Don’t Forget Token Decimal Places

One common related pitfall: the value parameter in transfer() expects the number of token wei units (not full tokens). If your token uses 18 decimals (standard), 10 full tokens would be web3.toWei(10, 'ether')—not just 10. Using raw integers here will send tiny fractions of a token instead of the amount you intend.

Full Fixed Code Example

Putting it all together:

var myContract = web3.eth.contract(abi)
var myContractInstance = myContract.at(address);
var total = myContractInstance.totalSupply();
var balance = myContractInstance.balanceOf(owner);
console.log('total', total);
console.log('balance', balance);

var to = "0x20....";
var checksumTo = web3.toChecksumAddress(to);
var isAddress = web3.isAddress(checksumTo);
console.log(isAddress);

var checksumOwner = web3.toChecksumAddress(owner);
// Adjust value to match your token's decimal places (18 here as example)
var value = web3.toWei(10, 'ether');

myContractInstance.transfer(checksumTo, value, { from: checksumOwner }, (err, res) => {
  if (err) {
    console.error("Transaction failed:", err);
  } else {
    console.log("Transaction sent! Hash:", res);
  }
});

内容的提问来源于stack exchange,提问作者rachel_hong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:51:45