Anchor
Anchor Escrow

Anchor Escrow

42 Graduates

Refund

We can now move to the refund instruction, that lives in the refund.rs and will perform this actions:

  • Close the escrow PDA and send its rent lamports back to the maker.
  • Move the full Token A balance out of the vault and back to the maker, then close the vault account.

Accounts

The accounts needed in this context are:

  • maker: the user that decided the terms of the exchange
  • escrow: the account where all the terms of this exchange lives
  • mint_a: the token that the maker has deposited
  • vault: the token account associated with the escrow and mint_a where the tokens have been deposited
  • maker_ata_a: the token account associated with the maker and mint_a that will receive the tokens from the vault
  • associated_token_program: the associated token program used to create the associated token accounts
  • token_program: the token program used to CPI the transfer
  • system_program: the system program used to create the Escrow

This time we're not going to help you by creating the Context, so try doing it yourself! Please make sure to use the right order of accounts or our tests will fail.

Logic

The logic is similar to the take instruction but this time we just move the tokens from the vault to the maker_ata_a before closing the now empty vault.

This time again is time you learn how to do it yourself, so we're not going to tell you what the solution is.

Just know that once this executes, the offer is void, the vault is gone, and the maker has their Token A and rent back in their wallet.

Entrypoint

Now that we created all the function in the different instruction, we can finally popoulate the lib.rs with all the function we created; like this:

rust
#[program]
pub mod blueshift_anchor_escrow {
    use super::*;
 
    pub fn make(ctx: Context<Make>, seed: u64, recieve: u64, amount: u64) -> Result<()> {
        instructions::make::handler(ctx, seed, recieve, amount)
    }
 
    pub fn take(ctx: Context<Take>) -> Result<()> {
        instructions::take::handler(ctx)
    }
 
    pub fn refund(ctx: Context<Refund>) -> Result<()> {
        instructions::refund::handler(ctx)
    }
}

Conclusion

You can now test your program against our unit tests and claim your NFTs!

Start by building your program using the following command in your terminal

anchor build

This generated a .so file directly in your target/deploy folder.

Now click on the take challenge button and drop the file there!

Ready to take the challenge?
Contents
View Source
Blueshift © 2025Commit: cc5f933