Web3j Transfer.sendFunds() 返回错误“gas 资金不足 * 价格 + 价值”

Arg*_*rgV 5 java blockchain ethereum go-ethereum web3-java

当在我的私人测试区块链中使用 web3j 库进行区块链交易时,我当前遇到标题为响应错误:*gas 资金不足*价格+价值*

我想要转入一些以太币的账户余额为 10000 以太币。我注销的Gas价格的值为18000000000作为 BigInt (它是 WEI?),并且Gas 限制是 web3j 使用的默认值,值为21000

所以问题是为什么我的交易不起作用?例如我想转账 10 以太币:

TransactionReceipt transactionReceipt = Transfer.sendFunds(web3, credentials, toAccount, BigDecimal.valueOf(10.0), Convert.Unit.ETHER).send();
Run Code Online (Sandbox Code Playgroud)

更多细节

创世文件如下所示:

{
  "config": {
      "chainId": 9999,
      "homesteadBlock": 0,
      "eip155Block": 0,
      "eip158Block": 0,
      "byzantiumBlock": 0
   },
   "difficulty": "400",
   "gasLimit": "2100000",
   "alloc": {
      "0x9b6301bf2cfe11066dbd641f91a2b82e0783130e": { 
          "balance": "100000000000000000000000" 
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

代码如下所示:

// create new account 
Admin admin = Admin.build(new HttpService());
NewAccountIdentifier newAccount = admin.personalNewAccount("PASSWORD").send();

// get current created account
Web3j web3 = Web3.build(new HttpService());
EthAccounts accounts = web3.ethAccounts().send();
String lastAccount = Iterables.getLast(accounts.getAccounts());
// get creadentials for the first account having some ether
String firstAccount = web3.ethAccounts().send().getAccounts().get(0);
Credentials credentials = Credentials.create(firstAccount);

// get current balance for first account
EthGetBalance balance = admin.ethGetBalance(firstAccount, DefaultBlockParameterName.LATEST).send();
BigDecimal balanceVaue = Convert.fromWei(balance.getBalance().toString(), Convert.Unit.ETHER);

// create transaction to give the new created account some ether from the first one
// log some stuff
System.out.println("Account: " + firstAccount);
System.out.println("Account balance: " + balanceVaue);
System.out.println("Gas Price admin.ethGasPrice() in Ether: " + Convert.fromWei(gasPrice.toString(), Convert.Unit.ETHER));
System.out.println("Transfer Gas Limit in Ether: " + Convert.fromWei(Transfer.GAS_LIMIT.toString(), Convert.Unit.ETHER));
System.out.println("Transfer Gas Price in Ether: " + Convert.fromWei(Transfer.GAS_PRICE.toString(), Convert.Unit.ETHER));

TransactionReceipt transactionReceipt = Transfer.sendFunds(web3, credentials, lastAccount, BigDecimal.valueOf(10.0), Convert.Unit.ETHER).send();
String transactionHash = transactionReceipt.getTransactionHash();
Run Code Online (Sandbox Code Playgroud)

这就导致了上面的交易错误:gas资金不足*价格+价值

这是日志输出:

Account: 0x9b6301bf2cfe11066dbd641f91a2b82e0783130e
Account balance: 100000
Gas Price admin.ethGasPrice() in Ether: 1.8E-8
Transfer Gas Limit in Ether: 2.1E-14
Transfer Gas Price in Ether: 2.2E-8
Funds transfer triggered ...
Run Code Online (Sandbox Code Playgroud)

Ada*_*nis 6

你的Credentials对象不正确。您使用的方法需要私钥,而不是以太坊地址。

Credentials credentials = Credentials.create(firstAccount);

应该

Credentials credentials = Credentials.create("<PRIVATE_KEY_HERE>");

  • @ArgV 如果这个答案解决了您的问题,您可以通过将答案标记为已接受(单击灰色勾号)来表明该答案 (3认同)