Springboot+web3j(4.7)+实战+填坑

本文介绍了如何在Springboot项目中使用web3j库来实现以太坊智能合约的事件(event)监听。通过集成web3j插件自动生成合约Java类,并创建事件过滤器和监听器,确保合约日志数据的实时获取。同时,为防止数据丢失,采用定时任务每5秒从etherscan.io API获取一次event数据,需注册并获取API key。
摘要由CSDN通过智能技术生成

实现功能:获取合约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
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一万次练习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值