Beispiel #1
0
        public IEnumerator SignAndSendTransaction(TransactionInput transactionInput, bool estimateGas)
        {
            if (estimateGas)
            {
                yield return(_ethEstimateGasUnityRequest.SendRequest(transactionInput));

                if (_ethEstimateGasUnityRequest.Exception == null)
                {
                    var gas = _ethEstimateGasUnityRequest.Result;
                    transactionInput.Gas = gas;
                }
                else
                {
                    this.Exception = _ethEstimateGasUnityRequest.Exception;
                    yield break;
                }
            }

            yield return(SignAndSendTransaction(transactionInput));
        }
        public IEnumerator EstimateContractFunction <TContractFunction>(TContractFunction function, string contractAdress) where TContractFunction : FunctionMessage
        {
            var callInput = function.CreateCallInput(contractAdress);

            yield return(_ethEstimateGasUnityRequest.SendRequest(callInput));
        }
        public IEnumerator SignAndSendTransaction(TransactionInput transactionInput)
        {
            if (transactionInput == null)
            {
                throw new ArgumentNullException("transactionInput");
            }

            if (!string.IsNullOrEmpty(_signedTxData))
            {
                yield return(_ethSendTransactionRequest.SendRequest(_signedTxData));
            }
            else
            {
                if (transactionInput.Gas == null)
                {
                    if (EstimateGas)
                    {
                        yield return(_ethEstimateGasUnityRequest.SendRequest(transactionInput));

                        if (_ethEstimateGasUnityRequest.Exception == null)
                        {
                            var gas = _ethEstimateGasUnityRequest.Result;
                            transactionInput.Gas = gas;
                        }
                        else
                        {
                            this.Exception = _ethEstimateGasUnityRequest.Exception;
                            yield break;
                        }
                    }
                    else
                    {
                        transactionInput.Gas = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT);
                    }
                }

                if (transactionInput.GasPrice == null)
                {
                    yield return(_ethGasPriceUnityRequest.SendRequest());

                    if (_ethGasPriceUnityRequest.Exception == null)
                    {
                        var gasPrice = _ethGasPriceUnityRequest.Result;
                        transactionInput.GasPrice = gasPrice;
                    }
                    else
                    {
                        this.Exception = _ethGasPriceUnityRequest.Exception;
                        yield break;
                    }
                }

                var nonce = transactionInput.Nonce;

                if (nonce == null)
                {
                    yield return(_transactionCountRequest.SendRequest(_account.Address, Nethereum.RPC.Eth.DTOs.BlockParameter.CreateLatest()));

                    if (_transactionCountRequest.Exception == null)
                    {
                        nonce = _transactionCountRequest.Result;
                    }
                    else
                    {
                        this.Exception = _transactionCountRequest.Exception;
                        yield break;
                    }
                }

                if (string.IsNullOrEmpty(transactionInput.From))
                {
                    transactionInput.From = _account.Address;
                }

                var value = transactionInput.Value;
                if (value == null)
                {
                    value = new HexBigInteger(0);
                }

                var signedTransaction = _transactionSigner.SignTransaction(_account.PrivateKey, transactionInput.To, value.Value, nonce,
                                                                           transactionInput.GasPrice.Value, transactionInput.Gas.Value, transactionInput.Data);

                yield return(_ethSendTransactionRequest.SendRequest(signedTransaction));

                _account.PrivateKey.ClearBytes();
                GC.Collect();
            }

            if (_ethSendTransactionRequest.Exception == null)
            {
                Result = _ethSendTransactionRequest.Result;
            }
            else
            {
                Exception = _ethSendTransactionRequest.Exception;
            }
        }
        public IEnumerator SignAndSendTransaction(TransactionInput transactionInput)
        {
            if (transactionInput == null)
            {
                throw new ArgumentNullException("transactionInput");
            }

            if (string.IsNullOrEmpty(transactionInput.From))
            {
                transactionInput.From = _account;
            }

            if (!transactionInput.From.IsTheSameAddress(_account))
            {
                throw new Exception("Transaction Input From address does not match private keys address");
            }

            if (transactionInput.Gas == null)
            {
                if (EstimateGas)
                {
                    yield return(_ethEstimateGasUnityRequest.SendRequest(transactionInput));

                    if (_ethEstimateGasUnityRequest.Exception == null)
                    {
                        var gas = _ethEstimateGasUnityRequest.Result;
                        transactionInput.Gas = gas;
                    }
                    else
                    {
                        this.Exception = _ethEstimateGasUnityRequest.Exception;
                        yield break;
                    }
                }
                else
                {
                    transactionInput.Gas = new HexBigInteger(Transaction.DEFAULT_GAS_LIMIT);
                }
            }

            if (transactionInput.GasPrice == null)
            {
                yield return(_ethGasPriceUnityRequest.SendRequest());

                if (_ethGasPriceUnityRequest.Exception == null)
                {
                    var gasPrice = _ethGasPriceUnityRequest.Result;
                    transactionInput.GasPrice = gasPrice;
                }
                else
                {
                    this.Exception = _ethGasPriceUnityRequest.Exception;
                    yield break;
                }
            }

            var nonce = transactionInput.Nonce;

            if (nonce == null)
            {
                yield return(_transactionCountRequest.SendRequest(_account, Nethereum.RPC.Eth.DTOs.BlockParameter.CreateLatest()));

                if (_transactionCountRequest.Exception == null)
                {
                    nonce = _transactionCountRequest.Result;
                }
                else
                {
                    this.Exception = _transactionCountRequest.Exception;
                    yield break;
                }
            }

            var value = transactionInput.Value;

            if (value == null)
            {
                value = new HexBigInteger(0);
            }

            string signedTransaction;

            if (_chainId == null)
            {
                signedTransaction = _transactionSigner.SignTransaction(_privateKey, transactionInput.To, value.Value, nonce,
                                                                       transactionInput.GasPrice.Value, transactionInput.Gas.Value, transactionInput.Data);
            }
            else
            {
                signedTransaction = _transactionSigner.SignTransaction(_privateKey, _chainId.Value, transactionInput.To, value.Value, nonce,
                                                                       transactionInput.GasPrice.Value, transactionInput.Gas.Value, transactionInput.Data);
            }


            yield return(_ethSendTransactionRequest.SendRequest(signedTransaction));

            if (_ethSendTransactionRequest.Exception == null)
            {
                this.Result = _ethSendTransactionRequest.Result;
            }
            else
            {
                this.Exception = _ethSendTransactionRequest.Exception;
                yield break;
            }
        }
        public IEnumerator SignAndSendTransaction(TransactionInput transactionInput)
        {
            if (transactionInput == null)
            {
                throw new ArgumentNullException("transactionInput");
            }

            if (string.IsNullOrEmpty(transactionInput.From))
            {
                transactionInput.From = _account;
            }

            if (!transactionInput.From.IsTheSameAddress(_account))
            {
                throw new Exception("Transaction Input From address does not match private keys address");
            }

            if (transactionInput.Gas == null)
            {
                if (EstimateGas)
                {
                    yield return(_ethEstimateGasUnityRequest.SendRequest(transactionInput));

                    if (_ethEstimateGasUnityRequest.Exception == null)
                    {
                        var gas = _ethEstimateGasUnityRequest.Result;
                        transactionInput.Gas = gas;
                    }
                    else
                    {
                        this.Exception = _ethEstimateGasUnityRequest.Exception;
                        yield break;
                    }
                }
                else
                {
                    transactionInput.Gas = new HexBigInteger(LegacyTransaction.DEFAULT_GAS_LIMIT);
                }
            }

            if (IsTransactionToBeSendAsEIP1559(transactionInput))
            {
                transactionInput.Type = new HexBigInteger(TransactionType.EIP1559.AsByte());
                if (transactionInput.MaxPriorityFeePerGas != null)
                {
                    if (transactionInput.MaxFeePerGas == null)
                    {
                        yield return(Fee1559SuggestionStrategy.SuggestFee(transactionInput.MaxPriorityFeePerGas.Value));

                        if (Fee1559SuggestionStrategy.Exception != null)
                        {
                            transactionInput.MaxFeePerGas = new HexBigInteger(Fee1559SuggestionStrategy.Result.MaxFeePerGas.Value);
                        }
                        else
                        {
                            this.Exception = Fee1559SuggestionStrategy.Exception;
                            yield break;
                        }
                    }
                }
                else
                {
                    yield return(Fee1559SuggestionStrategy.SuggestFee());

                    if (Fee1559SuggestionStrategy.Exception != null)
                    {
                        if (transactionInput.MaxFeePerGas == null)
                        {
                            transactionInput.MaxFeePerGas =
                                new HexBigInteger(Fee1559SuggestionStrategy.Result.MaxFeePerGas.Value);

                            transactionInput.MaxPriorityFeePerGas =
                                new HexBigInteger(Fee1559SuggestionStrategy.Result.MaxPriorityFeePerGas.Value);
                        }
                        else
                        {
                            if (transactionInput.MaxFeePerGas < Fee1559SuggestionStrategy.Result.MaxPriorityFeePerGas)
                            {
                                transactionInput.MaxPriorityFeePerGas = transactionInput.MaxFeePerGas;
                            }
                            else
                            {
                                transactionInput.MaxPriorityFeePerGas = new HexBigInteger(Fee1559SuggestionStrategy.Result.MaxPriorityFeePerGas.Value);
                            }
                        }
                    }
                    else
                    {
                        this.Exception = Fee1559SuggestionStrategy.Exception;
                        yield break;
                    }
                }
            }
            else
            {
                if (transactionInput.GasPrice == null)
                {
                    yield return(_ethGasPriceUnityRequest.SendRequest());

                    if (_ethGasPriceUnityRequest.Exception == null)
                    {
                        var gasPrice = _ethGasPriceUnityRequest.Result;
                        transactionInput.GasPrice = gasPrice;
                    }
                    else
                    {
                        this.Exception = _ethGasPriceUnityRequest.Exception;
                        yield break;
                    }
                }
            }


            var nonce = transactionInput.Nonce;

            if (nonce == null)
            {
                yield return(_transactionCountRequest.SendRequest(_account, Nethereum.RPC.Eth.DTOs.BlockParameter.CreateLatest()));

                if (_transactionCountRequest.Exception == null)
                {
                    nonce = _transactionCountRequest.Result;
                }
                else
                {
                    this.Exception = _transactionCountRequest.Exception;
                    yield break;
                }
            }

            var value = transactionInput.Value;

            if (value == null)
            {
                value = new HexBigInteger(0);
            }

            string signedTransaction;

            if (IsTransactionToBeSendAsEIP1559(transactionInput))
            {
                var transaction1559 = new Transaction1559(_chainId.Value, nonce, transactionInput.MaxPriorityFeePerGas.Value, transactionInput.MaxFeePerGas.Value,
                                                          transactionInput.Gas.Value, transactionInput.To, value.Value, transactionInput.Data,
                                                          null);
                transaction1559.Sign(new EthECKey(_privateKey));
                signedTransaction = transaction1559.GetRLPEncoded().ToHex();
            }
            else
            {
                if (_chainId == null)
                {
                    signedTransaction = _transactionSigner.SignTransaction(_privateKey, transactionInput.To, value.Value, nonce,
                                                                           transactionInput.GasPrice.Value, transactionInput.Gas.Value, transactionInput.Data);
                }
                else
                {
                    signedTransaction = _transactionSigner.SignTransaction(_privateKey, _chainId.Value, transactionInput.To, value.Value, nonce,
                                                                           transactionInput.GasPrice.Value, transactionInput.Gas.Value, transactionInput.Data);
                }
            }

            yield return(_ethSendTransactionRequest.SendRequest(signedTransaction));

            if (_ethSendTransactionRequest.Exception == null)
            {
                this.Result = _ethSendTransactionRequest.Result;
            }
            else
            {
                this.Exception = _ethSendTransactionRequest.Exception;
                yield break;
            }
        }