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