Skip to content

Commit

Permalink
Name service client (#275)
Browse files Browse the repository at this point in the history
Created a NameServiceClient that abstracts querying for name records.
This client allows to Query for:

-    .sol name to address
-    reverse address to owned .sol names
-    twitter handle to address
-    address to twitter handle
-    all names owned by address
-    mint address to token info
-    token ticker to mint address
  • Loading branch information
tiago18c committed Dec 31, 2021
1 parent fb6a7c5 commit 7a6520f
Show file tree
Hide file tree
Showing 18 changed files with 1,066 additions and 30 deletions.
100 changes: 100 additions & 0 deletions src/Solnet.Examples/NameServiceClientExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
using Solnet.Programs.Clients;
using Solnet.Rpc;
using System;

namespace Solnet.Examples
{

public class NameServiceClientExample : IExample
{
public void Run()
{
var rpc = ClientFactory.GetClient(Cluster.MainNet);
var wrpc = ClientFactory.GetStreamingClient(Cluster.MainNet);

var c = new NameServiceClient(rpc);

var test1 = c.GetAddressFromNameAsync("bonfida.sol").Result;
if (test1.WasSuccessful)
{
Console.WriteLine($"Name: 'bonfida.sol' \t Address: {test1.ParsedResult.Header.Owner}");
Console.WriteLine($"Account Contents: ");
Console.WriteLine("------------------------------------ BASE 64 ---------------------------------------");
Console.WriteLine(System.Convert.ToBase64String(test1.ParsedResult.Value));
Console.WriteLine("------------------------------------------------------------------------------------");
Console.WriteLine("--------------------------- TENTATIVE UTF8 DECODING --------------------------------");
Console.WriteLine(System.Text.Encoding.UTF8.GetString(test1.ParsedResult.Value));
Console.WriteLine("------------------------------------------------------------------------------------");
}
else
{
Console.WriteLine($"Unable to resolve name.");
}

var test2 = c.GetNamesFromAddressAsync("BriW4tTAiAm541uB2Fua3dUNoGayRa8Wt7pZUshUbrPB").Result;
if (test2 != null && test2.Count > 0)
{
foreach (var item in test2)
{
Console.WriteLine($"Record Type: {item.Type} \t Name: {item.Name} \t Address: {item.AccountAddress} ");
}
}

var test3 = c.GetAddressFromTwitterHandleAsync("bonfida").Result;
if (test3.WasSuccessful)
{
Console.WriteLine($"Twitter: '@bonfida' \t Address: {test3.ParsedResult.Header.Owner}");
Console.WriteLine($"Account Contents: ");
Console.WriteLine("------------------------------------ BASE 64 ---------------------------------------");
Console.WriteLine(System.Convert.ToBase64String(test3.ParsedResult.Value));
Console.WriteLine("------------------------------------------------------------------------------------");
Console.WriteLine("--------------------------- TENTATIVE UTF8 DECODING --------------------------------");
Console.WriteLine(System.Text.Encoding.UTF8.GetString(test3.ParsedResult.Value));
Console.WriteLine("------------------------------------------------------------------------------------");
}
else
{
Console.WriteLine($"Unable to resolve name.");
}

var test4 = c.GetTwitterHandleFromAddressAsync("FidaeBkZkvDqi1GXNEwB8uWmj9Ngx2HXSS5nyGRuVFcZ").Result;
if (test4.WasSuccessful)
{
Console.WriteLine($"Address: {test4.ParsedResult.Header.Owner} \t Twitter: {test4.ParsedResult.TwitterHandle}");
}
else
{
Console.WriteLine($"Unable to resolve name.");
}

var test5 = c.GetAllNamesByOwnerAsync("FidaeBkZkvDqi1GXNEwB8uWmj9Ngx2HXSS5nyGRuVFcZ").Result;
if (test5 != null && test5.Count > 0)
{
foreach (var item in test5)
{
Console.WriteLine($"Record Type: {item.Type} \t Address: {item.AccountAddress} \t Value: {item.GetValue()} ");
}
}

var test6 = c.GetTokenInfoFromMintAsync("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v").Result;
if (test6.WasSuccessful)
{
Console.WriteLine($"Mint Address: {test6.ParsedResult.Value.Mint} \t Ticker: ${test6.ParsedResult.Value.Ticker} \t Name: {test6.ParsedResult.Value.Name} \t Name: {test6.ParsedResult.Value.Website}");
}
else
{
Console.WriteLine($"Unable to resolve token mint.");
}

var test7 = c.GetMintFromTokenTickerAsync("USDC").Result;
if (test7.WasSuccessful)
{
Console.WriteLine($"Ticker: ${test6.ParsedResult.Value.Ticker} \t Mint Address: {test7.ParsedResult.Value} \t ");
}
else
{
Console.WriteLine($"Unable to resolve token mint.");
}
}
}
}
1 change: 0 additions & 1 deletion src/Solnet.Examples/NameServiceProgramExamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using Solnet.Rpc.Builders;
using Solnet.Wallet;
using System;
using System.Collections.Generic;

namespace Solnet.Examples
{
Expand Down
5 changes: 5 additions & 0 deletions src/Solnet.Programs/Abstract/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ private static T DeserializeAccount<T>(byte[] data) where T : class
System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Static,
null, new[] { typeof(byte[]) }, null);

if (m.IsGenericMethod)
{
m = m.MakeGenericMethod(typeof(T).GetGenericArguments());
}

if (m == null)
return null;
return (T)m.Invoke(null, new object[] { data });
Expand Down
Loading

0 comments on commit 7a6520f

Please sign in to comment.