web3j 扩充 getPendingTransactions

2021-08-11 17:18:00

由于web3j没有实现getPendingTransactions接口,业务又需要,所以补充下
参考下web3.js (Github Code)

 new Method({
     name: 'getPendingTransactions',
     call: 'eth_pendingTransactions',
     params: 0,
     outputFormatter: formatter.outputTransactionFormatter
 }),

通过查询(Github Code)

找到以下轮子 (Github Code

 @Override
    public List<Transaction> getPendingTransactions() throws EthereumException {
        // only works if a gexp node is used (pending transaction sent by one of the gexp accounts are returned)
        LOG.info("Get pending transactions sent by addr");
        JsonRpcRequest jsonRpcRequest = new JsonRpcRequest();
        jsonRpcRequest.setId(0);
        jsonRpcRequest.setMethod("eth_pendingTransactions");
        jsonRpcRequest.setJsonrpc("2.0");
        HttpEntity<JsonRpcRequest> request = new HttpEntity<>(jsonRpcRequest);
        PendingTransactionResponse response = doPOST(getNodeBaseUrl(), request, PendingTransactionResponse.class);

        List<Transaction> pendingTransactions = response
                .getPendingTransactions()
                .parallelStream()
                .map(DtoConverter::convert)
                .collect(Collectors.toList());

        LOG.info("Pending transactions found: " + pendingTransactions.size());
        return pendingTransactions;
    }

如果完整的,应该fork扩充到 (Github Code)

当前页面是本站的「Baidu MIP」版。发表评论请点击:完整版 »