# Exercice: Token Escrow
EXERCISE_ESCROW = '''
use anchor_lang::prelude::*;
use anchor_spl::token::{self, Mint, Token, TokenAccount};
#[program]
pub mod escrow {
use super::*;
pub fn make(
ctx: Context<Make>,
seed: u64,
receive_amount: u64,
) -> Result<()> {
let escrow = &mut ctx.accounts.escrow;
escrow.seed = seed;
escrow.maker = ctx.accounts.maker.key();
escrow.mint_a = ctx.accounts.mint_a.key();
escrow.mint_b = ctx.accounts.mint_b.key();
escrow.receive_amount = receive_amount;
escrow.bump = ctx.bumps.escrow;
// Transfer tokens to vault
token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
token::Transfer {
from: ctx.accounts.deposit.to_account_info(),
to: ctx.accounts.vault.to_account_info(),
authority: ctx.accounts.maker.to_account_info(),
},
),
ctx.accounts.deposit.amount,
)
}
pub fn take(ctx: Context<Take>) -> Result<()> {
// Transfer B to maker
token::transfer(
CpiContext::new(
ctx.accounts.token_program.to_account_info(),
token::Transfer {
from: ctx.accounts.depositor_token_b.to_account_info(),
to: ctx.accounts.maker_token_b.to_account_info(),
authority: ctx.accounts.depositor.to_account_info(),
},
),
ctx.accounts.escrow.receive_amount,
)?;
// Transfer A to depositor
token::transfer(
CpiContext::new_with_signer(
ctx.accounts.token_program.to_account_info(),
token::Transfer {
from: ctx.accounts.vault.to_account_info(),
to: ctx.accounts.depositor_token_a.to_account_info(),
authority: ctx.accounts.escrow.to_account_info(),
},
&[&[
b"escrow",
ctx.accounts.maker.key().as_ref(),
&ctx.accounts.escrow.seed.to_le_bytes()[..],
&[ctx.accounts.escrow.bump],
]],
),
ctx.accounts.vault.amount,
)
}
}
// --- Structs d'accounts (absentes de la version initiale) ---
// Sans ces structs, `Context<Make>` et `Context<Take>` ne compilent pas :
// Anchor resout ces types vers des structs `#[derive(Accounts)]` du meme nom.
#[derive(Accounts)]
pub struct Make<\'info> {
#[account(
init,
payer = maker,
space = 8 + Escrow::INIT_SPACE,
seeds = [b"escrow", maker.key().as_ref(), &seed.to_le_bytes()],
bump
)]
pub escrow: Account<\'info, Escrow>,
#[account(mut)]
pub maker: Signer<\'info>,
pub mint_a: Account<\'info, Mint>,
pub mint_b: Account<\'info, Mint>,
#[account(mut)]
pub deposit: Account<\'info, TokenAccount>,
#[account(mut)]
pub vault: Account<\'info, TokenAccount>,
pub token_program: Program<\'info, Token>,
pub system_program: Program<\'info, System>,
}
#[derive(Accounts)]
pub struct Take<\'info> {
#[account(
mut,
seeds = [b"escrow", maker.key().as_ref(), &escrow.seed.to_le_bytes()],
bump = escrow.bump
)]
pub escrow: Account<\'info, Escrow>,
/// CHECK: compte de l\'autorite du compte escrow, verifie par les seeds.
pub maker: UncheckedAccount<\'info>,
#[account(mut)]
pub depositor: Signer<\'info>,
#[account(mut)]
pub depositor_token_a: Account<\'info, TokenAccount>,
#[account(mut)]
pub depositor_token_b: Account<\'info, TokenAccount>,
#[account(mut)]
pub maker_token_b: Account<\'info, TokenAccount>,
#[account(mut)]
pub vault: Account<\'info, TokenAccount>,
pub token_program: Program<\'info, Token>,
}
#[account]
#[derive(InitSpace)]
pub struct Escrow {
pub maker: Pubkey,
pub mint_a: Pubkey,
pub mint_b: Pubkey,
pub receive_amount: u64,
pub seed: u64,
pub bump: u8,
}
'''
print("Exercice Escrow:")
print(EXERCISE_ESCROW)