This is a Solana program written using the Anchor framework that allows users to save and get back their tokens from a piggy bank.
Further to the previous Counter program which demonstrate how to store and manipulate data on Solana, this program demonstrates:
- Transfer of tokens
- Custody of user's tokens to a vault owned by PDA
- Closure of normal accounts and token accounts
- Using clock Solana
- Handling predefined errors
The PiggyBank program initializes a bank account with specifying the owner, the token mint allowed and the lock period.
pub fn init_bank(ctx: Context<InitBank>, timeout: i64)Deposit tokens into the bank.
pub fn deposit(ctx: Context<Deposit>, amount: u64)Check if the lock period expiries, withdraw all tokens deposited and close the bank.
pub fn close_bank(ctx: Context<CloseBank>)The program uses the following account structures:
#[account]
#[derive(InitSpace)]
pub struct PiggyBank {
pub owner: Pubkey,
pub mint: Pubkey,
pub balance: u64,
pub start_time: i64,
pub timeout: i64,
}git clone https://github.com/kenchan0824/anchor-piggybank.git
cd solana-piggybankanchor testThe tests for the counter program are located in
piggybank.ts
The tests include:
- Initialize the piggy bank
- Deposit into piggy bank
- Close the piggy bank should fail before timeout
- Close the piggy bank should fail before timeout
- Close the piggy bank
This project is licensed under the MIT License. See the LICENSE file for details.