gmsol_sdk/client/ops/
config.rs

1use std::ops::Deref;
2
3use gmsol_programs::gmsol_store::client::{accounts, args};
4use gmsol_solana_utils::transaction_builder::TransactionBuilder;
5use gmsol_utils::config::{
6    ActionDisabledFlag, AddressKey, AmountKey, DomainDisabledFlag, FactorKey,
7};
8use solana_sdk::{pubkey::Pubkey, signer::Signer};
9
10type Factor = u128;
11type Amount = u64;
12
13/// Operations for global configurations.
14pub trait ConfigOps<C> {
15    /// Toggle feature.
16    fn toggle_feature(
17        &self,
18        store: &Pubkey,
19        domian: DomainDisabledFlag,
20        action: ActionDisabledFlag,
21        enable: bool,
22    ) -> TransactionBuilder<C>;
23
24    /// Insert a global amount.
25    fn insert_global_amount(
26        &self,
27        store: &Pubkey,
28        key: &str,
29        amount: &Amount,
30    ) -> TransactionBuilder<C>;
31
32    /// Insert a global factor.
33    fn insert_global_factor(
34        &self,
35        store: &Pubkey,
36        key: &str,
37        factor: &Factor,
38    ) -> TransactionBuilder<C>;
39
40    /// Insert a global address.
41    fn insert_global_address(
42        &self,
43        store: &Pubkey,
44        key: &str,
45        address: &Pubkey,
46    ) -> TransactionBuilder<C>;
47
48    /// Insert a global amount by key.
49    fn insert_global_amount_by_key(
50        &self,
51        store: &Pubkey,
52        key: AmountKey,
53        amount: &Amount,
54    ) -> TransactionBuilder<C> {
55        let key = key.to_string();
56        self.insert_global_amount(store, &key, amount)
57    }
58
59    /// Insert a global factor by key.
60    fn insert_global_factor_by_key(
61        &self,
62        store: &Pubkey,
63        key: FactorKey,
64        factor: &Factor,
65    ) -> TransactionBuilder<C> {
66        let key = key.to_string();
67        self.insert_global_factor(store, &key, factor)
68    }
69
70    /// Insert a global address by key.
71    fn insert_global_address_by_key(
72        &self,
73        store: &Pubkey,
74        key: AddressKey,
75        address: &Pubkey,
76    ) -> TransactionBuilder<C> {
77        let key = key.to_string();
78        self.insert_global_address(store, &key, address)
79    }
80}
81
82impl<C: Deref<Target = impl Signer> + Clone> ConfigOps<C> for crate::Client<C> {
83    fn toggle_feature(
84        &self,
85        store: &Pubkey,
86        domian: DomainDisabledFlag,
87        action: ActionDisabledFlag,
88        enable: bool,
89    ) -> TransactionBuilder<C> {
90        self.store_transaction()
91            .anchor_args(args::ToggleFeature {
92                domain: domian.to_string(),
93                action: action.to_string(),
94                enable,
95            })
96            .anchor_accounts(accounts::ToggleFeature {
97                authority: self.payer(),
98                store: *store,
99            })
100    }
101
102    fn insert_global_amount(
103        &self,
104        store: &Pubkey,
105        key: &str,
106        amount: &Amount,
107    ) -> TransactionBuilder<C> {
108        let authority = self.payer();
109        self.store_transaction()
110            .anchor_args(args::InsertAmount {
111                key: key.to_string(),
112                amount: *amount,
113            })
114            .anchor_accounts(accounts::InsertAmount {
115                authority,
116                store: *store,
117            })
118    }
119
120    fn insert_global_factor(
121        &self,
122        store: &Pubkey,
123        key: &str,
124        factor: &Factor,
125    ) -> TransactionBuilder<C> {
126        let authority = self.payer();
127        self.store_transaction()
128            .anchor_args(args::InsertFactor {
129                key: key.to_string(),
130                factor: *factor,
131            })
132            .anchor_accounts(accounts::InsertFactor {
133                authority,
134                store: *store,
135            })
136    }
137
138    fn insert_global_address(
139        &self,
140        store: &Pubkey,
141        key: &str,
142        address: &Pubkey,
143    ) -> TransactionBuilder<C> {
144        let authority = self.payer();
145        self.store_transaction()
146            .anchor_args(args::InsertAddress {
147                key: key.to_string(),
148                address: *address,
149            })
150            .anchor_accounts(accounts::InsertAddress {
151                authority,
152                store: *store,
153            })
154    }
155}