public override async Task <string> BroadcastTransactionAsync(
            string signedTxData)
        {
            var serializedTransaction = signedTxData.HexToUTF8String();
            var transaction           = JsonConvert.DeserializeObject <DefaultRawTransaction>(serializedTransaction);

            if (await _ethApiClient.GetTransactionAsync(transaction.Hash) != null)
            {
                return(transaction.Hash);
            }


            var transactionHasBeenBroadcasted = false;

            for (var i = 0; i < 30; i++)
            {
                var pendingTransactionsCount = await _ethApiClient.GetTransactionCountAsync(transaction.From, true) -
                                               await _ethApiClient.GetTransactionCountAsync(transaction.From, false);

                if (pendingTransactionsCount < 4)
                {
                    await _ethApiClient.SendRawTransactionAsync(transaction.Data);

                    transactionHasBeenBroadcasted = true;
                }
                else
                {
                    await Task.Delay(1000);
                }
            }

            if (transactionHasBeenBroadcasted)
            {
                for (var i = 0; i < 10; i++)
                {
                    if (await _ethApiClient.GetTransactionAsync(transaction.Hash) != null)
                    {
                        return(transaction.Hash);
                    }
                    else
                    {
                        await Task.Delay(1000);
                    }
                }

                throw new Exception
                      (
                          $"Transaction [{transaction.Hash}] has been broadcasted, but did not appear in mempool in the specified period of time."
                      );
            }
            else
            {
                throw new Exception
                      (
                          $"Transaction [{transaction.Hash}] has not been broadcasted."
                      );
            }
        }
Esempio n. 2
0
        public override async Task <string> BroadcastTransactionAsync(
            string signedTxData)
        {
            var serializedTransaction = signedTxData.HexToUTF8String();
            var transaction           = Newtonsoft.Json.JsonConvert.DeserializeObject <DefaultRawTransaction>(serializedTransaction);

            if (await _ethApiClient.GetTransactionAsync(transaction.Hash) != null)
            {
                return(transaction.Hash);
            }


            var transactionHasBeenBroadcasted = false;

            for (var i = 0; i < 30; i++)
            {
                var pendingTransactionsCount = await _ethApiClient.GetTransactionCountAsync(transaction.From, true) -
                                               await _ethApiClient.GetTransactionCountAsync(transaction.From, false);

                if (pendingTransactionsCount < 4)
                {
                    try
                    {
                        await _ethApiClient.SendRawTransactionAsync(transaction.Data);
                    }
                    catch (RpcErrorException e) when(e.ErrorCode == -32010 && e.Message == "pending transaction with same hash already exists")
                    {
                        if (await _ethApiClient.GetTransactionAsync(transaction.Hash) != null)
                        {
                            return(transaction.Hash);
                        }

                        throw;
                    }

                    transactionHasBeenBroadcasted = true;
                    break;
                }
                else
                {
                    await Task.Delay(1000);
                }
            }

            if (transactionHasBeenBroadcasted)
            {
                for (var i = 0; i < 10; i++)
                {
                    if (await _ethApiClient.GetTransactionAsync(transaction.Hash) != null)
                    {
                        return(transaction.Hash);
                    }
                    else
                    {
                        await Task.Delay(1000);
                    }
                }

                throw new Exception
                      (
                          $"Transaction [{transaction.Hash}] has been broadcasted, but did not appear in mempool in the specified period of time."
                      );
            }
            else
            {
                throw new Exception
                      (
                          $"Transaction [{transaction.Hash}] has not been broadcasted."
                      );
            }
        }