java玩轉區塊鏈-基礎篇-solidity語法-基礎型別
- java環境配置
- 代碼準備
- maven
- 完整solidity
- 執行步驟
- 基礎型別
- 布爾型別
- 型別標識:
- 字面常量值:
- 運算子:
- 短路規則:
- example
- 整形
- 型別標識:
- 字面常量值:
- 除法截斷
- 代碼
- 浮點型別
- 地址型別
- 型別標識:
- 字面常量值:
可以不讓搞,但是不允許你不會
java環境配置
- jdk版本,jdk1.8+;
- IDE環境,ideas;
- 依賴包,web3;
- 區塊鏈網路,開發網,Ganache;
代碼準備
以下是本章所有實體的代碼
maven
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>contracts</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.web3j/web3j-unit -->
<dependency>
<groupId>org.web3j</groupId>
<artifactId>web3j-unit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.web3j</groupId>
<artifactId>web3j-maven-plugin</artifactId>
<version>4.6.5</version>
<configuration>
<soliditySourceFiles/>
</configuration>
</plugin>
</plugins>
</build>
<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>
完整solidity
pragma solidity ^0.5.0;
contract TypeDescContract {
constructor() public {
}
function boolTrunc() public view returns (uint){
uint a = 12;
a < 13 || a++ < 14;
return a;
}
function boolTrunc2() public view returns (uint){
uint a = 12;
a < 13 && a++ < 14;
return a;
}
function uintDivTrunc() public view returns (uint) {
uint a = 5/2.5;
return a;
}
function getSelfAddress() public view returns (address){
return msg.sender;
}
}
執行步驟
(1) 啟動ganache
(2) 在專案目錄resources中撰寫合約腳本

(3) 執行腳本轉化java類

雙擊執行插件命令,生成java類,
(4) 部署合約到ganache本地開發鏈
package com.lession.type;
import org.web3j.crypto.Credentials;
import org.web3j.model.ArrayContract;
import org.web3j.model.TypeDescContract;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger;
public class TypeDescMain {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("HTTP://127.0.0.1:7545"));
String pk = "d8673b8d4c7a9345c7cd8bb91574389d29b1e69cdbe0be9ac8fdf10f43e69c55";//(1)
Credentials credentials = Credentials.create(pk);//(2)
System.out.println("Account address: " + credentials.getAddress());
System.out.println("Account ak: " + credentials.getEcKeyPair().getPrivateKey().toString(16));
System.out.println("Account pk: " + credentials.getEcKeyPair().getPublicKey().toString(16));
StaticGasProvider provider = new StaticGasProvider(new BigInteger("20000000000"),new BigInteger("6721975"));
TypeDescContract contract = TypeDescContract.deploy(web3j,credentials,provider).send();//(3)
System.out.println("contractAddress:" + contract.getContractAddress());//(4)
}
}
代碼解釋:
(1) 從ganache中找一個賬戶的私鑰
(2) 通過私鑰生成鑒權資訊
(3) 部署合約
(4) 列印生成鏈上合約地址
(5) rpc呼叫合約
package com.lession.type;
import org.web3j.crypto.Credentials;
import org.web3j.model.TypeDescContract;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.StaticGasProvider;
import java.math.BigInteger;
public class TypeDescClient {
public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService("HTTP://127.0.0.1:7545"));
String pk = "d8673b8d4c7a9345c7cd8bb91574389d29b1e69cdbe0be9ac8fdf10f43e69c55";
Credentials credentials = Credentials.create(pk);
System.out.println("Account address: " + credentials.getAddress());//(1)
System.out.println("Account ak: " + credentials.getEcKeyPair().getPrivateKey().toString(16));
System.out.println("Account pk: " + credentials.getEcKeyPair().getPublicKey().toString(16));
StaticGasProvider provider = new StaticGasProvider(new BigInteger("20000000000"),new BigInteger("6721975"));
TypeDescContract contract = TypeDescContract.load("0x64de7ed9f8a2fe5c6303cc980c7d15ccac873f85",web3j,credentials,provider);//(2)
System.out.println("boolTrunc:" + contract.boolTrunc().send().toString()); //(3)
System.out.println("boolTrunc2:" + contract.boolTrunc2().send().toString());
System.out.println("uintDivTrunc:" + contract.uintDivTrunc().send().toString());
}
}
代碼解釋:
(1) 通過私鑰生成鑒權
(2) 通過合約地址,加載合約
(3) rpc呼叫合約函式
基礎型別
布爾型別
型別標識:
bool
字面常量值:
true 或 false
運算子:
!(邏輯非) ,&&(邏輯與),||(邏輯或),==(等于),!=(不等于),<=(小于等于),<(小于),>=(大于等于),>(大于)
短路規則:
||和&&遵循短路規則,f(x)||g(y)如果f(x)為true,則g(y)不會執行,f(x)&&g(y),如果f(x)為false,則g(y)不會執行,
example
合約代碼
function boolTrunc() public view returns (uint){
uint a = 12;
a < 13 || a++ < 14;
return a;
}
function boolTrunc2() public view returns (uint){
uint a = 12;
a < 13 && a++ < 14;
return a;
}
java代碼
System.out.println("boolTrunc:" + contract.boolTrunc().send().toString());
System.out.println("boolTrunc2:" + contract.boolTrunc2().send().toString());
執行結果:
boolTrunc:12
boolTrunc2:13
boolTrunc中的a++并未執行,boolTrunc2中的a++執行了變成了13
整形
型別標識:
uint,uint8~uint256
字面常量值:
12 --> 為uint256
除法截斷
5/3=1
代碼
solidity
function uintDivTrunc() public view returns (uint) {
uint a = 5/2.5;
return a;
}
java
System.out.println("uintDivTrunc:" + contract.uintDivTrunc().send().toString());
執行結果
uintDivTrunc:2
浮點型別
不支持
地址型別
型別標識:
address
字面常量值:
0x45b6d967bd2962ebb232c2a8065ce9e4abc93aab
地址型別存盤一個 20 位元組的值
java玩轉區塊鏈-基礎篇-賬戶
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/305523.html
標籤:區塊鏈
上一篇:四、Linux磁盤與檔案系統管理
