NodeJS 从一个账户向另一个账户转移ETH时账户地址无效

0g0grzrc  于 2023-05-06  发布在  Node.js
关注(0)|答案(1)|浏览(123)

我有这个node js代码来从账户1到2转移金额:

const Web3 = require('web3');
const contract = require('truffle-contract');
const TransferContract = contract(require('./build/contracts/Transfer.json'));

async function transferFunds(from, to, amount) {
  // Set up a Web3 provider, e.g. using Ganache or Infura
  const provider = new Web3.providers.HttpProvider('http://127.0.0.1:8545');
  TransferContract.setProvider(provider);

  // Get an instance of the contract deployed at the specified address
  const instance = await TransferContract.deployed();

  // Call the transfer() function on the contract instance
  await instance.transfer(to, amount, { from: from });
}

// Call the transferFunds function with the desired parameters
transferFunds('0xDD6889bcc75510f66566d9f7Ea9C814a5C6043d8', '0xB197d9415Ed9A7ee55d35cAC889C99243c486976', 100);

当我运行代码时,我看到这个错误:
var error = new Error();
错误:无效地址(arg=“account 2”,coderType=“address”,value=100)
原因:'无效地址',
代码:'INVALID_ARGUMENT',
arg:'account2',
coderType:'address',
值:100
另外,我的Solidity代码是这样的:

pragma solidity ^0.8.0;

contract Transfer {
    function transfer(address payable account1, address payable account2, uint amount) public {
        require(address(this).balance >= amount * 2, "Insufficient funds in contract");
        account1.transfer(amount);
        account2.transfer(amount);
    }
}

有人能告诉我是什么问题吗?
欲了解更多信息,我测试它在松露和Ganache网络和两个帐户有足够的信用,是真实的地址在网络中。

kcugc4gi

kcugc4gi1#

我发现了这个问题,在solidity中输入的顺序是不同的,所以我也改变了js脚本中的顺序。

const Web3 = require('web3');
const contract = require('truffle-contract');
const TransferContract = contract(require('./build/contracts/Transfer.json'));

async function transferFunds(from, to, amount) {
  // Set up a Web3 provider, e.g. using Ganache or Infura
  const provider = new Web3.providers.HttpProvider('http://127.0.0.1:8545');
  TransferContract.setProvider(provider);

  // Get an instance of the contract deployed at the specified address
  const instance = await TransferContract.deployed();

  // Call the transfer() function on the contract instance
  await instance.transfer(from, to, amount, { from: from });
}

// Call the transferFunds function with the desired parameters
transferFunds('0xDD6889bcc75510f66566d9f7Ea9C814a5C6043d8', '0xB197d9415Ed9A7ee55d35cAC889C99243c486976', 50);

相关问题