2021-12-02 07:28:12 -08:00
|
|
|
use crate::error::*;
|
2021-12-03 00:52:48 -08:00
|
|
|
use crate::state::*;
|
2021-12-02 07:28:12 -08:00
|
|
|
use anchor_lang::prelude::*;
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
pub struct UpdateVoterWeightRecord<'info> {
|
2021-12-08 23:22:22 -08:00
|
|
|
pub registrar: AccountLoader<'info, Registrar>,
|
2021-12-02 07:28:12 -08:00
|
|
|
|
2021-12-04 00:02:20 -08:00
|
|
|
// checking the PDA address it just an extra precaution,
|
|
|
|
// the other constraints must be exhaustive
|
|
|
|
#[account(
|
2021-12-04 00:22:22 -08:00
|
|
|
seeds = [registrar.key().as_ref(), b"voter".as_ref(), voter.load()?.voter_authority.key().as_ref()],
|
2021-12-04 00:02:20 -08:00
|
|
|
bump = voter.load()?.voter_bump,
|
|
|
|
has_one = registrar)]
|
2021-12-02 07:28:12 -08:00
|
|
|
pub voter: AccountLoader<'info, Voter>,
|
|
|
|
|
|
|
|
#[account(
|
|
|
|
mut,
|
2021-12-05 02:58:56 -08:00
|
|
|
seeds = [registrar.key().as_ref(), b"voter-weight-record".as_ref(), voter.load()?.voter_authority.key().as_ref()],
|
2021-12-02 07:28:12 -08:00
|
|
|
bump = voter.load()?.voter_weight_record_bump,
|
2021-12-08 23:22:22 -08:00
|
|
|
constraint = voter_weight_record.realm == registrar.load()?.realm,
|
2021-12-02 07:28:12 -08:00
|
|
|
constraint = voter_weight_record.governing_token_owner == voter.load()?.voter_authority,
|
2021-12-08 23:22:22 -08:00
|
|
|
constraint = voter_weight_record.governing_token_mint == registrar.load()?.realm_governing_token_mint,
|
2021-12-02 07:28:12 -08:00
|
|
|
)]
|
|
|
|
pub voter_weight_record: Account<'info, VoterWeightRecord>,
|
|
|
|
|
|
|
|
pub system_program: Program<'info, System>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculates the lockup-scaled, time-decayed voting power for the given
|
|
|
|
/// voter and writes it into a `VoteWeightRecord` account to be used by
|
|
|
|
/// the SPL governance program.
|
|
|
|
///
|
2021-12-03 00:19:13 -08:00
|
|
|
/// This "revise" instruction must be called immediately before voting, in
|
|
|
|
/// the same transaction.
|
2021-12-02 07:28:12 -08:00
|
|
|
pub fn update_voter_weight_record(ctx: Context<UpdateVoterWeightRecord>) -> Result<()> {
|
2021-12-08 23:22:22 -08:00
|
|
|
let registrar = &ctx.accounts.registrar.load()?;
|
2021-12-02 07:28:12 -08:00
|
|
|
let voter = ctx.accounts.voter.load()?;
|
|
|
|
let record = &mut ctx.accounts.voter_weight_record;
|
|
|
|
record.voter_weight = voter.weight(®istrar)?;
|
|
|
|
record.voter_weight_expiry = Some(Clock::get()?.slot);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|