Skip to content

Commit d76dc40

Browse files
committed
Fix balances methods
1 parent b545420 commit d76dc40

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

src/ExchangeSharp/API/Exchanges/MEXC/ExchangeMEXCAPI.cs

+46-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ protected override async Task<Dictionary<string, decimal>> OnGetAmountsAvailable
241241
{
242242
Currency = x["asset"].Value<string>(),
243243
AvailableBalance = x["free"].Value<decimal>()
244-
+ x["locked"].Value<decimal>()
245244
}
246245
)
247246
.ToDictionary(k => k.Currency, v => v.AvailableBalance);
@@ -475,6 +474,51 @@ await _socket.SendMessageAsync(new WebSocketSubscription
475474
}
476475
);
477476
}
477+
478+
protected override Task ProcessRequestAsync(
479+
IHttpWebRequest request,
480+
Dictionary<string, object>? payload
481+
)
482+
{
483+
if (
484+
CanMakeAuthenticatedRequest(payload)
485+
|| (payload == null && request.RequestUri.AbsoluteUri.Contains("userDataStream"))
486+
)
487+
{
488+
request.AddHeader("X-MEXC-APIKEY", PublicApiKey!.ToUnsecureString());
489+
}
490+
return base.ProcessRequestAsync(request, payload);
491+
}
492+
493+
protected override Uri ProcessRequestUrl(
494+
UriBuilder url,
495+
Dictionary<string, object>? payload,
496+
string? method
497+
)
498+
{
499+
if (CanMakeAuthenticatedRequest(payload))
500+
{
501+
// payload is ignored, except for the nonce which is added to the url query - bittrex puts all the "post" parameters in the url query instead of the request body
502+
var query = (url.Query ?? string.Empty).Trim('?', '&');
503+
string newQuery =
504+
"timestamp="
505+
+ payload!["nonce"].ToStringInvariant()
506+
+ (query.Length != 0 ? "&" + query : string.Empty)
507+
+ (
508+
payload.Count > 1
509+
? "&" + CryptoUtility.GetFormForPayload(payload, false)
510+
: string.Empty
511+
);
512+
string signature = CryptoUtility.SHA256Sign(
513+
newQuery,
514+
CryptoUtility.ToUnsecureBytesUTF8(PrivateApiKey)
515+
);
516+
newQuery += "&signature=" + signature;
517+
url.Query = newQuery;
518+
return url.Uri;
519+
}
520+
return base.ProcessRequestUrl(url, payload, method);
521+
}
478522

479523
private async Task<JToken> GetBalance()
480524
{
@@ -515,6 +559,7 @@ private static ExchangeOrderResult ParseOrder(JToken token)
515559
Amount = token["origQty"].ConvertInvariant<decimal>(),
516560
AmountFilled = token["executedQty"].ConvertInvariant<decimal>(),
517561
Price = token["price"].ConvertInvariant<decimal>(),
562+
AveragePrice = token["price"].ConvertInvariant<decimal>(),
518563
IsBuy = token["side"].ToStringInvariant() == "BUY",
519564
OrderDate = token["time"].ConvertInvariant<long>().UnixTimeStampToDateTimeMilliseconds(),
520565
Result = ParseOrderStatus(token["status"].ToStringInvariant())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using CommandLine;
2+
using ExchangeSharpConsole.Options.Interfaces;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
namespace ExchangeSharpConsole.Options;
10+
11+
[Verb(
12+
"balances",
13+
HelpText = "Displays the account balances for an exchange."
14+
)]
15+
public class BalancesOption : BaseOption, IOptionPerExchange, IOptionWithKey
16+
{
17+
public override async Task RunCommand()
18+
{
19+
using var api = await GetExchangeInstanceAsync(ExchangeName);
20+
21+
api.LoadAPIKeys(KeyPath);
22+
23+
if (Mode.Equals("all", StringComparison.OrdinalIgnoreCase))
24+
{
25+
Console.WriteLine("All Balances:");
26+
27+
var balances = await api.GetAmountsAsync();
28+
29+
foreach (var balance in balances)
30+
{
31+
Console.WriteLine($"{balance.Key}: {balance.Value}");
32+
}
33+
34+
Console.WriteLine();
35+
}
36+
else if (Mode.Equals("trade", StringComparison.OrdinalIgnoreCase))
37+
{
38+
Console.WriteLine("Balances available for trading:");
39+
40+
var balances = await api.GetAmountsAvailableToTradeAsync();
41+
42+
foreach (var balance in balances)
43+
{
44+
Console.WriteLine($"{balance.Key}: {balance.Value}");
45+
}
46+
47+
Console.WriteLine();
48+
}
49+
else
50+
{
51+
throw new ArgumentException($"Invalid mode: {Mode}");
52+
}
53+
}
54+
55+
public string ExchangeName { get; set; }
56+
57+
[Option(
58+
'm',
59+
"mode",
60+
Required = true,
61+
HelpText = "Mode of execution."
62+
+ "\n\tPossible values are \"all\" or \"trade\"."
63+
+ "\n\t\tall: Displays all funds, regardless if they are locked or not."
64+
+ "\n\t\ttrade: Displays the funds that can be used, at the current moment, to trade."
65+
)]
66+
67+
public string Mode { get; set; }
68+
69+
public string KeyPath { get; set; }
70+
}

src/ExchangeSharpConsole/Program.cs

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public partial class Program
1414

1515
public Type[] CommandOptions { get; } =
1616
{
17+
typeof(BalancesOption),
1718
typeof(BuyOption),
1819
typeof(CancelOrderOption),
1920
typeof(CandlesOption),

0 commit comments

Comments
 (0)