public async Task ShouldRetrieveBlock() { Block receivedMessage = null; var subscription = new EthNewBlockHeadersSubscription(StreamingClient); subscription.SubscriptionDataResponse += delegate(object sender, StreamingEventArgs <Block> args) { receivedMessage = args.Response; }; await subscription.SubscribeAsync(Guid.NewGuid().ToString()); try { await Task.Delay(10000); } catch (TaskCanceledException) { // swallow, escape hatch } Assert.NotNull(receivedMessage); }
static void Main(string[] args) { var client = new StreamingWebSocketClient("wss://mainnet.infura.io/ws"); // var client = new StreamingWebSocketClient("ws://127.0.0.1:8546"); var blockHeaderSubscription = new EthNewBlockHeadersObservableSubscription(client); blockHeaderSubscription.GetSubscribeResponseAsObservable().Subscribe(subscriptionId => Console.WriteLine("Block Header subscription Id: " + subscriptionId)); blockHeaderSubscription.GetSubscriptionDataResponsesAsObservable().Subscribe(block => Console.WriteLine("New Block: " + block.BlockHash)); blockHeaderSubscription.GetUnsubscribeResponseAsObservable().Subscribe(response => Console.WriteLine("Block Header unsubscribe result: " + response)); var blockHeaderSubscription2 = new EthNewBlockHeadersSubscription(client); blockHeaderSubscription2.SubscriptionDataResponse += (object sender, StreamingEventArgs <Block> e) => { Console.WriteLine("New Block from event: " + e.Response.BlockHash); }; var disposable = Observable.FromEventPattern <StreamingEventArgs <Block> >(h => blockHeaderSubscription2.SubscriptionDataResponse += h, h => blockHeaderSubscription2.SubscriptionDataResponse -= h).Subscribe(x => Console.WriteLine("New Block from observable from event : " + x.EventArgs.Response.BlockHash) ); var pendingTransactionsSubscription = new EthNewPendingTransactionObservableSubscription(client); pendingTransactionsSubscription.GetSubscribeResponseAsObservable().Subscribe(subscriptionId => Console.WriteLine("Pending transactions subscription Id: " + subscriptionId)); pendingTransactionsSubscription.GetSubscriptionDataResponsesAsObservable().Subscribe(transactionHash => Console.WriteLine("New Pending TransactionHash: " + transactionHash)); pendingTransactionsSubscription.GetUnsubscribeResponseAsObservable().Subscribe(response => Console.WriteLine("Pending transactions unsubscribe result: " + response)); var ethGetBalance = new EthGetBalanceObservableHandler(client); var subs = ethGetBalance.GetResponseAsObservable().Subscribe(balance => Console.WriteLine("Balance xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx: " + balance.Value.ToString())); var ethBlockNumber = new EthBlockNumberObservableHandler(client); ethBlockNumber.GetResponseAsObservable().Subscribe(blockNumber => Console.WriteLine("Block number: bbbbbbbbbbbbbb" + blockNumber.Value.ToString())); var ethLogs = new EthLogsObservableSubscription(client); ethLogs.GetSubscriptionDataResponsesAsObservable().Subscribe(log => Console.WriteLine("Log Address:" + log.Address)); //no contract address var filterTransfers = Event <TransferEventDTO> .GetEventABI().CreateFilterInput(); var ethLogsTokenTransfer = new EthLogsObservableSubscription(client); ethLogsTokenTransfer.GetSubscriptionDataResponsesAsObservable().Subscribe(log => { try { var decoded = Event <TransferEventDTO> .DecodeEvent(log); if (decoded != null) { Console.WriteLine("Contract address: " + log.Address + " Log Transfer from:" + decoded.Event.From); } else { Console.WriteLine("Found not standard transfer log"); } } catch (Exception ex) { Console.WriteLine("Log Address: " + log.Address + " is not a standard transfer log:", ex.Message); } }); client.Start().Wait(); blockHeaderSubscription.SubscribeAsync().Wait(); blockHeaderSubscription2.SubscribeAsync().Wait(); pendingTransactionsSubscription.SubscribeAsync().Wait(); ethGetBalance.SendRequestAsync("0x742d35cc6634c0532925a3b844bc454e4438f44e", BlockParameter.CreateLatest()).Wait(); ethBlockNumber.SendRequestAsync().Wait(); ethLogs.SubscribeAsync().Wait(); ethLogsTokenTransfer.SubscribeAsync(filterTransfers).Wait(); Thread.Sleep(30000); pendingTransactionsSubscription.UnsubscribeAsync().Wait(); Thread.Sleep(20000); blockHeaderSubscription.UnsubscribeAsync().Wait(); Thread.Sleep(20000); }
public override async Task ExecuteAsync(IStreamingClient client) { var subscription = new EthNewBlockHeadersSubscription(client); await subscription.SubscribeAsync(Guid.NewGuid().ToString()); }