0

I am trying to transfer the token to another account using the Solana rust program. I am currently using the Anchor framework. I have created the program for it.

Now, I am trying to execute the test case to check the transfer functionality. But I am getting an error as TypeError: Cannot read property 'toString' of undefined

I am not able to get the cause of the error. Please take a look at my code.

use anchor_lang::prelude::*;
use anchor_spl::token::{ Token, TokenAccount, Transfer, Mint, self };

pub fn transfer_sol(ctx:Context<TransferInfo>) -> Result<()>{
    let amt: u64 = 1000000;
    msg!("Lamport: {}",amt);
    msg!("Starting Tokens: {}",ctx.accounts.sender.amount);
    let cpi_ctx = CpiContext::new(
        ctx.accounts.token_program.to_account_info(),
        Transfer{
            from:ctx.accounts.sender.to_account_info(),
            to:ctx.accounts.receiver.to_account_info(),
            authority:ctx.accounts.authority.to_account_info(),
        }
    );
    token::transfer(cpi_ctx,amt)?;
    ctx.accounts.sender.reload()?;
    Ok(())
}

#[derive(Accounts)]
pub struct TransferInfo<'info>{
    #[account(mut)]
    pub sender: Box<Account<'info, TokenAccount>>,
    #[account(mut)]
    pub receiver: Box<Account<'info, TokenAccount>>,
    pub authority: Signer<'info>,
    pub mint: Box<Account<'info, Mint>>,
    pub token_program: Program<'info, Token>,
}
it("Is transfer!", async () =>{
      //let amount = new anchor.BN(1000000);
      //let amount = new anchor.web3.BN(LAMPORTS_PER_SOL);
      
      await program.methods.transfera()
        .accounts({
          sender:provider.wallet.publicKey,
          receiver:receiver_token.publicKey,
          authority:provider.wallet.publicKey,
          mint: mint.publicKey,
          tokenProgram:TOKEN_PROGRAM_ID
        })
        .signers([sender_token.publicKey])
        .rpc();

      let tokenbal = await provider.connection.getTokenAccountBalance(sender_token.publicKey);
      console.log("tokenbal ",tokenbal);

      let tokenbalm = await provider.connection.getTokenAccountBalance(receiver_token.publicKey);
      console.log("tokenbalm ",tokenbalm);

  });

Below is the error occured while running testcase.

Is transfer!:
     TypeError: Cannot read property 'toString' of undefined
      at Transaction.partialSign (node_modules/@solana/web3.js/src/transaction/legacy.ts:642:36)
      at /opt/projects/demo/rust-demo/second/node_modules/@project-serum/anchor/src/provider.ts:142:10
      at Array.forEach (<anonymous>)
      at AnchorProvider.sendAndConfirm (node_modules/@project-serum/anchor/src/provider.ts:141:21)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)
      at MethodsBuilder.rpc [as _rpcFn] (node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:29:16)

Please suggest any solution for it.

0 Answers0