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