From b35028ad38fad236978f96dcb7222cb432bf9990 Mon Sep 17 00:00:00 2001 From: bl4z Date: Wed, 4 Nov 2020 10:07:09 +0100 Subject: [PATCH] order settings --- NHB3/Api.cs | 54 ++++++++---- NHB3/ApiConnect.cs | 36 +++++++- NHB3/Home.cs | 60 +++++++++++-- NHB3/OrderForm.Designer.cs | 170 +++++++++++++++++++++++++++---------- NHB3/OrderForm.cs | 49 ++++++++++- 5 files changed, 297 insertions(+), 72 deletions(-) diff --git a/NHB3/Api.cs b/NHB3/Api.cs index 125c83d..6148cc6 100644 --- a/NHB3/Api.cs +++ b/NHB3/Api.cs @@ -1,6 +1,9 @@ -using System; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; using System.Collections.Generic; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; @@ -81,6 +84,7 @@ private static string JoinSegments(List segments) sb.Append(segment); } } + Console.ForegroundColor = ConsoleColor.DarkGray; Console.Out.WriteLine("req: [" + sb.ToString() + "]"); return sb.ToString(); } @@ -101,9 +105,14 @@ private static string CalcHMACSHA256Hash(string plaintext, string salt) private string srvrTime() { string timeResponse = this.get("/api/v2/time", false); - ServerTime serverTimeObject = Newtonsoft.Json.JsonConvert.DeserializeObject(timeResponse); - time = serverTimeObject.serverTime; - return time; + JObject timeObject = JsonConvert.DeserializeObject(timeResponse); + + if (timeObject["error_id"] == null) + { + this.time = "" + timeObject["serverTime"]; + return this.time; + } + return "0"; } public string get(string url, bool auth) @@ -124,9 +133,13 @@ public string get(string url, bool auth) } var response = client.Execute(request, RestSharp.Method.GET); - //Console.Out.WriteLine("res: [" + response.Content + "]"); - var content = response.Content; - return content; + Console.Out.WriteLine("res: [" + response.Content + "]"); + + if (response.StatusCode != HttpStatusCode.OK) + { + return "{error_id: -1}"; + } + return response.Content; } public string post(string url, string payload, bool requestId) @@ -156,9 +169,14 @@ public string post(string url, string payload, bool requestId) } var response = client.Execute(request, RestSharp.Method.POST); - //Console.Out.WriteLine("res: [" + response.Content + "]"); - var content = response.Content; - return content; + Console.ForegroundColor = ConsoleColor.DarkGray; + Console.Out.WriteLine("res: [" + response.Content + "]"); + + if (response.StatusCode != HttpStatusCode.OK) + { + return "{error_id: -1}"; + } + return response.Content; } public string delete(string url, bool requestId) @@ -181,14 +199,14 @@ public string delete(string url, bool requestId) } var response = client.Execute(request, RestSharp.Method.DELETE); - //Console.Out.WriteLine("res: [" + response.Content + "]"); - var content = response.Content; - return content; - } - } + Console.ForegroundColor = ConsoleColor.DarkGray; + Console.Out.WriteLine("res: [" + response.Content + "]"); - public class ServerTime - { - public string serverTime { get; set; } + if (response.StatusCode != HttpStatusCode.OK) + { + return "{error_id: -1}"; + } + return response.Content; + } } } \ No newline at end of file diff --git a/NHB3/ApiConnect.cs b/NHB3/ApiConnect.cs index 3fc7236..331120f 100644 --- a/NHB3/ApiConnect.cs +++ b/NHB3/ApiConnect.cs @@ -24,6 +24,7 @@ public void setup(ApiSettings settings) { //read algo settings string algosResponse = api.get("/main/api/v2/mining/algorithms", false); + JObject aslgoObject = JsonConvert.DeserializeObject(algosResponse); if (aslgoObject["error_id"] == null) { @@ -33,6 +34,10 @@ public void setup(ApiSettings settings) { public JArray getMarket() { string marketResponse = api.get("/main/api/v2/public/orders/active2", false); + if (String.IsNullOrEmpty(marketResponse)) { + return new JArray(); + } + JObject marektObject = JsonConvert.DeserializeObject(marketResponse); if (marektObject["error_id"] == null) { @@ -43,8 +48,8 @@ public JArray getMarket() { public bool settingsOk() { string accountsResponse = api.get("/main/api/v2/accounting/accounts2", true); - JObject accountsObject = JsonConvert.DeserializeObject(accountsResponse); + JObject accountsObject = JsonConvert.DeserializeObject(accountsResponse); if (accountsObject["error_id"] == null) { return true; @@ -54,13 +59,16 @@ public bool settingsOk() { public JObject getBalance(string currency) { string accountsResponse = api.get("/main/api/v2/accounting/accounts2", true); + JObject accountsObject = JsonConvert.DeserializeObject(accountsResponse); if (accountsObject["error_id"] != null) { //api call failed Console.WriteLine("Error reading API ... {0}", accountsObject); + return null; } + JArray currencies = accountsObject["currencies"] as JArray; foreach (JObject obj in currencies) { @@ -228,6 +236,17 @@ public ApiSettings readSettings() { return new ApiSettings(); } + public OrdersSettings readOrdersSettings() + { + String fileName = Path.Combine(Directory.GetCurrentDirectory(), "orders.json"); + if (File.Exists(fileName)) + { + OrdersSettings saved = JsonConvert.DeserializeObject(File.ReadAllText(@fileName)); + return saved; + } + return new OrdersSettings(); + } + public JObject getAlgo(string algo) { foreach (JObject obj in algorithms) { @@ -245,6 +264,9 @@ private string getApiUrl(int Enviorment) { return "https://api2.nicehash.com"; } + else if (Enviorment == 99) { + return "https://api-test-dev.nicehash.com"; + } return "https://api-test.nicehash.com"; } @@ -259,5 +281,17 @@ public class ApiSettings public int Enviorment { get; set; } } + public class OrdersSettings + { + public List OrderList { get; set; } + } + + public class OrderSettings + { + + public string Id { get; set; } + + public string MaxPrice { get; set; } + } } } diff --git a/NHB3/Home.cs b/NHB3/Home.cs index b8b5de4..3b5bcbb 100644 --- a/NHB3/Home.cs +++ b/NHB3/Home.cs @@ -31,6 +31,8 @@ public Home() ac = new ApiConnect(); ApiSettings saved = ac.readSettings(); + OrdersSettings orders = ac.readOrdersSettings(); + if (saved.OrganizationID != null) { ac.setup(saved); @@ -100,6 +102,9 @@ private void refreshBalance() { private void refreshOrders(bool fromThread) { + //read custom order settings + ApiConnect.OrdersSettings cos = ac.readOrdersSettings(); + if (ac.connected) { orders = ac.getOrders(); @@ -125,6 +130,10 @@ private void refreshOrders(bool fromThread) cleanOrder.Add("spentPercent", "" + spent_factor.ToString("0.00")+ "%"); cleanOrder.Add("limit", "" + order["limit"]); cleanOrder.Add("price", "" + order["price"]); + + //max price + cleanOrder.Add("maxPrice", getMaxPrice("" + order["id"], cos)); + cleanOrder.Add("rigsCount", "" + order["rigsCount"]); cleanOrder.Add("acceptedCurrentSpeed", "" + order["acceptedCurrentSpeed"]); cleanOrders.Add(cleanOrder); @@ -148,6 +157,17 @@ private void refreshOrders(bool fromThread) } } + private string getMaxPrice(string id, ApiConnect.OrdersSettings cos) { + foreach (var order in cos.OrderList) + { + if (id.Equals(order.Id)) + { + return order.MaxPrice; + } + } + return ""; + } + private void refreshMarket() { if (ac.connected) { @@ -197,7 +217,12 @@ private void runBot() { } toolStripStatusLabel1.Text = "Working"; + + //read order individual settings + ApiConnect.OrdersSettings cos = ac.readOrdersSettings(); + BotSettings saved = JsonConvert.DeserializeObject(File.ReadAllText(@fileName)); + Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("bot iteration tasks {0} {1} {2}", saved.reffilOrder, saved.lowerPrice, saved.increasePrice); Control.CheckForIllegalCrossThreadCalls = false; @@ -207,20 +232,23 @@ private void runBot() { refreshMarket(); } - //do refill?? + Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("orders to process: {0}", orders.Count); + //do refill?? if (saved.reffilOrder) { foreach (JObject order in orders) { float payed = float.Parse("" + order["payedAmount"], CultureInfo.InvariantCulture); float available = float.Parse("" + order["availableAmount"], CultureInfo.InvariantCulture); float spent_factor = payed/available*100; + Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("?refill?; order {0}, payed {1}, available {2}, percent {3}", order["id"], payed, available, spent_factor.ToString("0.00")); if (spent_factor > 90) { JObject algo = ac.getAlgo(""+order["algorithm"]["algorithm"]); + Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("===> refill order for {0}", algo["minimalOrderAmount"]); ac.refillOrder(""+order["id"], ""+algo["minimalOrderAmount"]); } @@ -233,17 +261,36 @@ private void runBot() { string order_type = "" + order["type"]["code"]; if (order_type.Equals("STANDARD")) { + //get order custom settings + String omp = getMaxPrice("" + order["id"], cos); + float maxOrderPriceLimit = 0F; + if (!String.IsNullOrEmpty(omp)) { + maxOrderPriceLimit = float.Parse("" + omp, CultureInfo.InvariantCulture); + } + JObject algo = ac.getAlgo("" + order["algorithm"]["algorithm"]); float order_speed = float.Parse("" + order["acceptedCurrentSpeed"], CultureInfo.InvariantCulture); + float rigs_count = float.Parse("" + order["rigsCount"], CultureInfo.InvariantCulture); float order_price = float.Parse("" + order["price"], CultureInfo.InvariantCulture); float price_step_down = float.Parse("" + algo["priceDownStep"], CultureInfo.InvariantCulture); - Console.WriteLine("?adjust price?; order {0}, speed {1}, price {2}, step_down {3}", order["id"], order_speed, order_price, price_step_down); - if (saved.increasePrice && order_speed == 0) { + Console.ForegroundColor = ConsoleColor.Green; + Console.WriteLine("?adjust price?; order {0}, speed {1}, rigs {2}, price {3}, step_down {4}, max order limit {5}", order["id"], order_speed, rigs_count, order_price, price_step_down, maxOrderPriceLimit); + + if (saved.increasePrice && (order_speed == 0 || rigs_count == 0)) { float new_price = (float)Math.Round(order_price + (price_step_down * -1), 4); - Console.WriteLine("===> price up order to {0}", new_price); - ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]); - } else if (saved.lowerPrice && order_speed > 0) { + + if (maxOrderPriceLimit > 0 && new_price > maxOrderPriceLimit) { + Console.ForegroundColor = ConsoleColor.Red; + Console.WriteLine("===> price up denied - max limit enforced {0} {1}", new_price, maxOrderPriceLimit); + } + else + { + Console.ForegroundColor = ConsoleColor.Yellow; + Console.WriteLine("===> price up order to {0}", new_price); + ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]); + } + } else if (saved.lowerPrice && (order_speed > 0 || rigs_count > 0)) { Dictionary market = getOrderPriceRangesForAlgoAndMarket("" + order["algorithm"]["algorithm"], "" + order["market"]); var list = market.Keys.ToList(); list.Sort(); @@ -260,6 +307,7 @@ private void runBot() { if (idx > 1) { float new_price = (float)Math.Round(order_price + price_step_down, 4); + Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("===> price down order to {0}", new_price); ac.updateOrder("" + order["algorithm"]["algorithm"], "" + order["id"], new_price.ToString(new CultureInfo("en-US")), "" + order["limit"]); } diff --git a/NHB3/OrderForm.Designer.cs b/NHB3/OrderForm.Designer.cs index a4e24ce..a5adf45 100644 --- a/NHB3/OrderForm.Designer.cs +++ b/NHB3/OrderForm.Designer.cs @@ -58,23 +58,30 @@ private void InitializeComponent() this.label16 = new System.Windows.Forms.Label(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); + this.tbId = new System.Windows.Forms.TextBox(); + this.amountDetailsLbl2 = new System.Windows.Forms.Label(); this.tbAvailableAmount = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.tabPage2 = new System.Windows.Forms.TabPage(); + this.limitDetailsLbl2 = new System.Windows.Forms.Label(); + this.priceDetailsLbl2 = new System.Windows.Forms.Label(); this.btnCancel = new System.Windows.Forms.Button(); this.btnUpdate = new System.Windows.Forms.Button(); + this.tabPage3 = new System.Windows.Forms.TabPage(); + this.btnSave = new System.Windows.Forms.Button(); + this.btnLoad = new System.Windows.Forms.Button(); + this.label5 = new System.Windows.Forms.Label(); + this.label7 = new System.Windows.Forms.Label(); + this.tbMaxPrice = new System.Windows.Forms.TextBox(); this.groupBox1 = new System.Windows.Forms.GroupBox(); this.lblCreate = new System.Windows.Forms.Label(); this.lblPool = new System.Windows.Forms.Label(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.lblErrorCreate = new System.Windows.Forms.Label(); - this.amountDetailsLbl2 = new System.Windows.Forms.Label(); - this.priceDetailsLbl2 = new System.Windows.Forms.Label(); - this.limitDetailsLbl2 = new System.Windows.Forms.Label(); - this.tbId = new System.Windows.Forms.TextBox(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); this.tabPage2.SuspendLayout(); + this.tabPage3.SuspendLayout(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); @@ -344,8 +351,9 @@ private void InitializeComponent() // this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Controls.Add(this.tabPage2); + this.tabControl1.Controls.Add(this.tabPage3); this.tabControl1.Enabled = false; - this.tabControl1.Location = new System.Drawing.Point(12, 392); + this.tabControl1.Location = new System.Drawing.Point(12, 385); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(610, 147); @@ -368,6 +376,25 @@ private void InitializeComponent() this.tabPage1.Text = "Refill order"; this.tabPage1.UseVisualStyleBackColor = true; // + // tbId + // + this.tbId.Location = new System.Drawing.Point(156, 75); + this.tbId.Name = "tbId"; + this.tbId.Size = new System.Drawing.Size(237, 26); + this.tbId.TabIndex = 32; + this.tbId.Visible = false; + // + // amountDetailsLbl2 + // + this.amountDetailsLbl2.AutoSize = true; + this.amountDetailsLbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.amountDetailsLbl2.ForeColor = System.Drawing.SystemColors.ControlDarkDark; + this.amountDetailsLbl2.Location = new System.Drawing.Point(399, 11); + this.amountDetailsLbl2.Name = "amountDetailsLbl2"; + this.amountDetailsLbl2.Size = new System.Drawing.Size(63, 17); + this.amountDetailsLbl2.TabIndex = 31; + this.amountDetailsLbl2.Text = "[amount]"; + // // tbAvailableAmount // this.tbAvailableAmount.Enabled = false; @@ -403,6 +430,28 @@ private void InitializeComponent() this.tabPage2.Text = "Edit order"; this.tabPage2.UseVisualStyleBackColor = true; // + // limitDetailsLbl2 + // + this.limitDetailsLbl2.AutoSize = true; + this.limitDetailsLbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.limitDetailsLbl2.ForeColor = System.Drawing.SystemColors.ControlDarkDark; + this.limitDetailsLbl2.Location = new System.Drawing.Point(399, 43); + this.limitDetailsLbl2.Name = "limitDetailsLbl2"; + this.limitDetailsLbl2.Size = new System.Drawing.Size(55, 17); + this.limitDetailsLbl2.TabIndex = 31; + this.limitDetailsLbl2.Text = "[speed]"; + // + // priceDetailsLbl2 + // + this.priceDetailsLbl2.AutoSize = true; + this.priceDetailsLbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.priceDetailsLbl2.ForeColor = System.Drawing.SystemColors.ControlDarkDark; + this.priceDetailsLbl2.Location = new System.Drawing.Point(399, 11); + this.priceDetailsLbl2.Name = "priceDetailsLbl2"; + this.priceDetailsLbl2.Size = new System.Drawing.Size(47, 17); + this.priceDetailsLbl2.TabIndex = 31; + this.priceDetailsLbl2.Text = "[price]"; + // // btnCancel // this.btnCancel.ForeColor = System.Drawing.Color.DarkRed; @@ -424,6 +473,68 @@ private void InitializeComponent() this.btnUpdate.UseVisualStyleBackColor = true; this.btnUpdate.Click += new System.EventHandler(this.btnUpdate_Click); // + // tabPage3 + // + this.tabPage3.Controls.Add(this.btnSave); + this.tabPage3.Controls.Add(this.btnLoad); + this.tabPage3.Controls.Add(this.label5); + this.tabPage3.Controls.Add(this.label7); + this.tabPage3.Controls.Add(this.tbMaxPrice); + this.tabPage3.Location = new System.Drawing.Point(4, 29); + this.tabPage3.Name = "tabPage3"; + this.tabPage3.Padding = new System.Windows.Forms.Padding(3); + this.tabPage3.Size = new System.Drawing.Size(602, 114); + this.tabPage3.TabIndex = 2; + this.tabPage3.Text = "Bot settings"; + this.tabPage3.UseVisualStyleBackColor = true; + // + // btnSave + // + this.btnSave.Location = new System.Drawing.Point(454, 72); + this.btnSave.Name = "btnSave"; + this.btnSave.Size = new System.Drawing.Size(140, 32); + this.btnSave.TabIndex = 36; + this.btnSave.Text = "Save"; + this.btnSave.UseVisualStyleBackColor = true; + this.btnSave.Click += new System.EventHandler(this.btnSave_Click); + // + // btnLoad + // + this.btnLoad.Location = new System.Drawing.Point(10, 72); + this.btnLoad.Name = "btnLoad"; + this.btnLoad.Size = new System.Drawing.Size(140, 32); + this.btnLoad.TabIndex = 35; + this.btnLoad.Text = "Load"; + this.btnLoad.UseVisualStyleBackColor = true; + this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click); + // + // label5 + // + this.label5.AutoSize = true; + this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.label5.ForeColor = System.Drawing.SystemColors.ControlDarkDark; + this.label5.Location = new System.Drawing.Point(399, 11); + this.label5.Name = "label5"; + this.label5.Size = new System.Drawing.Size(47, 17); + this.label5.TabIndex = 34; + this.label5.Text = "[price]"; + // + // label7 + // + this.label7.AutoSize = true; + this.label7.Location = new System.Drawing.Point(6, 9); + this.label7.Name = "label7"; + this.label7.Size = new System.Drawing.Size(76, 20); + this.label7.TabIndex = 32; + this.label7.Text = "Max price"; + // + // tbMaxPrice + // + this.tbMaxPrice.Location = new System.Drawing.Point(142, 6); + this.tbMaxPrice.Name = "tbMaxPrice"; + this.tbMaxPrice.Size = new System.Drawing.Size(251, 26); + this.tbMaxPrice.TabIndex = 33; + // // groupBox1 // this.groupBox1.Controls.Add(this.rbEU); @@ -479,47 +590,6 @@ private void InitializeComponent() this.lblErrorCreate.Text = "Error creating order"; this.lblErrorCreate.Visible = false; // - // amountDetailsLbl2 - // - this.amountDetailsLbl2.AutoSize = true; - this.amountDetailsLbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.amountDetailsLbl2.ForeColor = System.Drawing.SystemColors.ControlDarkDark; - this.amountDetailsLbl2.Location = new System.Drawing.Point(399, 11); - this.amountDetailsLbl2.Name = "amountDetailsLbl2"; - this.amountDetailsLbl2.Size = new System.Drawing.Size(63, 17); - this.amountDetailsLbl2.TabIndex = 31; - this.amountDetailsLbl2.Text = "[amount]"; - // - // priceDetailsLbl2 - // - this.priceDetailsLbl2.AutoSize = true; - this.priceDetailsLbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.priceDetailsLbl2.ForeColor = System.Drawing.SystemColors.ControlDarkDark; - this.priceDetailsLbl2.Location = new System.Drawing.Point(399, 11); - this.priceDetailsLbl2.Name = "priceDetailsLbl2"; - this.priceDetailsLbl2.Size = new System.Drawing.Size(47, 17); - this.priceDetailsLbl2.TabIndex = 31; - this.priceDetailsLbl2.Text = "[price]"; - // - // limitDetailsLbl2 - // - this.limitDetailsLbl2.AutoSize = true; - this.limitDetailsLbl2.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.limitDetailsLbl2.ForeColor = System.Drawing.SystemColors.ControlDarkDark; - this.limitDetailsLbl2.Location = new System.Drawing.Point(399, 43); - this.limitDetailsLbl2.Name = "limitDetailsLbl2"; - this.limitDetailsLbl2.Size = new System.Drawing.Size(55, 17); - this.limitDetailsLbl2.TabIndex = 31; - this.limitDetailsLbl2.Text = "[speed]"; - // - // tbId - // - this.tbId.Location = new System.Drawing.Point(156, 75); - this.tbId.Name = "tbId"; - this.tbId.Size = new System.Drawing.Size(237, 26); - this.tbId.TabIndex = 32; - this.tbId.Visible = false; - // // OrderForm // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); @@ -555,6 +625,8 @@ private void InitializeComponent() this.tabPage1.PerformLayout(); this.tabPage2.ResumeLayout(false); this.tabPage2.PerformLayout(); + this.tabPage3.ResumeLayout(false); + this.tabPage3.PerformLayout(); this.groupBox1.ResumeLayout(false); this.groupBox1.PerformLayout(); this.groupBox2.ResumeLayout(false); @@ -609,5 +681,11 @@ private void InitializeComponent() private System.Windows.Forms.Label limitDetailsLbl2; private System.Windows.Forms.Label priceDetailsLbl2; private System.Windows.Forms.TextBox tbId; + private System.Windows.Forms.TabPage tabPage3; + private System.Windows.Forms.Label label5; + private System.Windows.Forms.Label label7; + private System.Windows.Forms.TextBox tbMaxPrice; + private System.Windows.Forms.Button btnLoad; + private System.Windows.Forms.Button btnSave; } } \ No newline at end of file diff --git a/NHB3/OrderForm.cs b/NHB3/OrderForm.cs index 82ef5a8..782eff9 100644 --- a/NHB3/OrderForm.cs +++ b/NHB3/OrderForm.cs @@ -1,9 +1,12 @@ -using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.Globalization; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -268,5 +271,49 @@ private void btnCancel_Click(object sender, EventArgs e) this.Close(); } } + + private void btnLoad_Click(object sender, EventArgs e) + { + string id = "" + this.tbId.Text; + ApiConnect.OrdersSettings cos = ac.readOrdersSettings(); + + foreach (var order in cos.OrderList) { + if (id.Equals(order.Id)) { + this.tbMaxPrice.Text = ""+order.MaxPrice; + } + } + } + + private void btnSave_Click(object sender, EventArgs e) + { + string id = "" + this.tbId.Text; + + ApiConnect.OrdersSettings wos = ac.readOrdersSettings(); + wos.OrderList = new List(); + + ApiConnect.OrdersSettings cos = ac.readOrdersSettings(); + + bool added = false; + foreach (var order in cos.OrderList) + { + if (id.Equals(order.Id)) + { + order.MaxPrice = this.tbMaxPrice.Text; + added = true; + } + wos.OrderList.Add(order); + } + + if (!added) { + ApiConnect.OrderSettings osNew = new ApiConnect.OrderSettings(); + osNew.Id = id; + osNew.MaxPrice = this.tbMaxPrice.Text; + wos.OrderList.Add(osNew); + } + + String fileName = Path.Combine(Directory.GetCurrentDirectory(), "orders.json"); + File.WriteAllText(fileName, JsonConvert.SerializeObject(wos)); + this.Close(); + } } } \ No newline at end of file