首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用web3j在ethereum中部署智能契约

如何使用web3j在ethereum中部署智能契约
EN

Ethereum用户
提问于 2018-12-22 11:38:40
回答 1查看 1.3K关注 0票数 1

我正在学习本教程教程

在契约包装器一节中,我遵循了相同的步骤,但我在这里获得了<#>Working,这是完整的堆栈跟踪

代码语言:javascript
复制
java.lang.RuntimeException: java.lang.NoSuchMethodException: smartcontract.Counter1.(java.lang.String, org.web3j.protocol.Web3j, org.web3j.crypto.Credentials, org.web3j.tx.gas.ContractGasProvider)
    at org.web3j.tx.Contract.deploy(Contract.java:366)
    at org.web3j.tx.Contract.deploy(Contract.java:403)
    at org.web3j.tx.Contract.lambda$deployRemoteCall$5(Contract.java:426)
    at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:30)
    at smartcontract.CounterDemo.deployContract(CounterDemo.java:37)
    at controllers.SmartContractController$anonfun$counterContractDemo$1.apply(SmartContractController.scala:64)
    at controllers.SmartContractController$anonfun$counterContractDemo$1.apply(SmartContractController.scala:58)

这是我的代码

代码语言:javascript
复制
public static RemoteCall deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        return deployRemoteCall(Counter1.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
    }

    public static RemoteCall deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        return deployRemoteCall(Counter1.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
    }

    public static Counter1 load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
        return new Counter1(contractAddress, web3j, credentials, gasPrice, gasLimit);
    }

    public static Counter1 load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
        return new Counter1(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
    }

CounterDemo.java

代码语言:javascript
复制
public class CounterDemo {

    public String deployContract() {
        String deploy = "";
        BigInteger GAS_LIMIT = BigInteger.valueOf(6721975L);
        BigInteger GAS_PRICE = BigInteger.valueOf(20000000000L);
        try {

            Web3j web3j = Web3j.build(new org.web3j.protocol.http.HttpService("http://localhost:8080"));
            Credentials credentials = WalletUtils.loadCredentials("pswd",
                    "path to privatekey/UTC--2018-12-15T12-06-10.************************************1");
            System.out.println("credentails address " + credentials.getAddress());
            Counter1 counter = Counter1.deploy(web3j, credentials, GAS_PRICE,
                    GAS_LIMIT ).send();

            //Counter.deploy(web3j, credentials, GAS_PRICE,GAS_LIMIT);
            deploy = counter.getContractAddress();
            System.out.println("address is {}" + deploy);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } // defaults to http://localhost:8545/
        catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return deploy;
    }

}

下面是play控制器操作,我将根据该操作获得一个已部署的契约地址。

代码语言:javascript
复制
def counterContractDemo() = Action {
    val web3j = Web3j.build(new HttpService("http://localhost:8080/"))
    val CounterDemo = new CounterDemo();

    Ok(CounterDemo.deployContract())
  }

我使用的是运行框架2.3.4,并通过了正确数量的参数,以便使用正确的参数

EN

回答 1

Ethereum用户

发布于 2018-12-26 17:43:53

代码语言:javascript
复制
protected Counter1(String contractAddress, Web3j web3j, TransactionManager transactionManager, ContractGasProvider gasProvider) {
    super(BINARY, contractAddress, web3j, transactionManager, gasProvider);
}

如果我记得很好,我就有了这个问题,并通过将它添加到生成的java类中来解决它。

票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/64479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档