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::*;
|
2021-12-03 05:05:03 -08:00
|
|
|
use anchor_spl::token::Mint;
|
2021-12-02 07:28:12 -08:00
|
|
|
use spl_governance::state::realm;
|
|
|
|
use std::mem::size_of;
|
|
|
|
|
|
|
|
#[derive(Accounts)]
|
2021-12-09 02:58:15 -08:00
|
|
|
#[instruction(registrar_bump: u8)]
|
2021-12-02 07:28:12 -08:00
|
|
|
pub struct CreateRegistrar<'info> {
|
2021-12-02 10:30:41 -08:00
|
|
|
/// The voting registrar. There can only be a single registrar
|
2021-12-02 07:28:12 -08:00
|
|
|
/// per governance realm and governing mint.
|
|
|
|
#[account(
|
|
|
|
init,
|
|
|
|
seeds = [realm.key().as_ref(), b"registrar".as_ref(), realm_governing_token_mint.key().as_ref()],
|
|
|
|
bump = registrar_bump,
|
|
|
|
payer = payer,
|
|
|
|
space = 8 + size_of::<Registrar>()
|
|
|
|
)]
|
2021-12-08 23:22:22 -08:00
|
|
|
pub registrar: AccountLoader<'info, Registrar>,
|
2021-12-02 07:28:12 -08:00
|
|
|
|
2021-12-02 10:30:41 -08:00
|
|
|
/// An spl-governance realm
|
|
|
|
///
|
|
|
|
/// realm is validated in the instruction:
|
|
|
|
/// - realm is owned by the governance_program_id
|
|
|
|
/// - realm_governing_token_mint must be the community or council mint
|
|
|
|
/// - realm_authority is realm.authority
|
2021-12-02 07:28:12 -08:00
|
|
|
pub realm: UncheckedAccount<'info>,
|
|
|
|
|
2021-12-02 10:30:41 -08:00
|
|
|
/// The program id of the spl-governance program the realm belongs to.
|
2021-12-02 07:28:12 -08:00
|
|
|
pub governance_program_id: UncheckedAccount<'info>,
|
2021-12-02 10:30:41 -08:00
|
|
|
/// Either the realm community mint or the council mint.
|
2021-12-02 07:28:12 -08:00
|
|
|
pub realm_governing_token_mint: Account<'info, Mint>,
|
|
|
|
pub realm_authority: Signer<'info>,
|
|
|
|
|
|
|
|
#[account(mut)]
|
|
|
|
pub payer: Signer<'info>,
|
|
|
|
|
|
|
|
pub system_program: Program<'info, System>,
|
|
|
|
pub rent: Sysvar<'info, Rent>,
|
|
|
|
}
|
|
|
|
|
2021-12-02 10:30:41 -08:00
|
|
|
/// Creates a new voting registrar.
|
|
|
|
///
|
|
|
|
/// `vote_weight_decimals` is the number of decimals used on the vote weight. It must be
|
|
|
|
/// larger or equal to all token mints used for voting.
|
|
|
|
///
|
2021-12-03 11:36:42 -08:00
|
|
|
/// To use the registrar, call ConfigVotingMint to register token mints that may be
|
2021-12-02 10:30:41 -08:00
|
|
|
/// used for voting.
|
2021-12-09 02:58:15 -08:00
|
|
|
pub fn create_registrar(ctx: Context<CreateRegistrar>, registrar_bump: u8) -> Result<()> {
|
2021-12-08 23:22:22 -08:00
|
|
|
let registrar = &mut ctx.accounts.registrar.load_init()?;
|
2021-12-02 07:28:12 -08:00
|
|
|
registrar.bump = registrar_bump;
|
|
|
|
registrar.governance_program_id = ctx.accounts.governance_program_id.key();
|
|
|
|
registrar.realm = ctx.accounts.realm.key();
|
|
|
|
registrar.realm_governing_token_mint = ctx.accounts.realm_governing_token_mint.key();
|
|
|
|
registrar.realm_authority = ctx.accounts.realm_authority.key();
|
|
|
|
registrar.time_offset = 0;
|
|
|
|
|
|
|
|
// Verify that "realm_authority" is the expected authority on "realm"
|
|
|
|
// and that the mint matches one of the realm mints too.
|
|
|
|
let realm = realm::get_realm_data_for_governing_token_mint(
|
|
|
|
®istrar.governance_program_id,
|
|
|
|
&ctx.accounts.realm.to_account_info(),
|
|
|
|
®istrar.realm_governing_token_mint,
|
|
|
|
)?;
|
|
|
|
require!(
|
|
|
|
realm.authority.unwrap() == ctx.accounts.realm_authority.key(),
|
|
|
|
ErrorCode::InvalidRealmAuthority
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|