public static void CancelMyOrder(OpenOrder ord) { //This function takes the order that we created, cancel it then broadcast the request to the connected critical node //First things first, cancel the order on my side (so even if server is down, order is still cancelled) lock (MyOpenOrderList) { if (ord.order_stage >= 3) { return; } //Can't cancel as order is in trade for (int i = 0; i < MyOpenOrderList.Count; i++) { if (MyOpenOrderList[i].order_nonce.Equals(ord.order_nonce) == true) { OpenOrder myord = MyOpenOrderList[i]; RemoveSavedOrder(ord); //Take the order out of the saved table MyOpenOrderList.RemoveAt(i); break; //Take it out } } } if (ord.queued_order == true) { //Order is already not posted anyway return; } //Next take it out of the open orders if (ord.is_request == false) { lock (OpenOrderList[ord.market]) { for (int i = OpenOrderList[ord.market].Count - 1; i >= 0; i--) { //Remove any order that matches our nonce if (OpenOrderList[ord.market][i].order_nonce.Equals(ord.order_nonce) == true) { OpenOrderList[ord.market].RemoveAt(i); } } } //Now remove the order from view if (NebliDex_UI != null) { NebliDex_UI.RemoveOrderFromView(ord); } } //Now get an CN and send the request DexConnection dex = null; lock (DexConnectionList) { for (int i = 0; i < DexConnectionList.Count; i++) { if (DexConnectionList[i].contype == 3 && DexConnectionList[i].outgoing == true) { dex = DexConnectionList[i]; break; //Found our connection } } } if (dex == null) { return; } JObject js = new JObject(); js["cn.method"] = "cn.cancelorder"; js["cn.response"] = 0; js["order.nonce"] = ord.order_nonce; js["order.market"] = ord.market; js["order.type"] = ord.type; js["order.is_request"] = ord.is_request.ToString(); string json_encoded = JsonConvert.SerializeObject(js); try { SendCNServerAction(dex, 22, json_encoded); //Send cancel request, no need to wait } catch (Exception) { NebliDexNetLog("Failed to broadcast cancellation request"); } return; //Otherwise it is ok to cancel our order }
public static bool CancelMarketOrder(JObject jord, bool checkip) { //This will attempt to find the order and remove it OpenOrder ord = new OpenOrder(); ord.order_nonce = jord["order.nonce"].ToString(); ord.type = Convert.ToInt32(jord["order.type"].ToString()); if (ord.type != 0 && ord.type != 1) { return(false); } ord.market = Convert.ToInt32(jord["order.market"].ToString()); ord.is_request = Convert.ToBoolean(jord["order.is_request"].ToString()); if (ord.market < 0 || ord.market >= total_markets) { return(false); } //Unsupported data string ip = ""; string port = ""; string cn_ip = ""; if (checkip == true) { ip = jord["order.ip"].ToString(); port = jord["order.port"].ToString(); cn_ip = jord["order.cn_ip"].ToString(); } if (ord.is_request == true) { return(false); } //Cannot remove order request this way bool order_exist = false; lock (OpenOrderList[ord.market]) { for (int i = OpenOrderList[ord.market].Count - 1; i >= 0; i--) { if (OpenOrderList[ord.market][i].order_nonce.Equals(ord.order_nonce) == true) { //Found order if (checkip == true) { //CNs needs to validate that the cancellation request was made by the person who created it if (OpenOrderList[ord.market][i].ip_address_port[0].Equals(ip) == true && OpenOrderList[ord.market][i].ip_address_port[1].Equals(port) == true && OpenOrderList[ord.market][i].cn_relayer_ip.Equals(cn_ip) == true) { OpenOrderList[ord.market].RemoveAt(i); order_exist = true; } else { return(false); //Someone elses order } } else { OpenOrderList[ord.market].RemoveAt(i); //Assume the CN did its job order_exist = true; } } } } //If the order doesn't exist yet, it may be on the way, store as cancellation token for when it gets here if (order_exist == false) { lock (CancelOrderTokenList) { CancelOrderToken tk = new CancelOrderToken(); tk.arrivetime = UTCTime(); //Will delete in 5 minutes if no order arrives tk.order_nonce = ord.order_nonce; CancelOrderTokenList.Add(tk); } } else { //Now remove from view if (NebliDex_UI != null) { NebliDex_UI.RemoveOrderFromView(ord); } } //Also remove the order request that this order was linked to lock (MyOpenOrderList) { for (int i = MyOpenOrderList.Count - 1; i >= 0; i--) { if (MyOpenOrderList[i].order_nonce.Equals(ord.order_nonce) == true && MyOpenOrderList[i].queued_order == false) { if (program_loaded == true) { if (MyOpenOrderList[i].is_request == true) { //let the user know that the other user closed the order ShowTradeMessage("Trade Failed:\nOrder was cancelled by the creator or the creator is offline!"); } } if (MyOpenOrderList[i].is_request == true) { OpenOrder myord = MyOpenOrderList[i]; MyOpenOrderList.RemoveAt(i); } else { QueueMyOrderNoLock(MyOpenOrderList[i], null); } } } } //Successfully removed order return(true); }