【web3j】Web3j入门

  最近在研究以太坊智能合约,发现以太坊推出了web3.js的nodejs库,对于学Java出身的我而言很是的不习惯。node

  在github中寻找到了Java版本的web3j,在这里与你们分享一下。git

  web3j github地址:https://github.com/web3j/web3jgithub

  web3j 文档地址:https://web3j.readthedocs.io/en/latest/web

  

  一个简单的例子网络

  一、建立一个maven项目maven

  二、增长dependencies测试

  

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

  

  三、申请测试网络ui

    (1)本地私链(geth)编码

    (2)partyspa

    (3)infura

    第一种是本地搭建geth客户端或者搭建私链,这个优势在于所有是本地环境想干吗干吗,搭建私链教程地址:http://blog.csdn.net/chenyufeng1991/article/details/53471576,可是这种方式比较麻烦。

    第二种是一个以太坊的钱包,优势能够存储帐号,便于管理,缺点是须要下载测试或者主网的全部block(主网的block大概40多个G)

    第三种是一个客户端,免费的节点钱包,比较方便。

    本次代码就基于infura来进行

  四、入门编码

    

 1 import org.web3j.protocol.Web3j;
 2 import org.web3j.protocol.core.methods.response.Web3ClientVersion;
 3 import org.web3j.protocol.http.HttpService;
 4 
 5 public class Web3Blog1_1 {
 6     public static void main(String[] args) throws Exception{
 7         Web3j web3 = Web3j.build(new HttpService("https://kovan.infura.io/yXDUNwlNOcx0UJCWjzNr"));
 8         Web3ClientVersion web3ClientVersion = web3.web3ClientVersion().send();
 9         String clientVersion = web3ClientVersion.getWeb3ClientVersion();
10         System.out.println(clientVersion);
11     }
12 }

运行结果:

相关文章
相关标签/搜索