Giter VIP home page Giter VIP logo

anchor-escrow's Introduction

Anchor Example: Escrow Program

See this doc for more implementation details

Overview

Since this program is extended from the original Escrow Program, I assumed you have gone through the original blog post at least once.

However, there is one major difference between this exmaple and the original Escrow program: Instead of letting initializer create a token account to be reset to a PDA authority, we create a token account Vault that has both a PDA key and a PDA authority.

Initialize

Initializer can send a transaction to the escrow program to initialize the Vault. In this transaction, two new accounts: Vault and EscrowState, will be created and tokens (Token A) to be exchanged will be transfered from Initializer to Vault.

Cancel

Initializer can also send a transaction to the escrow program to cancel the demand of escrow. The tokens will be transfered back to the Initialzer and both Vault and EscrowState will be closed in this case.

Exchange

Taker can send a transaction to the escrow to exchange Token B for Token A. First, tokens (Token B) will be transfered from Taker to Initializer. Afterward, the tokens (Token A) kept in the Vault will be transfered to Taker. Finally, both Vault and EscrowState will be closed.

Install, Build, Deploy and Test

Let's run the test once to see what happens.

Install anchor

First, make sure that anchor is installed:

Install avm:

$ cargo install --git https://github.com/coral-xyz/anchor avm --locked --force
...

Install latest anchor version:

$ avm install 0.26.0
...
$ avm use 0.26.0
...

If you haven't installed cargo, please refer to this doc for installation steps.

Extra Dependencies on Linux (Optional)

You may have to install some extra dependencies on Linux (ex. Ubuntu):

$ sudo apt-get update && sudo apt-get upgrade && sudo apt-get install -y pkg-config build-essential libudev-dev
...

Verify the Installation

Check if Anchor is successfully installed:

$ anchor --version
anchor-cli 0.26.0

Install Dependencies

Next, install dependencies:

$ yarn

Build anchor-escrow

Update program_id

Get the public key of the deploy key. This keypair is generated automatically so a different key is exptected:

$ anchor keys list
anchor_escrow: GW65RiuuG2zU27S39FW83Yug1t13RxWWwHSCWRwSaybC

Replace the default value of program_id with this new value:

# Anchor.toml

[programs.localnet]
anchor_escrow = "GW65RiuuG2zU27S39FW83Yug1t13RxWWwHSCWRwSaybC"

...
// lib.rs

...

declare_id!("GW65RiuuG2zU27S39FW83Yug1t13RxWWwHSCWRwSaybC");

...

Build the program:

$ anchor build

Deploy anchor-escrow

Let's deploy the program. Notice that anchor-escrow will be deployed on a mainnet-fork test validator run by Dappio:

$ solana config set --url https://rpc-mainnet-fork.epochs.studio
...
$ solana config set --ws wss://rpc-mainnet-fork.epochs.studio/ws
...
$ anchor deploy
...

Program Id: GW65RiuuG2zU27S39FW83Yug1t13RxWWwHSCWRwSaybC

Deploy success

Finally, run the test:

$ anchor test --skip-build --skip-deploy

anchor-escrow's People

Contributors

ironaddicteddog avatar 0xcatrovacer avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.