-
Notifications
You must be signed in to change notification settings - Fork 110
/
set_reward_emissions.rs
48 lines (39 loc) · 1.49 KB
/
set_reward_emissions.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
use anchor_lang::prelude::*;
use anchor_spl::token::TokenAccount;
use crate::errors::ErrorCode;
use crate::manager::whirlpool_manager::next_whirlpool_reward_infos;
use crate::math::checked_mul_shift_right;
use crate::state::Whirlpool;
use crate::util::to_timestamp_u64;
const DAY_IN_SECONDS: u128 = 60 * 60 * 24;
#[derive(Accounts)]
#[instruction(reward_index: u8)]
pub struct SetRewardEmissions<'info> {
#[account(mut)]
pub whirlpool: Account<'info, Whirlpool>,
#[account(address = whirlpool.reward_infos[reward_index as usize].authority)]
pub reward_authority: Signer<'info>,
#[account(address = whirlpool.reward_infos[reward_index as usize].vault)]
pub reward_vault: Account<'info, TokenAccount>,
}
pub fn handler(
ctx: Context<SetRewardEmissions>,
reward_index: u8,
emissions_per_second_x64: u128,
) -> Result<()> {
let whirlpool = &ctx.accounts.whirlpool;
let reward_vault = &ctx.accounts.reward_vault;
let emissions_per_day = checked_mul_shift_right(DAY_IN_SECONDS, emissions_per_second_x64)?;
if reward_vault.amount < emissions_per_day {
return Err(ErrorCode::RewardVaultAmountInsufficient.into());
}
let clock = Clock::get()?;
let timestamp = to_timestamp_u64(clock.unix_timestamp)?;
let next_reward_infos = next_whirlpool_reward_infos(whirlpool, timestamp)?;
Ok(ctx.accounts.whirlpool.update_emissions(
reward_index as usize,
next_reward_infos,
timestamp,
emissions_per_second_x64,
)?)
}