0

我遇到错误“处理事务请求时出错:处理事务时出现 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

}
4

2 回答 2

0

看起来这是一个与 Ganache 相关的问题https://github.com/trufflesuite/ganache-cli/issues/465

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

于 2020-03-09T10:01:49.500 回答
0

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

于 2020-03-09T14:47:58.313 回答