Skip to content

Instantly share code, notes, and snippets.

@cqfd
Created November 27, 2021 01:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cqfd/ffbdebbbcd3460ec822c3e63572cc282 to your computer and use it in GitHub Desktop.
Save cqfd/ffbdebbbcd3460ec822c3e63572cc282 to your computer and use it in GitHub Desktop.
pub fn goofy(ctx: Context<Goofy>) -> ProgramResult {
// Manually pay the fresh account some money
**ctx
.accounts
.payer // some account owned by us, with some money
.to_account_info()
.try_borrow_mut_lamports()? -= 2000000; // random big-enough amount
**ctx.accounts.new_account.try_borrow_mut_lamports()? += 2000000;
// Allocate the new_account some space (it has money now).
let mut allocate_ix = anchor_lang::solana_program::system_instruction::allocate(
ctx.accounts.new_account.key,
128,
);
// Horrible hack, need to do this b/c of basically a bug in solana
// the payer account shouldn't need to go here
allocate_ix.accounts.push(AccountMeta {
pubkey: ctx.accounts.payer.key(),
is_signer: false,
is_writable: false,
});
anchor_lang::solana_program::program::invoke(
&allocate_ix,
&[
ctx.accounts.new_account.to_account_info(),
// Need to pass the payer here too, due to horrible hack/bug mentioned above
ctx.accounts.payer.to_account_info(),
],
)?;
let mut ownership_ix = anchor_lang::solana_program::system_instruction::assign(
ctx.accounts.new_account.key,
&ID,
);
// Same horrible hack/bug as above
ownership_ix.accounts.push(AccountMeta {
pubkey: ctx.accounts.payer.key(),
is_signer: false,
is_writable: false,
});
anchor_lang::solana_program::program::invoke(
&ownership_ix,
&[
ctx.accounts.new_account.to_account_info(),
// Same horrible/hack as above, gotta pass this irrelevant account.
ctx.accounts.payer.to_account_info(),
],
)?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment