This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Binding] | |
[AttributeUsage(AttributeTargets.Parameter)] | |
public sealed class EthereumEventTriggerAttribute : Attribute | |
{ | |
private const string DefaultUrl = "http://localhost:8545/"; | |
public EthereumEventTriggerAttribute(string abi, string contractAddress, string eventName, string url = DefaultUrl) | |
{ | |
Url = url; | |
Abi = abi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public async Task StartAsync(CancellationToken cancellationToken) | |
{ | |
_cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | |
var web3 = new Web3(_attribute.Url); | |
var contract = web3.Eth.GetContract(_attribute.Abi, _attribute.ContractAddress); | |
_event = contract.GetEvent(_attribute.EventName); | |
_filter = await _event.CreateFilterAsync(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private async void ListenAsync(CancellationToken cancellationToken) | |
{ | |
await Task.Run(async () => | |
{ | |
while (!cancellationToken.IsCancellationRequested) | |
{ | |
var eventsData = await _event.GetFilterChangeDefault(_filter); | |
ProcessEvents(eventsData, cancellationToken); | |
await Task.Delay(EventPollingDelay); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void ProcessEvents(List<EventLog<List<ParameterOutput>>> eventsData, CancellationToken cancellationToken) | |
{ | |
eventsData | |
.Select(eventData => ExtractEventData(eventData.Event, eventData.Log)) | |
.ToList() | |
.ForEach(ethereumEventData => _executor.TryExecuteAsync(new TriggeredFunctionData { TriggerValue = ethereumEventData }, cancellationToken)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private EthereumEventData ExtractEventData(List<ParameterOutput> eventParams, FilterLog log) | |
{ | |
Dictionary<string, string> values = eventParams.ToDictionary(eventParam => eventParam.Parameter.Name, eventParam => eventParam.Result.ToString()); | |
return new EthereumEventData | |
{ | |
Values = values, | |
BlockNumber = log.BlockNumber | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
internal class EthereumEventTriggerBinding : ITriggerBinding | |
{ | |
private readonly EthereumEventTriggerAttribute _attribute; | |
public EthereumEventTriggerBinding(EthereumEventTriggerAttribute attribute) | |
{ | |
_attribute = attribute; | |
} | |
public Type TriggerValueType => typeof(EthereumEventData); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EthereumEventTriggerBindingProvider : ITriggerBindingProvider | |
{ | |
public Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext context) | |
{ | |
var result = Task.FromResult<ITriggerBinding>(default); | |
var attribute = context.Parameter.GetCustomAttribute<EthereumEventTriggerAttribute>(false); | |
if (attribute != null) | |
{ | |
result = Task.FromResult<ITriggerBinding>(new EthereumEventTriggerBinding(attribute)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Extension("EthereumEventTrigger")] | |
public class EtheremEventConfigProvider : IExtensionConfigProvider | |
{ | |
public void Initialize(ExtensionConfigContext context) | |
{ | |
context | |
.AddBindingRule<EthereumEventTriggerAttribute>() | |
.BindToTrigger<EthereumEventData>(new EthereumEventTriggerBindingProvider()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EthereumEventListener : IListener | |
{ | |
private const int EventPollingDelay = 1000; | |
private readonly ITriggeredFunctionExecutor _executor; | |
private readonly EthereumEventTriggerAttribute _attribute; | |
private CancellationTokenSource _cts = null; | |
private Event _event = null; | |
private HexBigInteger _filter = null; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class EthereumEventTriggeredFunction | |
{ | |
private const string Abi = ""; | |
private const string ContractAddress = ""; | |
private const string EventName = ""; | |
[FunctionName(nameof(EthereumEventTriggeredFunction))] | |
public static void Run([EthereumEventTrigger(abi: Abi, contractAddress: ContractAddress, eventName: EventName)]EthereumEventData eventData, ILogger logger) | |
{ | |
string logMessage = $"Event data:\nBlock number: {eventData.BlockNumber}\n{string.Join('\n', eventData.Values.Select(value => $"{value.Key}: {value.Value}"))}"; |
OlderNewer