如何通过以太获取最近的交易?



如何从EVM加载最近的10或20个事务?

我发现使用下面的代码,我可以收听"pending"。事务。但是我想加载所有最近的20个事务,不管它们处于什么状态。

var url = "...some-node-or-alchemy-"
var provider = new ethers.providers.JsonRpcProvider(url);

provider.on("pending", (tx) => {
console.log(tx);
});

怎么做?文档并没有真正的帮助。它只显示了可以从特定散列加载事务的函数。但我不知道哈希值,需要先获取最新的哈希值。

// Note this is V6 of ethers
const provider = new ethers.JsonRpcProvider(`https://mainnet.infura.io/v3/${process.env.YOUR_INFURA_API_KEY}`);
const eth_getBlockByNumber = async () => {
const blockByNumber = await provider.send("eth_getBlockByNumber", ["pending", false]);
const transactions = blockByNumber.transactions;
const first50Transactions = transactions.slice(0, 50);
console.log("First 50 transactions:", first50Transactions);
};
eth_getBlockByNumber();

你可以使用ether的EtherscanProvider api。JsonRpcProvider将连接到以太坊区块链中的节点,EtherscanProvider将连接到etherscanapi

let etherscanProvider = new ethers.providers.EtherscanProvider();
// Getting the current Ethereum price
etherscanProvider.getEtherPrice().then(function(price) {
console.log("Ether price in USD: " + price);
});

// Getting the transaction history of an address
let address = '0xb2682160c482eB985EC9F3e364eEc0a904C44C23';
let startBlock = 3135808;
let endBlock = 5091477;
etherscanProvider.getHistory(address, startBlock, endBlock).then(function(history) {
console.log(history);

或者你可以注册etherscan API这里是https://api.etherscan.io/apis

但无论如何,我认为你不能指定事务的数量。该实现依赖于RPC服务器。如果它们没有构建,您可以在前端处理这个问题,并只向用户返回指定数量的事务

待定事务和确认事务是两个非常不同的交易类型,一个是不稳定的,另一个是被确认为区块链的一部分。

连接到节点并使用以太流pending事件是获取最新挂起事务的好方法。

对于已确认的交易,您可以使用block事件的类似流来获取最新的块编号,然后使用eth_getBlockByNumber调用检查块中包含的交易。

最新更新