DigitalRuby / ExchangeSharp

ExchangeSharp is a powerful, fast and easy to use .NET/C# API for interfacing with many crypto currency exchanges. REST and web sockets are supported.
https://www.digitalruby.com
MIT License
745 stars 375 forks source link

HitBTC.GetBankAmountsAsync() throws System.ArgumentException: 'Accessed JArray values with invalid key value: "balance". Int32 array index expected.' #415

Closed jjziets closed 5 years ago

jjziets commented 5 years ago

Stick at something simple

            Dictionary<string, decimal> BankBalance = await HitBTC.GetBankAmountsAsync();
            foreach (KeyValuePair<string, decimal> entry in BankBalance)
            {
                if (entry.Key.Contains("NIM") == true) COIN = entry.Value;
            }

results in exception

System.ArgumentException: 'Accessed JArray values with invalid key value: "balance". Int32 array index expected

Is this a bug in the HitBTC code or my implementations

jjziets commented 5 years ago

This works fine. So I'm a little lost why getBankAmounts will not work.

            Dictionary<string, decimal> balances = await HitBTC.GetAmountsAvailableToTradeAsync();
            foreach (KeyValuePair<string, decimal> entry in balances)
            {
                stats += " " + entry.Key + " " + entry.Value.ToString();
            }
jjziets commented 5 years ago

I think the problem is here. There is no "balance"

    public async Task<Dictionary<string, decimal>> GetBankAmountsAsync()
    {
        Dictionary<string, decimal> amounts = new Dictionary<string, decimal>();
        JToken obj = await MakeJsonRequestAsync<JToken>("/account/balance", null, await GetNoncePayloadAsync());
        foreach (JToken token in obj["balance"])
        {
            decimal amount = token["available"].ConvertInvariant<decimal>();
            if (amount > 0m) amounts[token["currency"].ToStringInvariant()] = amount;
        }
        return amounts;
    }
  Name Value Type
obj

I have no idea how to pull a request but this seems to have fix the problem.

    public async Task<Dictionary<string, decimal>> GetBankAmountsAsync()
    {
        Dictionary<string, decimal> amounts = new Dictionary<string, decimal>();
        JToken obj = await MakeJsonRequestAsync<JToken>("/account/balance", null, await GetNoncePayloadAsync());
        foreach (JToken token in obj)

        {
            decimal amount = token["available"].ConvertInvariant<decimal>();
            if (amount > 0m) amounts[token["currency"].ToStringInvariant()] = amount;
        }
        return amounts;
    }