Simplification and doc comments
This commit is contained in:
parent
f5ea3180d1
commit
6d15fb8d77
|
@ -5,20 +5,22 @@ use anchor_spl::associated_token::AssociatedToken;
|
||||||
use anchor_spl::token::{Mint, Token, TokenAccount};
|
use anchor_spl::token::{Mint, Token, TokenAccount};
|
||||||
|
|
||||||
#[derive(Accounts)]
|
#[derive(Accounts)]
|
||||||
#[instruction(idx: u16, mint: Pubkey, rate: u64, decimals: u8)]
|
#[instruction(idx: u16, rate: u64, decimals: u8)]
|
||||||
pub struct CreateExchangeRate<'info> {
|
pub struct CreateExchangeRate<'info> {
|
||||||
#[account(mut, has_one = realm_authority)]
|
#[account(mut, has_one = realm_authority)]
|
||||||
pub registrar: Box<Account<'info, Registrar>>,
|
pub registrar: Box<Account<'info, Registrar>>,
|
||||||
pub realm_authority: Signer<'info>,
|
pub realm_authority: Signer<'info>,
|
||||||
|
|
||||||
|
/// Token account that all funds for this mint will be stored in
|
||||||
#[account(
|
#[account(
|
||||||
init,
|
init,
|
||||||
payer = payer,
|
payer = payer,
|
||||||
associated_token::authority = registrar,
|
associated_token::authority = registrar,
|
||||||
associated_token::mint = deposit_mint,
|
associated_token::mint = mint,
|
||||||
)]
|
)]
|
||||||
pub exchange_vault: Account<'info, TokenAccount>,
|
pub vault: Account<'info, TokenAccount>,
|
||||||
pub deposit_mint: Account<'info, Mint>,
|
/// Tokens of this mint will produce vote weight
|
||||||
|
pub mint: Account<'info, Mint>,
|
||||||
|
|
||||||
#[account(mut)]
|
#[account(mut)]
|
||||||
pub payer: Signer<'info>,
|
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
|
/// 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.
|
/// exchange rate per mint.
|
||||||
///
|
///
|
||||||
/// WARNING: This can be freely called when any of the rates are empty.
|
/// `idx`: index of the rate to be set
|
||||||
/// This should be called immediately upon creation of a Registrar.
|
/// `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(
|
pub fn create_exchange_rate(
|
||||||
ctx: Context<CreateExchangeRate>,
|
ctx: Context<CreateExchangeRate>,
|
||||||
idx: u16,
|
idx: u16,
|
||||||
mint: Pubkey,
|
|
||||||
rate: u64,
|
rate: u64,
|
||||||
decimals: u8,
|
decimals: u8,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
@ -47,6 +54,6 @@ pub fn create_exchange_rate(
|
||||||
let registrar = &mut ctx.accounts.registrar;
|
let registrar = &mut ctx.accounts.registrar;
|
||||||
require!((idx as usize) < registrar.rates.len(), InvalidIndex);
|
require!((idx as usize) < registrar.rates.len(), InvalidIndex);
|
||||||
require!(registrar.rates[idx as usize].rate == 0, RateNotZero);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@ use std::mem::size_of;
|
||||||
#[derive(Accounts)]
|
#[derive(Accounts)]
|
||||||
#[instruction(vote_weight_decimals: u8, registrar_bump: u8)]
|
#[instruction(vote_weight_decimals: u8, registrar_bump: u8)]
|
||||||
pub struct CreateRegistrar<'info> {
|
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.
|
/// per governance realm and governing mint.
|
||||||
#[account(
|
#[account(
|
||||||
init,
|
init,
|
||||||
|
@ -19,16 +19,22 @@ pub struct CreateRegistrar<'info> {
|
||||||
)]
|
)]
|
||||||
pub registrar: Box<Account<'info, Registrar>>,
|
pub registrar: Box<Account<'info, Registrar>>,
|
||||||
|
|
||||||
// realm is validated in the instruction:
|
/// An spl-governance realm
|
||||||
// - realm is owned by the governance_program_id
|
///
|
||||||
// - realm_governing_token_mint must be the community or council mint
|
/// realm is validated in the instruction:
|
||||||
// - realm_authority is realm.authority
|
/// - 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>,
|
pub realm: UncheckedAccount<'info>,
|
||||||
|
|
||||||
|
/// The program id of the spl-governance program the realm belongs to.
|
||||||
pub governance_program_id: UncheckedAccount<'info>,
|
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_governing_token_mint: Account<'info, Mint>,
|
||||||
pub realm_authority: Signer<'info>,
|
pub realm_authority: Signer<'info>,
|
||||||
|
|
||||||
|
/// The authority that may use the clawback() instruction
|
||||||
|
// TODO: Just use the realm_authority?
|
||||||
pub clawback_authority: UncheckedAccount<'info>,
|
pub clawback_authority: UncheckedAccount<'info>,
|
||||||
|
|
||||||
#[account(mut)]
|
#[account(mut)]
|
||||||
|
@ -39,8 +45,13 @@ pub struct CreateRegistrar<'info> {
|
||||||
pub rent: Sysvar<'info, Rent>,
|
pub rent: Sysvar<'info, Rent>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new voting registrar. There can only be a single registrar
|
/// Creates a new voting registrar.
|
||||||
/// per governance realm.
|
///
|
||||||
|
/// `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(
|
pub fn create_registrar(
|
||||||
ctx: Context<CreateRegistrar>,
|
ctx: Context<CreateRegistrar>,
|
||||||
vote_weight_decimals: u8,
|
vote_weight_decimals: u8,
|
||||||
|
|
|
@ -36,8 +36,6 @@ pub struct CreateVoter<'info> {
|
||||||
#[account(mut)]
|
#[account(mut)]
|
||||||
pub payer: Signer<'info>,
|
pub payer: Signer<'info>,
|
||||||
|
|
||||||
pub token_program: Program<'info, Token>,
|
|
||||||
pub associated_token_program: Program<'info, AssociatedToken>,
|
|
||||||
pub system_program: Program<'info, System>,
|
pub system_program: Program<'info, System>,
|
||||||
pub rent: Sysvar<'info, Rent>,
|
pub rent: Sysvar<'info, Rent>,
|
||||||
|
|
||||||
|
|
|
@ -17,19 +17,18 @@ pub struct Deposit<'info> {
|
||||||
associated_token::authority = registrar,
|
associated_token::authority = registrar,
|
||||||
associated_token::mint = deposit_mint,
|
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>>,
|
pub deposit_mint: Box<Account<'info, Mint>>,
|
||||||
#[account(mut)]
|
|
||||||
pub deposit_authority: Signer<'info>,
|
|
||||||
#[account(
|
#[account(
|
||||||
mut,
|
mut,
|
||||||
constraint = deposit_token.mint == deposit_mint.key(),
|
constraint = deposit_token.mint == deposit_mint.key(),
|
||||||
constraint = deposit_token.owner == deposit_authority.key(),
|
constraint = deposit_token.owner == deposit_authority.key(),
|
||||||
)]
|
)]
|
||||||
pub deposit_token: Box<Account<'info, TokenAccount>>,
|
pub deposit_token: Box<Account<'info, TokenAccount>>,
|
||||||
|
pub deposit_authority: Signer<'info>,
|
||||||
|
|
||||||
pub token_program: Program<'info, Token>,
|
pub token_program: Program<'info, Token>,
|
||||||
pub associated_token_program: Program<'info, AssociatedToken>,
|
|
||||||
pub system_program: Program<'info, System>,
|
pub system_program: Program<'info, System>,
|
||||||
pub rent: Sysvar<'info, Rent>,
|
pub rent: Sysvar<'info, Rent>,
|
||||||
}
|
}
|
||||||
|
@ -39,7 +38,7 @@ impl<'info> Deposit<'info> {
|
||||||
let program = self.token_program.to_account_info();
|
let program = self.token_program.to_account_info();
|
||||||
let accounts = token::Transfer {
|
let accounts = token::Transfer {
|
||||||
from: self.deposit_token.to_account_info(),
|
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(),
|
authority: self.deposit_authority.to_account_info(),
|
||||||
};
|
};
|
||||||
CpiContext::new(program, accounts)
|
CpiContext::new(program, accounts)
|
||||||
|
|
|
@ -28,7 +28,7 @@ pub struct WithdrawOrClawback<'info> {
|
||||||
associated_token::authority = registrar,
|
associated_token::authority = registrar,
|
||||||
associated_token::mint = withdraw_mint,
|
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>>,
|
pub withdraw_mint: Box<Account<'info, Mint>>,
|
||||||
|
|
||||||
#[account(mut)]
|
#[account(mut)]
|
||||||
|
@ -41,7 +41,7 @@ impl<'info> WithdrawOrClawback<'info> {
|
||||||
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
|
pub fn transfer_ctx(&self) -> CpiContext<'_, '_, '_, 'info, token::Transfer<'info>> {
|
||||||
let program = self.token_program.to_account_info();
|
let program = self.token_program.to_account_info();
|
||||||
let accounts = token::Transfer {
|
let accounts = token::Transfer {
|
||||||
from: self.exchange_vault.to_account_info(),
|
from: self.vault.to_account_info(),
|
||||||
to: self.destination.to_account_info(),
|
to: self.destination.to_account_info(),
|
||||||
authority: self.registrar.to_account_info(),
|
authority: self.registrar.to_account_info(),
|
||||||
};
|
};
|
||||||
|
|
|
@ -70,11 +70,10 @@ pub mod voter_stake_registry {
|
||||||
pub fn create_exchange_rate(
|
pub fn create_exchange_rate(
|
||||||
ctx: Context<CreateExchangeRate>,
|
ctx: Context<CreateExchangeRate>,
|
||||||
idx: u16,
|
idx: u16,
|
||||||
mint: Pubkey,
|
|
||||||
rate: u64,
|
rate: u64,
|
||||||
decimals: u8,
|
decimals: u8,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
instructions::create_exchange_rate(ctx, idx, mint, rate, decimals)
|
instructions::create_exchange_rate(ctx, idx, rate, decimals)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_voter(
|
pub fn create_voter(
|
||||||
|
|
|
@ -23,8 +23,8 @@ pub struct RegistrarCookie {
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
pub struct ExchangeRateCookie {
|
pub struct ExchangeRateCookie {
|
||||||
pub deposit_mint: MintCookie,
|
pub mint: MintCookie,
|
||||||
pub exchange_vault: Pubkey,
|
pub vault: Pubkey,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct VoterCookie {
|
pub struct VoterCookie {
|
||||||
|
@ -107,7 +107,7 @@ impl AddinCookie {
|
||||||
rate: u64,
|
rate: u64,
|
||||||
) -> ExchangeRateCookie {
|
) -> ExchangeRateCookie {
|
||||||
let deposit_mint = mint.pubkey.unwrap();
|
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(
|
||||||
®istrar.address,
|
®istrar.address,
|
||||||
&deposit_mint,
|
&deposit_mint,
|
||||||
);
|
);
|
||||||
|
@ -115,7 +115,6 @@ impl AddinCookie {
|
||||||
let data = anchor_lang::InstructionData::data(
|
let data = anchor_lang::InstructionData::data(
|
||||||
&voter_stake_registry::instruction::CreateExchangeRate {
|
&voter_stake_registry::instruction::CreateExchangeRate {
|
||||||
idx: index,
|
idx: index,
|
||||||
mint: deposit_mint,
|
|
||||||
rate,
|
rate,
|
||||||
decimals: mint.decimals,
|
decimals: mint.decimals,
|
||||||
},
|
},
|
||||||
|
@ -123,8 +122,8 @@ impl AddinCookie {
|
||||||
|
|
||||||
let accounts = anchor_lang::ToAccountMetas::to_account_metas(
|
let accounts = anchor_lang::ToAccountMetas::to_account_metas(
|
||||||
&voter_stake_registry::accounts::CreateExchangeRate {
|
&voter_stake_registry::accounts::CreateExchangeRate {
|
||||||
exchange_vault,
|
vault,
|
||||||
deposit_mint,
|
mint: deposit_mint,
|
||||||
registrar: registrar.address,
|
registrar: registrar.address,
|
||||||
realm_authority: authority.pubkey(),
|
realm_authority: authority.pubkey(),
|
||||||
payer: payer.pubkey(),
|
payer: payer.pubkey(),
|
||||||
|
@ -152,8 +151,8 @@ impl AddinCookie {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
ExchangeRateCookie {
|
ExchangeRateCookie {
|
||||||
deposit_mint: mint.clone(),
|
mint: mint.clone(),
|
||||||
exchange_vault,
|
vault,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -193,8 +192,6 @@ impl AddinCookie {
|
||||||
registrar: registrar.address,
|
registrar: registrar.address,
|
||||||
voter_authority: authority.pubkey(),
|
voter_authority: authority.pubkey(),
|
||||||
payer: payer.pubkey(),
|
payer: payer.pubkey(),
|
||||||
token_program: spl_token::id(),
|
|
||||||
associated_token_program: spl_associated_token_account::id(),
|
|
||||||
system_program: solana_sdk::system_program::id(),
|
system_program: solana_sdk::system_program::id(),
|
||||||
rent: solana_program::sysvar::rent::id(),
|
rent: solana_program::sysvar::rent::id(),
|
||||||
instructions: solana_program::sysvar::instructions::id(),
|
instructions: solana_program::sysvar::instructions::id(),
|
||||||
|
@ -247,7 +244,7 @@ impl AddinCookie {
|
||||||
registrar: registrar.address,
|
registrar: registrar.address,
|
||||||
voter: voter.address,
|
voter: voter.address,
|
||||||
voter_authority: voter_authority.pubkey(),
|
voter_authority: voter_authority.pubkey(),
|
||||||
deposit_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
|
deposit_mint: exchange_rate.mint.pubkey.unwrap(),
|
||||||
},
|
},
|
||||||
None,
|
None,
|
||||||
);
|
);
|
||||||
|
@ -286,12 +283,11 @@ impl AddinCookie {
|
||||||
&voter_stake_registry::accounts::Deposit {
|
&voter_stake_registry::accounts::Deposit {
|
||||||
registrar: registrar.address,
|
registrar: registrar.address,
|
||||||
voter: voter.address,
|
voter: voter.address,
|
||||||
exchange_vault: exchange_rate.exchange_vault,
|
vault: exchange_rate.vault,
|
||||||
deposit_token: token_address,
|
deposit_token: token_address,
|
||||||
deposit_authority: authority.pubkey(),
|
deposit_authority: authority.pubkey(),
|
||||||
deposit_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
|
deposit_mint: exchange_rate.mint.pubkey.unwrap(),
|
||||||
token_program: spl_token::id(),
|
token_program: spl_token::id(),
|
||||||
associated_token_program: spl_associated_token_account::id(),
|
|
||||||
system_program: solana_sdk::system_program::id(),
|
system_program: solana_sdk::system_program::id(),
|
||||||
rent: solana_program::sysvar::rent::id(),
|
rent: solana_program::sysvar::rent::id(),
|
||||||
},
|
},
|
||||||
|
@ -333,8 +329,8 @@ impl AddinCookie {
|
||||||
registrar: registrar.address,
|
registrar: registrar.address,
|
||||||
voter: voter.address,
|
voter: voter.address,
|
||||||
token_owner_record: token_owner_record.address,
|
token_owner_record: token_owner_record.address,
|
||||||
exchange_vault: exchange_rate.exchange_vault,
|
vault: exchange_rate.vault,
|
||||||
withdraw_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
|
withdraw_mint: exchange_rate.mint.pubkey.unwrap(),
|
||||||
destination: token_address,
|
destination: token_address,
|
||||||
authority: clawback_authority.pubkey(),
|
authority: clawback_authority.pubkey(),
|
||||||
token_program: spl_token::id(),
|
token_program: spl_token::id(),
|
||||||
|
@ -378,8 +374,8 @@ impl AddinCookie {
|
||||||
registrar: registrar.address,
|
registrar: registrar.address,
|
||||||
voter: voter.address,
|
voter: voter.address,
|
||||||
token_owner_record: token_owner_record.address,
|
token_owner_record: token_owner_record.address,
|
||||||
exchange_vault: exchange_rate.exchange_vault,
|
vault: exchange_rate.vault,
|
||||||
withdraw_mint: exchange_rate.deposit_mint.pubkey.unwrap(),
|
withdraw_mint: exchange_rate.mint.pubkey.unwrap(),
|
||||||
destination: token_address,
|
destination: token_address,
|
||||||
authority: authority.pubkey(),
|
authority: authority.pubkey(),
|
||||||
token_program: spl_token::id(),
|
token_program: spl_token::id(),
|
||||||
|
@ -508,10 +504,7 @@ impl AddinCookie {
|
||||||
|
|
||||||
impl ExchangeRateCookie {
|
impl ExchangeRateCookie {
|
||||||
pub async fn vault_balance(&self, solana: &SolanaCookie) -> u64 {
|
pub async fn vault_balance(&self, solana: &SolanaCookie) -> u64 {
|
||||||
solana
|
solana.get_account::<TokenAccount>(self.vault).await.amount
|
||||||
.get_account::<TokenAccount>(self.exchange_vault)
|
|
||||||
.await
|
|
||||||
.amount
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue