Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

140
Vistas
How do you create a function to transfer Solana in Rust and then call it in your test script?

Context

I'm making a dApp where users can post GIFs on the Solana chain and I want to add a button that would allow a user that has their wallet connected to transfer exactly 0.01 SOL to another user that has posted a GIF they like. I'm trying to write the send_sol function in my Rust program but I keep getting errors when running anchor test that I haven't been able to find a solution for (I'm new to Rust).

Rust send_sol Function

I read that this is how this function should be written so this is how I wrote it:

pub fn send_sol(ctx: Context<SendSol>, amount: u64) -> ProgramResult {
        let ix = anchor_lang::solana_program::system_instruction::transfer(
            &ctx.accounts.from.key(),
            &ctx.accounts.to.key(),
            amount,
        );
        anchor_lang::solana_program::program::invoke(
            &ix,
            &[
                ctx.accounts.from.to_account_info(),
                ctx.accounts.to.to_account_info(),
            ],
        )
    }

However, when I ran anchor test I got this error:

error[E0412]: cannot find type `ProgramResult` in this scope

So I rewrote ProgramResult to Result <()>, but then I got this error:

error[E0308]: mismatched types
----------- expected `std::result::Result<(), anchor_lang::error::Error>` because of return type

I tried adding everything the error said I was missing but then I always got a new error. This is the main issue, but I am also confused on how to test this function if I get it to work.

Test Function

This is my current test where baseAccount = anchor.web3.Keypair.generate(); and the to parameter is the public key of my secondary wallet:

const lamportsToSend = LAMPORTS_PER_SOL / 100;
  await program.rpc.sendSol(lamportsToSend, {
    accounts: {
      from: baseAccount,
      to: "GiDPb7nxAUCy3nQoFYhHX17PSw74wbqdCNoof5PZJC2w",
      systemProgram: SystemProgram.programId,
    },
  });

However, I'm not sure if I'm passing in the parameters correctly or if I should even be using baseAccount in the from parameter.

Conclusion

This is my first Stack Overflow question so I'm sorry for this convoluted question structure. I mainly want to know how I need to structure my send_sol function to stop getting the parameter errors but any help with my test script would be greatly appreciated as well.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda