Skip to content

Commit

Permalink
Fixed AccountInfo to return proper values.
Browse files Browse the repository at this point in the history
  • Loading branch information
tiago18c committed May 27, 2021
1 parent ae12bc2 commit f93818b
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
55 changes: 55 additions & 0 deletions src/Solnet.Rpc/Models/AccountData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Solnet.Rpc.Models
{
public class TokenAccountInfo
{
public TokenBalance TokenAmount { get; set; }

public string? Delegate { get; set; }

public ulong DelegatedAmount { get; set; }

public string State { get; set; }

public bool IsNative { get; set; }

public string Mint { get; set; }

public string Owner { get; set; }
}

public class ParsedTokenAccountData
{
public string Type { get; set; }

public TokenAccountInfo Info { get; set; }
}

public class TokenAccountData
{
public string Program { get; set; }

public ParsedTokenAccountData Parsed { get; set; }
}

public class LargeAccount : TokenBalance
{
public string Address { get; set; }
}

public class TokenBalance
{
public string Amount { get; set; }

public int Decimals { get; set; }

public decimal? UiAmount { get; set; }

public string UiAmountString { get; set; }
}
}
28 changes: 28 additions & 0 deletions src/Solnet.Rpc/Models/AccountDataJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Solnet.Rpc.Models
{
public class AccountDataJsonConverter : JsonConverter<object>
{
public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if(reader.TokenType == JsonTokenType.StartArray)
{
return JsonSerializer.Deserialize<List<string>>(ref reader, options);
}
else if (reader.TokenType == JsonTokenType.StartObject)
{
return JsonSerializer.Deserialize<TokenAccountData>(ref reader, options);
}
return null;
}

public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
}
}
32 changes: 31 additions & 1 deletion src/Solnet.Rpc/Models/AccountInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Solnet.Rpc.Models
{
Expand All @@ -12,6 +13,35 @@ public class AccountInfo

public ulong RentEpoch { get; set; }

public IList<string> Data { get; set; }
[JsonConverter(typeof(AccountDataJsonConverter))]
public object Data { get; set; }

/// <summary>
/// Tries to retrieve the Account data as a TokenAccountData encoded object.
/// </summary>
/// <param name="accData">The account data.</param>
/// <returns>Returns true if the account data was foud as a TokenAccountData object, false otherwise.</returns>
public bool TryGetAccountData(out TokenAccountData accData)
{
accData = (TokenAccountData)Data;
return accData != null;
}

/// <summary>
/// Tries to retrieve the Account data as a base64 encoded string.
/// </summary>
/// <param name="accData">The account data.</param>
/// <returns>Returns true if the account data was foud as a string, false otherwise.</returns>
public bool TryGetAccountData(out string accData)
{
accData = null;
var list = (IList<string>)Data;
if(list.Count > 0)
{
accData = list[0];
return true;
}
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/Solnet.Rpc/SolanaStreamingRpcClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public async Task<SubscriptionState> SubscribeAccountInfoAsync(string pubkey, Ac
{
var sub = new SubscriptionState<ResponseValue<AccountInfo>>(this, SubscriptionChannel.Account, callback, new List<object> { pubkey });

var msg = new JsonRpcRequest(_idGenerator.GetNextId(), "accountSubscribe", new List<object> { pubkey, new Dictionary<string, string> { { "encoding", "base64" } } });
var msg = new JsonRpcRequest(_idGenerator.GetNextId(), "accountSubscribe", new List<object> { pubkey, new Dictionary<string, string> { { "encoding", "jsonParsed" } } });

var json = JsonSerializer.SerializeToUtf8Bytes(msg, new JsonSerializerOptions { WriteIndented = false, PropertyNamingPolicy = JsonNamingPolicy.CamelCase });

Expand Down

0 comments on commit f93818b

Please sign in to comment.