forked from blockworks-foundation/voter-stake-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant.rs
194 lines (169 loc) · 6.68 KB
/
grant.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
use crate::error::*;
use crate::state::*;
use anchor_lang::prelude::*;
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{self, Mint, Token, TokenAccount};
use std::convert::TryFrom;
use std::mem::size_of;
#[derive(Accounts)]
pub struct Grant<'info> {
pub registrar: AccountLoader<'info, Registrar>,
#[account(
init_if_needed,
seeds = [registrar.key().as_ref(), b"voter".as_ref(), voter_authority.key().as_ref()],
bump,
payer = payer,
space = 8 + size_of::<Voter>(),
)]
pub voter: AccountLoader<'info, Voter>,
/// The account of the grantee / the address controlling the voter
/// that the grant is going to.
/// CHECK: May be any address
pub voter_authority: UncheckedAccount<'info>,
/// 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.
#[account(
init_if_needed,
seeds = [registrar.key().as_ref(), b"voter-weight-record".as_ref(), voter_authority.key().as_ref()],
bump,
payer = payer,
space = size_of::<VoterWeightRecord>(),
)]
pub voter_weight_record: Account<'info, VoterWeightRecord>,
#[account(
init_if_needed,
associated_token::authority = voter,
associated_token::mint = deposit_mint,
payer = payer
)]
pub vault: Box<Account<'info, TokenAccount>>,
#[account(
mut,
constraint = deposit_token.owner == token_authority.key(),
constraint = deposit_token.mint == deposit_mint.key(),
)]
pub deposit_token: Box<Account<'info, TokenAccount>>,
/// Authority for transfering tokens away from deposit_token
pub token_authority: Signer<'info>,
/// Authority for making a grant to this voter account
///
/// Verification inline in instruction
pub grant_authority: Signer<'info>,
/// Rent payer if a new account is to be created
#[account(mut)]
pub payer: Signer<'info>,
pub deposit_mint: Box<Account<'info, Mint>>,
pub system_program: Program<'info, System>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub rent: Sysvar<'info, Rent>,
}
impl<'info> Grant<'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.deposit_token.to_account_info(),
to: self.vault.to_account_info(),
authority: self.token_authority.to_account_info(),
};
CpiContext::new(program, accounts)
}
}
/// Returns if the anchor discriminator on the account is still unset
pub fn is_freshly_initialized(account_info: &AccountInfo) -> Result<bool> {
let data = account_info.try_borrow_data()?;
let mut disc_bytes = [0u8; 8];
disc_bytes.copy_from_slice(&data[..8]);
let discriminator = u64::from_le_bytes(disc_bytes);
Ok(discriminator == 0)
}
/// Gives a grant to a voter.
///
/// The voter may or may not exist in advance.
/// Creates a new deposit entry -- errors if no free ones are available.
#[allow(clippy::too_many_arguments)]
pub fn grant(
ctx: Context<Grant>,
voter_bump: u8,
voter_weight_record_bump: u8,
kind: LockupKind,
start_ts: Option<u64>,
periods: u32,
allow_clawback: bool,
amount: u64,
) -> Result<()> {
require_eq!(voter_bump, *ctx.bumps.get("voter").unwrap());
require_eq!(
voter_weight_record_bump,
*ctx.bumps.get("voter_weight_record").unwrap()
);
// Load accounts.
let registrar = &ctx.accounts.registrar.load()?;
let voter_authority = ctx.accounts.voter_authority.key();
// Get the exchange rate entry associated with this deposit.
let mint_idx = registrar.voting_mint_config_index(ctx.accounts.deposit_token.mint)?;
let mint_config = ®istrar.voting_mints[mint_idx];
// The grant instruction creates a new deposit entry for the target voter. This is a
// limited resource. If anyone could call "grant" then it could be used for denial of
// service by filling all deposit entries with tiny amounts and long lockup times.
let grant_authority = ctx.accounts.grant_authority.key();
require!(
grant_authority == registrar.realm_authority
|| grant_authority == mint_config.grant_authority
|| grant_authority == voter_authority,
VsrError::InvalidAuthority
);
// Init the voter if it hasn't been already.
let new_voter = is_freshly_initialized(ctx.accounts.voter.as_ref())?;
let mut voter = if new_voter {
ctx.accounts.voter.load_init()?
} else {
ctx.accounts.voter.load_mut()?
};
if new_voter {
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();
// Initializing the voter weight record exactly when setting up the voter is fine.
// Note that vote_weight_record is not an Anchor account, is_freshly_initialized()
// would not work.
let voter_weight_record = &mut ctx.accounts.voter_weight_record;
voter_weight_record.account_discriminator =
spl_governance_addin_api::voter_weight::VoterWeightRecord::ACCOUNT_DISCRIMINATOR;
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;
}
// Get and init the first free deposit entry.
let free_entry_idx = voter
.deposits
.iter()
.position(|d_entry| !d_entry.is_used)
.ok_or(VsrError::DepositEntryFull)?;
let d_entry = &mut voter.deposits[free_entry_idx];
let curr_ts = registrar.clock_unix_timestamp();
let start_ts = if let Some(v) = start_ts {
i64::try_from(v).unwrap()
} else {
curr_ts
};
// Set up a deposit.
*d_entry = DepositEntry::default();
d_entry.is_used = true;
d_entry.voting_mint_config_idx = mint_idx as u8;
d_entry.allow_clawback = allow_clawback;
d_entry.lockup = Lockup::new_from_periods(kind, curr_ts, start_ts, periods)?;
// Deposit tokens, locking them all.
token::transfer(ctx.accounts.transfer_ctx(), amount)?;
d_entry.amount_deposited_native = amount;
d_entry.amount_initially_locked_native = amount;
msg!(
"Granted amount {} at deposit index {} with lockup kind {:?} for {} periods",
amount,
free_entry_idx,
d_entry.lockup.kind,
periods,
);
Ok(())
}