Transfer to tokens owned by Solana Program: Step by step Guide
As Solana’s developer, he is likely to know the flexibility and scalability of the Solana blockchain. However, when cooperating with token ownership models, one common challenge is to transfer tokens from the program to another account. In this article, we examine how to execute an instruction in Solana’s program, which allows tokens.
Understanding the ownership of token
Before you get into the solution, it is essential to understand the token ownership in Solana. The token keeps the user “owned” if they are associated with a unique public key (title). This public key is used as the owner’s identity and the transaction affecting all tokens is re -tied to this address.
Instruction: Transfer
The "Transfer" instruction is the most commonly used method for transferring tokens between invoices in Solana. This allows you to transmit a specific amount of chips from one account to another as a source title of the program's public key and the addressed account as the target address.
Here's an example of how to execute this instruction:
Solana
Use the Solana Program-Lib :: Instructions :: {Instruction, Program};
Use Solana_program :: {{
Account_info :: Next_account_info,
Entry point :: Programmer,
Program_Error :: Printerror,
PUBKEY :: PUBKEY,
};
PUB STRUCT TOKENOWNERSHIPINSTRUCTION {
src_account: Pubkey,
Dst_account: Pubkey,
Amount: U64,
}
Imp tokenownershipinstruction {
PUB FN New (SRC_ACCOUNT: PUBKEY, DST_ACCOUNT: PUBKEY, sum: U64) -> Self {
Self {src_account, dst_account, sum}
}
PUB FN Execute (& Self, Context: & Mut Solana_program :: Exec_Context :: Exectext) -> ProgramResult {
Be the Next_account_info = Next_account_info (context)?;
Let src_user = next_account_info.user_id;
Let SRC_TOken = Self.src_account;
// Check that token owner has sufficient balance
If src_user.balance
Back to Solana_program :: Error :: Invvargument (
Format! (“Not enough balance for token transfer: {}”,
src_user.balance),
);
}
// Create a new instruction to hand over tokens
Have instructions = instructions :: new (
Tokenownershipinstruction :: New (SRC_TOKEN, SELF.DST_ACCOUNT, SELF.AMOUNT),
& [],
);
// Complete the instruction with the Solana-Program-Lib" "Transfer" function
Solana_program :: Program_Lib :: Instructions :: Invoke_single (& instruction, & self.src_account)?;
Return Reason (())
}
}
Example Use the case
To show how to use this instruction in the program, you can create a simple user account and hand over some SPL -TOKENS:
“ rust
Use Solana_program :: {{
Account_info :: {Next_account_info, Pubkey},
Entry point :: Programmer,
Program_Error :: Printerror,
PUBKEY :: PUBKEY,
};
PUB STRUCT USERACCOUNT {
Accounts: VEC
}
Int unuseaccount {
fn New () -> Self {
Let user_account = Next_account_info (PUBKEY :: New ())? ;;
// initialize the SPL token account with an empty balance
Let SL_TOken_account = Next_account_info (Pubkey :: New ()). Ewrap ();
Solana_program :: Instructions :: Invoke_single (
& Solana_program :: Instructions :: Transfer (
& user_account,
SPL_TOken_account,
100U64,
),
& [],
)?