1

我正在尝试使用 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

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

谢谢你。

4

1 回答 1

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;
}

谢谢你。

于 2018-07-16T17:19:49.940 回答