Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[solana] Add close_vesting_balance instruction #242

Merged
merged 7 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions solana/programs/staking/src/contexts/close_vesting_balance.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use crate::context::{VESTING_BALANCE_SEED, VESTING_CONFIG_SEED};
use crate::error::VestingError;
use crate::state::{VestingBalance, VestingConfig};
use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token_interface::{Mint, TokenAccount, TokenInterface};

#[derive(Accounts)]
#[instruction()]
pub struct CloseVestingBalance<'info> {
#[account(mut)]
/// CHECK: This account is the original rent_payer for the vesting_balance account
rent_payer: Signer<'info>,
mint: InterfaceAccount<'info, Mint>,
#[account(
seeds = [VESTING_CONFIG_SEED.as_bytes(), mint.key().as_ref(), config.seed.to_le_bytes().as_ref()],
bump = config.bump
)]
config: Account<'info, VestingConfig>,
#[account(
mut,
has_one = rent_payer,
constraint = vesting_balance.total_vesting_balance == 0 @ VestingError::NotFullyVested,
seeds = [VESTING_BALANCE_SEED.as_bytes(), config.key().as_ref(), vester_ta.owner.key().as_ref()],
bump,
close = rent_payer,
)]
vesting_balance: Account<'info, VestingBalance>,
#[account(
associated_token::mint = mint,
associated_token::authority = vester_ta.owner,
associated_token::token_program = token_program
)]
vester_ta: InterfaceAccount<'info, TokenAccount>,
associated_token_program: Program<'info, AssociatedToken>,
token_program: Interface<'info, TokenInterface>,
system_program: Program<'info, System>,
}

impl<'info> CloseVestingBalance<'info> {
pub fn close_vesting_balance(&mut self) -> Result<()> {
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl<'info> CreateVestingBalance<'info> {
stake_account_metadata: Pubkey::default(),
total_vesting_balance: 0,
bump,
rent_payer: self.admin.key(),
});

Ok(())
Expand Down
2 changes: 2 additions & 0 deletions solana/programs/staking/src/contexts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ pub use cancel_vesting::*;

pub mod withdraw_surplus;
pub use withdraw_surplus::*;
mod close_vesting_balance;
mod create_vesting_balance;

pub use close_vesting_balance::*;
pub use create_vesting_balance::*;

pub mod transfer_vesting;
Expand Down
5 changes: 5 additions & 0 deletions solana/programs/staking/src/contexts/transfer_vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ impl<'info> crate::contexts::TransferVesting<'info> {
.checked_add(self.vest.amount)
.ok_or(VestingError::Overflow)?,
bump: new_vesting_balance_bump,
rent_payer: if self.new_vesting_balance.rent_payer == Pubkey::default() {
self.vester.key()
} else {
self.new_vesting_balance.rent_payer
},
});

self.vesting_balance.total_vesting_balance = self
Expand Down
5 changes: 5 additions & 0 deletions solana/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ pub mod staking {
.create_vesting_balance(ctx.bumps.vesting_balance)
}

// Closes a vesting balance account
pub fn close_vesting_balance(ctx: Context<CloseVestingBalance>) -> Result<()> {
ctx.accounts.close_vesting_balance()
}

// Finalize a Config, disabling any further creation or cancellation of Vesting accounts
pub fn finalize_vesting_config(ctx: Context<Finalize>) -> Result<()> {
ctx.accounts.finalize()
Expand Down
3 changes: 2 additions & 1 deletion solana/programs/staking/src/state/vesting_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct VestingBalance {
pub total_vesting_balance: u64,
pub bump: u8,
pub stake_account_metadata: Pubkey,
pub rent_payer: Pubkey,
}

impl VestingBalance {
Expand All @@ -21,6 +22,6 @@ pub mod tests {

#[test]
fn check_size() {
assert!(VestingBalance::LEN == 8 + 32 + 8 + 1 + 32); // 81
assert!(VestingBalance::LEN == 8 + 32 + 8 + 1 + 32 + 32); // 113
}
}
Loading