/// <summary> /// Function returning all open orders (BUY or SELL) placed on any crypto currencies /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// </summary> /// <returns> /// a List of <see cref="Order"/>, representing all open orders /// </returns> override public List <Order> GetOpenOrders(string currency) { Models.OrderType oneOrderType; Models.OrderStyle oneOrderStyle = Models.OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.CEXIO; MainCryptos ccyBase; MainCryptos ccyPair; double amount; double orderPrice; List <Order> orderList = new List <Order>(); Order oneNewOrder; string ccyId = ""; string api = "https://api.coinbase.com/v2/accounts/:account_id/transactions"; Dictionary <string, string> accountId = this.GetAccountId(); foreach (KeyValuePair <string, string> row in accountId) { if (row.Key.ToUpper() == currency.ToString()) { ccyId = row.Value; } } api = "https://api.coinbase.com/v2/accounts/" + ccyId + "/transactions"; this.response = this.coinbase.SendRequest(api, "", RestSharp.Method.GET); try { if (this.response.Errors == null && this.response.Data.ToString() != "[]") { this.dicoResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.response.Data.ToString()); } else { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "NO OPEN ORDERS" } }; } } catch (Exception e) { Console.WriteLine(e.Message); this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "ERROR" } }; } return(orderList); }
/// <summary> /// Function that send an order to sell <paramref name="amount"/> <paramref name="currency"/>, /// at a limit price of <paramref name="price"/>, to coinbase market /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// <paramref name="price"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="price">limit price of the order.</param> /// <returns> /// A <see cref="Order"/> object, representing the new Sell order that has been send to Coinbase market /// </returns> override public Order Sell(string currency, double amount, double price) { Dictionary <string, string> body = new Dictionary <string, string>(); Models.OrderType oneOrderType = Models.OrderType.BUY; Models.OrderStyle oneOrderStyle = Models.OrderStyle.LIMIT; Exchanges oneExchange = Exchanges.COINBASE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = MainCryptos.USDT; Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, price); string ccyId; if (System.Linq.Enumerable.Contains(this.GetAccountId().Keys, currency)) { ccyId = this.GetAccountId()[currency]; } else { ccyId = ""; } string api = "https://api.coinbase.com/"; string queryString = "v2/accounts/" + ccyId + "/sells"; body.Add("currency", currency); body.Add("amount", amount.ToString()); this.response = this.coinbase.SendRequest(api + queryString, body, RestSharp.Method.POST); if (this.response.Errors != null) { oneNewOrder.success = false; oneNewOrder.message = this.response.Errors[0].Message; } else { oneNewOrder.success = false; oneNewOrder.message = this.response.Data.ToString(); } return(oneNewOrder); }
/// <summary> /// Function that cancel an order placed on coinbase markets, /// identified with its <paramref name="orderId"/> /// </summary> /// <param name="orderId">the ID of the order to cancel.</param> /// <returns> /// a <see cref="Order"/> object, representing the cancelled order /// </returns> override public Order CancelOrder(string currency, string orderId) { string ccyId = ""; string api = "https://api.coinbase.com/v2/accounts/:account_id/addresses"; Models.OrderType oneOrderType = Models.OrderType.CANCEL; Models.OrderStyle oneOrderStyle = Models.OrderStyle.CANCELLATION; Exchanges oneExchange = Exchanges.BINANCE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper());; Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, 0, 0); Dictionary <string, string> dicoCcyIds = this.GetAccountId(); foreach (KeyValuePair <string, string> row in dicoCcyIds) { if (row.Key.ToUpper() == currency.ToUpper()) { ccyId = row.Value; } } api = "https://api.coinbase.com/v2/accounts/" + ccyId + "/addresses"; this.response = this.coinbase.SendRequest(api, "", RestSharp.Method.POST); try { this.dicoResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.response.Data.ToString()); oneNewOrder.success = true; oneNewOrder.message = this.dicoResponse["success"]; } catch (Exception e) { Console.WriteLine(e.Message); oneNewOrder.success = false; oneNewOrder.message = "ERROR"; } return(oneNewOrder); }
/// <summary> /// Function that send <paramref name="amount"/> <paramref name="currency"/>, /// to the specified <paramref name="adress"/> /// <paramref name="currency"/> have to be in <see cref="MainCryptos"/> /// <paramref name="amount"/> have to be positive /// </summary> /// <param name="currency">the name of the cryptocurrency.</param> /// <param name="amount">amount to buy.</param> /// <param name="currency">the crypto currency to send.</param> /// <returns> /// A <see cref="Order"/> object representing the sending. /// </returns> override public Order SendCryptos(string adress, string currency, double amount) { Models.OrderType oneOrderType = Models.OrderType.SEND; Models.OrderStyle oneOrderStyle = Models.OrderStyle.WITHDRAWAL; Exchanges oneExchange = Exchanges.COINBASE; MainCryptos ccyBase = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); MainCryptos ccyPair = (MainCryptos)Enum.Parse(typeof(MainCryptos), currency.ToUpper()); Order oneNewOrder = new Order(oneExchange, oneOrderType, oneOrderStyle, false, ccyBase, ccyPair, amount, 0); string ccyId = ""; string api = "https://api.coinbase.com/v2/accounts/:account_id/transactions"; Dictionary <string, string> body = new Dictionary <string, string>(); Dictionary <string, string> accountId = this.GetAccountId(); foreach (KeyValuePair <string, string> row in accountId) { if (row.Key.ToUpper() == currency.ToString()) { ccyId = row.Value; } } api = "https://api.coinbase.com/v2/accounts/" + ccyId + "/transactions"; body.Add("type", "send"); body.Add("to", adress); body.Add("amount", amount.ToString()); body.Add("currency", currency); this.response = this.coinbase.SendRequest(api, body, RestSharp.Method.POST); try { if (this.response.Data.ToString() != "[]") { this.dicoResponse = JsonConvert.DeserializeObject <Dictionary <dynamic, dynamic> >(this.response.Data.ToString()); oneNewOrder.success = true; oneNewOrder.message = this.dicoResponse["message"]; } else { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "ERROR WHILE SENDING CRYPTOS" } }; oneNewOrder.success = false; oneNewOrder.message = "ERROR WHILE SENDING CRYPTOS"; } } catch (Exception e) { Console.WriteLine(e.Message); if (this.response != null) { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", this.response.Errors[0].Message } }; oneNewOrder.success = false; oneNewOrder.message = this.response.Errors[0].Message; return(oneNewOrder); } else { this.dicoResponse = new Dictionary <dynamic, dynamic>() { { "ERROR", "This currency isn't handled on Coinbase" } }; oneNewOrder.success = false; oneNewOrder.message = "This crypto isn't handled on Coinbase"; return(oneNewOrder); } } return(oneNewOrder); }