This project implements an escrow program on the Solana blockchain using the Anchor framework. The program allows users to create escrow accounts where they can deposit tokens securely, facilitating a two-party exchange. The program supports operations for making an escrow, taking from an escrow, and refunding deposits.
-
Clone the repository:
git clone https://github.com/yourusername/solana-anchor-escrow.git cd solana-anchor-escrow
-
Install the required dependencies:
anchor build
-
Ensure your Solana wallet is set up and funded on the devnet.
The project consists of several key files:
File | Description |
---|---|
lib.rs |
Contains the main logic of the escrow program, including methods for making, taking, and refunding escrows. |
state/escrow.rs |
Defines the data structure for the escrow account. |
instructions/make.rs |
Contains logic for creating an escrow and depositing tokens. |
instructions/take.rs |
Contains logic for taking tokens from an escrow. |
instructions/refund.rs |
Handles refunding tokens back to the maker. |
mod.rs |
Module management for organizing related functionalities. |
The make
function allows a user to create a new escrow account and deposit tokens into it.
pub fn make(ctx: Context<Make>, seed: u64, deposit: u64, receive: u64) -> Result<()> {
ctx.accounts.deposit(deposit);
ctx.accounts.init_escrow(seed, receive, &ctx.bumps)
}
- Depositing Tokens: The user deposits tokens into the escrow account.
- Initializing Escrow: Sets up the escrow account with necessary parameters.
The take
function allows a user to take tokens from an existing escrow account.
pub fn take(ctx: Context<Take>) -> Result<()> {
ctx.accounts.deposit()?;
ctx.accounts.withdraw()?;
ctx.accounts.close()?;
Ok(())
}
- Depositing Tokens: The taker deposits their tokens into the escrow.
- Withdrawing Tokens: The taker withdraws tokens from the vault.
- Closing Escrow: Finalizes and closes the escrow account.
The refund
function allows a user to refund their tokens back from the escrow account.
pub fn refund(ctx: Context<Refund>) -> Result<()> {
ctx.accounts.refund()?;
ctx.accounts.close_refund()?;
Ok(())
}
- Refunding Tokens: Transfers tokens back to the maker's associated token account.
- Closing Escrow: Closes the escrow account after refunding.
To run tests for the escrow program, execute the following command:
anchor test
-
Deploy the Program: Deploy your program to the Solana devnet using:
anchor deploy --provider.cluster devnet
-
Interact with the Program: You can interact with your deployed program using client-side scripts or through a frontend application that connects to Solana.
-
Example Commands:
- Making an Escrow:
const tx = await program.methods.make(seed, depositAmount, receiveAmount).rpc(); console.log("Your transaction signature", tx);
- Taking from an Escrow:
const tx = await program.methods.take().rpc(); console.log("Take transaction signature", tx);
- Refunding an Escrow:
const tx = await program.methods.refund().rpc(); console.log("Refund transaction signature", tx);
- Making an Escrow:
This project is licensed under the MIT License - see the LICENSE file for details.