Skip to content

Commit 6ed0bdf

Browse files
authored
Native typed dto special get event logs fn (#97)
1 parent cfa931f commit 6ed0bdf

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

Assets/Thirdweb/Core/Scripts/Contract.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
using Nethereum.ABI.FunctionEncoding;
99
using System.Linq;
1010
using System;
11+
using Nethereum.Contracts;
12+
using Nethereum.RPC.Eth.DTOs;
13+
using Nethereum.ABI.FunctionEncoding.Attributes;
1114

1215
namespace Thirdweb
1316
{
@@ -143,6 +146,23 @@ public List<ParameterOutput> Decode(string functionName, string encodedArgs)
143146
return function.DecodeInput(encodedArgs);
144147
}
145148

149+
/// <summary>
150+
/// Get the events of a contract. For WebGL use contract.Events class instead.
151+
/// <returns>A list of <see cref="EventLog"/> (extending IEventDTO) objects representing the events.</returns>
152+
/// </summary>
153+
public async Task<List<EventLog<TEventDTO>>> GetEventLogs<TEventDTO>(ulong? fromBlock = null, ulong? toBlock = null)
154+
where TEventDTO : IEventDTO, new()
155+
{
156+
var web3 = new Web3(ThirdwebManager.Instance.SDK.session.RPC);
157+
var transferEventHandler = web3.Eth.GetEvent<TEventDTO>(this.address);
158+
var filter = transferEventHandler.CreateFilterInput(
159+
fromBlock: fromBlock == null ? BlockParameter.CreateEarliest() : new BlockParameter(fromBlock.Value),
160+
toBlock: toBlock == null ? BlockParameter.CreateLatest() : new BlockParameter(toBlock.Value)
161+
);
162+
var allTransferEventsForContract = await transferEventHandler.GetAllChangesAsync(filter);
163+
return allTransferEventsForContract;
164+
}
165+
146166
/// <summary>
147167
/// Execute a write transaction on a contract.
148168
/// </summary>

Assets/Thirdweb/Examples/Scripts/Prefabs/Prefab_Events.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System.Collections.Generic;
22
using UnityEngine;
33
using Thirdweb;
4+
using Nethereum.ABI.FunctionEncoding.Attributes;
5+
using System.Numerics;
6+
using Newtonsoft.Json;
47

5-
// Your Event data structure
8+
// Your Event type (WebGL)
69
[System.Serializable]
710
public struct TransferEvent
811
{
@@ -12,7 +15,26 @@ public struct TransferEvent
1215

1316
public override string ToString()
1417
{
15-
return $"TransferEvent:" + $"\n>prevURI: {from}" + $"\n>newURI: {to}" + $"\n>tokenId: {tokenId}";
18+
return $"TransferEvent:" + $"\n>from: {from}" + $"\n>to: {to}" + $"\n>tokenId: {tokenId}";
19+
}
20+
}
21+
22+
// Your Event type (Native platforms)
23+
[Event("Transfer")]
24+
public class TransferEventDTO : IEventDTO
25+
{
26+
[Parameter("address", "from", 1, true)]
27+
public string From { get; set; }
28+
29+
[Parameter("address", "to", 2, true)]
30+
public string To { get; set; }
31+
32+
[Parameter("uint256", "tokenId", 3, true)]
33+
public BigInteger TokenId { get; set; }
34+
35+
public override string ToString()
36+
{
37+
return $"TransferEvent:" + $"\n>from: {From}" + $"\n>to: {To}" + $"\n>tokenId: {TokenId}";
1638
}
1739
}
1840

@@ -26,12 +48,24 @@ public async void GetEvents()
2648
{
2749
Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389");
2850

29-
// Optional event query options
30-
Dictionary<string, object> filters = new Dictionary<string, object> { { "tokenId", 20 } };
31-
EventQueryOptions options = new EventQueryOptions(filters);
51+
if (Utils.IsWebGLBuild())
52+
{
53+
// Optional event query options
54+
Dictionary<string, object> filters = new Dictionary<string, object> { { "tokenId", 20 } };
55+
EventQueryOptions options = new EventQueryOptions(filters);
56+
57+
List<ContractEvent<TransferEvent>> allEvents = await contract.events.Get<TransferEvent>("Transfer", options);
58+
Debugger.Instance.Log("[Get Events] Get - TransferEvent #1", allEvents[0].ToString());
59+
}
60+
else
61+
{
62+
// Optional event query options
63+
ulong? fromBlock = null;
64+
ulong? toBlock = null;
3265

33-
List<ContractEvent<TransferEvent>> allEvents = await contract.events.Get<TransferEvent>("Transfer", options);
34-
Debugger.Instance.Log("[Get Events] Get - TransferEvent #1", allEvents[0].ToString());
66+
var allEvents = await contract.GetEventLogs<TransferEventDTO>(fromBlock, toBlock);
67+
Debugger.Instance.Log("[Get Events] Get - TransferEvent #1", allEvents[0].Event.ToString());
68+
}
3569
}
3670
catch (System.Exception e)
3771
{

0 commit comments

Comments
 (0)