surfpool_sdk/cheatcodes/builders/
set_token_account.rs1use solana_pubkey::Pubkey;
2
3use crate::cheatcodes::builders::CheatcodeBuilder;
4
5pub struct SetTokenAccount {
26 owner: Pubkey,
27 mint: Pubkey,
28 amount: Option<u64>,
29 delegate: Option<Option<Pubkey>>,
30 state: Option<String>,
31 delegated_amount: Option<u64>,
32 close_authority: Option<Option<Pubkey>>,
33 token_program: Option<Pubkey>,
34}
35
36impl SetTokenAccount {
37 pub fn new(owner: Pubkey, mint: Pubkey) -> Self {
39 Self {
40 owner,
41 mint,
42 amount: None,
43 delegate: None,
44 state: None,
45 delegated_amount: None,
46 close_authority: None,
47 token_program: None,
48 }
49 }
50
51 pub fn amount(mut self, amount: u64) -> Self {
53 self.amount = Some(amount);
54 self
55 }
56
57 pub fn delegate(mut self, delegate: Pubkey) -> Self {
59 self.delegate = Some(Some(delegate));
60 self
61 }
62
63 pub fn clear_delegate(mut self) -> Self {
65 self.delegate = Some(None);
66 self
67 }
68
69 pub fn state(mut self, state: impl Into<String>) -> Self {
71 self.state = Some(state.into());
72 self
73 }
74
75 pub fn delegated_amount(mut self, delegated_amount: u64) -> Self {
77 self.delegated_amount = Some(delegated_amount);
78 self
79 }
80
81 pub fn close_authority(mut self, close_authority: Pubkey) -> Self {
83 self.close_authority = Some(Some(close_authority));
84 self
85 }
86
87 pub fn clear_close_authority(mut self) -> Self {
89 self.close_authority = Some(None);
90 self
91 }
92
93 pub fn token_program(mut self, token_program: Pubkey) -> Self {
97 self.token_program = Some(token_program);
98 self
99 }
100}
101
102impl CheatcodeBuilder for SetTokenAccount {
103 const METHOD: &'static str = "surfnet_setTokenAccount";
104
105 fn build(self) -> serde_json::Value {
107 let mut update = serde_json::Map::new();
108 if let Some(amount) = self.amount {
109 update.insert("amount".to_string(), amount.into());
110 }
111 if let Some(delegate) = self.delegate {
112 update.insert(
113 "delegate".to_string(),
114 delegate
115 .map(|pubkey| pubkey.to_string().into())
116 .unwrap_or_else(|| "null".into()),
117 );
118 }
119 if let Some(state) = self.state {
120 update.insert("state".to_string(), state.into());
121 }
122 if let Some(delegated_amount) = self.delegated_amount {
123 update.insert("delegatedAmount".to_string(), delegated_amount.into());
124 }
125 if let Some(close_authority) = self.close_authority {
126 update.insert(
127 "closeAuthority".to_string(),
128 close_authority
129 .map(|pubkey| pubkey.to_string().into())
130 .unwrap_or_else(|| "null".into()),
131 );
132 }
133
134 let mut params = vec![
135 self.owner.to_string().into(),
136 self.mint.to_string().into(),
137 update.into(),
138 ];
139
140 if let Some(token_program) = self.token_program {
141 params.push(token_program.to_string().into());
142 }
143
144 serde_json::Value::Array(params)
145 }
146}