Objective: Transferring HNT from one account to another using rust programming language.
account details:
- Both sender and receiver has more than 0.02 SOLs in their account and has enough HNTs to make the actual transaction.
The transfer function logic i am using (Code):
pub fn transfer_spl_tokens(
from_keypair: &Keypair,
token_mint_pubkey: &Pubkey,
to_pubkey: &Pubkey,
amount: u64, // Amount is now in token units
rpc_url: &str,
priority_fee: u64, // Additional priority fee in SOL
) -> Result<String, String> {
let client = RpcClient::new_with_commitment(rpc_url.to_string(), CommitmentConfig::confirmed());
let from_pubkey = from_keypair.pubkey();
let token_program_id = spl_token::id();
let ata_source = get_associated_token_address(
&from_pubkey, // Owner's address
&token_mint_pubkey, // mint address
);
let ata_destination = get_associated_token_address(
&to_pubkey,
&token_mint_pubkey,
);
// Create instruction to transfer tokens
let transfer_instruction = transfer(
&token_program_id,
&ata_source,
&ata_destination,
&from_pubkey,
&[&from_pubkey],
amount,
)
.map_err(|err| err.to_string())?;
let token_transfer_message = Message::new(
&[transfer_instruction],
Some(&from_pubkey),
);
let recent_blockhash = client
.get_latest_blockhash()
.map_err(|err| err.to_string())?;
// Create the transaction for token transfer
let token_transfer_transaction = Transaction::new(&[from_keypair], token_transfer_message, recent_blockhash);
// Send the token transfer transaction
let _ = client
.send_and_confirm_transaction(&token_transfer_transaction)
.map_err(|err| err.to_string())?;
// Create instruction to transfer SOL for priority fee
let sol_transfer_instruction = system_instruction::transfer(
&from_pubkey,
&solana_sdk::system_program::id(), // Transfer SOL to the system account representing validators
priority_fee,
);
// Create the message for SOL transfer
let sol_transfer_message = Message::new(
&[sol_transfer_instruction],
Some(&from_pubkey),
);
// Create the transaction for SOL transfer
let sol_transfer_transaction = Transaction::new(&[from_keypair], sol_transfer_message, recent_blockhash);
// Send the SOL transfer transaction
let signature = client
.send_and_confirm_transaction(&sol_transfer_transaction)
.map_err(|err| err.to_string())?;
Ok(signature.to_string())
}
All the keys I provided are right, I checked them over 10 times.