1

does anyone know how to fix this please? I tried using a let expression but it ended up in a halting problem... The error ocucrs because of the "seeds" variable

pub fn exec_payout(ctx: Context<MyInstruction>, amount: u64, bump:u8, p1:Pubkey, p2:Pubkey, rstring:String) -> Result<()> {
    let cpi_program = ctx.accounts.system_program.to_account_info();
    let cpi_accounts = system_program::Transfer {
        from: ctx.accounts.account_a.clone(),
        to: ctx.accounts.account_b.clone(),
    };
    let seeds = [rstring.as_bytes().as_ref(), p1.as_ref(), p2.as_ref(), &[bump]].as_slice();
    let seedz = &[seeds.clone()];

    let cpi_context = CpiContext::new(cpi_program, cpi_accounts)
        .with_signer(seedz);
    system_program::transfer(cpi_context, amount)?; 
    Ok(())
}

I tried writing it like this but it ended up into the same error... can someone help me please

let cpi_context = CpiContext::new(cpi_program, cpi_accounts)
        .with_signer(&[&[rstring.as_bytes().as_ref(), p1.as_ref(), p2.as_ref(), &[bump]]]);

the error i get

Sing
  • 13
  • 3
  • Why are you taking a reference to a clone when a reference to the original would suffice? Do you need a reference to an array with a reference in it? – tadman Aug 13 '22 at 01:50
  • can you also add the error which you are getting with the compiler error code, it points at the exact point of error in the program and helps in debugging. – Ashish Singh Aug 13 '22 at 11:23
  • yes I updated with a screenshot of the error – Sing Aug 13 '22 at 12:03

1 Answers1

0

Essentially, each time you do &[XYZ] it needs to be stored in a variable1 so that it stays alive:

let bumps = &[bump];
let seeds = &[rstring.as_bytes().as_ref(), p1.as_ref(), p2.as_ref(), bumps][..];
let seedz = &[seeds];

All your values are references and normally you can't keep around the reference of a temporary value... unless it is in the form let x = &... and then the lifetime will be automatically extended for you. See this Q&A for more details: Why is it legal to borrow a temporary? Doing [XYZ].as_slice() doesn't work, use [..] at the end if you need to coerce it into a slice instead of simply a reference to an array.

Another route would be to construct your slices and use them all at once, but it must be done in a single expression since that's how long temporary values live. Your last attempt didn't work since CpiContext also just references the values, you'd need to use it as well (by passing it to transfer()):

system_program::transfer(
    CpiContext::new_with_signer(
        cpi_program,
        cpi_accounts,
        &[&[rstring.as_bytes().as_ref(), p1.as_ref(), p2.as_ref(), &[bump]]],
    amount)?; 

1: unless the value can be const-promoted

kmdreko
  • 42,554
  • 6
  • 57
  • 106