java玩轉區塊鏈-基礎篇-環境搭建
- java環境配置
- 基礎概念
- java環境搭建
- maven包參考
- java代碼
- Ganache
- 執行結果
- 代碼解釋
可以不讓搞,但是不允許你不會
java環境配置
- jdk版本,jdk1.8+;
- IDE環境,ideas;
- 依賴包,web3;
- 區塊鏈網路,開發網,Ganache;
基礎概念
區塊鏈網路: 提供區塊鏈交易的網路,測驗網和正式網由以太坊提供,開發網路可以本地安裝,ganache,Hardhat是兩個可以本地安裝的區塊鏈服務器,ganache是本教程使用的開發區塊鏈服務器,
web3:與區塊鏈進行互動的網路協議,web3目前有python,nodejs,java,typeScript等語言實作
gas:交易手續費,就是我們所說的(挖礦)費,區塊鏈上的所有操作都由網路帶寬存盤的成本,這部分費用需要由交易方提供
java環境搭建
jdk1.8

ideas創建java專案引導
file—>New—>Project

ideas創建java專案,命名 ethJavaLession01
maven包參考
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<repositories>
<repository>
<id>central</id>
<name>central</name>
<url>https://maven.aliyun.com/repository/central</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>jcenter</id>
<name>jcenter</name>
<url>http://jcenter.bintray.com/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>maven-net-cn</id>
<name>Maven China Mirror</name>
<url>https://dl.bintray.com/ethereum/maven</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
java代碼

package com.eth;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.EthBlockNumber;
import org.web3j.protocol.core.methods.response.EthGasPrice;
import org.web3j.protocol.core.methods.response.Web3ClientVersion;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
public class EthTest {
public static void main(String[] args) {
System.out.println("Connecting to Ethereum ...");
Web3j web3 = Web3j.build(new HttpService("HTTP://127.0.0.1:7545"));
System.out.println("Successfuly connected to Ethereum");
try {
// web3_clientVersion returns the current client version.
Web3ClientVersion clientVersion = web3.web3ClientVersion().send();
// eth_blockNumber returns the number of most recent block.
EthBlockNumber blockNumber = web3.ethBlockNumber().send();
// eth_gasPrice, returns the current price per gas in wei.
EthGasPrice gasPrice = web3.ethGasPrice().send();
// Print result
System.out.println("Client version: " + clientVersion.getWeb3ClientVersion());
System.out.println("Block number: " + blockNumber.getBlockNumber());
System.out.println("Gas price: " + gasPrice.getGasPrice());
} catch (IOException ex) {
throw new RuntimeException("Error whilst sending json-rpc requests", ex);
}
}
}
Ganache
Ganache為可以本地啟動的開發區塊鏈服務器
ganache官網地址




執行結果
Successfuly connected to Ethereum
Client version: EthereumJS TestRPC/v2.13.1/ethereum-js
Block number: 0
Gas price: 20000000000
代碼解釋
Web3j web3 = Web3j.build(new HttpService(“HTTP://127.0.0.1:7545”))
連接區塊鏈服務器
Web3ClientVersion clientVersion = web3.web3ClientVersion().send();
客戶端版本號
EthBlockNumber blockNumber = web3.ethBlockNumber().send();
獲取當前區塊鏈中塊個數
EthGasPrice gasPrice = web3.ethGasPrice().send();
獲取gas值
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/304379.html
標籤:區塊鏈
