Exemplo n.º 1
0
        private async void buttonSend_Click(object sender, EventArgs e)
        {
            buttonSend.Enabled = false;

            string wallet  = comboBoxWallet.Text;
            string account = comboBoxAccount.Text;

            List <RecipientModel> recipients = new List <RecipientModel>();

            Money how = Money.FromUnit(numericTextBoxAmount.DecimalValue, MoneyUnit.BTC);

            recipients.Add(new RecipientModel(textBoxPayTo.Text.Trim(), how.ToString()));

            List <OutpointRequest> outpoints = null;

            if (SelectedTransactions != null)
            {
                outpoints = new List <OutpointRequest>();

                foreach (var item in SelectedTransactions)
                {
                    outpoints.Add(new OutpointRequest(item.TxId.ToString(), item.Index));
                }

                //свой адрес для сдачи можно установить если сами выбираем входа, иначе не узнать какие выберет программа
                if (checkBoxCustomChangeAddress.Checked && !String.IsNullOrEmpty(textBoxCustomChangeAddress.Text.Trim()))
                {
                    //нужно посчитать сколько будет сдачи и, если больше 0, направить все это на адрес
                    string changeAddress = textBoxCustomChangeAddress.Text.Trim();
                    Money  change        = Money.FromUnit(SelectedTransactions.Sum(x => x.Amount), MoneyUnit.Satoshi) - how - Common.CurrentSettings.MinTxFee;
                    if (change > Money.Zero)
                    {
                        recipients.Add(new RecipientModel(changeAddress, change.ToString()));
                    }
                }
            }

            #region Fee - на Stratis MainNet почти тестировал!!!
            if (Common.CurrentSettings.IsStratis)
            {
                //Оценка комиссии - Для Статиса только? Но все равно комиссия посчитается автоматически так как передаю ее тип а не значение
                //х42 фии нет но для стратиса можно оценить api/wallet/estimate-txfee по POST а у х42 GET
                ApiClient client = new ApiClient();
                var       fee    = await client.GetEstimateFee(wallet, account, recipients, outpoints);

                if (fee == null)
                {
                    ErrorMessage("Error GetEstimateFee: " + client.Error);
                    return;
                }
                if (fee > Money.Zero) //нужно подтверждение
                {
                    if (DialogResult.Yes != MessageBox.Show($"The commission will be {fee} coins. Do you agree to pay for it?", "Commission", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                    {
                        ErrorMessage($"You refused to pay a commission in {fee}");
                        return;
                    }
                }
            }
            #endregion Fee

            //Запросим пароль
            string       password;
            PasswordForm pass = new PasswordForm();
            pass.ShowDialog();
            if (DialogResult.OK == pass.DialogResult)
            {
                password = pass.Password;
            }
            else
            {
                ErrorMessage("You have not entered a password");
                return;
            }

            var result = await SendTransaction(wallet, account, password, recipients, outpoints);

            if (result != null)
            {
                MessageForm frm = new MessageForm();
                frm.InputBox("Transaction ID: ", "Transaction has been successfully sent", result);
            }
            else
            {
                MessageBox.Show("Error: see in log ", "Failed to send transaction", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }