Web3j使用Ganache出现堆栈下溢错误

问题描述 投票:0回答:2

我基本上在任何合同中都遇到错误“处理事务请求时出错:处理事务时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

}
ethereum web3 remix web3-java ganache
2个回答
0
投票

似乎是与Ganache相关的一个问题https://github.com/trufflesuite/ganache-cli/issues/465

根据我的测试,它可以很好地在我本地的Ganache 2.1.2中运行


0
投票

最后我找出了问题所在。 Web3j-cli不接受Remix-IDE提供的二进制文件,而仅接受“ binary”字段。将仅包含二进制数据的文件作为输入提供给Web3j-cli,可以生成正确的包装器。

© www.soinside.com 2019 - 2024. All rights reserved.