Simplification and doc comments

This commit is contained in:
Christian Kamm 2021-12-02 19:30:41 +01:00
parent f5ea3180d1
commit 6d15fb8d77
7 changed files with 56 additions and 49 deletions

View File

@ -5,20 +5,22 @@ use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
#[derive(Accounts)]
#[instruction(idx: u16, mint: Pubkey, rate: u64, decimals: u8)]
#[instruction(idx: u16, rate: u64, decimals: u8)]
pub struct CreateExchangeRate<'info> {
#[account(mut, has_one = realm_authority)]
pub registrar: Box<Account<'info, Registrar>>,
pub realm_authority: Signer<'info>,
/// Token account that all funds for this mint will be stored in
#[account(
init,
payer = payer,
associated_token::authority = registrar,
associated_token::mint = deposit_mint,
associated_token::mint = mint,
)]
pub exchange_vault: Account<'info, TokenAccount>,
pub deposit_mint: Account<'info, Mint>,
pub vault: Account<'info, TokenAccount>,
/// Tokens of this mint will produce vote weight
pub mint: Account<'info, Mint>,
#[account(mut)]
pub payer: Signer<'info>,
@ -30,15 +32,20 @@ pub struct CreateExchangeRate<'info> {
}
/// Creates a new exchange rate for a given mint. This allows a voter to
/// deposit the mint in exchange for vTokens. There can only be a single
/// deposit the mint in exchange for vote weight. There can only be a single
/// exchange rate per mint.
///
/// WARNING: This can be freely called when any of the rates are empty.
/// This should be called immediately upon creation of a Registrar.
/// `idx`: index of the rate to be set
/// `rate`: multiplier to apply for converting tokens to vote weight
/// `decimals`: number of decimals of mint that make one unit of token
///
/// The vote weight for one native token will be:
/// ```
/// rate * 10^vote_weight_decimals / 10^decimals
/// ```
pub fn create_exchange_rate(
ctx: Context<CreateExchangeRate>,
idx: u16,
mint: Pubkey,
rate: u64,
decimals: u8,
) -> Result<()> {
@ -47,6 +54,6 @@ pub fn create_exchange_rate(
let registrar = &mut ctx.accounts.registrar;
require!((idx as usize) < registrar.rates.len(), InvalidIndex);
require!(registrar.rates[idx as usize].rate == 0, RateNotZero);
registrar.rates[idx as usize] = registrar.new_rate(mint, decimals, rate)?;
registrar.rates[idx as usize] = registrar.new_rate(ctx.accounts.mint.key(), decimals, rate)?;
Ok(())
}

View File

@ -8,7 +8,7 @@ use std::mem::size_of;
#[derive(Accounts)]
#[instruction(vote_weight_decimals: u8, registrar_bump: u8)]
pub struct CreateRegistrar<'info> {
/// a voting registrar. There can only be a single registrar
/// The voting registrar. There can only be a single registrar
/// per governance realm and governing mint.
#[account(
init,
@ -19,16 +19,22 @@ pub struct CreateRegistrar<'info> {
)]
pub registrar: Box<Account<'info, Registrar>>,
// 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
/// 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
pub realm: UncheckedAccount<'info>,
/// The program id of the spl-governance program the realm belongs to.
pub governance_program_id: UncheckedAccount<'info>,
/// Either the realm community mint or the council mint.
pub realm_governing_token_mint: Account<'info, Mint>,
pub realm_authority: Signer<'info>,
/// The authority that may use the clawback() instruction
// TODO: Just use the realm_authority?
pub clawback_authority: UncheckedAccount<'info>,
#[account(mut)]
@ -39,8 +45,13 @@ pub struct CreateRegistrar<'info> {
pub rent: Sysvar<'info, Rent>,
}
/// Creates a new voting registrar. There can only be a single registrar
/// per governance realm.
/// 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.
///
/// To use the registrar, call CreateExchangeRate to register token mints that may be
/// used for voting.
pub fn create_registrar(
ctx: Context<CreateRegistrar>,
vote_weight_decimals: u8,

View File

@ -36,8 +36,6 @@ pub struct CreateVoter<'info> {
#[account(mut)]
pub payer: Signer<'info>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,

View File

@ -17,19 +17,18 @@ pub struct Deposit<'info> {
associated_token::authority = registrar,
associated_token::mint = deposit_mint,
)]
pub exchange_vault: Box<Account<'info, TokenAccount>>,
pub vault: Box<Account<'info, TokenAccount>>,
pub deposit_mint: Box<Account<'info, Mint>>,
#[account(mut)]
pub deposit_authority: Signer<'info>,
#[account(
mut,
constraint = deposit_token.mint == deposit_mint.key(),
constraint = deposit_token.owner == deposit_authority.key(),
)]
pub deposit_token: Box<Account<'info, TokenAccount>>,
pub deposit_authority: Signer<'info>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
pub rent: Sysvar<'info, Rent>,
}
@ -39,7 +38,7 @@ impl<'info> Deposit<'info> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.deposit_token.to_account_info(),
to: self.exchange_vault.to_account_info(),
to: self.vault.to_account_info(),
authority: self.deposit_authority.to_account_info(),
};
CpiContext::new(program, accounts)

