Skip to content

Commit

Permalink
test: Support token or token-2022 in proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Feb 7, 2023
1 parent de644f8 commit ecc94e7
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 171 deletions.
6 changes: 6 additions & 0 deletions tests/spl/token-proxy/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ token_proxy = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
test = "yarn run mocha -t 1000000 tests/"

[features]

[test.validator]
url = "https://api.mainnet-beta.solana.com"

[[test.validator.clone]]
address = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
2 changes: 1 addition & 1 deletion tests/spl/token-proxy/programs/token-proxy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ cpi = ["no-entrypoint"]
[dependencies]
anchor-lang = { path = "../../../../../lang" }
anchor-spl = { path = "../../../../../spl" }
spl-token = { version = "3.1.1", features = ["no-entrypoint"] }
spl-token-2022 = { version = "0.5.0", features = ["no-entrypoint"] }
72 changes: 41 additions & 31 deletions tests/spl/token-proxy/programs/token-proxy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! This example demonstrates the use of the `anchor_spl::token` CPI client.

use anchor_lang::prelude::*;
use anchor_spl::token::{self, Burn, MintTo, SetAuthority, Transfer};
use anchor_spl::token_interface::{
self, Burn, Mint, MintTo, SetAuthority, TokenAccount, TokenInterface, Transfer,
};

declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");

Expand All @@ -10,23 +12,24 @@ mod token_proxy {
use super::*;

pub fn proxy_transfer(ctx: Context<ProxyTransfer>, amount: u64) -> Result<()> {
token::transfer(ctx.accounts.into(), amount)
#[allow(deprecated)]
token_interface::transfer(ctx.accounts.into(), amount)
}

pub fn proxy_mint_to(ctx: Context<ProxyMintTo>, amount: u64) -> Result<()> {
token::mint_to(ctx.accounts.into(), amount)
token_interface::mint_to(ctx.accounts.into(), amount)
}

pub fn proxy_burn(ctx: Context<ProxyBurn>, amount: u64) -> Result<()> {
token::burn(ctx.accounts.into(), amount)
token_interface::burn(ctx.accounts.into(), amount)
}

pub fn proxy_set_authority(
ctx: Context<ProxySetAuthority>,
authority_type: AuthorityType,
new_authority: Option<Pubkey>,
) -> Result<()> {
token::set_authority(ctx.accounts.into(), authority_type.into(), new_authority)
token_interface::set_authority(ctx.accounts.into(), authority_type.into(), new_authority)
}
}

Expand All @@ -45,55 +48,60 @@ pub enum AuthorityType {
#[derive(Accounts)]
pub struct ProxyTransfer<'info> {
#[account(signer)]
/// CHECK:
pub authority: AccountInfo<'info>,
#[account(mut)]
pub from: AccountInfo<'info>,
pub from: InterfaceAccount<'info, TokenAccount>,
#[account(mut)]
pub to: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub to: InterfaceAccount<'info, TokenAccount>,
pub token_program: Interface<'info, TokenInterface>,
}

#[derive(Accounts)]
pub struct ProxyMintTo<'info> {
#[account(signer)]
/// CHECK:
pub authority: AccountInfo<'info>,
#[account(mut)]
pub mint: AccountInfo<'info>,
pub mint: InterfaceAccount<'info, Mint>,
#[account(mut)]
pub to: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub to: InterfaceAccount<'info, TokenAccount>,
pub token_program: Interface<'info, TokenInterface>,
}

#[derive(Accounts)]
pub struct ProxyBurn<'info> {
#[account(signer)]
/// CHECK:
pub authority: AccountInfo<'info>,
#[account(mut)]
pub mint: AccountInfo<'info>,
pub mint: InterfaceAccount<'info, Mint>,
#[account(mut)]
pub from: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub from: InterfaceAccount<'info, TokenAccount>,
pub token_program: Interface<'info, TokenInterface>,
}

#[derive(Accounts)]
pub struct ProxySetAuthority<'info> {
#[account(signer)]
/// CHECK:
pub current_authority: AccountInfo<'info>,
#[account(mut)]
/// CHECK:
pub account_or_mint: AccountInfo<'info>,
pub token_program: AccountInfo<'info>,
pub token_program: Interface<'info, TokenInterface>,
}

