diff --git a/tests/spl/token-proxy/programs/token-proxy/src/lib.rs b/tests/spl/token-proxy/programs/token-proxy/src/lib.rs index 012c0d32be..8dd869c434 100644 --- a/tests/spl/token-proxy/programs/token-proxy/src/lib.rs +++ b/tests/spl/token-proxy/programs/token-proxy/src/lib.rs @@ -1,6 +1,7 @@ //! This example demonstrates the use of the `anchor_spl::token` CPI client. use anchor_lang::prelude::*; +use anchor_spl::associated_token::AssociatedToken; use anchor_spl::token_interface::{ self, Burn, Mint, MintTo, SetAuthority, TokenAccount, TokenInterface, Transfer, }; @@ -31,6 +32,20 @@ mod token_proxy { ) -> Result<()> { token_interface::set_authority(ctx.accounts.into(), authority_type.into(), new_authority) } + + pub fn proxy_create_token_account(_ctx: Context) -> Result<()> { + Ok(()) + } + + pub fn proxy_create_associated_token_account( + _ctx: Context, + ) -> Result<()> { + Ok(()) + } + + pub fn proxy_create_mint(_ctx: Context, _name: String) -> Result<()> { + Ok(()) + } } #[derive(AnchorSerialize, AnchorDeserialize)] @@ -92,6 +107,57 @@ pub struct ProxySetAuthority<'info> { pub token_program: Interface<'info, TokenInterface>, } +#[derive(Accounts)] +pub struct ProxyCreateTokenAccount<'info> { + #[account(mut)] + pub authority: Signer<'info>, + pub mint: InterfaceAccount<'info, Mint>, + #[account(init, + token::mint = mint, + token::authority = authority, + seeds = [authority.key().as_ref(), mint.key().as_ref(), b"token-proxy-account"], + bump, + payer = authority + )] + pub token_account: InterfaceAccount<'info, TokenAccount>, + pub system_program: Program<'info, System>, + pub token_program: Interface<'info, TokenInterface>, +} + +#[derive(Accounts)] +pub struct ProxyCreateAssociatedTokenAccount<'info> { + #[account(mut)] + pub authority: Signer<'info>, + #[account( + init, + associated_token::mint = mint, + payer = authority, + associated_token::authority = authority, + )] + pub token_account: InterfaceAccount<'info, TokenAccount>, + pub mint: InterfaceAccount<'info, Mint>, + pub system_program: Program<'info, System>, + pub token_program: Interface<'info, TokenInterface>, + pub associated_token_program: Program<'info, AssociatedToken>, +} + +#[derive(Accounts)] +#[instruction(name: String)] +pub struct ProxyCreateMint<'info> { + #[account(mut)] + pub authority: Signer<'info>, + #[account(init, + mint::decimals = 9, + mint::authority = authority, + seeds = [authority.key().as_ref(), name.as_bytes(), b"token-proxy-mint"], + bump, + payer = authority + )] + pub mint: InterfaceAccount<'info, Mint>, + pub system_program: Program<'info, System>, + pub token_program: Interface<'info, TokenInterface>, +} + impl<'a, 'b, 'c, 'info> From<&mut ProxyTransfer<'info>> for CpiContext<'a, 'b, 'c, 'info, Transfer<'info>> { diff --git a/tests/spl/token-proxy/tests/token-proxy.js b/tests/spl/token-proxy/tests/token-proxy.js index b2709f3bc2..9618af416b 100644 --- a/tests/spl/token-proxy/tests/token-proxy.js +++ b/tests/spl/token-proxy/tests/token-proxy.js @@ -29,6 +29,68 @@ describe("program", () => { to = await createTokenAccount(tokenProgram, mint, provider.wallet.publicKey); }); + it("Creates a token account", async () => { + const newMint = await createMint(tokenProgram); + const authority = provider.wallet.publicKey; + const [tokenAccount] = anchor.web3.PublicKey.findProgramAddressSync( + [authority.toBytes(), newMint.toBytes(), Buffer.from("token-proxy-account")], + program.programId + ); + await program.rpc.proxyCreateTokenAccount({ + accounts: { + authority, + mint: newMint, + tokenAccount, + systemProgram: anchor.web3.SystemProgram.programId, + tokenProgram: tokenProgram.programId, + }, + }); + const account = await getTokenAccount(provider, tokenAccount); + assert.isTrue(account.amount.eq(new anchor.BN(0))); + }); + + it("Creates an associated token account", async () => { + const newMint = await createMint(tokenProgram); + const authority = provider.wallet.publicKey; + const associatedTokenProgram = new anchor.web3.PublicKey("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL"); + const [tokenAccount] = anchor.web3.PublicKey.findProgramAddressSync( + [authority.toBytes(), tokenProgram.programId.toBytes(), newMint.toBytes()], + associatedTokenProgram + ); + + await program.rpc.proxyCreateAssociatedTokenAccount({ + accounts: { + tokenAccount, + mint: newMint, + authority, + systemProgram: anchor.web3.SystemProgram.programId, + tokenProgram: tokenProgram.programId, + associatedTokenProgram, + }, + }); + const account = await getTokenAccount(provider, tokenAccount); + assert.isTrue(account.amount.eq(new anchor.BN(0))); + }); + + it("Creates a mint", async () => { + const authority = provider.wallet.publicKey; + const [newMint] = anchor.web3.PublicKey.findProgramAddressSync( + [authority.toBytes(), Buffer.from(name), Buffer.from("token-proxy-mint")], + program.programId + ); + await program.rpc.proxyCreateMint( + name, + { + accounts: { + authority, + mint: newMint, + systemProgram: anchor.web3.SystemProgram.programId, + tokenProgram: tokenProgram.programId, + }, + } + ); + }); + it("Mints a token", async () => { await program.rpc.proxyMintTo(new anchor.BN(1000), { accounts: {