public async Task TradeClearedEventTest() { Trace.Listeners.Add(new TextWriterTraceListener(Console.Out)); Trace.AutoFlush = true; Trace.WriteLine("Test starts"); var listener = new ContractEventListener <TradeClearedEvent>(_ethereumClient, _contractAbi, "0x57f51b46798f91e36906a3c16acb8ee8dd798961", "TradeCleared", ProcessEvent(), null); await listener.Init(); await listener.GetAllChanges(); /*await listener.Start(); * while(true) * { * await listener.GetChanges(); * await Task.Delay(5000); * } */ }
public static async Task ListenForInsuranceBoughtEvents([TimerTrigger("*/30 * * * * *")] TimerInfo myTimer, ILogger log, [Inject(typeof(IBlockchainClientRepository))] IBlockchainClientRepository blockchainRepo, [Inject(typeof(IBlobContractFileRepository))] IBlobContractFileRepository blobContractFileRepository, [Inject(typeof(IDictionary <string, ContractEventListener <InsuranceBoughtEvent> >))] IDictionary <string, ContractEventListener <InsuranceBoughtEvent> > listeners, [Inject(typeof(IRepository <Purchase>))] IRepository <Purchase> purchaseRepo, [Inject(typeof(IRepository <DeployedContract>))] IRepository <DeployedContract> contractRepo) { try { log.LogInformation($"Listen for Insurance Bought Events trigger function executed at: {DateTime.Now}"); var activeContracts = await contractRepo.Find(c => c.ExpirationDateTime >= DateTime.UtcNow); foreach (var contract in activeContracts) { if (listeners.ContainsKey(contract.Address)) { continue; } var client = blockchainRepo.GetClient(contract.Network.Name); if (client == null) { continue; } var blobContractFile = await blobContractFileRepository.GetBlobContractFile(contract.OwnerAddress, contract.ContractFile.Name, true, true, false); var listener = new ContractEventListener <InsuranceBoughtEvent>(client, blobContractFile.Abi.ToString(), contract.Address, "InsuranceBought", (string contractAddress, InsuranceBoughtEvent capturedEvent) => { var purchase = new Purchase() { Contract = contract, UserAddress = capturedEvent.User, Notional = UnitConversion.Convert.FromWei(capturedEvent.Notional), Premium = UnitConversion.Convert.FromWei(capturedEvent.Premium), Timestamp = DateTime.UtcNow }; var result = purchaseRepo.AddNew(purchase).Result; log.LogInformation($"Insurance bought: {contractAddress} {capturedEvent.Notional} {capturedEvent.Premium}"); }, log); await listener.Start(); listeners.Add(contract.Address, listener); } foreach (var listener in listeners) { await listener.Value.GetChanges(); } } catch (Exception ex) { log.LogError($"Insurance Bought Events trigger error: {ex.Message}"); throw ex; } finally { log.LogInformation($"Insurance Bought Events trigger finished at {DateTime.UtcNow.ToString()}"); } }