Nethereum

Nethereum contract.getPastEvents 等價物

  • December 1, 2019

我想查詢有關與 Nethereum 契約的所有轉賬。這意味著,最後我想擁有所有交易,以便我可以列印對帳單。

我想使用 web3.js 庫,契約上的呼叫會是這樣的:

contract.getPastEvents('Transfer', {fromBlock: 1, toBlock: 'latest'}, callbackFunc)

getPastEvents但是,我在 Nethereum中沒有找到等價物。

Kevin Small 在 Gitter Nethereum 聊天中的直接對話中用這個例子回答了這個問題:

using System;
using System.Numerics;
using System.Threading.Tasks;
using Nethereum.ABI.FunctionEncoding.Attributes;
using Nethereum.Contracts;
using Nethereum.RPC.Eth.DTOs;
using Nethereum.Web3;

internal class Program
{
   // Define event we want to look for
   // Note: in a visual studio project outside of this playground, you have the option to install nuget package
   // Nethereum.StandardTokenEIP20 and add a using Nethereum.StandardTokenEIP20 to provide class TransferEventDTO,
   // instead of defining it here.
   [Event("Transfer")]
   public class TransferEventDTO : IEventDTO
   {
       [Parameter("address", "_from", 1, true)]
       public string From { get; set; }

       [Parameter("address", "_to", 2, true)]
       public string To { get; set; }

       [Parameter("uint256", "_value", 3, false)]
       public BigInteger Value { get; set; }
   }

   private static async Task Main(string[] args)
   {
       var url = "https://mainnet.infura.io/v3/7238211010344719ad14a89db874158c";
       var web3 = new Web3(url);
       // This is the contract address of the DAI Stablecoin v1.0 ERC20 token on mainnet
       // See https://etherscan.io/address/0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359
       var erc20TokenContractAddress = "0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359";

       var transferEventHandler = web3.Eth.GetEvent<TransferEventDTO>(erc20TokenContractAddress);
       // Just examine a few blocks by specifying start and end blocks
       var filter = transferEventHandler.CreateFilterInput(
           fromBlock: new BlockParameter(8450678),
           toBlock: new BlockParameter(8450698));
       var logs = await transferEventHandler.GetAllChanges(filter);

       Console.WriteLine($"Token Transfer Events for ERC20 Token at Contract Address: {erc20TokenContractAddress}");
       foreach (var logItem in logs)
           Console.WriteLine(
               $"tx:{logItem.Log.TransactionHash} from:{logItem.Event.From} to:{logItem.Event.To} value:{logItem.Event.Value}");
   }
}

您還可以在此處查看、編輯和執行此答案。

引用自:https://ethereum.stackexchange.com/questions/77944