Creating a Token Vesting Contract on Solana Blockchain

Posted By : Ashutosh

Oct 29, 2024

In the world of crypto/token development and blockchain, token vesting is a vital mechanism used to allocate tokens to individuals over a specified period rather than all at once. This approach helps to align the interests of contributors, advisors, and investors with the long-term success of a project. In this blog, we'll explore the concept of token vesting, and how it works, and dive into a practical implementation using the Simple Token Vesting contract written in Rust with the Anchor framework.

 

What is Token Vesting?

 

Token vesting involves gradually releasing tokens to individuals (beneficiaries) based on predefined schedules and conditions. This helps prevent immediate sell-offs and incentivises participants to stay committed to the project. The key benefits of token vesting include:

 

  • Promoting Long-Term Commitment: Beneficiaries are motivated to remain involved with the project.
  • Preventing Market Manipulation: Reduces the risk of large sell-offs that could affect the token's price.
  • Aligning Interests: Ensures that all parties work toward the project's success over time.

     

    Also, Explore | How to Build a Crypto Portfolio Tracker
     

    The Structure of the Simple Token Vesting Contract

     

    The Simple Token Vesting contract provides a framework for managing token vesting on the Solana blockchain. Let's break down its main components:

     

  • Initialization: The Admin sets up the contract with a list of beneficiaries and allocates tokens for them.
  • Releasing Tokens: The Admin can release a percentage of tokens to beneficiaries periodically.
  • Claiming Tokens: Beneficiaries can claim their vested tokens based on the amount released.

     

    #[program]
    pub mod token_vesting {
        use super::*;
    
        pub fn initialize(ctx: Context, beneficiaries: Vec, amount: u64, decimals: u8) -> Result<()> {
            // Initialization logic here...
        }
    
        pub fn release(ctx: Context, percent: u8) -> Result<()> {
            // Release logic here...
        }
    
        pub fn claim(ctx: Context, data_bump: u8) -> Result<()> {
            // Claim logic here...
        }
    }
    

     

    Also, Read | How to Deploy a TRC-20 Token on the TRON Blockchain

     

    How the Contract Works

     

1. Initialisation Function

 

During initialization, the Admin calls the initialise function to set up the vesting contract. This function takes a list of beneficiaries, the total amount of tokens to vest, and the token's decimals. Here's how it looks in the code:

 

pub fn initialize(ctx: Context, beneficiaries: Vec, amount: u64, decimals: u8) -> Result<()> {
    let data_account = &mut ctx.accounts.data_account;
    data_account.beneficiaries = beneficiaries;
    data_account.token_amount = amount;
    data_account.decimals = decimals;

    // Transfer tokens from Admin to escrow wallet
    let transfer_instruction = Transfer {
        from: ctx.accounts.wallet_to_withdraw_from.to_account_info(),
        to: ctx.accounts.escrow_wallet.to_account_info(),
        authority: ctx.accounts.sender.to_account_info(),
    };

    let cpi_ctx = CpiContext::new(
        ctx.accounts.token_program.to_account_info(),
        transfer_instruction,
    );

    token::transfer(cpi_ctx, amount * u64::pow(10, decimals as u32))?;
    Ok(())
}

 

Explanation:

 

  • Parameters: The function takes a list of beneficiaries, the total token amount to be vested, and the decimals.
  • Data Account: Initialises a data account to keep track of the beneficiaries and their allocations.
  • Token Transfer: Transfers the specified amount of tokens from the Admin's wallet to the escrow wallet for distribution.

     

    You may also like | How to Create an ERC 721C Contract

     

    2. Release Function

     

The release function allows the Admin to specify what percentage of the total tokens is available for beneficiaries to claim. Here's the code:

 

pub fn release(ctx: Context, percent: u8) -> Result<()> {
    let data_account = &mut ctx.accounts.data_account;
    data_account.percent_available = percent; // Set the available percentage
    Ok(())
}

 

Explanation:

 

Setting Percent Available: The Admin can call this function to set a percentage that beneficiaries can claim. For example, if percent is set to 20, beneficiaries can claim 20% of their allocated tokens.

 

3. Claim Function

 

  • Beneficiaries use the claim function to withdraw their available tokens. Here's how it works:

     

    pub fn claim(ctx: Context, data_bump: u8) -> Result<()> {
        let data_account = &mut ctx.accounts.data_account;
        let beneficiaries = &data_account.beneficiaries;
    
        let (index, beneficiary) = beneficiaries.iter().enumerate().find(|(_, beneficiary)| beneficiary.key == *sender.to_account_info().key)
            .ok_or(VestingError::BeneficiaryNotFound)?;
    
        let amount_to_transfer = ((data_account.percent_available as f32 / 100.0) * beneficiary.allocated_tokens as f32) as u64;
    
        // Transfer tokens to beneficiary's wallet
        let transfer_instruction = Transfer {
            from: ctx.accounts.escrow_wallet.to_account_info(),
            to: beneficiary_ata.to_account_info(),
            authority: data_account.to_account_info(),
        };
    
        let cpi_ctx = CpiContext::new_with_signer(
            token_program.to_account_info(),
            transfer_instruction,
            signer_seeds
        );
    
        token::transfer(cpi_ctx, amount_to_transfer * u64::pow(10, data_account.decimals as u32))?;
        data_account.beneficiaries[index].claimed_tokens += amount_to_transfer;
    
        Ok(())
    }


    Explanation:

     

  • Finding Beneficiary: The function identifies the calling beneficiary from the list.
  • Calculating Transfer Amount: It calculates how much the beneficiary can claim based on the percentage available.
  • Token Transfer: Transfers the calculated amount from the escrow wallet to the beneficiary's associated token account.

 

Also, Check | How to Create and Deploy an ERC404 token contract

 

Conclusion

 

Token vesting is a powerful tool in the cryptocurrency ecosystem that promotes long-term commitment among participants. The Simple Token Vesting contract provides a straightforward implementation for managing vesting schedules on the Solana blockchain, allowing for flexible token distribution over time.

With the ability to define beneficiaries, release tokens, and claim rewards, this contract exemplifies how token vesting can align the interests of a project's contributors with its long-term success. Whether you are a developer looking to implement a vesting mechanism or a project owner aiming to incentivize your team, understanding and utilizing token vesting is crucial in today's crypto landscape. Looking for assistance with a similar project, connect with our crypto/token developers to get started. 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 8, 2024 at 08:25 pm

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text