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
use anchor_lang::{prelude::*, AccountsClose};
use anchor_spl::token::Mint;
use jet_airspace::state::Airspace;
use crate::{seeds::TOKEN_CONFIG_SEED, ErrorCode, TokenAdmin, TokenConfig, TokenKind};
#[derive(AnchorDeserialize, AnchorSerialize, Debug, Eq, PartialEq, Clone)]
pub struct TokenConfigUpdate {
pub underlying_mint: Pubkey,
pub admin: TokenAdmin,
pub token_kind: TokenKind,
pub value_modifier: u16,
pub max_staleness: u64,
}
#[derive(Accounts)]
pub struct ConfigureToken<'info> {
pub authority: Signer<'info>,
#[account(has_one = authority)]
pub airspace: Account<'info, Airspace>,
#[account(mut)]
pub payer: Signer<'info>,
pub mint: Account<'info, Mint>,
#[account(init_if_needed,
seeds = [
TOKEN_CONFIG_SEED,
airspace.key().as_ref(),
mint.key().as_ref()
],
bump,
payer = payer,
space = 8 + std::mem::size_of::<TokenConfig>(),
)]
pub token_config: Account<'info, TokenConfig>,
pub system_program: Program<'info, System>,
}
pub fn configure_token_handler(
ctx: Context<ConfigureToken>,
updated_config: Option<TokenConfigUpdate>,
) -> Result<()> {
let config = &mut ctx.accounts.token_config;
let updated_config = match updated_config {
Some(update) => update,
None => return config.close(ctx.accounts.payer.to_account_info()),
};
if config.underlying_mint != Pubkey::default()
&& updated_config.underlying_mint != config.underlying_mint
{
msg!("underlying mint cannot be changed");
return err!(ErrorCode::InvalidConfig);
}
config.mint = ctx.accounts.mint.key();
config.airspace = ctx.accounts.airspace.key();
config.underlying_mint = updated_config.underlying_mint;
config.admin = updated_config.admin;
config.token_kind = updated_config.token_kind;
config.value_modifier = updated_config.value_modifier;
config.max_staleness = updated_config.max_staleness;
config.validate()?;
Ok(())
}