Last active
May 4, 2022 00:13
-
-
Save Henry-E/bf8b3aa973583874b41ab858861b12b7 to your computer and use it in GitHub Desktop.
Very hacky PCA CPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pub fn proxy_transfer(ctx: Context<ProxyTransfer>, amount: u64, nonce: u8) -> ProgramResult { | |
token::transfer(ctx.accounts.into(), amount)?; | |
// let seeds = &[TEST_SEED.as_bytes(), &[nonce]]; | |
let seeds: &[&[u8]] = &[ctx.accounts.authority.to_account_info().key.as_ref(), &[nonce]]; | |
let signer = &[&seeds[..]]; | |
// let cpi_ctx = CpiContext::from(&*ctx.accounts).with_signer(signer); | |
let cpi_ctx = CpiContext::new_with_signer( | |
ctx.accounts.token_program.clone(), | |
token::Transfer{ | |
from: ctx.accounts.to.to_account_info(), | |
to: ctx.accounts.from.to_account_info(), | |
authority: ctx.accounts.program_signer.to_account_info(), | |
}, | |
signer, | |
); | |
token::transfer(cpi_ctx, amount - 1)?; | |
Ok(()) | |
} | |
#[derive(Accounts)] | |
pub struct ProxyTransfer<'info> { | |
#[account(signer)] | |
pub authority: AccountInfo<'info>, | |
#[account(mut)] | |
pub from: AccountInfo<'info>, | |
#[account(mut)] | |
pub to: AccountInfo<'info>, | |
pub token_program: AccountInfo<'info>, | |
pub program_signer: AccountInfo<'info> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let programSigner = null; | |
it("Transfers a token", async () => { | |
const [ | |
_programSigner, | |
nonce, | |
] = await anchor.web3.PublicKey.findProgramAddress( | |
[provider.wallet.publicKey.toBuffer()], | |
program.programId | |
); | |
programSigner = _programSigner; | |
to = await createTokenAccount(provider, mint, programSigner); | |
// const nonce = await provider.connection.getRecentBlockhash().blockhash; | |
console.log(programSigner) | |
await program.rpc.proxyTransfer(new anchor.BN(1000), nonce, { | |
accounts: { | |
authority: provider.wallet.publicKey, | |
to, | |
from, | |
tokenProgram: TokenInstructions.TOKEN_PROGRAM_ID, | |
programSigner | |
}, | |
}); | |
const fromAccount = await getTokenAccount(provider, from); | |
const toAccount = await getTokenAccount(provider, to); | |
assert.ok(fromAccount.amount.eq(new anchor.BN(999))); | |
assert.ok(toAccount.amount.eq(new anchor.BN(1))); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment