From d5f39fb1d1af2acf2aca6c949fa4471591008879 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Fri, 13 Jan 2023 16:49:36 +1100 Subject: [PATCH] Added comments and fixed names --- BidderGUI/Form1.Designer.cs | 44 ++++---- BidderGUI/Form1.cs | 195 +++++++++++++++++++++++++++++------- 2 files changed, 179 insertions(+), 60 deletions(-) diff --git a/BidderGUI/Form1.Designer.cs b/BidderGUI/Form1.Designer.cs index 579302a..ffec5e6 100644 --- a/BidderGUI/Form1.Designer.cs +++ b/BidderGUI/Form1.Designer.cs @@ -58,7 +58,7 @@ private void InitializeComponent() this.groupBox2 = new System.Windows.Forms.GroupBox(); this.removebutton = new System.Windows.Forms.Button(); this.button7 = new System.Windows.Forms.Button(); - this.button2 = new System.Windows.Forms.Button(); + this.addbutton = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.domaintextBox = new System.Windows.Forms.TextBox(); this.button8 = new System.Windows.Forms.Button(); @@ -93,7 +93,7 @@ private void InitializeComponent() this.button1.TabStop = false; this.button1.Text = "Import"; this.button1.UseVisualStyleBackColor = false; - this.button1.Click += new System.EventHandler(this.button1_Click); + this.button1.Click += new System.EventHandler(this.import_button_Click); // // domainslistBox // @@ -170,7 +170,7 @@ private void InitializeComponent() this.button3.TabStop = false; this.button3.Text = "Test"; this.button3.UseVisualStyleBackColor = false; - this.button3.Click += new System.EventHandler(this.button3_Click); + this.button3.Click += new System.EventHandler(this.test_button_Click); // // label5 // @@ -242,7 +242,7 @@ private void InitializeComponent() this.button4.TabStop = false; this.button4.Text = "Clear"; this.button4.UseVisualStyleBackColor = false; - this.button4.Click += new System.EventHandler(this.button4_Click); + this.button4.Click += new System.EventHandler(this.clear_button_Click); // // label8 // @@ -266,7 +266,7 @@ private void InitializeComponent() // timer1 // this.timer1.Interval = 1000; - this.timer1.Tick += new System.EventHandler(this.timer1_Tick); + this.timer1.Tick += new System.EventHandler(this.batch_timer_Tick); // // startbutton // @@ -278,7 +278,7 @@ private void InitializeComponent() this.startbutton.TabIndex = 6; this.startbutton.Text = "Start"; this.startbutton.UseVisualStyleBackColor = false; - this.startbutton.Click += new System.EventHandler(this.button5_Click); + this.startbutton.Click += new System.EventHandler(this.start_button_Click); // // stopbutton // @@ -290,7 +290,7 @@ private void InitializeComponent() this.stopbutton.TabIndex = 7; this.stopbutton.Text = "Stop"; this.stopbutton.UseVisualStyleBackColor = false; - this.stopbutton.Click += new System.EventHandler(this.button6_Click); + this.stopbutton.Click += new System.EventHandler(this.stop_button_Click); // // label9 // @@ -385,7 +385,7 @@ private void InitializeComponent() this.groupBox2.BackColor = System.Drawing.Color.Black; this.groupBox2.Controls.Add(this.removebutton); this.groupBox2.Controls.Add(this.button7); - this.groupBox2.Controls.Add(this.button2); + this.groupBox2.Controls.Add(this.addbutton); this.groupBox2.Controls.Add(this.label1); this.groupBox2.Controls.Add(this.domaintextBox); this.groupBox2.Controls.Add(this.button8); @@ -424,20 +424,20 @@ private void InitializeComponent() this.button7.TabStop = false; this.button7.Text = "Clear All"; this.button7.UseVisualStyleBackColor = false; - this.button7.Click += new System.EventHandler(this.button7_Click); + this.button7.Click += new System.EventHandler(this.clear_button_Click); // - // button2 + // addbutton // - this.button2.BackColor = System.Drawing.SystemColors.ControlDarkDark; - this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat; - this.button2.Location = new System.Drawing.Point(114, 111); - this.button2.Name = "button2"; - this.button2.Size = new System.Drawing.Size(110, 23); - this.button2.TabIndex = 7; - this.button2.TabStop = false; - this.button2.Text = "Add to list"; - this.button2.UseVisualStyleBackColor = false; - this.button2.Click += new System.EventHandler(this.button2_Click_1); + this.addbutton.BackColor = System.Drawing.SystemColors.ControlDarkDark; + this.addbutton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.addbutton.Location = new System.Drawing.Point(114, 111); + this.addbutton.Name = "addbutton"; + this.addbutton.Size = new System.Drawing.Size(110, 23); + this.addbutton.TabIndex = 7; + this.addbutton.TabStop = false; + this.addbutton.Text = "Add to list"; + this.addbutton.UseVisualStyleBackColor = false; + this.addbutton.Click += new System.EventHandler(this.add_domain_button_Click); // // label1 // @@ -470,7 +470,7 @@ private void InitializeComponent() this.button8.TabStop = false; this.button8.Text = "Export"; this.button8.UseVisualStyleBackColor = false; - this.button8.Click += new System.EventHandler(this.button8_Click); + this.button8.Click += new System.EventHandler(this.export_button_Click); // // biddinggroupBox // @@ -648,7 +648,7 @@ private void InitializeComponent() private Label label11; private GroupBox walletgroupBox; private GroupBox groupBox2; - private Button button2; + private Button addbutton; private Label label1; private TextBox domaintextBox; private Button button7; diff --git a/BidderGUI/Form1.cs b/BidderGUI/Form1.cs index c98f4a0..0f2262b 100644 --- a/BidderGUI/Form1.cs +++ b/BidderGUI/Form1.cs @@ -7,8 +7,15 @@ public Form1() InitializeComponent(); } - private void button1_Click(object sender, EventArgs e) + // Create http client to connect to the api + HttpClient httpClient = new HttpClient(); + // Create a int to store the time since last batch + int timetaken = 0; + + private void import_button_Click(object sender, EventArgs e) { + // Import domains from text file + OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { @@ -16,18 +23,17 @@ private void button1_Click(object sender, EventArgs e) } } - private void button2_Click(object sender, EventArgs e) - { - - } void refreshdomain(string file) { // Add try to stop missing file errors try { + // Open file for reading StreamReader filereader = new StreamReader(file); + // While more lines to read while (!filereader.EndOfStream) { + // Add domain to list if it isn't already in the list string domain = filereader.ReadLine(); if (!domainslistBox.Items.Contains(domain)) { @@ -40,6 +46,7 @@ void refreshdomain(string file) } filereader.Close(); } + // Log errors to log textbox catch (Exception error) { logtextBox.Text += "Error: " + error.Message + Environment.NewLine; @@ -48,43 +55,55 @@ void refreshdomain(string file) } - + // Test API + // Requires API key and wallet name. Password isn't used async void test() { + // This will curl the below URL and return the result //curl http://x:api-key@127.0.0.1:12039/wallet/$id/account + logtextBox.Text = logtextBox.Text + "Testing: http://x:" + apitextBox.Text + "@127.0.0.1:12039" + Environment.NewLine; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://127.0.0.1:12039/wallet/"+wallettextBox.Text+ "/account"); + // Add API key to header request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:"+apitextBox.Text))); + try { + // Send request and log response HttpResponseMessage response = await httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine; } + // Log errors to log textbox catch (Exception error) { logtextBox.Text = logtextBox.Text + "Error: "+ error.Message+ Environment.NewLine; } } - private void button3_Click(object sender, EventArgs e) + + private void test_button_Click(object sender, EventArgs e) { + // Run test on test button click test(); } - HttpClient httpClient = new HttpClient(); + - private void button4_Click(object sender, EventArgs e) + private void clear_button_Click(object sender, EventArgs e) { + // Clear log logtextBox.Text = ""; } - private void button6_Click(object sender, EventArgs e) + private void stop_button_Click(object sender, EventArgs e) { - //Stop sending + //Stop timers countdowntimer.Stop(); timer1.Stop(); + + // Make all fields editable and enabled apitextBox.ReadOnly = false; wallettextBox.ReadOnly = false; passtextBox.ReadOnly = false; @@ -93,19 +112,22 @@ private void button6_Click(object sender, EventArgs e) modecomboBox.Enabled = true; bidnumericUpDown.ReadOnly = false; blindnumericUpDown.ReadOnly = false; + + // Reset time label timelabel.Text = "Time till next batch:"; } - int timetaken = 0; - private void button5_Click(object sender, EventArgs e) + + private void start_button_Click(object sender, EventArgs e) { - // start sending + // Reset timers and start timetaken = 0; countdowntimer.Start(); timer1.Interval = (int)intervalnumericUpDown.Value*1000*60; timer1.Start(); + // Make all fields uneditable apitextBox.ReadOnly = true; wallettextBox.ReadOnly = true; passtextBox.ReadOnly = true; @@ -114,225 +136,316 @@ private void button5_Click(object sender, EventArgs e) modecomboBox.Enabled = false; bidnumericUpDown.ReadOnly = true; blindnumericUpDown.ReadOnly = true; - + + // Run first batch sendtransaction(); } - private async void timer1_Tick(object sender, EventArgs e) + private async void batch_timer_Tick(object sender, EventArgs e) { + // reset time since last batch variable timetaken = 0; + // Run send transaction function sendtransaction(); } async void sendtransaction() { - // Check mode is legit + // Send a batch of transactions + + // Check the selected mode is legit string[] modes = { "OPEN", "BID", "REVEAL", "REDEEM" }; if (modes.Contains(modecomboBox.Text)) { + // If there is only 1 domain left in the list + // Send a single transaction if (domainslistBox.Items.Count == 1) { + // Get domain string domain = domainslistBox.Items[0].ToString(); + // Log transaction attempt logtextBox.Text = logtextBox.Text + "Sending " + modecomboBox.Text + " for: " + domain + Environment.NewLine; + // Select mode if (modecomboBox.Text == "BID") { + // Create API call HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://127.0.0.1:12039/wallet/" + wallettextBox.Text + "/bid"); request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:" + apitextBox.Text))); string curltext = "{\"passphrase\":\"" + passtextBox.Text + "\",\"name\":\"" + domain + "\",\"broadcast\":true,\"sign\":true,\"bid\":" + bidnumericUpDown.Value * 1000000 + ",\"lockup\":" + blindnumericUpDown.Value * 1000000 + "}"; request.Content = new StringContent(curltext); + + // Send request sendapicall(request, domain); } else { + // Create API call HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://127.0.0.1:12039/wallet/" + wallettextBox.Text + "/" + modecomboBox.Text.ToLower()); request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:" + apitextBox.Text))); string curltext = "{\"passphrase\":\"" + passtextBox.Text + "\",\"name\":\"" + domain + "\",\"broadcast\":true,\"sign\":true}"; request.Content = new StringContent(curltext); + + // Send request sendapicall(request, domain); } } + // If there is more than 1 domain left in the list + // Send a batch of transactions else if (domainslistBox.Items.Count > 1) { - + // Get the top batch of domains string[] domains = domainslistBox.Items.OfType().ToArray(); domains = domains.Take((int)batchsizenumericud.Value).ToArray(); + // Select mode if (modecomboBox.Text == "BID") { - sendbatchbid(domains, modecomboBox.Text); + // Send API call + sendbatchbid(domains); } else { + // Send API call sendbatch(domains, modecomboBox.Text); } } + // If there are no domains left in the list else { + // Log error logtextBox.Text = logtextBox.Text + "No Domains Found. Cancelled Sending" + Environment.NewLine; + // Stop timers stopbutton.PerformClick(); } } + // If the selected mode is not legit else - { - logtextBox.Text = logtextBox.Text + "Invalid Mode" + Environment.NewLine; + { + // Log error + logtextBox.Text = logtextBox.Text + "Invalid Mode. Cancelled Sending" + Environment.NewLine; + // Stop timers stopbutton.PerformClick(); } } - async void sendbatchbid(string[] domains,string method) + + async void sendbatchbid(string[] domains) { + // Send a batch of bid transactions for domains try { + // Create a HTTP request with the API key HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://127.0.0.1:12039"); request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:" + apitextBox.Text))); + // Create the JSON for the API call string batch = "["; foreach (string domain in domains) { - batch = batch + "['" + method + "', '" + domain + "', " + bidnumericUpDown.Value + ", " + (bidnumericUpDown.Value + blindnumericUpDown.Value) + "], "; + batch = batch + "['BID', '" + domain + "', " + bidnumericUpDown.Value + ", " + (bidnumericUpDown.Value + blindnumericUpDown.Value) + "], "; } + // Finish the JSON by removing the last comma and adding a closing bracket batch = batch.Substring(0, batch.Length - 2) + "]"; + + // Log transaction attempt logtextBox.Text = logtextBox.Text + "Sending: " + batch + Environment.NewLine; + // Create the API call request.Content = new StringContent("{\"method\": \"sendbatch\",\"params\": [ \"" + batch + "\"]}"); + + // Send request HttpResponseMessage response = await httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); + + // Log response logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine; + // Remove domains from list foreach (string domain in domains) { domainslistBox.Items.Remove(domain); } + // If there are no domains left in the list + // Stop timers if (domainslistBox.Items.Count == 0) { logtextBox.Text = logtextBox.Text + "All domains sent" + Environment.NewLine; stopbutton.PerformClick(); } } - catch (Exception error) + // If there is an error + catch (Exception ex) { - logtextBox.Text = logtextBox.Text + "ERROR: " + error.Message + Environment.NewLine; + // Log error + logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine; + // Stop timers stopbutton.PerformClick(); } } async void sendbatch(string[] domains, string method) { + // Send a batch of transactions for domains try { + // Create a HTTP request with the API key HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "http://127.0.0.1:12039"); request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes("x:" + apitextBox.Text))); + // Create the JSON for the API call string batch = "["; foreach (string domain in domains) { batch = batch + "['" + method + "', '" + domain + "'], "; } + // Finish the JSON by removing the last comma and adding a closing bracket batch = batch.Substring(0, batch.Length - 2) + "]"; + + // Log transaction attempt logtextBox.Text = logtextBox.Text + "Sending: " + batch + Environment.NewLine; + // Create the API call request.Content = new StringContent("{\"method\": \"sendbatch\",\"params\": [ \"" + batch + "\"]}"); + + // Send request HttpResponseMessage response = await httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); + + // Log response logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine; + + // Remove domains from list foreach (string domain in domains) { domainslistBox.Items.Remove(domain); } + // If there are no domains left in the list + // Stop timers if (domainslistBox.Items.Count == 0) { logtextBox.Text = logtextBox.Text + "All domains sent" + Environment.NewLine; stopbutton.PerformClick(); } } - catch (Exception error) + // If there is an error + catch (Exception ex) { - logtextBox.Text = logtextBox.Text + "ERROR: " + error.Message + Environment.NewLine; + // Log error + logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine; + // Stop timers stopbutton.PerformClick(); } - + } async void sendapicall(HttpRequestMessage request,string domain) { + // Send a single transaction for a domain try { + // Send request HttpResponseMessage response = await httpClient.SendAsync(request); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); + // Log response logtextBox.Text = logtextBox.Text + responseBody + Environment.NewLine; + + // Remove domain from list domainslistBox.Items.Remove(domain); } - catch (Exception error) + // If there is an error + catch (Exception ex) { - logtextBox.Text = logtextBox.Text + "Error: " + error.Message + Environment.NewLine; - timer1.Stop(); + // Log error + logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine; + // Stop timers + stopbutton.PerformClick(); } } - private void button2_Click_1(object sender, EventArgs e) + private void add_domain_button_Click(object sender, EventArgs e) { + // If there is a domain in the textbox and it is not already in the list if (domaintextBox.Text != "" && !domainslistBox.Items.Contains(domaintextBox.Text)) { + // Add the domain to the list domainslistBox.Items.Add(domaintextBox.Text); domaintextBox.Text = ""; } } - private void button7_Click(object sender, EventArgs e) + private void clear_list_button_Click(object sender, EventArgs e) { + // Clear the domain list domainslistBox.Items.Clear(); removebutton.Enabled = false; } private void removebutton_Click(object sender, EventArgs e) { + // Remove the selected domain from the list domainslistBox.Items.Remove(domainslistBox.SelectedItem.ToString()); removebutton.Enabled = false; } private void domainslistBox_SelectedIndexChanged(object sender, EventArgs e) { + // If there is a domain selected in the list enable the remove button removebutton.Enabled = true; } private void modecomboBox_SelectedIndexChanged(object sender, EventArgs e) { + // If the mode is set to bid if (modecomboBox.Text == "BID") { + // Show the bid and blind bid fields biddinggroupBox.Show(); } else { + // Hide the bid and blind bid fields biddinggroupBox.Hide(); } } - private void button8_Click(object sender, EventArgs e) + private void export_button_Click(object sender, EventArgs e) { + // If there are domains in the list if (domainslistBox.Items.Count > 0) { + // Create a new save file dialog SaveFileDialog save = new SaveFileDialog(); save.Filter = "Text File|*.txt"; + + // If user saves file if (save.ShowDialog() == DialogResult.OK) { + // Write domains to file try { + // Create a new file stream StreamWriter writer = new StreamWriter(save.FileName); + + // For each file in the domain list box foreach (string domain in domainslistBox.Items.OfType().ToArray()) { + // Write domain to file writer.WriteLine(domain); } + // Close the file stream writer.Dispose(); } - catch (Exception error) + // If there is an error + catch (Exception ex) { - logtextBox.Text += "Error: " + error.Message + Environment.NewLine; + // Log error + logtextBox.Text = logtextBox.Text + "Error: " + ex.Message + Environment.NewLine; } } @@ -342,19 +455,25 @@ private void button8_Click(object sender, EventArgs e) private void domaintextBox_KeyPress(object sender, KeyPressEventArgs e) { + // If the enter key is pressed if (e.KeyChar == (char)Keys.Enter) { - button2_Click_1(sender, e); + // Press the add domain button + addbutton.PerformClick(); } } private void countdowntimer_Tick(object sender, EventArgs e) { + // Add time since last batch timetaken += 1; + + // Calculate time to minutes and seconds int timeleftsec = (int)intervalnumericUpDown.Value * 60 - timetaken; TimeSpan time = TimeSpan.FromSeconds(timeleftsec); - timelabel.Text = "Time till next batch: " + time.ToString(@"hh\:mm\:ss"); + // Update time label + timelabel.Text = "Time till next batch: " + time.ToString(@"mm\:ss"); } } } \ No newline at end of file