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::*;
|
|
|
|
use anchor_lang::solana_program::sysvar::instructions as tx_instructions;
|
|
|
|
use spl_governance::addins::voter_weight::VoterWeightAccountType;
|
|
|
|
use std::mem::size_of;
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
|
|
|
#[instruction(voter_bump: u8, voter_weight_record_bump: u8)]
|
|
|
|
pub struct CreateVoter<'info> {
|
2021-12-08 23:22:22 -08:00
|
|
|
pub registrar: AccountLoader<'info, Registrar>,
|
2021-12-02 07:28:12 -08:00
|
|
|
|
|
|
|
#[account(
|
2021-12-16 04:41:42 -08:00
|
|
|
init,
|
2021-12-02 07:28:12 -08:00
|
|
|
seeds = [registrar.key().as_ref(), b"voter".as_ref(), voter_authority.key().as_ref()],
|
|
|
|
bump = voter_bump,
|
|
|
|
payer = payer,
|
|
|
|
space = 8 + size_of::<Voter>(),
|
|
|
|
)]
|
|
|
|
pub voter: AccountLoader<'info, Voter>,
|
|
|
|
|
2021-12-05 05:34:03 -08:00
|
|
|
/// The authority controling the voter. Must be the same as the
|
|
|
|
/// `governing_token_owner` in the token owner record used with
|
|
|
|
/// spl-governance.
|
2021-12-16 04:41:42 -08:00
|
|
|
pub voter_authority: Signer<'info>,
|
2021-12-03 00:48:10 -08:00
|
|
|
|
2021-12-03 00:19:13 -08:00
|
|
|
/// The voter weight record is the account that will be shown to spl-governance
|
|
|
|
/// to prove how much vote weight the voter has. See update_voter_weight_record.
|
2021-12-02 07:28:12 -08:00
|
|
|
#[account(
|
2021-12-16 04:41:42 -08:00
|
|
|
init,
|
2021-12-05 02:58:56 -08:00
|
|
|
seeds = [registrar.key().as_ref(), b"voter-weight-record".as_ref(), voter_authority.key().as_ref()],
|
2021-12-02 07:28:12 -08:00
|
|
|
bump = voter_weight_record_bump,
|
|
|
|
payer = payer,
|
2021-12-16 04:35:22 -08:00
|
|
|
space = size_of::<VoterWeightRecord>(),
|
2021-12-02 07:28:12 -08:00
|
|
|
)]
|
|
|
|
pub voter_weight_record: Account<'info, VoterWeightRecord>,
|
|
|
|
|
|
|
|
#[account(mut)]
|
|
|
|
pub payer: Signer<'info>,
|
|
|
|
|
|
|
|
pub system_program: Program<'info, System>,
|
|
|
|
pub rent: Sysvar<'info, Rent>,
|
|
|
|
|
|
|
|
#[account(address = tx_instructions::ID)]
|
|
|
|
pub instructions: UncheckedAccount<'info>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a new voter account. There can only be a single voter per
|
2021-12-02 12:30:15 -08:00
|
|
|
/// voter_authority.
|
|
|
|
///
|
|
|
|
/// The user must register with spl-governance using the same voter_authority.
|
|
|
|
/// Their token owner record will be required for withdrawing funds later.
|
2021-12-02 07:28:12 -08:00
|
|
|
pub fn create_voter(
|
|
|
|
ctx: Context<CreateVoter>,
|
|
|
|
voter_bump: u8,
|
|
|
|
voter_weight_record_bump: u8,
|
|
|
|
) -> Result<()> {
|
|
|
|
// Forbid creating voter accounts from CPI. The goal is to make automation
|
|
|
|
// impossible that weakens some of the limitations intentionally imposed on
|
|
|
|
// locked tokens.
|
|
|
|
{
|
|
|
|
let ixns = ctx.accounts.instructions.to_account_info();
|
|
|
|
let current_index = tx_instructions::load_current_index_checked(&ixns)? as usize;
|
|
|
|
let current_ixn = tx_instructions::load_instruction_at_checked(current_index, &ixns)?;
|
|
|
|
require!(
|
|
|
|
current_ixn.program_id == *ctx.program_id,
|
|
|
|
ErrorCode::ForbiddenCpi
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load accounts.
|
2021-12-08 23:22:22 -08:00
|
|
|
let registrar = &ctx.accounts.registrar.load()?;
|
2021-12-03 00:48:10 -08:00
|
|
|
let voter_authority = ctx.accounts.voter_authority.key();
|
2021-12-02 07:28:12 -08:00
|
|
|
|
2021-12-16 04:41:42 -08:00
|
|
|
let voter = &mut ctx.accounts.voter.load_init()?;
|
|
|
|
voter.voter_bump = voter_bump;
|
|
|
|
voter.voter_weight_record_bump = voter_weight_record_bump;
|
|
|
|
voter.voter_authority = voter_authority;
|
|
|
|
voter.registrar = ctx.accounts.registrar.key();
|
2021-12-02 07:28:12 -08:00
|
|
|
|
2021-12-16 04:41:42 -08:00
|
|
|
let voter_weight_record = &mut ctx.accounts.voter_weight_record;
|
|
|
|
voter_weight_record.account_type = VoterWeightAccountType::VoterWeightRecord;
|
|
|
|
voter_weight_record.realm = registrar.realm;
|
|
|
|
voter_weight_record.governing_token_mint = registrar.realm_governing_token_mint;
|
|
|
|
voter_weight_record.governing_token_owner = voter_authority;
|
2021-12-02 07:28:12 -08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|