由于最近做的项目是关于波场链的,所以写一下
波场链和以太坊有很大的不同,它没有开源节点,只能使用私有节点或者波场官方的节点,官方推荐的java客户端是trident-java。
1、安装trident-java
trident-java没有现成的maven包,需要用GitHub源码自行打包,Github源码地址:https://github.com/tronprotocol/trident
要编译打包trident项目,需要java 1.8.0_231版本,和Gradle 5.6.4版本,java和gradle的安装,请自行百度,这里就不多说。
将源码下载下来后,解压,然后需要把这些依赖包加入build.gradle配置文件中:
dependencies {
// protobuf & grpc
implementation 'com.google.protobuf:protobuf-java:3.11.0'
implementation 'io.grpc:grpc-netty-shaded:1.31.0'
implementation 'io.grpc:grpc-netty:1.31.0'
implementation 'io.grpc:grpc-protobuf:1.31.0'
implementation 'io.grpc:grpc-stub:1.31.0'
implementation "org.bouncycastle:bcprov-jdk15on:1.68"
implementation fileTree(dir:'../core')
implementation fileTree(dir:'../utils')
implementation fileTree(dir:'../abi')
//if you are using the *.jar files, ues the following line
implementation fileTree(dir:'your path', include: '*.jar')
implementation 'com.google.guava:guava:28.0-jre'
}
配置好之后,进入解压后的目录,运行命令:
gradle install
这个命令会直接将打包好的jar包,安装进本地maven仓库中,于是在本地的maven项目,就可以引用依赖:
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>abi</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>utils</artifactId>
<version>0.8.0</version>
</dependency>
<dependency>
<groupId>org.tron.trident</groupId>
<artifactId>core</artifactId>
<version>0.8.0</version>
</dependency>
2、使用trident-java
配置好maven依赖之后,就可以使用了,在trident-java中,ApiWrapper是访问波场api以及智能合约方法的入口:
可以有几种方法创建入口实体:
(1)如果你有自己的私有节点,那就使用这个方法
ApiWrapper wrapper = new ApiWrapper("grpc endpoint", "solidity grpc endpoint", "hex private key");
(2)如果你要使用波场公开的主网节点,需要去TronGrid注册一个ApiKey,并通过以下方式创建,TronGrid网址: https://www.trongrid.io/
ApiWrapper wrapper = ApiWrapper.ofMainnet("hex private key", "API key");
(3)对于只获取数据,非交易类型的接口,并不需要private key,那么"private key"可以随意填写64个字母长度的字符串替代
ApiWrapper wrapper = ApiWrapper.ofMainnet("64 characters", "API key");
这里写一个小应用案例,获取某个hash的交易详情:
public static void main(String[] args) throws IllegalException {
String privateKey = "c71fcbba5e6e23944a9a339e4368afbb5549a16a348b30fdd91dfb05cfff6d05";
String apiKey = "be40fbd3-0fa7-49ba-98fb-ddca96398ca8";
String hash = "b989f26676e951906854f43315505275aa43c24e84a6f0060d3214961234d067";
ApiWrapper apiWrapper = ApiWrapper.ofMainnet(privateKey, apiKey);
Chain.Transaction transaction = apiWrapper.getTransactionById(hash);
System.out.println("transaction: " + JSONUtil.toJsonStr(transaction));
}
- 《Battle Gang》:超能萌宠,团队欢乐竞技新体验!
- 最新版本iPhoneSE将在2025年3月发布 依然搭载苹果5G调制解调器
- Bungie射击新作《Marathon》上架Steam页面
- 《Save Your Nuts》:坚果保卫战,智斗与趣味的碰撞
- Google Play 上架审核标准又收紧,换种解题思路行不行?
- 《糖豆人》与《星球大战》联动,4款IP皮肤上线
- 第一个高速6G原型设备推出 可达到100Gbps速度
- 苹果2024首场发布会时间将为“35分钟左右”?
- iPhone16会有全新的“沙漠钛色”和“钛灰色”选择
- 新版iPadmini要来了 苹果5月7日即将举行新品活动
- 苹果再次和OpenAI进行谈判后者提供人工智能技术给iPhone等产品
- 苹果推出的12.9英寸iPad Air将采用LCD屏