Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

137
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda