Skip to main content

surfpool_sdk/cheatcodes/builders/
set_account.rs

1use solana_pubkey::Pubkey;
2
3use crate::cheatcodes::builders::CheatcodeBuilder;
4
5/// Builder for `surfnet_setAccount`.
6///
7/// This builder starts with the required account address and lets tests add
8/// whichever optional account fields they want to override before execution.
9///
10/// ```rust
11/// use surfpool_sdk::{Pubkey, Surfnet};
12/// use surfpool_sdk::cheatcodes::builders::SetAccount;
13///
14/// # async fn example() {
15/// let surfnet = Surfnet::start().await.unwrap();
16/// let cheats = surfnet.cheatcodes();
17/// let address = Pubkey::new_unique();
18/// let owner = Pubkey::new_unique();
19///
20/// cheats
21///     .execute(
22///         SetAccount::new(address)
23///             .lamports(500_000)
24///             .owner(owner)
25///             .data(vec![0xAA, 0xBB]),
26///     )
27///     .unwrap();
28/// # }
29/// ```
30pub struct SetAccount {
31    address: Pubkey,
32    lamports: Option<u64>,
33    data: Option<Vec<u8>>,
34    owner: Option<Pubkey>,
35    rent_epoch: Option<u64>,
36    executable: Option<bool>,
37}
38
39impl SetAccount {
40    /// Create a new account-update builder for the given address.
41    pub fn new(address: Pubkey) -> Self {
42        Self {
43            address,
44            lamports: None,
45            data: None,
46            owner: None,
47            rent_epoch: None,
48            executable: None,
49        }
50    }
51
52    /// Set the target lamport balance for the account.
53    pub fn lamports(mut self, lamports: u64) -> Self {
54        self.lamports = Some(lamports);
55        self
56    }
57
58    /// Set raw account data bytes.
59    ///
60    /// The builder hex-encodes these bytes when producing the final RPC payload.
61    pub fn data(mut self, data: Vec<u8>) -> Self {
62        self.data = Some(data);
63        self
64    }
65
66    /// Set the owning program for the account.
67    pub fn owner(mut self, owner: Pubkey) -> Self {
68        self.owner = Some(owner);
69        self
70    }
71
72    /// Set the account rent epoch.
73    pub fn rent_epoch(mut self, rent_epoch: u64) -> Self {
74        self.rent_epoch = Some(rent_epoch);
75        self
76    }
77
78    /// Set whether the account is executable.
79    pub fn executable(mut self, executable: bool) -> Self {
80        self.executable = Some(executable);
81        self
82    }
83}
84
85impl CheatcodeBuilder for SetAccount {
86    const METHOD: &'static str = "surfnet_setAccount";
87
88    /// Build the JSON-RPC parameter array for `surfnet_setAccount`.
89    fn build(self) -> serde_json::Value {
90        let mut account_info = serde_json::Map::new();
91        if let Some(lamports) = self.lamports {
92            account_info.insert("lamports".to_string(), lamports.into());
93        }
94        if let Some(data) = self.data {
95            account_info.insert("data".to_string(), hex::encode(data).into());
96        }
97        if let Some(owner) = self.owner {
98            account_info.insert("owner".to_string(), owner.to_string().into());
99        }
100        if let Some(rent_epoch) = self.rent_epoch {
101            account_info.insert("rentEpoch".to_string(), rent_epoch.into());
102        }
103        if let Some(executable) = self.executable {
104            account_info.insert("executable".to_string(), executable.into());
105        }
106
107        serde_json::json!([self.address.to_string(), account_info])
108    }
109}