java 调用区块链 发布和调用智能合约

java连接区块链 很简单 ,调用智能合约要麻烦一些.

先说连接 区块链查询数据. 

1 maven 项目导入 web3j 的依赖.

	<dependency>
		    <groupId>org.web3j</groupId>
		    <artifactId>core</artifactId>
		    <version>4.5.0</version>
		</dependency>

  

2 需要 链上开启 rpc 端口 . 不开启 本地可以通过 指定  geth.ipc 连接. 

package com.cxygg.testweb3j;

import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.response.EthGetBalance;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;

public class Web3jDemo2 {

	private static final String RPC_URL = "http://192.168.1.199:9001";
	private static final Web3j web3j = Web3j.build(new HttpService(RPC_URL));

	public static void main(String[] args) throws Exception {

		getAge();

	}

	public static void getAge() throws Exception {
		// 这里要填写真实的钱包地址
		EthGetBalance ethGetBalance = web3j
				.ethGetBalance("0xbcbbd4e9bbf41a149652da55c42a4b28b6e39599", DefaultBlockParameterName.LATEST).send();

		if (ethGetBalance != null) {
			System.out.println("余额:" + Convert.fromWei(ethGetBalance.getBalance().toString(), Convert.Unit.ETHER));
		}

	}

}

  

发布和调用智能合约部分

调用智能合约通过 abi 文件  和  合约二进制文件生成 java的 代理类,然后调用. 下次在写.

猜你喜欢

转载自www.cnblogs.com/cxygg/p/11507354.html
今日推荐