web3j 智能合约部署

智能合约部署流程预备事项:

1、已创建一个钱包地址,并且有足够的eth

2、智能合约编译生成对应Java类

3、代码实现

 

测试eth充值地址:https://faucet.ropsten.be/

部署代码:

    public void deploy(){
        Web3j web3j = Web3j.build(new HttpService("https://ropsten.infura.io/v3/xxxxxxxxx"));
        Credentials credentials = EthPublic.credentials("密码", "创建钱包时生成的json文件路径");
        BigInteger bigInteger = this.gasPrice(); //获取gas
        RemoteCall deploy = Test.deploy(web3j, credentials, new StaticGasProvider(bigInteger, new BigInteger("3000000")));
        try {
            //Test为智能合约编译生成的Java类
            Test send = deploy.send();
            String contractAddress = send.getContractAddress(); //获取智能合约地址
            System.out.println("==========contractAddress=========" + contractAddress);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

你可能感兴趣的:(区块链,区块链,java,json,maven)