栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Cocos2d-x

java通过web3j获取ETH交易明细

Cocos2d-x 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java通过web3j获取ETH交易明细

    我们在项目里面如果想要得到用户的ETH交易明细怎么做呢?有两种方式:

   1、直接获取ETH最新块的交易明细。

   2、通过块获取用户的交易明细。

废话不多说,直接贴代码看了

        

package com.example.demo.web3jLog;

import org.springframework.stereotype.Component;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.DefaultBlockParameterName;
import org.web3j.protocol.core.methods.request.EthFilter;
import org.web3j.protocol.core.methods.response.*;
import org.web3j.protocol.core.methods.response.Transaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.utils.Convert;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

@Component
public class ETHWeb3jTest {

    
    public static Web3j web3j = Web3j.build(new HttpService("https://org:8545/"));

    public static void main(String[] args) {
        BigInteger latestBlock;
        try {
            //获取ETH的最新区块号
            latestBlock = web3j.ethBlockNumber().send().getBlockNumber();
            //通过区块号获取交易
            List ethGetBlance = web3j.ethGetBlockByNumber(DefaultBlockParameter.valueOf(latestBlock.subtract(new BigInteger("3"))),true).send().getBlock().getTransactions();
            //通过hash获取交易
            Optional transactions = web3j.ethGetTransactionByHash("hash").send().getTransaction();
        } catch (IOException e) {
            e.printStackTrace();
        }
        List txs = null;
        try {
            //也可以直接获取最新交易
            txs = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, true).send().getBlock().getTransactions();
        } catch (IOException e) {
            e.printStackTrace();
        }
        txs.forEach(tx -> {
            EthBlock.TransactionObject transaction = (EthBlock.TransactionObject) tx.get();
            System.out.println(transaction.getFrom());
        });
    }

}

下面是项目的相关依赖:


    org.web3j
    core
    3.4.0



    com.google.guava
    guava
    29.0-jre


    org.bitcoinj
    bitcoinj-core
    0.15.5

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/952078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号