gmsol/store/
config.rs

1use std::ops::Deref;
2
3use anchor_client::solana_sdk::{pubkey::Pubkey, signer::Signer};
4use gmsol_solana_utils::transaction_builder::TransactionBuilder;
5use gmsol_store::{
6    accounts, instruction,
7    states::{AddressKey, Amount, AmountKey, Factor, FactorKey},
8};
9
10/// Config Operations.
11pub trait ConfigOps<C> {
12    /// Insert a global amount.
13    fn insert_global_amount(
14        &self,
15        store: &Pubkey,
16        key: &str,
17        amount: &Amount,
18    ) -> TransactionBuilder<C>;
19
20    /// Insert a global factor.
21    fn insert_global_factor(
22        &self,
23        store: &Pubkey,
24        key: &str,
25        factor: &Factor,
26    ) -> TransactionBuilder<C>;
27
28    /// Insert a global address.
29    fn insert_global_address(
30        &self,
31        store: &Pubkey,
32        key: &str,
33        address: &Pubkey,
34    ) -> TransactionBuilder<C>;
35
36    /// Insert a global amount by key.
37    fn insert_global_amount_by_key(
38        &self,
39        store: &Pubkey,
40        key: AmountKey,
41        amount: &Amount,
42    ) -> TransactionBuilder<C> {
43        let key = key.to_string();
44        self.insert_global_amount(store, &key, amount)
45    }
46
47    /// Insert a global factor by key.
48    fn insert_global_factor_by_key(
49        &self,
50        store: &Pubkey,
51        key: FactorKey,
52        factor: &Factor,
53    ) -> TransactionBuilder<C> {
54        let key = key.to_string();
55        self.insert_global_factor(store, &key, factor)
56    }
57
58    /// Insert a global address by key.
59    fn insert_global_address_by_key(
60        &self,
61        store: &Pubkey,
62        key: AddressKey,
63        address: &Pubkey,
64    ) -> TransactionBuilder<C> {
65        let key = key.to_string();
66        self.insert_global_address(store, &key, address)
67    }
68}
69
70impl<C, S> ConfigOps<C> for crate::Client<C>
71where
72    C: Deref<Target = S> + Clone,
73    S: Signer,
74{
75    fn insert_global_amount(
76        &self,
77        store: &Pubkey,
78        key: &str,
79        amount: &Amount,
80    ) -> TransactionBuilder<C> {
81        let authority = self.payer();
82        self.store_transaction()
83            .anchor_args(instruction::InsertAmount {
84                key: key.to_string(),
85                amount: *amount,
86            })
87            .anchor_accounts(accounts::InsertConfig {
88                authority,
89                store: *store,
90            })
91    }
92
93    fn insert_global_factor(
94        &self,
95        store: &Pubkey,
96        key: &str,
97        factor: &Factor,
98    ) -> TransactionBuilder<C> {
99        let authority = self.payer();
100        self.store_transaction()
101            .anchor_args(instruction::InsertFactor {
102                key: key.to_string(),
103                factor: *factor,
104            })
105            .anchor_accounts(accounts::InsertConfig {
106                authority,
107                store: *store,
108            })
109    }
110
111    fn insert_global_address(
112        &self,
113        store: &Pubkey,
114        key: &str,
115        address: &Pubkey,
116    ) -> TransactionBuilder<C> {
117        let authority = self.payer();
118        self.store_transaction()
119            .anchor_args(instruction::InsertAddress {
120                key: key.to_string(),
121                address: *address,
122            })
123            .anchor_accounts(accounts::InsertConfig {
124                authority,
125                store: *store,
126            })
127    }
128}