Skip to content

Commit 712ca79

Browse files
authored
Events Prefab/Class (#21)
1 parent 193bad8 commit 712ca79

File tree

9 files changed

+920
-0
lines changed

9 files changed

+920
-0
lines changed

Assets/Thirdweb/Core/Scripts/Contract.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public class Contract : Routable
3030
/// Call any Pack supported functions
3131
/// </summary>
3232
public Pack pack;
33+
/// <summary>
34+
/// Call any Contract Event functions
35+
/// </summary>
36+
public Events events;
3337

3438
public Contract(string chain, string address, string abi = null) : base(abi != null ? $"{address}{Routable.subSeparator}{abi}" : address)
3539
{
@@ -41,6 +45,7 @@ public Contract(string chain, string address, string abi = null) : base(abi != n
4145
this.ERC1155 = new ERC1155(baseRoute);
4246
this.marketplace = new Marketplace(chain, address);
4347
this.pack = new Pack(chain, address);
48+
this.events = new Events(baseRoute);
4449
}
4550

4651
public async Task<CurrencyValue> GetBalance()
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace Thirdweb
5+
{
6+
public class Events : Routable
7+
{
8+
public Events(string parentRoute) : base(Routable.append(parentRoute, "events"))
9+
{
10+
11+
}
12+
13+
// READ FUNCTIONS
14+
15+
/// <summary>
16+
/// Request specific events from a contract
17+
/// </summary>
18+
/// <param name="eventName">The event name as defined in the contract</param>
19+
/// <param name="eventQueryOptions">Optional query filters</param>
20+
/// <typeparam name="T">The event data structure to deserialize into</typeparam>
21+
/// <returns>List of ContractEvent<T></returns>
22+
public async Task<List<ContractEvent<T>>> Get<T>(string eventName, EventQueryOptions eventQueryOptions = null)
23+
{
24+
return await Bridge.InvokeRoute<List<ContractEvent<T>>>(getRoute("getEvents"), Utils.ToJsonStringArray(eventName, eventQueryOptions));
25+
}
26+
27+
/// <summary>
28+
/// Request all events from a contract
29+
/// </summary>
30+
/// <param name="eventQueryOptions">Optional query filters</param>
31+
/// <returns>List of ContractEvent<object></returns>
32+
public async Task<List<ContractEvent<object>>> GetAll(EventQueryOptions eventQueryOptions = null)
33+
{
34+
return await Bridge.InvokeRoute<List<ContractEvent<object>>>(getRoute("getAllEvents"), Utils.ToJsonStringArray(eventQueryOptions));
35+
}
36+
}
37+
}

Assets/Thirdweb/Core/Scripts/Events.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Thirdweb/Core/Scripts/Types.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,4 +456,73 @@ public struct FundWalletOptions
456456
public int chainId;
457457
public List<string> assets;
458458
}
459+
460+
// Events
461+
462+
[System.Serializable]
463+
public class EventQueryOptions
464+
{
465+
public int? fromBlock;
466+
public string order; // "asc" or "desc"
467+
public int? toBlock;
468+
public Dictionary<string, object> filters;
469+
470+
public EventQueryOptions(Dictionary<string, object> filters = null, int? fromBlock = null, int? toBlock = null, string order = null)
471+
{
472+
this.fromBlock = fromBlock;
473+
this.order = order;
474+
this.toBlock = toBlock;
475+
this.filters = filters;
476+
}
477+
}
478+
479+
[System.Serializable]
480+
public struct ContractEvent<T>
481+
{
482+
public string eventName;
483+
public T data;
484+
public EventTransaction transaction;
485+
486+
public override string ToString()
487+
{
488+
return
489+
$"ContractEvent:"
490+
+ $"\n>eventName: {eventName}"
491+
+ $"\n>data: {data.ToString()}"
492+
+ $"\n{transaction.ToString()}";
493+
}
494+
}
495+
496+
[System.Serializable]
497+
public struct EventTransaction
498+
{
499+
public int blockNumber;
500+
public string blockHash;
501+
public int transactionIndex;
502+
public bool removed;
503+
public string address;
504+
public string data;
505+
public List<string> topics;
506+
public string transactionHash;
507+
public int logIndex;
508+
public string @event;
509+
public string eventSignature;
510+
511+
public override string ToString()
512+
{
513+
return
514+
$"EventTransaction:"
515+
+ $"\n>blockNumber: {blockNumber}"
516+
+ $"\n>blockHash: {blockHash}"
517+
+ $"\n>transactionIndex: {transactionIndex}"
518+
+ $"\n>removed: {removed}"
519+
+ $"\n>address: {address}"
520+
+ $"\n>data: {data}"
521+
+ $"\n>topics: {topics}"
522+
+ $"\n>transactionHash: {transactionHash}"
523+
+ $"\n>logIndex: {logIndex}"
524+
+ $"\n>event: {@event}"
525+
+ $"\n>eventSignature: {eventSignature}";
526+
}
527+
}
459528
}

0 commit comments

Comments
 (0)