Web3j 使用 Ganache 获取堆栈下溢错误
Posted
技术标签:
【中文标题】Web3j 使用 Ganache 获取堆栈下溢错误【英文标题】:Web3j gets stack underflow error with Ganache 【发布时间】:2020-06-20 05:50:51 【问题描述】:我遇到错误“处理交易请求时出错:处理交易时出现 VM 异常:堆栈下溢”,基本上适用于任何合同。我正在使用 Ganache v2.1.2 和 Web3j 4.5.15。 Ganache CLI v6.9.1(ganache-core:2.10.2)也是如此。我可以毫无问题地使用 Remix IDE 和 Metamask 插件部署合约。
Java 代码:
public class contractCR
static class OSCGasProvider implements ContractGasProvider
public OSCGasProvider()
@Override
public BigInteger getGasPrice(String string)
return Convert.toWei("1", Convert.Unit.GWEI).toBigInteger();
@Override
public BigInteger getGasLimit(String string)
return BigInteger.valueOf(3000000);
@Override
public BigInteger getGasPrice()
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
@Override
public BigInteger getGasLimit()
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
public static void main (String args[])
System.out.println("Connecting to Ethereum ...");
Web3j web3 = Web3j.build(new HttpService("http://localhost:7545"));
System.out.println("Successfuly connected to Ethereum");
try
// web3_clientVersion returns the current client version.
Web3ClientVersion clientVersion = web3.web3ClientVersion().send();
Credentials credentials=Credentials.create(privateKey);
Faucet osc = Faucet.deploy(web3, credentials, new OSCGasProvider()).send();
String contractAddress = osc.getContractAddress();
System.out.println("The contract address is: "+contractAddress);
catch (IOException ex)
throw new RuntimeException("Error while sending json-rpc requests", ex);
catch (Exception ex)
System.out.println(ex.toString());
简单的水龙头合约:
// Version of Solidity compiler this program was written for
pragma solidity ^0.5.12;
// Our first contract is a faucet!
contract Faucet
address payable owner_addr; //the owner address
//initialize the contract
constructor() public
owner_addr=msg.sender;
//contract destructor
modifier owner_allowed
require (msg.sender==owner_addr, "Only contract owner is allowed to call this function");
_;
function destroy() public owner_allowed
selfdestruct(owner_addr);
// Give out ether to anyone who asks
function withdraw(uint withdraw_amount) public
// Limit withdrawal amount
require(withdraw_amount <= 100000000000000000); //0.1ether
// Send the amount to the address that requested it
msg.sender.transfer(withdraw_amount);
// Accept any incoming amount
function () external payable //fallback or default function
【问题讨论】:
【参考方案1】:看起来这是一个与 Ganache 相关的问题 https://github.com/trufflesuite/ganache-cli/issues/465
根据我的测试,它可以很好地运行我本地的 Ganache 2.1.2
【讨论】:
所以,我猜今天没有解决办法。 我不确定您是否尝试过以下操作: 1. 重新启动 ganache 2. 尝试不同的 ganache 版本 3. 使用一个私有 geth 节点而不是 ganache 您的代码可以正常运行,无需停留我认为甘纳许【参考方案2】:我终于找到了问题所在。 Web3j-cli 不接受 Remix-IDE 提供的二进制文件,而只接受“二进制”字段。将仅包含二进制数据的文件作为输入提供给 Web3j-cli 会生成正确的包装器。
【讨论】:
以上是关于Web3j 使用 Ganache 获取堆栈下溢错误的主要内容,如果未能解决你的问题,请参考以下文章
springboot操作以太坊(eth),使用web3j,转账等