Refund
We can now move to the refund instruction, that lives in the refund.rs and will perform these 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 exchangeescrow: the account where all the terms of this exchange livesmint_a: the token that themakerhas depositedvault: the token account associated with theescrowandmint_awhere the tokens have been depositedmaker_ata_a: the token account associated with themakerandmint_athat will receive the tokens from thevaultassociated_token_program: the associated token program used to create the associated token accountstoken_program: the token program used to CPI the transfersystem_program: the system program used to create theEscrow
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 it's your turn to 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 populate the lib.rs with all the function we created; like this:
#[program]
pub mod blueshift_anchor_escrow {
use super::*;
pub fn make(ctx: Context<Make>, seed: u64, receive: u64, amount: u64) -> Result<()> {
instructions::make::handler(ctx, seed, receive, 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 buildThis generated a .so file directly in your target/deploy folder.
Now click on the take challenge button and drop the file there!