以太坊私链 利用Java 发布智能合约
使用前需要将合约转换为java 代码
详见:https://blog.csdn.net/Keith003/article/details/81873154
创建maven 项目
pom.xml 这里版本号需要根据生成java 文档得工具进行修改
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>3.3.1</version>
</dependency>
启动本地私链:https://blog.csdn.net/Keith003/article/details/80178739
启动方式需要增加:
--rpc --rpcaddr "0.0.0.0" --rpccorsdomain "*" --rpcapi="db,eth,net,web3,personal,web3"
java 代码
Web3j web3j = Web3j.build(new HttpService("http://127.0.0.1:8545/"));
Credentials credentials = WalletUtils.loadCredentials(
"账户密码",
"账户文件全路径"); // 第一个变量填入账户的密码,第二个变量填入账户文件的 path
RemoteCall<Futurechain> deploy = Futurechain.deploy(web3j, credentials, web3j.ethGasPrice().send().getGasPrice(), GAS_LIMIT);
Futurechain send = deploy.send();
System.out.println(send.getContractAddress());//返回合约地址
注:以太坊解决Error: exceeds block gas limit undefined的问题
智能合约部署时,可能会出现Error: exceeds block gas limit undefined
的报错信息,表示当前合约所需的gas超过了区块的最大gas。这可能是由于创世区块的配置文件genesis.json
中的参数gasLimit
设置过小有关,以下是可能的解决办法。
1、将创世区块的配置文件genesis.json
中的参数gasLimit
设置为0xffffffff
(十进制值为4294967295
)。重新初始化,生成区块,查看区块信息。
2、使用--targetgaslimit value
就可以设置最低gas
限制。value
传gas
最低值,默认4712388
。
还没有评论,来说两句吧...