|
1 | 1 | using System.Collections.Generic;
|
2 | 2 | using UnityEngine;
|
3 |
| -using Thirdweb; |
4 | 3 | using Nethereum.ABI.FunctionEncoding.Attributes;
|
5 | 4 | using System.Numerics;
|
6 | 5 | using Newtonsoft.Json;
|
7 | 6 |
|
8 |
| -// Your Event type (WebGL) |
9 |
| -[System.Serializable] |
10 |
| -public struct TransferEvent |
| 7 | +namespace Thirdweb.Examples |
11 | 8 | {
|
12 |
| - public string from; |
13 |
| - public string to; |
14 |
| - public string tokenId; |
15 |
| - |
16 |
| - public override string ToString() |
| 9 | + // Your Event type (WebGL) |
| 10 | + [System.Serializable] |
| 11 | + public struct TransferEvent |
17 | 12 | {
|
18 |
| - return $"TransferEvent:" + $"\n>from: {from}" + $"\n>to: {to}" + $"\n>tokenId: {tokenId}"; |
| 13 | + public string from; |
| 14 | + public string to; |
| 15 | + public string tokenId; |
| 16 | + |
| 17 | + public override string ToString() |
| 18 | + { |
| 19 | + return $"TransferEvent:" + $"\n>from: {from}" + $"\n>to: {to}" + $"\n>tokenId: {tokenId}"; |
| 20 | + } |
19 | 21 | }
|
20 |
| -} |
21 | 22 |
|
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; } |
| 23 | + // Your Event type (Native platforms) |
| 24 | + [Event("Transfer")] |
| 25 | + public class TransferEventDTO : IEventDTO |
| 26 | + { |
| 27 | + [Parameter("address", "from", 1, true)] |
| 28 | + public string From { get; set; } |
28 | 29 |
|
29 |
| - [Parameter("address", "to", 2, true)] |
30 |
| - public string To { get; set; } |
| 30 | + [Parameter("address", "to", 2, true)] |
| 31 | + public string To { get; set; } |
31 | 32 |
|
32 |
| - [Parameter("uint256", "tokenId", 3, true)] |
33 |
| - public BigInteger TokenId { get; set; } |
| 33 | + [Parameter("uint256", "tokenId", 3, true)] |
| 34 | + public BigInteger TokenId { get; set; } |
34 | 35 |
|
35 |
| - public override string ToString() |
36 |
| - { |
37 |
| - return $"TransferEvent:" + $"\n>from: {From}" + $"\n>to: {To}" + $"\n>tokenId: {TokenId}"; |
| 36 | + public override string ToString() |
| 37 | + { |
| 38 | + return $"TransferEvent:" + $"\n>from: {From}" + $"\n>to: {To}" + $"\n>tokenId: {TokenId}"; |
| 39 | + } |
38 | 40 | }
|
39 |
| -} |
40 |
| - |
41 |
| -public class Prefab_Events : MonoBehaviour |
42 |
| -{ |
43 |
| - // Get all events filtered by name (and optionally add more filters) |
44 | 41 |
|
45 |
| - public async void GetEvents() |
| 42 | + public class Prefab_Events : MonoBehaviour |
46 | 43 | {
|
47 |
| - try |
48 |
| - { |
49 |
| - Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
| 44 | + // Get all events filtered by name (and optionally add more filters) |
50 | 45 |
|
51 |
| - if (Utils.IsWebGLBuild()) |
| 46 | + public async void GetEvents() |
| 47 | + { |
| 48 | + try |
52 | 49 | {
|
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()); |
| 50 | + Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
| 51 | + |
| 52 | + if (Utils.IsWebGLBuild()) |
| 53 | + { |
| 54 | + // Optional event query options |
| 55 | + Dictionary<string, object> filters = new Dictionary<string, object> { { "tokenId", 20 } }; |
| 56 | + EventQueryOptions options = new EventQueryOptions(filters); |
| 57 | + |
| 58 | + List<ContractEvent<TransferEvent>> allEvents = await contract.events.Get<TransferEvent>("Transfer", options); |
| 59 | + Debugger.Instance.Log("[Get Events] Get - TransferEvent #1", allEvents[0].ToString()); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + // Optional event query options |
| 64 | + ulong? fromBlock = null; |
| 65 | + ulong? toBlock = null; |
| 66 | + |
| 67 | + var allEvents = await contract.GetEventLogs<TransferEventDTO>(fromBlock, toBlock); |
| 68 | + Debugger.Instance.Log("[Get Events] Get - TransferEvent #1", allEvents[0].Event.ToString()); |
| 69 | + } |
59 | 70 | }
|
60 |
| - else |
| 71 | + catch (System.Exception e) |
61 | 72 | {
|
62 |
| - // Optional event query options |
63 |
| - ulong? fromBlock = null; |
64 |
| - ulong? toBlock = null; |
65 |
| - |
66 |
| - var allEvents = await contract.GetEventLogs<TransferEventDTO>(fromBlock, toBlock); |
67 |
| - Debugger.Instance.Log("[Get Events] Get - TransferEvent #1", allEvents[0].Event.ToString()); |
| 73 | + Debugger.Instance.Log("[Get Events] Error", e.Message); |
68 | 74 | }
|
69 | 75 | }
|
70 |
| - catch (System.Exception e) |
71 |
| - { |
72 |
| - Debugger.Instance.Log("[Get Events] Error", e.Message); |
73 |
| - } |
74 |
| - } |
75 | 76 |
|
76 |
| - // Get all contract events |
| 77 | + // Get all contract events |
77 | 78 |
|
78 |
| - public async void GetAllEvents() |
79 |
| - { |
80 |
| - try |
| 79 | + public async void GetAllEvents() |
81 | 80 | {
|
82 |
| - Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
| 81 | + try |
| 82 | + { |
| 83 | + Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
83 | 84 |
|
84 |
| - // Optional event query options |
85 |
| - EventQueryOptions options = new EventQueryOptions(null, 0, 16500000, "desc"); |
| 85 | + // Optional event query options |
| 86 | + EventQueryOptions options = new EventQueryOptions(null, 0, 16500000, "desc"); |
86 | 87 |
|
87 |
| - List<ContractEvent<object>> allContractEvents = await contract.events.GetAll(options); |
88 |
| - Debugger.Instance.Log("[Get All Events] Get - ContractEvent #1", allContractEvents[0].ToString()); |
89 |
| - } |
90 |
| - catch (System.Exception e) |
91 |
| - { |
92 |
| - Debugger.Instance.Log("[Get All Events] Error", e.Message); |
| 88 | + List<ContractEvent<object>> allContractEvents = await contract.events.GetAll(options); |
| 89 | + Debugger.Instance.Log("[Get All Events] Get - ContractEvent #1", allContractEvents[0].ToString()); |
| 90 | + } |
| 91 | + catch (System.Exception e) |
| 92 | + { |
| 93 | + Debugger.Instance.Log("[Get All Events] Error", e.Message); |
| 94 | + } |
93 | 95 | }
|
94 |
| - } |
95 | 96 |
|
96 |
| - // Event listeners |
| 97 | + // Event listeners |
97 | 98 |
|
98 |
| - public void ListenToAllEvents() |
99 |
| - { |
100 |
| - try |
101 |
| - { |
102 |
| - Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
103 |
| - contract.events.ListenToAll((ContractEvent<object> anyEvent) => OnEventTriggered(anyEvent)); |
104 |
| - Debugger.Instance.Log("Listening to all events!", "Try to trigger an event on the specified contract to get a callback."); |
105 |
| - } |
106 |
| - catch (System.Exception e) |
| 99 | + public void ListenToAllEvents() |
107 | 100 | {
|
108 |
| - Debugger.Instance.Log("[Listen To All Events] Error", e.Message); |
| 101 | + try |
| 102 | + { |
| 103 | + Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
| 104 | + contract.events.ListenToAll((ContractEvent<object> anyEvent) => OnEventTriggered(anyEvent)); |
| 105 | + Debugger.Instance.Log("Listening to all events!", "Try to trigger an event on the specified contract to get a callback."); |
| 106 | + } |
| 107 | + catch (System.Exception e) |
| 108 | + { |
| 109 | + Debugger.Instance.Log("[Listen To All Events] Error", e.Message); |
| 110 | + } |
109 | 111 | }
|
110 |
| - } |
111 | 112 |
|
112 |
| - public async void RemoveAllEventListeners() |
113 |
| - { |
114 |
| - try |
| 113 | + public async void RemoveAllEventListeners() |
115 | 114 | {
|
116 |
| - Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
117 |
| - await contract.events.RemoveAllListeners(); |
118 |
| - Debugger.Instance.Log("Removed all event listeners!", "Events emitted will not trigger callbacks anymore."); |
| 115 | + try |
| 116 | + { |
| 117 | + Contract contract = ThirdwebManager.Instance.SDK.GetContract("0x2e01763fA0e15e07294D74B63cE4b526B321E389"); |
| 118 | + await contract.events.RemoveAllListeners(); |
| 119 | + Debugger.Instance.Log("Removed all event listeners!", "Events emitted will not trigger callbacks anymore."); |
| 120 | + } |
| 121 | + catch (System.Exception e) |
| 122 | + { |
| 123 | + Debugger.Instance.Log("[Remove All Event Listeners] Error", e.Message); |
| 124 | + } |
119 | 125 | }
|
120 |
| - catch (System.Exception e) |
| 126 | + |
| 127 | + public void OnEventTriggered<T>(ContractEvent<T> contractEvent) |
121 | 128 | {
|
122 |
| - Debugger.Instance.Log("[Remove All Event Listeners] Error", e.Message); |
| 129 | + Debugger.Instance.Log("[EventListener] OnEventTriggered", $"An event was just emitted!\n{contractEvent.ToString()}"); |
123 | 130 | }
|
124 | 131 | }
|
125 |
| - |
126 |
| - public void OnEventTriggered<T>(ContractEvent<T> contractEvent) |
127 |
| - { |
128 |
| - Debugger.Instance.Log("[EventListener] OnEventTriggered", $"An event was just emitted!\n{contractEvent.ToString()}"); |
129 |
| - } |
130 | 132 | }
|
0 commit comments