impl<'a, 'b, 'c, 'info> From<&mut ProxyTransfer<'info>>
for CpiContext<'a, 'b, 'c, 'info, Transfer<'info>>
{
fn from(accounts: &mut ProxyTransfer<'info>) -> CpiContext<'a, 'b, 'c, 'info, Transfer<'info>> {
let cpi_accounts = Transfer {
from: accounts.from.clone(),
to: accounts.to.clone(),
from: accounts.from.to_account_info().clone(),
to: accounts.to.to_account_info().clone(),
authority: accounts.authority.clone(),
};
let cpi_program = accounts.token_program.clone();
let cpi_program = accounts.token_program.to_account_info();
CpiContext::new(cpi_program, cpi_accounts)
}
}
Expand All @@ -103,23 +111,23 @@ impl<'a, 'b, 'c, 'info> From<&mut ProxyMintTo<'info>>
{
fn from(accounts: &mut ProxyMintTo<'info>) -> CpiContext<'a, 'b, 'c, 'info, MintTo<'info>> {
let cpi_accounts = MintTo {
mint: accounts.mint.clone(),
to: accounts.to.clone(),
mint: accounts.mint.to_account_info().clone(),
to: accounts.to.to_account_info().clone(),
authority: accounts.authority.clone(),
};
let cpi_program = accounts.token_program.clone();
let cpi_program = accounts.token_program.to_account_info();
CpiContext::new(cpi_program, cpi_accounts)
}
}

impl<'a, 'b, 'c, 'info> From<&mut ProxyBurn<'info>> for CpiContext<'a, 'b, 'c, 'info, Burn<'info>> {
fn from(accounts: &mut ProxyBurn<'info>) -> CpiContext<'a, 'b, 'c, 'info, Burn<'info>> {
let cpi_accounts = Burn {
mint: accounts.mint.clone(),
from: accounts.from.clone(),
mint: accounts.mint.to_account_info().clone(),
from: accounts.from.to_account_info().clone(),
authority: accounts.authority.clone(),
};
let cpi_program = accounts.token_program.clone();
let cpi_program = accounts.token_program.to_account_info();
CpiContext::new(cpi_program, cpi_accounts)
}
}
Expand All @@ -134,18 +142,20 @@ impl<'a, 'b, 'c, 'info> From<&mut ProxySetAuthority<'info>>
account_or_mint: accounts.account_or_mint.clone(),
current_authority: accounts.current_authority.clone(),
}; // TODO: Support multisig signers
let cpi_program = accounts.token_program.clone();
let cpi_program = accounts.token_program.to_account_info();
CpiContext::new(cpi_program, cpi_accounts)
}
}

impl From<AuthorityType> for spl_token::instruction::AuthorityType {
fn from(authority_ty: AuthorityType) -> spl_token::instruction::AuthorityType {
impl From<AuthorityType> for spl_token_2022::instruction::AuthorityType {
fn from(authority_ty: AuthorityType) -> spl_token_2022::instruction::AuthorityType {
match authority_ty {
AuthorityType::MintTokens => spl_token::instruction::AuthorityType::MintTokens,
AuthorityType::FreezeAccount => spl_token::instruction::AuthorityType::FreezeAccount,
AuthorityType::AccountOwner => spl_token::instruction::AuthorityType::AccountOwner,
AuthorityType::CloseAccount => spl_token::instruction::AuthorityType::CloseAccount,
AuthorityType::MintTokens => spl_token_2022::instruction::AuthorityType::MintTokens,
AuthorityType::FreezeAccount => {
spl_token_2022::instruction::AuthorityType::FreezeAccount
}
AuthorityType::AccountOwner => spl_token_2022::instruction::AuthorityType::AccountOwner,
AuthorityType::CloseAccount => spl_token_2022::instruction::AuthorityType::CloseAccount,
}
}
}
Loading

0 comments on commit ecc94e7

Please sign in to comment.