Solana's .NET SDK and integration library.
For maintainability and due to the size/complexity of some other programs, this repository focuses on Solana's Native Programs and SPL programs. For other commonly used programs see:
Maintained by Bifrost
Use the .NET CLI to install Solnet packages into your project (choose the ones you need):
# Rpc client and streaming
dotnet add package Solana.Rpc
dotnet add package Solana.Programs
# Wallets and keys
dotnet add package Solana.Wallet
# Token helpers
dotnet add package Solana.Extensions
# Keystore utilities
dotnet add package Solana.KeystoreLatest Packages on NuGet:
A minimal example to fetch a balance and send a memo:
…The Solnet.Examples project contains runnable code examples. Some examples derive accounts from a seed and may fail if an account at a derivation index already exists with the same seed. If you see simulation logs like account address is ... already in use, increment the derivation index (e.g., wallet.GetAccount(index + 1)).
The Solnet.Wallet project implements wallet and key generation compatible with both solana-keygen and Phantom.
var account = Account.FromSecretKey("");var wallet = new Wallet("mnemonic words ...", WordList.English);
var account = wallet.GetAccount(10);var wallet = new Wallet("mnemonic words ...", WordList.English, "passphrase", SeedMode.Bip39);
var account = wallet.Account; // solana-keygen uses a fixed derivation pathvar newMnemonic = new Mnemonic(WordList.English, WordCount.Twelve);
var wallet = new Wallet(newMnemonic);The Solnet.KeyStore project enables secure storage of keys, seeds, and mnemonics. It implements the Web3 Secret Storage Definition and includes SolanaKeyStoreService to read keys generated by solana-keygen.
var secretKeyStoreService = new SecretKeyStoreService();
var jsonString = secretKeyStoreService.EncryptAndGenerateDefaultKeyStoreAsJson(password, data, address);
try
{
var decrypted = KeyStore.DecryptKeyStoreFromJson(password, jsonString);
}
catch (Exception)
{
Console.WriteLine("Invalid password!");
}var solanaKeyStoreService = new SolanaKeyStoreService();
var wallet = solanaKeyStoreService.RestoreKeystore(filePath, passphrase);The Solnet.Rpc project contains a full implementation of the Solana JSON RPC.
var rpcClient = ClientFactory.GetClient(Cluster.MainNet, logger);
var paidClient = ClientFactory.GetClient("https://your.paid.rpc", logger);
var streamingRpcClient = ClientFactory.GetStreamingClient(Cluster.MainNet, logger);…var txSig = rpcClient.SendTransaction(tx);
var subscription = streamingRpcClient.SubscribeSignature(txSig.Result, (state, response) =>
{
// handle confirmation
}, Commitment.Finalized);Poorly optimized transactions can get dropped due to high compute demand. Always specify both compute unit limit and price to improve inclusion probability.
var txBuilder = new TransactionBuilder()
.AddInstruction(ComputeBudgetProgram.SetComputeUnitLimit(30000))
.AddInstruction(ComputeBudgetProgram.SetComputeUnitPrice(1000000));………var tx = Transaction.Deserialize(txData);
var msg = Message.Deserialize(msgData);
var signedTx = Transaction.Populate(msg, new List { account.Sign(msgData) });The Solnet.Programs project contains implementations of several Native and SPL programs. See Solnet.Examples for more depth, including multi-signature operations.
var memoInstruction = MemoProgram.NewMemo(wallet.Account, "Hello Solana World, using Solnet :)");
var recentHash = rpcClient.GetLatestBlockHash();
var tx = new TransactionBuilder()
.SetFeePayer(wallet.Account)
.AddInstruction(ComputeBudgetProgram.SetComputeUnitLimit(30000))
.AddInstruction(ComputeBudgetProgram.SetComputeUnitPrice(1000000))
.AddInstruction(memoInstruction)
.SetRecentBlockHash(recentHash.Result.Value.Blockhash)
.Build(wallet.Account);……This pattern is useful when a later transaction needs more account addresses than fit comfortably in the legacy message format. In Solnet, you typically create and populate the ALT with TransactionBuilder, wait for confirmation, and then reference it from a VersionedTransaction when building the larger v0 message.
…v1 Transactions no longer require the computebudget program, but instead require TransactionConfig. For more details on v1 can be found here: Sending v1 Transactions
…var msg = Message.Deserialize(msgBase64);
var decodedInstructions = InstructionDecoder.DecodeInstructions(msg);We encourage everyone to contribute, submit issues and PRs, and join discussions. Every kind of help is welcome.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE file for details.
No open issues yet, or sync has not completed.