扫块监听逻辑
跑定时任务,每隔固定时间执行一次轮询,这里我采用的间隔时间是距上一次执行完后45秒;
定时任务:从上一次轮询的最后区块开始扫描,获取每个区块中所有交易,进行对应查询数据库操作和判断操作,执行完后记录轮询到的区块高度,方便下一次查询;
相关依赖
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.0.3</version>
</dependency>
- 以下是使用spring管理的定时任务方法案例:
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.methods.response.EthBlock;
import org.web3j.protocol.http.HttpService;
import java.io.IOException;
import java.math.BigInteger;
@Component
public class TransactionWatcherExample {
private Web3j web3j = Web3j.build(new HttpService("http://localhost:8545"));
//上次轮询到的块高度
private BigInteger lastBlockNumber;
/**
* 扫块监听eth交易,每45秒一次轮询
* @throws IOException
*/
@Scheduled(fixedDelay = 45 * 1000)
public void watchEthTransaction() throws IOException {
//获取当前块高度
BigInteger currentBlockNumber = web3j.ethBlockNumber().send().getBlockNumber();
//运行初始化
if (lastBlockNumber == null || currentBlockNumber.subtract(lastBlockNumber).compareTo(BigInteger.valueOf(100)) > 0) {
lastBlockNumber = BigInteger.valueOf(currentBlockNumber.longValue());
return;
}
long futureBlockNum = currentBlockNumber.intValue() + 1;
//遍历区块
for (int i = lastBlockNumber.intValue(); i < futureBlockNum; i++) {
//获取block
EthBlock.Block block = web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(BigInteger.valueOf(i)), true).send().getBlock();
if (block == null) {
continue;
}
// 遍历block中的交易
for (EthBlock.TransactionResult tx : block.getTransactions()) {
if (tx instanceof EthBlock.TransactionObject) {
//transaction: 块中的单笔交易
EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx;
// todo 交易判断和处理逻辑
}
}
}
lastBlockNumber = BigInteger.valueOf(futureBlockNum);
}
}
网友评论