Skip to content

gameyoo/spl-token-timelock

Repository files navigation

🔐 spl-token-timelock

License Contributors

spl-token lockup programs on Solana.

This project demonstrates how to write a program that allows you to lock arbitrary SPL tokens and release the locked tokens with a determined unlock schedule.

The project comprises of:

  • An on-chain lockup tokens program
  • A set of test cases for interacting with the on-chain program

Prerequisites

Install Rust && Solana Cli && Anchor

Install rustup

$ curl https://sh.rustup.rs -sSf | sh
...

$ rustup component add rustfmt
...

$ rustup update
...

$ rustup install 1.61.0
...

Install solana-cli

$ sh -c "$(curl -sSfL https://release.solana.com/stable/install)"
...

$ solana -V
solana-cli 1.8.17 (src:f63505df; feat:3263758455)

$ solana-keygen new
...

Config to local cluster:

solana config set --url localhost
...

Install rust-analyzer (Optional)

rust-analyzer can be very handy if you are using Visual Studio Code. For example, the analyzer can help download the missing dependencies for you automatically.

Install avm & anchor

$ cargo install --git https://github.com/project-serum/anchor avm --locked --force
...

Use latest anchor version:

avm use 0.20.1

Extra Dependencies on Linux(Optional)

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

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

Verify the Installation

Check if Anchor is successfully installed.

$ anchor --version
anchor-cli 0.20.1

Build and Deployment

Frist,let's clone the repo:

$ git clone https://github.com/gameyoo/spl-token-timelock.git
...

$ cd spl-token-timelock
...
$ npm install
...

Run solana-test-validator in another terminal session:

$ cd ~
$ solana-test-validator
Ledger location: test-ledger
Log: test-ledger/validator.log
Identity: 7YbUvia6fEB9yJZbt6o7RhrSQqUQGAAV8anF4rsfhawU
Genesis Hash: 2Md358rLRXS9Q9rYLSzbixxm8nHisbhJAuNsDsMXi9bj
Version: 1.8.17
Shred Version: 56413
Gossip Address: 127.0.0.1:1024
TPU Address: 127.0.0.1:1027
JSON RPC URL: http://127.0.0.1:8899
...

Run solana logs to fetch logs in another terminal session:

$ solana logs
Streaming transaction logs. Confirmed commitment
...

Next, let's compile the token-faucet program:

$ anchor build

BPF SDK: ~/.local/share/solana/install/releases/stable-f63505df33b76ed694257b87231c91620f4b8d68/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
...

Deploy the program after compilation:

$ anchor deploy
Deploying workspace: <http://localhost:8899>
Upgrade authority: ~/.config/solana/id.json
Deploying program "spl-token-timelock"...
...

If you encounter an insuffficient fund error, you may have to request for an aidrop:

$ solana airdrop 1

Test

Finally, let's run all tests by using following command:

$ anchor test
BPF SDK: ~/.local/share/solana/install/releases/stable-f63505df33b76ed694257b87231c91620f4b8d68/solana-release/bin/sdk/bpf
cargo-build-bpf child: rustup toolchain list -v
cargo-build-bpf child: cargo +bpf build --target bpfel-unknown-unknown --release
    Finished release [optimized] target(s) in 2.36
...
    spl-token-timelock
        ✔ Create vesting (444ms)
        ✔ Withdraw (10334ms)
        ✔ Cancel (12117ms)
3 passing (24s)

✨  Done in 38.00s.

We should see all test cases passed,that's it!

License

spl-token-timelock is licensed under the MIT License.