1use std::ops::Deref;
2
3use anchor_client::solana_sdk::{pubkey::Pubkey, signer::Signer};
4
5use gmsol_solana_utils::transaction_builder::TransactionBuilder;
6use gmsol_store::{accounts, instruction};
7
8pub trait RolesOps<C> {
10 fn enable_role(&self, store: &Pubkey, role: &str) -> TransactionBuilder<C>;
12
13 fn disable_role(&self, store: &Pubkey, role: &str) -> TransactionBuilder<C>;
15
16 fn grant_role(&self, store: &Pubkey, user: &Pubkey, role: &str) -> TransactionBuilder<C>;
18
19 fn revoke_role(&self, store: &Pubkey, user: &Pubkey, role: &str) -> TransactionBuilder<C>;
21}
22
23impl<C, S> RolesOps<C> for crate::Client<C>
24where
25 C: Deref<Target = S> + Clone,
26 S: Signer,
27{
28 fn enable_role(&self, store: &Pubkey, role: &str) -> TransactionBuilder<C> {
29 let authority = self.payer();
30 self.store_transaction()
31 .anchor_accounts(accounts::EnableRole {
32 authority,
33 store: *store,
34 })
35 .anchor_args(instruction::EnableRole {
36 role: role.to_string(),
37 })
38 }
39
40 fn disable_role(&self, store: &Pubkey, role: &str) -> TransactionBuilder<C> {
41 self.store_transaction()
42 .anchor_accounts(accounts::DisableRole {
43 authority: self.payer(),
44 store: *store,
45 })
46 .anchor_args(instruction::DisableRole {
47 role: role.to_string(),
48 })
49 }
50
51 fn grant_role(&self, store: &Pubkey, user: &Pubkey, role: &str) -> TransactionBuilder<C> {
52 let authority = self.payer();
53 self.store_transaction()
54 .anchor_accounts(accounts::GrantRole {
55 authority,
56 store: *store,
57 })
58 .anchor_args(instruction::GrantRole {
59 user: *user,
60 role: role.to_string(),
61 })
62 }
63
64 fn revoke_role(&self, store: &Pubkey, user: &Pubkey, role: &str) -> TransactionBuilder<C> {
65 self.store_transaction()
66 .anchor_args(instruction::RevokeRole {
67 user: *user,
68 role: role.to_string(),
69 })
70 .anchor_accounts(accounts::RevokeRole {
71 authority: self.payer(),
72 store: *store,
73 })
74 }
75}