实现功能:获取合约event数据(相当于日志)。
中文文档
目前我找的比较好的文档是 汇智网 的,java以太坊库web3j文档
搭建项目
Springboot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
web3j依赖
<!--Java 操作智能合约 开始-->
<!--web3j-spring-boot-starter使用的web3j版本为3.x。本项目使用web3j的4.x版本-->
<!-- <dependency>-->
<!-- <groupId>org.web3j</groupId>-->
<!-- <artifactId>web3j-spring-boot-starter</artifactId>-->
<!-- <version>1.6.0</version>-->
<!-- </dependency>-->
<!-- https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib -->
<!--springboot 2.3.4 不需要,2.1.7 需要 -->
<!-- <dependency>-->
<!-- <groupId>org.jetbrains.kotlin</groupId>-->
<!-- <artifactId>kotlin-stdlib</artifactId>-->
<!-- <version>1.3.70</version>-->
<!-- </dependency>-->
<!--okhttp与logging-interceptor可根据项目实际情况选择是否添加-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>4.3.1</version>
</dependency>
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.7.0</version>
</dependency>
<!--Java 操作智能合约 结束-->
fasterxml依赖
<!--这个依赖可根据项目实际情况选择是否添加-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
web3j maven plugin
我们把合约文件(.sol)放在resources目录下,运行插件,即可生成合约对应的Java类。这个插件会根据你的合约版本自动下载对应的solidity编译器,真正实现一键生成合约java类,非常好用,老外就是牛皮。
<!--mvn web3j:generate-sources-->
<plugin>
<groupId>org.web3j</groupId>
<artifactId>web3j-maven-plugin</artifactId>
<version>4.6.5</version>
<configuration>
<packageName>com.contract</packageName>
<sourceDestination>src/main/java/generated</sourceDestination>
<nativeJavaType>true</nativeJavaType>
<outputFormat>java,bin</outputFormat>
<soliditySourceFiles>
<directory>src/main/resources</directory>
<includes>
<include>**/*.sol</include>
</includes>
</soliditySourceFiles>
<outputDirectory>
<java>src/java/generated</java>
<bin>src/bin/generated</bin>
<abi>src/abi/generated</abi>
</outputDirectory>
<contract>
<!--<includes>-->
<!-- <include>Chip</include>-->
<!--</includes>-->
<!--<excludes>-->
<!-- <exclude>Hello</exclude>-->
<!-- <exclude>Chip</exclude>-->
<!--</excludes>-->
</contract>
<pathPrefixes>
<pathPrefix>dep=../dependencies</pathPrefix>
</pathPrefixes>
</configuration>
</plugin>
创建event过滤器
package com.fc.task.contract.config;
import com.fc.task.contract.contract.Chip;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.http.HttpService;
import org