/// <summary> /// <see cref="GetWallet"/> /// </summary> public async Task <Wallet> GetWalletAsync() { string apiUrl = "/accounts"; string response = await request.Query("GET", apiUrl, null, true).ConfigureAwait(false); if (string.IsNullOrEmpty(response)) { throw new OkexNoResponse(string.Format("Request to {0} returned empty", apiUrl)); } JToken jsonResponse = JsonConvert.DeserializeObject <JToken>(response); ValidateResponse(apiUrl, jsonResponse); var wallet = new Wallet(); JArray jArray = jsonResponse as JArray; if (jArray.Count == 0) { return(wallet); // user has not any token in wallet } try { foreach (var jToken in jArray) { wallet.Currencies.Add(new Holding() { Currency = jToken["currency"].Value <string>(), Balance = jToken["balance"].Value <double>(), Available = jToken["available"].Value <double>() }); } } catch { throw new OkexResponseParseError(string.Format("Response of {0} couldn't be parsed as expected", apiUrl)); } //TODO: Calculate USD and BTC equivalents of wallet by also requesting ticker information. //wallet.BtcEquivalent = null; //wallet.UsdEquivalent = null; return(wallet); }
/// <summary> /// <see cref="GetMargin(string)"/> /// </summary> /// <param name="symbol">symbol, i.e. "BTC-USD-SWAP"</param> public async Task <Margin> GetMarginAsync(string symbol) { string apiUrl = "/" + symbol + "/accounts"; string response = await request.Query("GET", apiUrl, null, true).ConfigureAwait(false); if (string.IsNullOrEmpty(response)) { throw new OkexNoResponse(string.Format("Request to {0} returned empty", apiUrl)); } JToken jsonResponse = JsonConvert.DeserializeObject <JToken>(response); ValidateResponse(apiUrl, jsonResponse); var margin = new Margin(); try { margin.Symbol = jsonResponse["info"]["instrument_id"].Value <string>(); margin.Balance = jsonResponse["info"]["equity"].Value <double>(); margin.Available = jsonResponse["info"]["total_avail_balance"].Value <double>(); margin.UnrealizedPnL = jsonResponse["info"]["unrealized_pnl"].Value <double>(); margin.MarginLeverage = jsonResponse["info"]["margin_ratio"].Value <double>(); margin.MarginType = jsonResponse["info"]["margin_mode"].Value <string>() == "crossed" ? MarginType.Cross : MarginType.Fixed; } catch { throw new OkexResponseParseError(string.Format("Response of {0} couldn't be parsed as expected", apiUrl)); } return(margin); }