Posted By : Ashutosh
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.
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:
Aligning Interests: Ensures that all parties work toward the project's success over time.
Also, Explore | How to Build a Crypto Portfolio Tracker
The Simple Token Vesting contract provides a framework for managing token vesting on the Solana blockchain. Let's break down its main components:
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
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:
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
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.
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:
Also, Check | How to Create and Deploy an ERC404 token contract
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.
November 8, 2024 at 08:25 pm
Your comment is awaiting moderation.