View File

@ -28,7 +28,7 @@ pub struct WithdrawOrClawback<'info> {
associated_token::authority = registrar,
associated_token::mint = withdraw_mint,
)]
pub exchange_vault: Box<Account<'info, TokenAccount>>,
pub vault: Box<Account<'info, TokenAccount>>,
pub withdraw_mint: Box<Account<'info, Mint>>,
#[account(mut)]
@ -41,7 +41,7 @@ impl<'info> WithdrawOrClawback<'info> {
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
let program = self.token_program.to_account_info();
let accounts = token::Transfer {
from: self.exchange_vault.to_account_info(),
from: self.vault.to_account_info(),
to: self.destination.to_account_info(),
authority: self.registrar.to_account_info(),
};

View File

@ -70,11 +70,10 @@ pub mod voter_stake_registry {
pub fn create_exchange_rate(
ctx: Context<CreateExchangeRate>,
idx: u16,
mint: Pubkey,
rate: u64,
decimals: u8,
) -> Result<()> {
instructions::create_exchange_rate(ctx, idx, mint, rate, decimals)
instructions::create_exchange_rate(ctx, idx, rate, decimals)
}
pub fn create_voter(

View File

@ -23,8 +23,8 @@ pub struct RegistrarCookie {
#[derive(Clone, Copy)]
pub struct ExchangeRateCookie {
pub deposit_mint: MintCookie,
pub exchange_vault: Pubkey,
pub mint: MintCookie,
pub vault: Pubkey,
}
pub struct VoterCookie {
@ -107,7 +107,7 @@ impl AddinCookie {
rate: u64,
) -> ExchangeRateCookie {
let deposit_mint = mint.pubkey.unwrap();
let exchange_vault = spl_associated_token_account::get_associated_token_address(
let vault = spl_associated_token_account::get_associated_token_address(
&registrar.address,
&deposit_mint,
);
@ -115,7 +115,6 @@ impl AddinCookie {
let data = anchor_lang::InstructionData::data(
&voter_stake_registry::instruction::CreateExchangeRate {
idx: index,
mint: deposit_mint,
rate,
decimals: mint.decimals,
},
@ -123,8 +122,8 @@ impl AddinCookie {
let accounts = anchor_lang::ToAccountMetas::to_account_metas(
&voter_stake_registry::accounts::CreateExchangeRate {
exchange_vault,
deposit_mint,
vault,
mint: deposit_mint,
registrar: registrar.address,
realm_authority: authority.pubkey(),
payer: payer.pubkey(),
@ -152,8 +151,8 @@ impl AddinCookie {
.unwrap();
ExchangeRateCookie {
deposit_mint: mint.clone(),
exchange_vault,
mint: mint.clone(),
vault,
}
}
@ -193,8 +192,6 @@ impl AddinCookie {
registrar: registrar.address,
voter_authority: authority.pubkey(),
payer: payer.pubkey(),
token_program: spl_token::id(),
associated_token_program: spl_associated_token_account::id(),
system_program: solana_sdk::system_program::id(),
rent: solana_program::sysvar::rent::id(),
instructions: solana_program::sysvar::instructions::id(),
@ -247,7 +244,7 @@ impl AddinCookie {
registrar: registrar.address,
voter: voter.address,
voter_authority: voter_authority.pubkey(),
deposit_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
deposit_mint: exchange_rate.mint.pubkey.unwrap(),
},
None,
);
@ -286,12 +283,11 @@ impl AddinCookie {
&voter_stake_registry::accounts::Deposit {
registrar: registrar.address,
voter: voter.address,
exchange_vault: exchange_rate.exchange_vault,
vault: exchange_rate.vault,
deposit_token: token_address,
deposit_authority: authority.pubkey(),
deposit_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
deposit_mint: exchange_rate.mint.pubkey.unwrap(),
token_program: spl_token::id(),
associated_token_program: spl_associated_token_account::id(),
system_program: solana_sdk::system_program::id(),
rent: solana_program::sysvar::rent::id(),
},
@ -333,8 +329,8 @@ impl AddinCookie {
registrar: registrar.address,
voter: voter.address,
token_owner_record: token_owner_record.address,
exchange_vault: exchange_rate.exchange_vault,
withdraw_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
vault: exchange_rate.vault,
withdraw_mint: exchange_rate.mint.pubkey.unwrap(),
destination: token_address,
authority: clawback_authority.pubkey(),
token_program: spl_token::id(),
@ -378,8 +374,8 @@ impl AddinCookie {
registrar: registrar.address,
voter: voter.address,
token_owner_record: token_owner_record.address,
exchange_vault: exchange_rate.exchange_vault,
withdraw_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
vault: exchange_rate.vault,
withdraw_mint: exchange_rate.mint.pubkey.unwrap(),
destination: token_address,
authority: authority.pubkey(),
token_program: spl_token::id(),
@ -508,10 +504,7 @@ impl AddinCookie {
impl ExchangeRateCookie {
pub async fn vault_balance(&self, solana: &SolanaCookie) -> u64 {
solana
.get_account::<TokenAccount>(self.exchange_vault)
.await
.amount
solana.get_account::<TokenAccount>(self.vault).await.amount
}
}