-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding transactions, more rpc endpoints and documenting code.
- Loading branch information
1 parent
c10043d
commit bc68ee4
Showing
28 changed files
with
1,405 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,58 @@ | ||
using System; | ||
using Solnet.Rpc; | ||
using Solnet.Rpc.Core.Sockets; | ||
using Solnet.Rpc.Models; | ||
|
||
namespace Solnet.Examples | ||
{ | ||
class SolnetRpcTester | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
|
||
var c = new SolanaRpcClient("https://testnet.solana.com"); | ||
|
||
//var accInfo = c.GetAccountInfo("4K1oSvRCvALnJAaQdyxXLenV4fcxHyXDY2nYY6WDyKZT"); | ||
//var balance = c.GetBalance("9UGxCidmZtU1PM7Tbhv2twQ8ChsS6S3HdL1xo56fSVWn"); | ||
//var accInfo = c.GetGenesisHash(); | ||
|
||
//var blockCommitment = c.GetBlockCommitment(78561320); | ||
|
||
//var blockTime = c.GetBlockTime(78561320); | ||
|
||
var cn = c.GetClusterNodes(); | ||
//var cn = c.GetClusterNodes(); | ||
|
||
/* Large accounts for Token Mint PubKey | ||
var largeAccounts = c.GetTokenLargestAccounts("7ugkvt26sFjMdiFQFP5AQX8m8UkxWaW7rk2nBk4R6Gf2"); | ||
foreach (var cluster in cn.Result) | ||
foreach (var acc in largeAccounts.Result.Value) | ||
{ | ||
Console.WriteLine("cluster node " + cluster.PubKey ); | ||
Console.WriteLine($"Acc: {acc.Address} Balance: {acc.UiAmountString}"); | ||
} | ||
*/ | ||
|
||
/* Token balance for Account PubKey | ||
var tokenBalance = c.GetTokenAccountBalance("7247amxcSBamBSKZJrqbj373CiJSa1v21cRav56C3WfZ"); | ||
Console.WriteLine($"Token Balance: {tokenBalance.Result.Value.UiAmountString}"); | ||
*/ | ||
|
||
var tokenAccounts = c.GetTokenAccountsByOwner( | ||
"9we6kjtbcZ2vy3GSLLsZTEhbAqXPTRvEyoxa8wxSqKp5",null, "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"); | ||
foreach (var acc in tokenAccounts.Result.Value) | ||
{ | ||
Console.WriteLine("--------------------------------------------------------------------"); | ||
var accInfo = c.GetAccountInfoJson(acc.PublicKey); | ||
TokenAccountData tokenAccData = null; | ||
var data = accInfo.Result.Value.TryGetAccountData(out tokenAccData); | ||
Console.WriteLine( | ||
$"Token Account:\n" + | ||
$"\tAccount PubKey: {acc.PublicKey}\n" + | ||
$"\tAccount Lamport Balance: {acc.Account.Lamports}\n" + | ||
$"\tAccount Encoded Data: {acc.Account.Data}\n" + | ||
$"Account Info for {acc.PublicKey}:\n" + | ||
$"\tAccount Owner: {tokenAccData.Parsed.Info.Owner}\n" + | ||
$"\tToken Balance: {tokenAccData.Parsed.Info.TokenAmount.UiAmountString}\n" + | ||
$"\tToken Mint: {tokenAccData.Parsed.Info.Mint}" | ||
); | ||
Console.WriteLine("--------------------------------------------------------------------"); | ||
} | ||
} | ||
|
||
private static void Sub_SubscriptionChanged(object sender, SubscriptionEvent e) | ||
{ | ||
Console.WriteLine("subcription changed to: " + e.Status); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using Solnet.Programs; | ||
using Solnet.Rpc; | ||
using Solnet.Rpc.Builders; | ||
using Solnet.Rpc.Messages; | ||
using Solnet.Rpc.Models; | ||
using Solnet.Util; | ||
using Solnet.Wallet; | ||
|
||
namespace Solnet.Examples | ||
{ | ||
public class TransactionBuilderExample | ||
{ | ||
public static string PrettyPrintTransactionSimulationLogs(Log logMessage) | ||
{ | ||
var logString = ""; | ||
foreach (var log in logMessage.Logs) | ||
{ | ||
logString += $"\t\t{log}\n"; | ||
} | ||
|
||
return logString; | ||
} | ||
|
||
static void TransactionAndMemoExample(string[] args) | ||
{ | ||
var rpcClient = new SolanaRpcClient("https://testnet.solana.com"); | ||
var wallet = new Wallet.Wallet("route clerk disease box emerge airport loud waste attitude film army tray forward deal onion eight catalog surface unit card window walnut wealth medal"); | ||
|
||
var fromAccount = wallet.GetAccount(0); | ||
var toAccount = wallet.GetAccount(1); | ||
|
||
var blockHash = rpcClient.GetRecentBlockHash(); | ||
//Console.WriteLine($"BlockHash >> {blockHash.Blockhash}"); | ||
|
||
var tx = new TransactionBuilder(). | ||
SetRecentBlockHash(blockHash.Result.Value.Blockhash). | ||
AddInstruction(MemoProgram.NewMemo(fromAccount, "Hello from Sol.Net :)")). | ||
AddInstruction(SystemProgram.Transfer(fromAccount.GetPublicKey, toAccount.GetPublicKey, 10000000)). | ||
Build(fromAccount); | ||
|
||
Console.WriteLine($"Tx base64: {Convert.ToBase64String(tx)}"); | ||
var txSim = rpcClient.SimulateTransaction(tx); | ||
var logs = PrettyPrintTransactionSimulationLogs(txSim.Result.Value); | ||
Console.WriteLine($"Transaction Simulation:\n\tError: {txSim.Result.Value.Error}\n\tLogs: \n" + logs ); | ||
var firstSig = rpcClient.SendTransaction(tx); | ||
Console.WriteLine($"First Tx Signature: {firstSig.Result}"); | ||
} | ||
|
||
static void TokenMintExample(string[] args) | ||
{ | ||
var rpcClient = new SolanaRpcClient("https://testnet.solana.com"); | ||
var wallet = new Wallet.Wallet("route clerk disease box emerge airport loud waste attitude film army tray forward deal onion eight catalog surface unit card window walnut wealth medal"); | ||
|
||
var blockHash = rpcClient.GetRecentBlockHash(); | ||
var minBalanceForExemption = rpcClient.GetMinimumBalanceForRentExemption(SystemProgram.AccountDataSize).Result.Value; | ||
|
||
var mintAccount = wallet.GetAccount(3); | ||
var ownerAccount = wallet.GetAccount(4); | ||
var initialAccount = wallet.GetAccount(5); | ||
|
||
var txBuilder = new TransactionBuilder().SetRecentBlockHash(blockHash.Result.Value.Blockhash) | ||
.AddInstruction(SystemProgram.CreateAccount( | ||
ownerAccount.GetPublicKey, | ||
mintAccount.GetPublicKey, | ||
minBalanceForExemption, | ||
SystemProgram.AccountDataSize, | ||
TokenProgram.ProgramId)); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using NBitcoin.DataEncoders; | ||
using Solnet.Rpc.Models; | ||
using Solnet.Wallet; | ||
|
||
namespace Solnet.Programs | ||
{ | ||
/// <summary> | ||
/// Helper class for the Memo Program. | ||
/// <remarks> | ||
/// Used to write UTF-8 data into Solana transactions. | ||
/// </remarks> | ||
/// </summary> | ||
public static class MemoProgram | ||
{ | ||
/// <summary> | ||
/// The base58 encoder instance. | ||
/// </summary> | ||
private static readonly Base58Encoder Encoder = new (); | ||
|
||
/// <summary> | ||
/// The address of the Memo Program. | ||
/// </summary> | ||
private static string ProgramId = "Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo"; | ||
|
||
/// <summary> | ||
/// Initialize a new transaction instruction which interacts with the Memo Program. | ||
/// </summary> | ||
/// <param name="account">The account associated with the memo.</param> | ||
/// <param name="memo">The memo to be included in the transaction.</param> | ||
/// <returns>The <see cref="TransactionInstruction"/> which includes the memo data.</returns> | ||
public static TransactionInstruction NewMemo(Account account, string memo) | ||
{ | ||
var keys = new List<AccountMeta> | ||
{ | ||
new (account.PublicKey, true, false) | ||
}; | ||
var memoBytes = Encoding.UTF8.GetBytes(memo); | ||
|
||
return new TransactionInstruction | ||
{ | ||
ProgramId = Encoder.DecodeData(ProgramId), | ||
Keys = keys, | ||
Data = memoBytes | ||
}; | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Solnet.Rpc\Solnet.Rpc.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.