| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 
 | use anchor_lang::prelude::*;use anchor_spl::token::{self, CloseAccount, Mint, SetAuthority, TokenAccount, Transfer};
 use spl_token::instruction::AuthorityType;
 
 declare_id!("CVXMDc2cNvT94Ghz9m9UUbskSwPfd6nHcZpxXZwJFE8b");
 
 #[program]
 pub mod anchor_escrow {
 use super::*;
 
 const ESCROW_PDA_SEED: &[u8] = b"escrow";
 
 pub fn initialize(
 ctx: Context<Initialize>,
 _vault_account_bump: u8,
 initializer_amount: u64,
 taker_amount: u64,
 ) -> Result<()> {
 ctx.accounts.escrow_account.initializer_key = *ctx.accounts.initializer.key;
 ctx.accounts
 .escrow_account
 .initializer_deposit_token_account = *ctx
 .accounts
 .initializer_deposit_token_account
 .to_account_info()
 .key;
 ctx.accounts
 .escrow_account
 .initializer_receive_token_account = *ctx
 .accounts
 .initializer_receive_token_account
 .to_account_info()
 .key;
 ctx.accounts.escrow_account.initializer_amount = initializer_amount;
 ctx.accounts.escrow_account.taker_amount = taker_amount;
 
 let (vault_authority, _vault_authority_bump) =
 Pubkey::find_program_address(&[ESCROW_PDA_SEED], ctx.program_id);
 token::set_authority(
 ctx.accounts.into_set_authority_context(),
 AuthorityType::AccountOwner,
 Some(vault_authority),
 )?;
 
 token::transfer(
 ctx.accounts.into_transfer_to_pda_context(),
 ctx.accounts.escrow_account.initializer_amount,
 )?;
 
 Ok(())
 }
 
 pub fn cancel(ctx: Context<Cancel>) -> Result<()> {
 let (_vault_authority, vault_authority_bump) =
 Pubkey::find_program_address(&[ESCROW_PDA_SEED], ctx.program_id);
 let authority_seeds = &[&ESCROW_PDA_SEED[..], &[vault_authority_bump]];
 
 token::transfer(
 ctx.accounts
 .into_transfer_to_initializer_context()
 .with_signer(&[&authority_seeds[..]]),
 ctx.accounts.escrow_account.initializer_amount,
 )?;
 
 token::close_account(
 ctx.accounts
 .into_close_context()
 .with_signer(&[&authority_seeds[..]]),
 )?;
 
 Ok(())
 }
 
 pub fn exchange(ctx: Context<Exchange>) -> Result<()> {
 let (_vault_authority, vault_authority_bump) =
 Pubkey::find_program_address(&[ESCROW_PDA_SEED], ctx.program_id);
 let authority_seeds = &[&ESCROW_PDA_SEED[..], &[vault_authority_bump]];
 
 token::transfer(
 ctx.accounts.into_transfer_to_initializer_context(),
 ctx.accounts.escrow_account.taker_amount,
 )?;
 
 token::transfer(
 ctx.accounts
 .into_transfer_to_taker_context()
 .with_signer(&[&authority_seeds[..]]),
 ctx.accounts.escrow_account.initializer_amount,
 )?;
 
 token::close_account(
 ctx.accounts
 .into_close_context()
 .with_signer(&[&authority_seeds[..]]),
 )?;
 
 Ok(())
 }
 }
 
 #[derive(Accounts)]
 #[instruction(vault_account_bump: u8, initializer_amount: u64)]
 pub struct Initialize<'info> {
 
 #[account(mut, signer)]
 pub initializer: AccountInfo<'info>,
 pub mint: Account<'info, Mint>,
 #[account(
 init,
 seeds = [b"token-seed".as_ref()],
 bump,
 payer = initializer,
 token::mint = mint,
 token::authority = initializer,
 )]
 pub vault_account: Account<'info, TokenAccount>,
 #[account(
 mut,
 constraint = initializer_deposit_token_account.amount >= initializer_amount
 )]
 pub initializer_deposit_token_account: Account<'info, TokenAccount>,
 pub initializer_receive_token_account: Account<'info, TokenAccount>,
 #[account(zero)]
 pub escrow_account: Box<Account<'info, EscrowAccount>>,
 
 pub system_program: AccountInfo<'info>,
 pub rent: Sysvar<'info, Rent>,
 
 pub token_program: AccountInfo<'info>,
 }
 
 #[derive(Accounts)]
 pub struct Cancel<'info> {
 
 #[account(mut, signer)]
 pub initializer: AccountInfo<'info>,
 #[account(mut)]
 pub vault_account: Account<'info, TokenAccount>,
 
 pub vault_authority: AccountInfo<'info>,
 #[account(mut)]
 pub initializer_deposit_token_account: Account<'info, TokenAccount>,
 #[account(
 mut,
 constraint = escrow_account.initializer_key == *initializer.key,
 constraint = escrow_account.initializer_deposit_token_account == *initializer_deposit_token_account.to_account_info().key,
 close = initializer
 )]
 pub escrow_account: Box<Account<'info, EscrowAccount>>,
 
 pub token_program: AccountInfo<'info>,
 }
 
 #[derive(Accounts)]
 pub struct Exchange<'info> {
 #[account(signer)]
 
 pub taker: AccountInfo<'info>,
 #[account(mut)]
 pub taker_deposit_token_account: Box<Account<'info, TokenAccount>>,
 #[account(mut)]
 pub taker_receive_token_account: Box<Account<'info, TokenAccount>>,
 #[account(mut)]
 pub initializer_deposit_token_account: Box<Account<'info, TokenAccount>>,
 #[account(mut)]
 pub initializer_receive_token_account: Box<Account<'info, TokenAccount>>,
 
 #[account(mut)]
 pub initializer: AccountInfo<'info>,
 #[account(
 mut,
 constraint = escrow_account.taker_amount <= taker_deposit_token_account.amount,
 constraint = escrow_account.initializer_deposit_token_account == *initializer_deposit_token_account.to_account_info().key,
 constraint = escrow_account.initializer_receive_token_account == *initializer_receive_token_account.to_account_info().key,
 constraint = escrow_account.initializer_key == *initializer.key,
 close = initializer
 )]
 pub escrow_account: Box<Account<'info, EscrowAccount>>,
 #[account(mut)]
 pub vault_account: Box<Account<'info, TokenAccount>>,
 
 pub vault_authority: AccountInfo<'info>,
 
 pub token_program: AccountInfo<'info>,
 }
 
 #[account]
 pub struct EscrowAccount {
 pub initializer_key: Pubkey,
 pub initializer_deposit_token_account: Pubkey,
 pub initializer_receive_token_account: Pubkey,
 pub initializer_amount: u64,
 pub taker_amount: u64,
 }
 
 impl<'info> Initialize<'info> {
 fn into_transfer_to_pda_context(&self) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> {
 let cpi_accounts = Transfer {
 from: self
 .initializer_deposit_token_account
 .to_account_info()
 .clone(),
 to: self.vault_account.to_account_info().clone(),
 authority: self.initializer.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 
 fn into_set_authority_context(&self) -> CpiContext<'_, '_, '_, 'info, SetAuthority<'info>> {
 let cpi_accounts = SetAuthority {
 account_or_mint: self.vault_account.to_account_info().clone(),
 current_authority: self.initializer.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 }
 
 impl<'info> Cancel<'info> {
 fn into_transfer_to_initializer_context(
 &self,
 ) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> {
 let cpi_accounts = Transfer {
 from: self.vault_account.to_account_info().clone(),
 to: self
 .initializer_deposit_token_account
 .to_account_info()
 .clone(),
 authority: self.vault_authority.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 
 fn into_close_context(&self) -> CpiContext<'_, '_, '_, 'info, CloseAccount<'info>> {
 let cpi_accounts = CloseAccount {
 account: self.vault_account.to_account_info().clone(),
 destination: self.initializer.clone(),
 authority: self.vault_authority.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 }
 
 impl<'info> Exchange<'info> {
 fn into_transfer_to_initializer_context(
 &self,
 ) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> {
 let cpi_accounts = Transfer {
 from: self.taker_deposit_token_account.to_account_info().clone(),
 to: self
 .initializer_receive_token_account
 .to_account_info()
 .clone(),
 authority: self.taker.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 
 fn into_transfer_to_taker_context(&self) -> CpiContext<'_, '_, '_, 'info, Transfer<'info>> {
 let cpi_accounts = Transfer {
 from: self.vault_account.to_account_info().clone(),
 to: self.taker_receive_token_account.to_account_info().clone(),
 authority: self.vault_authority.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 
 fn into_close_context(&self) -> CpiContext<'_, '_, '_, 'info, CloseAccount<'info>> {
 let cpi_accounts = CloseAccount {
 account: self.vault_account.to_account_info().clone(),
 destination: self.initializer.clone(),
 authority: self.vault_authority.clone(),
 };
 CpiContext::new(self.token_program.clone(), cpi_accounts)
 }
 }
 
 |