尝试使用Web3j部署以太坊智能合约时出错

时间:2018-07-13 16:42:14

标签: java ethereum web3-java

我正在尝试使用web3j和Ganache在Java中部署智能合约。 为合同创建Java包装器后,用于部署合同的代码为:

public void deployGreeter(String pw, String walletFile, String _greeting) throws IOException, CipherException, Exception{
    Credentials credentials = WalletUtils.loadCredentials(pw, walletFile);
    Greeter g = Greeter.deploy(web3, credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, _greeting).send();

}

但是我遇到以下错误:

Exception in thread "main" java.lang.RuntimeException: 
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: 
buffer(okhttp3.internal.http1.Http1Codec$ChunkedSource@10aa41f2).inputStream(); line: 1, column: 188] (through reference chain: org.web3j.protocol.core.methods.response.EthSendTransaction["error"]->org.web3j.protocol.core.Response$Error["data"])
at org.web3j.tx.Contract.deploy(Contract.java:302)
at org.web3j.tx.Contract.lambda$deployRemoteCall$5(Contract.java:334)
at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
at com.monsanto.ethereumtest1.Ganache.deployGreeter(Ganache.java:125)
at com.monsanto.ethereumtest1.Main.main(Main.java:61)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: buffer(okhttp3.internal.http1.Http1Codec$ChunkedSource@10aa41f2).inputStream(); line: 1, column: 188] (through reference chain: org.web3j.protocol.core.methods.response.EthSendTransaction["error"]->org.web3j.protocol.core.Response$Error["data"])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:270)
at com.fasterxml.jackson.databind.DeserializationContext.reportMappingException(DeserializationContext.java:1234)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1122)
at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1075)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:60)
at com.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:101)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:499)
at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:101)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:276)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:140)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3798)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2908)
at org.web3j.protocol.Service.send(Service.java:33)
at org.web3j.protocol.core.Request.send(Request.java:71)
at org.web3j.tx.RawTransactionManager.signAndSend(RawTransactionManager.java:107)
at org.web3j.tx.RawTransactionManager.sendTransaction(RawTransactionManager.java:91)
at org.web3j.tx.TransactionManager.executeTransaction(TransactionManager.java:49)
at org.web3j.tx.ManagedTransaction.send(ManagedTransaction.java:83)
at org.web3j.tx.Contract.executeTransaction(Contract.java:242)
at org.web3j.tx.Contract.create(Contract.java:271)
at org.web3j.tx.Contract.deploy(Contract.java:300)
... 4 more

我也尝试过部署另一个合同,但是错误输出是相同的。 我做错什么了吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

使用Adam Kippis的建议,我设法部署了合同,并基于Credentials.create方法创建了新的凭据。

请在下面查看对我有用的代码:

public String deployContract(String privateKey, String _lotNumber, int _weight) throws Exception{
    BigInteger biWeight = BigInteger.valueOf(_weight);
    Credentials credentials = Credentials.create(privateKey);
    cst = CST.deploy(web3, credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, _lotNumber, biWeight).send();
    String ca = cst.getContractAddress();

    System.out.println("Contract deployed! Address: " + ca);
    return ca;
}

谢谢。