FireWalletCosmos/FireWalletLite/SendForm.cs

102 lines
3.5 KiB
C#
Raw Normal View History

using FireWallet;
2023-07-07 14:04:47 +10:00
using Newtonsoft.Json.Linq;
2023-07-15 22:17:49 +10:00
namespace FireWalletLite;
public partial class SendForm : Form
{
2023-07-15 22:17:49 +10:00
private readonly int fee = 1;
private readonly MainForm main;
private readonly decimal unlockedbalance;
public SendForm(decimal Unlockedbalance, MainForm main)
{
2023-07-15 22:17:49 +10:00
InitializeComponent();
this.main = main;
unlockedbalance = Unlockedbalance;
2023-07-07 14:04:47 +10:00
2023-07-15 22:17:49 +10:00
// Theme form
BackColor = ColorTranslator.FromHtml(main.Theme["background"]);
ForeColor = ColorTranslator.FromHtml(main.Theme["foreground"]);
foreach (Control control in Controls) main.ThemeControl(control);
2023-07-07 14:04:47 +10:00
2023-07-15 22:17:49 +10:00
labelMax.Text = "Max: " + (unlockedbalance - fee) + " HNS";
if (unlockedbalance < fee) labelMax.Text = "Max: 0 HNS";
//buttonSend.Enabled = false;
// Allign controls
labelAddress.Left = (ClientSize.Width - labelAddress.Width) / 2;
labelAmount.Left = (ClientSize.Width - labelAmount.Width) / 2;
textBoxAddress.Left = (ClientSize.Width - textBoxAddress.Width) / 2;
labelMax.Left = (ClientSize.Width - labelMax.Width) / 2;
textBoxAmount.Left = (ClientSize.Width - textBoxAmount.Width - labelHNSToken.Width - 10) / 2;
labelHNSToken.Left = textBoxAmount.Left + textBoxAmount.Width + 10;
buttonSend.Left = (ClientSize.Width - buttonSend.Width) / 2;
}
private async void buttonSend_Click(object sender, EventArgs e)
{
buttonSend.Enabled = false;
var address = textBoxAddress.Text;
if (textBoxAddress.Text.Substring(0, 1) == "@")
{
// HIP-02 not supported yet
var notify = new NotifyForm("HIP-02 not supported yet");
notify.ShowDialog();
notify.Dispose();
buttonSend.Enabled = true;
return;
}
2023-07-07 14:04:47 +10:00
2023-07-15 22:17:49 +10:00
var valid = await main.ValidAddress(address);
if (!valid)
{
var notify = new NotifyForm("Invalid address");
notify.ShowDialog();
notify.Dispose();
buttonSend.Enabled = true;
return;
2023-07-07 14:04:47 +10:00
}
2023-07-15 22:17:49 +10:00
decimal amount = 0;
if (!decimal.TryParse(textBoxAmount.Text, out amount))
2023-07-07 14:04:47 +10:00
{
2023-07-15 22:17:49 +10:00
var notify = new NotifyForm("Invalid amount");
notify.ShowDialog();
notify.Dispose();
return;
}
2023-07-07 14:04:47 +10:00
2023-07-15 22:17:49 +10:00
if (amount > unlockedbalance - fee)
{
var notify = new NotifyForm("Insufficient balance");
notify.ShowDialog();
notify.Dispose();
return;
}
2023-07-07 14:04:47 +10:00
2023-07-15 22:17:49 +10:00
var content = "{\"method\": \"sendtoaddress\",\"params\": [ \"" + address + "\", " +
amount + "]}";
var output = await main.APIPost("", true, content);
var APIresp = JObject.Parse(output);
if (APIresp["error"].ToString() != "")
{
main.AddLog("Failed:");
main.AddLog(APIresp.ToString());
var error = JObject.Parse(APIresp["error"].ToString());
var ErrorMessage = error["message"].ToString();
2023-07-07 14:04:47 +10:00
2023-07-15 22:17:49 +10:00
var notify = new NotifyForm("Error Transaction Failed\n" + ErrorMessage);
notify.ShowDialog();
notify.Dispose();
return;
}
2023-07-15 22:17:49 +10:00
var hash = APIresp["result"].ToString();
var link = main.TXExplorer + hash;
var notifySuccess = new NotifyForm("Transaction Sent\nThis transaction could take up to 20 minutes to mine",
"Explorer", link);
notifySuccess.ShowDialog();
notifySuccess.Dispose();
Close();
}
2023-07-15 22:17:49 +10:00
}