java玩转区块链-基础篇-solidity语法-基础类型
Posted 链霓裳
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java玩转区块链-基础篇-solidity语法-基础类型相关的知识,希望对你有一定的参考价值。
java玩转区块链-基础篇-solidity语法-基础类型
可以不让搞,但是不允许你不会
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玩转区块链-基础篇-solidity语法-基础类型的主要内容,如果未能解决你的问题,请参考以下文章