0

Objective: Transferring HNT from one account to another using rust programming language.

account details:

  1. 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.

1 Answer 1

0

There's a few things to fixup here.

First, and most importantly, you do not pay priority fees by sending SOL to the system program, that's a total waste of SOL. You need to use the compute_budget instructions defined at https://github.com/anza-xyz/agave/blob/0168e0ab69a631d5998507924debaba1caa566ba/sdk/src/compute_budget.rs#L25. I'm not sure who told you to do that, but you should go yell at them!

Second, you need to add the compute budget instructions to your spl_token message, and not to a new message.

Last, the call to transfer is incorrect by passing from_pubkey as one of the multisig signers. Instead, you need to do:

    let transfer_instruction = transfer(
        &token_program_id,
        &ata_source,
        &ata_destination,
        &from_pubkey,
        &[],
        amount,
    )
    .map_err(|err| err.to_string())?;
1
  • Thank you so much for your support. It worked immediately 20 hours ago

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.