Ejemplo n.º 1
0
        private async Task GetEstimateGasAsync()
        {
            _estimateGasLabel = new Label(PositionX, 21, "eth_estimateGas: fetching...");
            _window.Add(_estimateGasLabel);

            var gasLimit = await Extensions.TryExecuteAsync(() =>
                                                            _ethJsonRpcClientProxy.eth_estimateGas(_transaction,
                                                                                                   BlockParameterModel.FromNumber(_latestBlock.Number)));

            SetEstimateGas(gasLimit);
            _window.Remove(_estimateGasLabel);
            _estimateGasLabel = new Label(PositionX, 21, $"Gas limit: {_estimateGas}");
            _window.Add(_estimateGasLabel);
        }
Ejemplo n.º 2
0
        private async Task MakeTransferAsync()
        {
            if (!CorrectData(_toAddress, _value, _passphrase))
            {
                Application.Run(_transferWindow);
                return;
            }

            var averageGasPrice = await GetAverageGasPrice();

            var transactionCountResult = await _ethJsonRpcClientProxy.eth_getTransactionCount(_address);

            if (!transactionCountResult.Result.HasValue)
            {
                throw new Exception("There was an error when getting transaction count.");
            }

            _currentNonce = transactionCountResult.Result.Value;

            Address from        = _address;
            Address to          = new Address(_toAddress);
            UInt256 value       = EthToWei(_value);
            var     transaction = CreateTransaction(from, to, value, _gasLimit, averageGasPrice, _currentNonce);

            var resultGasEstimate = await _ethJsonRpcClientProxy.eth_estimateGas(transaction);

            var txFee = decimal.Zero;

            if (resultGasEstimate.IsValid)
            {
                _estimateGas = resultGasEstimate.Result.ToHexString();
                txFee        = decimal.Parse(_estimateGas) * _averageGasPriceNumber;
            }

            var confirmed = MessageBox.Query(40, 10, "Confirmation",
                                             $"Do you confirm the transaction?" +
                                             $"{Environment.NewLine}" +
                                             $"Gas limit: {_estimateGas}" +
                                             $"{Environment.NewLine}" +
                                             $"Gas price: {_averageGasPriceNumber}" +
                                             $"{Environment.NewLine}" +
                                             $"Transaction fee: {WeiToEth(txFee)}", "Yes", "No");

            if (confirmed == 0)
            {
                DeleteLabels();

                var unlockAccountResult = await _jsonRpcWalletClientProxy.personal_unlockAccount(_address, _passphrase);

                if (!unlockAccountResult.Result)
                {
                    MessageBox.ErrorQuery(40, 7, "Error", "Unlocking account failed.");
                    Application.Run(_transferWindow);
                }

                _unlockedLabel = new Label(1, 11, "Account unlocked.");
                _transferWindow.Add(_unlockedLabel);
                if (unlockAccountResult.Result)
                {
                    var sendingTransactionLabel = new Label(1, 12, $"Sending transaction with nonce {transaction.Nonce}.");
                    var sendTransactionResult   = await _ethJsonRpcClientProxy.eth_sendTransaction(transaction);

                    _transferWindow.Add(sendingTransactionLabel);
                    _transferWindow.Remove(_backButton);
                    _transferWindow.Remove(_transferButton);

                    do
                    {
                        var newNoneResult = await _ethJsonRpcClientProxy.eth_getTransactionCount(_address);

                        _newNonce = newNoneResult.Result;
                    } while (_newNonce == _currentNonce);

                    _transferWindow.Add(_backButton, _transferButton);
                    _transferWindow.Remove(sendingTransactionLabel);

                    var sentLbl = new Label(1, 12, $"Transaction sent with nonce {transaction.Nonce}.");
                    _transferWindow.Add(sentLbl);
                    if (sendTransactionResult.IsValid)
                    {
                        _txHashLabel = new Label(1, 13, $"Transaction hash: {sendTransactionResult.Result}");
                        _transferWindow.Add(_txHashLabel);
                    }

                    var resultLockingAccount = await _jsonRpcWalletClientProxy.personal_lockAccount(_address);

                    if (resultLockingAccount.IsValid)
                    {
                        _accountLockedLabel = new Label(1, 14, "Account locked.");
                        _transferWindow.Add(_accountLockedLabel);
                    }
                }
            }
        }