简体   繁体   English

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

[英]Error when trying to deploy Ethereum smart contract using web3j

I'm trying to deploy a smart contract in java using web3j and Ganache. 我正在尝试使用web3j和Ganache在Java中部署智能合约。 After the creation of the java wrapper for my contract, the code that I'm using to deploy the contract is: 在为合同创建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();

}

But I'm getting the following error: 但我收到以下错误:

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

I've also tried to deploy another contract, but the error output is the same. 我也尝试过部署另一个合同,但是错误输出是相同的。 Am I doing anything wrong? 我做错什么了吗?

Thank you. 谢谢。

Using the suggestion of Adam Kippis , I managed to deploy the contract creating a new credential based on the Credentials.create method. 根据Adam Kippis的建议,我设法部署了合约,并基于Credentials.create方法创建了一个新的凭证。

Please see bellow the code that worked for me: 请在下面查看对我有用的代码:

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

Thank you. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM