using CodexContractsPlugin.Marketplace; using GethPlugin; using Logging; using Nethereum.Contracts; using Nethereum.Hex.HexTypes; using NethereumWorkflow.BlockUtils; using Utils; namespace CodexContractsPlugin { public interface ICodexContractsEvents { BlockInterval BlockInterval { get; } Request[] GetStorageRequests(); RequestFulfilledEventDTO[] GetRequestFulfilledEvents(); RequestCancelledEventDTO[] GetRequestCancelledEvents(); RequestFailedEventDTO[] GetRequestFailedEvents(); SlotFilledEventDTO[] GetSlotFilledEvents(); SlotFreedEventDTO[] GetSlotFreedEvents(); SlotReservationsFullEventDTO[] GetSlotReservationsFull(); } public class CodexContractsEvents : ICodexContractsEvents { private readonly ILog log; private readonly IGethNode gethNode; private readonly CodexContractsDeployment deployment; public CodexContractsEvents(ILog log, IGethNode gethNode, CodexContractsDeployment deployment, BlockInterval blockInterval) { this.log = log; this.gethNode = gethNode; this.deployment = deployment; BlockInterval = blockInterval; } public BlockInterval BlockInterval { get; } public Request[] GetStorageRequests() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); var i = new ContractInteractions(log, gethNode); return events.Select(e => { var requestEvent = i.GetRequest(deployment.MarketplaceAddress, e.Event.RequestId); var result = requestEvent.ReturnValue1; result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); result.RequestId = e.Event.RequestId; return result; }).ToArray(); } public RequestFulfilledEventDTO[] GetRequestFulfilledEvents() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); return events.Select(SetBlockOnEvent).ToArray(); } public RequestCancelledEventDTO[] GetRequestCancelledEvents() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); return events.Select(SetBlockOnEvent).ToArray(); } public RequestFailedEventDTO[] GetRequestFailedEvents() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); return events.Select(SetBlockOnEvent).ToArray(); } public SlotFilledEventDTO[] GetSlotFilledEvents() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); return events.Select(e => { var result = e.Event; result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); result.Host = GetEthAddressFromTransaction(e.Log.TransactionHash); return result; }).ToArray(); } public SlotFreedEventDTO[] GetSlotFreedEvents() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); return events.Select(SetBlockOnEvent).ToArray(); } public SlotReservationsFullEventDTO[] GetSlotReservationsFull() { var events = gethNode.GetEvents(deployment.MarketplaceAddress, BlockInterval); return events.Select(SetBlockOnEvent).ToArray(); } private T SetBlockOnEvent(EventLog e) where T : IHasBlock { var result = e.Event; result.Block = GetBlock(e.Log.BlockNumber.ToUlong()); return result; } private BlockTimeEntry GetBlock(ulong number) { return gethNode.GetBlockForNumber(number); } private EthAddress GetEthAddressFromTransaction(string transactionHash) { var transaction = gethNode.GetTransaction(transactionHash); return new EthAddress(transaction.From); } } }