gmsol_store/instructions/
config.rs

1use crate::states::{Amount, Factor};
2use anchor_lang::prelude::*;
3
4use crate::{states::Store, utils::internal};
5
6/// The accounts definition of instructions for updating configs.
7#[derive(Accounts)]
8pub struct InsertConfig<'info> {
9    /// Caller.
10    #[account(mut)]
11    pub authority: Signer<'info>,
12    /// Store.
13    #[account(mut)]
14    pub store: AccountLoader<'info, Store>,
15}
16
17impl<'info> internal::Authentication<'info> for InsertConfig<'info> {
18    fn authority(&self) -> &Signer<'info> {
19        &self.authority
20    }
21
22    fn store(&self) -> &AccountLoader<'info, Store> {
23        &self.store
24    }
25}
26
27/// CHECK: only CONFIG_KEEPER is allowed to invoke.
28pub(crate) fn unchecked_insert_amount(
29    ctx: Context<InsertConfig>,
30    key: &str,
31    amount: Amount,
32) -> Result<()> {
33    *ctx.accounts.store.load_mut()?.get_amount_mut(key)? = amount;
34    Ok(())
35}
36
37/// CHECK: only CONFIG_KEEPER is allowed to invoke, except for:
38///
39/// - [`OrderFeeDiscountForReferredUser`](crate::states::FactorKey::OrderFeeDiscountForReferredUser)
40///   which can also be updated using
41///   [`insert_order_fee_discount_for_referred_user`](crate::gmsol_store::insert_order_fee_discount_for_referred_user)
42pub(crate) fn unchecked_insert_factor(
43    ctx: Context<InsertConfig>,
44    key: &str,
45    factor: Factor,
46) -> Result<()> {
47    *ctx.accounts.store.load_mut()?.get_factor_mut(key)? = factor;
48    Ok(())
49}
50
51/// CHECK: only CONFIG_KEEPER is allowed to invoke.
52pub(crate) fn unchecked_insert_address(
53    ctx: Context<InsertConfig>,
54    key: &str,
55    address: Pubkey,
56) -> Result<()> {
57    *ctx.accounts.store.load_mut()?.get_address_mut(key)? = address;
58    Ok(())
59}