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 RecordedBalanceChanged and RecordedVestingBalanceChanged events #219

Merged
merged 1 commit into from
Dec 19, 2024
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
7 changes: 5 additions & 2 deletions solana/programs/staking/src/contexts/claim_vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,15 @@ impl<'info> ClaimVesting<'info> {
VestingError::InvalidStakeAccountOwner
);

// Update the recorded vesting balance
stake_account_metadata.recorded_vesting_balance = stake_account_metadata
let new_recorded_vesting_balance = stake_account_metadata
.recorded_vesting_balance
.checked_sub(self.vest.amount)
.ok_or(VestingError::Underflow)?;

// Update the recorded vesting balance
stake_account_metadata
.update_recorded_vesting_balance(new_recorded_vesting_balance);

// Update checkpoints
let current_delegate_checkpoints_account_info =
stake_account_checkpoints.to_account_info();
Expand Down
10 changes: 8 additions & 2 deletions solana/programs/staking/src/contexts/transfer_vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,14 @@ impl<'info> crate::contexts::TransferVesting<'info> {
VestingError::InvalidStakeAccountMetadataPDA
);

stake_account_metadata.recorded_vesting_balance = stake_account_metadata
let new_recorded_vesting_balance = stake_account_metadata
.recorded_vesting_balance
.checked_sub(self.vest.amount)
.ok_or(VestingError::Underflow)?;

stake_account_metadata
.update_recorded_vesting_balance(new_recorded_vesting_balance);

// Update checkpoints
let current_delegate_checkpoints_account_info =
stake_account_checkpoints.to_account_info();
Expand Down Expand Up @@ -226,11 +229,14 @@ impl<'info> crate::contexts::TransferVesting<'info> {
VestingError::InvalidStakeAccountMetadataPDA
);

new_stake_account_metadata.recorded_vesting_balance = new_stake_account_metadata
let new_recorded_vesting_balance = new_stake_account_metadata
.recorded_vesting_balance
.checked_add(self.vest.amount)
.ok_or(VestingError::Overflow)?;

new_stake_account_metadata
.update_recorded_vesting_balance(new_recorded_vesting_balance);

let current_delegate_checkpoints_account_info =
new_stake_account_checkpoints.to_account_info();

Expand Down
10 changes: 6 additions & 4 deletions solana/programs/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ pub mod staking {
);

vesting_balance.stake_account_metadata = stake_account_metadata.key();
stake_account_metadata.recorded_vesting_balance =
vesting_balance.total_vesting_balance;
stake_account_metadata
.update_recorded_vesting_balance(vesting_balance.total_vesting_balance);
}
}
}
Expand Down Expand Up @@ -292,7 +292,7 @@ pub mod staking {
}

if current_stake_balance != stake_account_metadata.recorded_balance {
stake_account_metadata.recorded_balance = current_stake_balance;
stake_account_metadata.update_recorded_balance(current_stake_balance);
}

let delegatee_stake_account_checkpoints =
Expand Down Expand Up @@ -466,7 +466,9 @@ pub mod staking {
drop(loaded_checkpoints);
}

ctx.accounts.stake_account_metadata.recorded_balance = *current_stake_balance;
ctx.accounts
.stake_account_metadata
.update_recorded_balance(*current_stake_balance);

Ok(())
}
Expand Down
34 changes: 34 additions & 0 deletions solana/programs/staking/src/state/stake_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ pub struct StakeAccountMetadata {
pub stake_account_checkpoints_last_index: u8,
}

#[event]
pub struct RecordedBalanceChanged {
pub owner: Pubkey,
pub previous_balance: u64,
pub new_balance: u64,
}

#[event]
pub struct RecordedVestingBalanceChanged {
pub owner: Pubkey,
pub previous_balance: u64,
pub new_balance: u64,
}

impl StakeAccountMetadata {
pub const LEN: usize =
StakeAccountMetadata::DISCRIMINATOR.len() + size_of::<StakeAccountMetadata>();
Expand All @@ -40,6 +54,26 @@ impl StakeAccountMetadata {
self.delegate = *delegate;
self.stake_account_checkpoints_last_index = stake_account_checkpoints_last;
}

pub fn update_recorded_balance(&mut self, new_recorded_balance: u64) {
emit!(RecordedBalanceChanged {
owner: self.owner,
previous_balance: self.recorded_balance,
new_balance: new_recorded_balance,
});

self.recorded_balance = new_recorded_balance;
}

pub fn update_recorded_vesting_balance(&mut self, new_recorded_vesting_balance: u64) {
emit!(RecordedVestingBalanceChanged {
owner: self.owner,
previous_balance: self.recorded_vesting_balance,
new_balance: new_recorded_vesting_balance,
});

self.recorded_vesting_balance = new_recorded_vesting_balance;
}
}

#[cfg(test)]
Expand Down
Loading