Skip to content

Commit f0f56a9

Browse files
authored
Add RecordedBalanceChanged and RecordedVestingBalanceChanged events (#219)
1 parent abe4af1 commit f0f56a9

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

solana/programs/staking/src/contexts/claim_vesting.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,15 @@ impl<'info> ClaimVesting<'info> {
101101
VestingError::InvalidStakeAccountOwner
102102
);
103103

104-
// Update the recorded vesting balance
105-
stake_account_metadata.recorded_vesting_balance = stake_account_metadata
104+
let new_recorded_vesting_balance = stake_account_metadata
106105
.recorded_vesting_balance
107106
.checked_sub(self.vest.amount)
108107
.ok_or(VestingError::Underflow)?;
109108

109+
// Update the recorded vesting balance
110+
stake_account_metadata
111+
.update_recorded_vesting_balance(new_recorded_vesting_balance);
112+
110113
// Update checkpoints
111114
let current_delegate_checkpoints_account_info =
112115
stake_account_checkpoints.to_account_info();

solana/programs/staking/src/contexts/transfer_vesting.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,14 @@ impl<'info> crate::contexts::TransferVesting<'info> {
144144
VestingError::InvalidStakeAccountMetadataPDA
145145
);
146146

147-
stake_account_metadata.recorded_vesting_balance = stake_account_metadata
147+
let new_recorded_vesting_balance = stake_account_metadata
148148
.recorded_vesting_balance
149149
.checked_sub(self.vest.amount)
150150
.ok_or(VestingError::Underflow)?;
151151

152+
stake_account_metadata
153+
.update_recorded_vesting_balance(new_recorded_vesting_balance);
154+
152155
// Update checkpoints
153156
let current_delegate_checkpoints_account_info =
154157
stake_account_checkpoints.to_account_info();
@@ -226,11 +229,14 @@ impl<'info> crate::contexts::TransferVesting<'info> {
226229
VestingError::InvalidStakeAccountMetadataPDA
227230
);
228231

229-
new_stake_account_metadata.recorded_vesting_balance = new_stake_account_metadata
232+
let new_recorded_vesting_balance = new_stake_account_metadata
230233
.recorded_vesting_balance
231234
.checked_add(self.vest.amount)
232235
.ok_or(VestingError::Overflow)?;
233236

237+
new_stake_account_metadata
238+
.update_recorded_vesting_balance(new_recorded_vesting_balance);
239+
234240
let current_delegate_checkpoints_account_info =
235241
new_stake_account_checkpoints.to_account_info();
236242

solana/programs/staking/src/lib.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ pub mod staking {
202202
);
203203

204204
vesting_balance.stake_account_metadata = stake_account_metadata.key();
205-
stake_account_metadata.recorded_vesting_balance =
206-
vesting_balance.total_vesting_balance;
205+
stake_account_metadata
206+
.update_recorded_vesting_balance(vesting_balance.total_vesting_balance);
207207
}
208208
}
209209
}
@@ -292,7 +292,7 @@ pub mod staking {
292292
}
293293

294294
if current_stake_balance != stake_account_metadata.recorded_balance {
295-
stake_account_metadata.recorded_balance = current_stake_balance;
295+
stake_account_metadata.update_recorded_balance(current_stake_balance);
296296
}
297297

298298
let delegatee_stake_account_checkpoints =
@@ -466,7 +466,9 @@ pub mod staking {
466466
drop(loaded_checkpoints);
467467
}
468468

469-
ctx.accounts.stake_account_metadata.recorded_balance = *current_stake_balance;
469+
ctx.accounts
470+
.stake_account_metadata
471+
.update_recorded_balance(*current_stake_balance);
470472

471473
Ok(())
472474
}

solana/programs/staking/src/state/stake_account.rs

+34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ pub struct StakeAccountMetadata {
2020
pub stake_account_checkpoints_last_index: u8,
2121
}
2222

23+
#[event]
24+
pub struct RecordedBalanceChanged {
25+
pub owner: Pubkey,
26+
pub previous_balance: u64,
27+
pub new_balance: u64,
28+
}
29+
30+
#[event]
31+
pub struct RecordedVestingBalanceChanged {
32+
pub owner: Pubkey,
33+
pub previous_balance: u64,
34+
pub new_balance: u64,
35+
}
36+
2337
impl StakeAccountMetadata {
2438
pub const LEN: usize =
2539
StakeAccountMetadata::DISCRIMINATOR.len() + size_of::<StakeAccountMetadata>();
@@ -40,6 +54,26 @@ impl StakeAccountMetadata {
4054
self.delegate = *delegate;
4155
self.stake_account_checkpoints_last_index = stake_account_checkpoints_last;
4256
}
57+
58+
pub fn update_recorded_balance(&mut self, new_recorded_balance: u64) {
59+
emit!(RecordedBalanceChanged {
60+
owner: self.owner,
61+
previous_balance: self.recorded_balance,
62+
new_balance: new_recorded_balance,
63+
});
64+
65+
self.recorded_balance = new_recorded_balance;
66+
}
67+
68+
pub fn update_recorded_vesting_balance(&mut self, new_recorded_vesting_balance: u64) {
69+
emit!(RecordedVestingBalanceChanged {
70+
owner: self.owner,
71+
previous_balance: self.recorded_vesting_balance,
72+
new_balance: new_recorded_vesting_balance,
73+
});
74+
75+
self.recorded_vesting_balance = new_recorded_vesting_balance;
76+
}
4377
}
4478

4579
#[cfg(test)]

0 commit comments

Comments
 (0)