Exemplo n.º 1
0
        public async Task Subscribe(CancellationToken cancellationToken)
        {
            // http://docs.nethereum.com/en/latest/nethereum-subscriptions-streaming/
            using var streamingClient = new StreamingWebSocketClient(_batBotOptions.BlockchainEndpointWssUrl);
            var subscription = new EthNewPendingTransactionObservableSubscription(streamingClient);

            await _ethereumService.HandleSubscription(streamingClient, subscription, async() => await subscription.SubscribeAsync(), async th =>
            {
                await _messagingService.SendLogMessage($"Pending Transaction Hash: {th}");

                // Don't process transactions (discard) when one is being processed for front running.
                if (_backoffService.BackOff())
                {
                    return;
                }

                await _backoffService.Protect(async() => await _transactionProcessorService.Process(await _ethereumService.GetSwap(th), cancellationToken));
            }, null, cancellationToken);
        }
Exemplo n.º 2
0
        public async Task Handle(JsonElement transaction, JsonElement contractCall, TransactionSource transactionSource, CancellationToken cancellationToken)
        {
            if (contractCall.GetProperty(Blocknative.Properties.MethodName).ValueEquals(Uniswap.SwapExactEthForTokens))
            {
                var transactionHash = transaction.GetProperty(Blocknative.Properties.Hash).GetString();
                _logger.LogTrace($"Transaction {transactionHash} received");

                var transactionStatus = EnumHelper.GetValueFromDescription <TransactionStatus>(transaction.GetProperty(Blocknative.Properties.Status).GetString());

                switch (transactionStatus)
                {
                case TransactionStatus.Pending:
                    var @params = contractCall.GetProperty(Blocknative.Properties.Params);
                    await _transactionProcessorService.Process(new Swap
                    {
                        TransactionHash = transactionHash,
                        AmountIn        = TryParseJsonString(transaction, Blocknative.Properties.Value),
                        Gas             = transaction.GetProperty(Blocknative.Properties.Gas).GetUInt64(),
                        GasPrice        = TryParseJsonString(transaction, Blocknative.Properties.GasPrice),
                        AmountOutMin    = TryParseJsonString(@params, Blocknative.Properties.AmountOutMin),
                        Deadline        = (long)TryParseJsonString(@params, Blocknative.Properties.Deadline),
                        Path            = @params.GetProperty(Blocknative.Properties.Path).EnumerateArray().Select(e => e.GetString()).ToList(),
                        Source          = transactionSource
                    }, cancellationToken);