Rust
Pinocchio Escrow

Pinocchio Escrow

18 Graduates

Make

The make instruction does three jobs:

  • Initialises the Escrow record and stores all deal terms.
  • Creates the Vault (an ATA for mint_a owned by the escrow).
  • Moves the maker's Token A into that vault with a CPI to the SPL-Token program.

Required Accounts

Below are the accounts the context needs:

  • maker: the creator of the escrow. Must be a signer and mutable
  • escrow: the escrow account that we're initializing. Must be mutable
  • mint_a: the token we're depositing in the escrow
  • mint_b: the token we want to receive
  • maker_ata_a: the associated token account owned by the maker. Must be mutable
  • vault: the associated token account owned by the escrow. Must be mutable
  • associated_token_program: the associated token program. Must be executable
  • token_program: the token program. Must be executable
  • system_program: the system program. Must be executable

Note: We're going to use the types that introduced in the Introduction to Pinocchio.

In code, this looks like:

rust
pub struct MakeAccounts<'a> {
  pub maker: &'a AccountInfo,
  pub escrow: &'a AccountInfo,
  pub mint_a: &'a AccountInfo,
  pub mint_b: &'a AccountInfo,
  pub maker_ata_a: &'a AccountInfo,
  pub vault: &'a AccountInfo,
  pub system_program: &'a AccountInfo,
  pub token_program: &'a AccountInfo,
}
 
impl<'a> TryFrom<&'a [AccountInfo]> for MakeAccounts<'a> {
  type Error = ProgramError;
 
  fn try_from(accounts: &'a [AccountInfo]) -> Result<Self, Self::Error> {
    let [maker, escrow, mint_a, mint_b, maker_ata_a, vault, system_program, token_program, _] = accounts else {
      return Err(ProgramError::NotEnoughAccountKeys);
    };
 
    // Basic Accounts Checks
    SignerAccount::check(maker)?;
    MintInterface::check(mint_a)?;
    MintInterface::check(mint_b)?;
    AssociatedTokenAccount::check(maker_ata_a, maker, mint_a, token_program)?;
 
    // Return the accounts
    Ok(Self {
      maker,
      escrow,
      mint_a,
      mint_b,
      maker_ata_a,
      vault,
      system_program,
      token_program,
    })
  }
}

Instruction Data

Here's the instruction data we need to pass in:

  • seed: the random number used during seed derivation. Must be a u64
  • receive: the amount that the maker wants to receive. Must be a u64
  • amount: the amount that the maker wants to deposit. Must be a u64

We'll check to make sure the amount isn't zero, since that wouldn't make sense for an escrow.

Here's how it looks in code:

rust
pub struct MakeInstructionData {
  pub seed: u64,
  pub receive: u64,
  pub amount: u64,
}
 
impl<'a> TryFrom<&'a [u8]> for MakeInstructionData {
  type Error = ProgramError;
 
  fn try_from(data: &'a [u8]) -> Result<Self, Self::Error> {
    if data.len() != size_of::<u64>() * 3 {
      return Err(ProgramError::InvalidInstructionData);
    }
 
    let seed = u64::from_le_bytes(data[0..8].try_into().unwrap());
    let receive = u64::from_le_bytes(data[8..16].try_into().unwrap());
    let amount = u64::from_le_bytes(data[16..24].try_into().unwrap());
 
    // Instruction Checks
    if amount == 0 {
      return Err(ProgramError::InvalidInstructionData);
    }
 
    Ok(Self {
      seed,
      receive,
      amount,
    })
  }
}

Instruction Logic

We begin by initializing the required accounts in the TryFrom implementation, after we've deserialized both the instruction_data and the accounts.

For this step, we create the Escrow account using the ProgramAccount::init::<Escrow> trait from the helper functions introduced in the Introduction to Pinocchio. Similarly, we initialize the Vault account since it needs to be created fresh:

rust
pub struct Make<'a> {
  pub accounts: MakeAccounts<'a>,
  pub instruction_data: MakeInstructionData,
  pub bump: u8,
}
 
impl<'a> TryFrom<(&'a [u8], &'a [AccountInfo])> for Make<'a> {
  type Error = ProgramError;
  
  fn try_from((data, accounts): (&'a [u8], &'a [AccountInfo])) -> Result<Self, Self::Error> {
    let accounts = MakeAccounts::try_from(accounts)?;
    let instruction_data = MakeInstructionData::try_from(data)?;
 
    // Initialize the Accounts needed
    let (_, bump) = find_program_address(&[b"escrow", accounts.maker.key(), &instruction_data.seed.to_le_bytes()], &crate::ID);
 
    let seed_binding = instruction_data.seed.to_le_bytes();
    let bump_binding = [bump];
    let escrow_seeds = [
      Seed::from(b"escrow"),
      Seed::from(accounts.maker.key().as_ref()),
      Seed::from(&seed_binding),
      Seed::from(&bump_binding),
    ];
            
    ProgramAccount::init::<Escrow>(
      accounts.maker,
      accounts.escrow,
      &escrow_seeds,
      Escrow::LEN
    )?;
 
    // Initialize the vault
    AssociatedTokenAccount::init(
      accounts.vault,
      accounts.mint_a,
      accounts.maker,
      accounts.escrow,
      accounts.system_program,
      accounts.token_program,
    )?;
 
    Ok(Self {
      accounts,
      instruction_data,
      bump,
    })
  }
}

We can now focus on the logic itself that will just be populating the escrow account and then transfering tokens to the vault.

rust
impl<'a> Make<'a> {
  pub const DISCRIMINATOR: &'a u8 = &0;
  
  pub fn process(&mut self) -> ProgramResult {
    // Populate the escrow account
    let mut data = self.accounts.escrow.try_borrow_mut_data()?;
    let escrow = Escrow::load_mut(data.as_mut())?;
    
    escrow.set_inner(
      self.instruction_data.seed,
      *self.accounts.maker.key(),
      *self.accounts.mint_a.key(),
      *self.accounts.mint_b.key(),
      self.instruction_data.receive,
      [self.bump],
    );
 
    // Transfer tokens to vault
    Transfer {
      from: self.accounts.maker_ata_a,
      to: self.accounts.vault,
      authority: self.accounts.maker,
      amount: self.instruction_data.amount
    }.invoke()?;
 
    Ok(())
  }
}
Next PageTake
OR SKIP TO THE CHALLENGE
Ready to take the challenge?
Contents
View Source
Blueshift © 2025Commit: cc5f933