Core Concepts
Transfer Token
Framework and SDKs
Framework
- BOLT Framework
- Mapping and State listening
- Getting Started
SDKs
- Solana Unity SDK
- Introduction
- Contribution Guide
- Getting Started
- Core Concepts
- Guides
- On-chain identity
Sessions
- Session Keys
Core Concepts
Transfer Token
The Token program defines a common implementation for Fungible and Non Fungible tokens.
You can learn about Token Program on the official Solana documentation
Balances can be transferred between Accounts using the Transfer instruction. The owner of the source Account must be present as a signer in the Transfer instruction when the source and destination accounts are different.
public class TransferTokenExample : IExample
{
private static readonly IRpcClient rpcClient = ClientFactory.GetClient(Cluster.TestNet);
private const string MnemonicWords =
"route clerk disease box emerge airport loud waste attitude film army tray " +
"forward deal onion eight catalog surface unit card window walnut wealth medal";
public void Run()
{
Wallet.Wallet wallet = new Wallet.Wallet(MnemonicWords);
RequestResult<ResponseValue<BlockHash>> blockHash = rpcClient.GetRecentBlockHash();
ulong minBalanceForExemptionAcc = rpcClient.GetMinimumBalanceForRentExemption(TokenProgram.TokenAccountDataSize).Result;
Console.WriteLine($"MinBalanceForRentExemption Account >> {minBalanceForExemptionAcc}");
Account mintAccount = wallet.GetAccount(31);
Console.WriteLine($"MintAccount: {mintAccount}");
Account ownerAccount = wallet.GetAccount(10);
Console.WriteLine($"OwnerAccount: {ownerAccount}");
Account initialAccount = wallet.GetAccount(32);
Console.WriteLine($"InitialAccount: {initialAccount}");
Account newAccount = wallet.GetAccount(33);
Console.WriteLine($"NewAccount: {newAccount}");
byte[] tx = new TransactionBuilder().SetRecentBlockHash(blockHash.Result.Value.Blockhash)
.SetFeePayer(ownerAccount)
.AddInstruction(SystemProgram.CreateAccount(
ownerAccount.PublicKey,
newAccount.PublicKey,
minBalanceForExemptionAcc,
TokenProgram.TokenAccountDataSize,
TokenProgram.ProgramIdKey))
.AddInstruction(TokenProgram.InitializeAccount(
newAccount.PublicKey,
mintAccount.PublicKey,
ownerAccount.PublicKey))
.AddInstruction(TokenProgram.Transfer(
initialAccount.PublicKey,
newAccount.PublicKey,
25000,
ownerAccount))
.AddInstruction(MemoProgram.NewMemo(initialAccount, "Hello from Sol.Net"))
.Build(new List<Account> { ownerAccount, newAccount, initialAccount });
Console.WriteLine($"Tx: {Convert.ToBase64String(tx)}");
RequestResult<ResponseValue<SimulationLogs>> txSim = rpcClient.SimulateTransaction(tx);
string logs = Examples.PrettyPrintTransactionSimulationLogs(txSim.Result.Value.Logs);
Console.WriteLine($"Transaction Simulation:\n\tError: {txSim.Result.Value.Error}\n\tLogs: \n" + logs);
RequestResult<string> txReq = rpcClient.SendTransaction(tx);
Console.WriteLine($"Tx Signature: {txReq.Result}");
}
}
Was this page helpful?