public static async Task Main() { //DAI contract address var contractAddress = "0x6b175474e89094c44da98b954eedeac495271d0f"; using (var client = new StreamingWebSocketClient("wss://mainnet.infura.io/ws/v3/7238211010344719ad14a89db874158c")) { try { var eventSubscription = new EthLogsObservableSubscription(client); eventSubscription.GetSubscriptionDataResponsesAsObservable().Subscribe(log => { var transfer = log.DecodeEvent <TransferEventDTO>(); Console.WriteLine($@"{transfer.Log.TransactionHash}, From:{transfer.Event.From}, To:{transfer.Event.To}, Value:{Web3.Convert.FromWei(transfer.Event.Value)}"); }); eventSubscription.GetSubscribeResponseAsObservable().Subscribe(id => Console.WriteLine($"Subscribed with id: {id}")); var filterAuction = Event <TransferEventDTO> .GetEventABI().CreateFilterInput(contractAddress); await client.StartAsync(); await eventSubscription.SubscribeAsync(filterAuction); Console.ReadLine(); await eventSubscription.UnsubscribeAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } } }
public static async Task SubscribeToSwaps() //https://uniswap.org/docs/v2/smart-contracts/pair/ Emitted each time a swap occurs via swap. { var token0 = "DAI"; var token1 = "ETH"; var pairContractAddress = "0xa478c2975ab1ea89e8196811f51a7b7ade33eb11"; using (var client = new StreamingWebSocketClient("wss://mainnet.infura.io/ws/v3/7238211010344719ad14a89db874158c")) { Console.WriteLine($"Uniswap trades for {token0} and {token1}"); try { var eventSubscription = new EthLogsObservableSubscription(client); eventSubscription.GetSubscriptionDataResponsesAsObservable().Subscribe(log => { var swap = log.DecodeEvent <SwapEventDTO>(); var amount0Out = Web3.Web3.Convert.FromWei(swap.Event.Amount0Out); var amount1In = Web3.Web3.Convert.FromWei(swap.Event.Amount1In); var amount0In = Web3.Web3.Convert.FromWei(swap.Event.Amount0In); var amount1Out = Web3.Web3.Convert.FromWei(swap.Event.Amount1Out); if (swap.Event.Amount0In == 0 && swap.Event.Amount1Out == 0) { var price = amount0Out / amount1In; var quantity = amount1In; Console.WriteLine($"Sell {token1} Price: {price.ToString("F4")} Quantity: {quantity.ToString("F4")}, From: {swap.Event.To} Block: {swap.Log.BlockNumber}"); } else { var price = amount0In / amount1Out; var quantity = amount1Out; Console.WriteLine($"Buy {token1} Price: {price.ToString("F4")} Quantity: {quantity.ToString("F4")}, From: {swap.Event.To} Block: {swap.Log.BlockNumber}"); } } ); eventSubscription.GetSubscribeResponseAsObservable().Subscribe(id => Console.WriteLine($"Subscribed with id: {id}")); var filterAuction = Event <SwapEventDTO> .GetEventABI().CreateFilterInput(pairContractAddress); await client.StartAsync(); await eventSubscription.SubscribeAsync(filterAuction); Console.ReadLine(); await eventSubscription.UnsubscribeAsync(); } catch (Exception e) { Console.WriteLine(e.Message); } } }