solana_web3_sys/
connection.rs

1//!
2//! [`Connection`](https://solana-labs.github.io/solana-web3.js/classes/Connection.html) class bindings.
3//!
4use crate::account::ProgramAccount;
5use crate::api::*;
6use crate::imports::*;
7use crate::publickey::PublicKey;
8use solana_sdk::account::Account;
9//use workflow_log::log_trace;
10
11#[wasm_bindgen]
12extern "C" {
13    #[wasm_bindgen(js_namespace=solanaWeb3, js_name = Connection)]
14    #[derive(Debug, Clone)]
15    pub type Connection;
16
17    #[wasm_bindgen(constructor, js_namespace=["solanaWeb3"])]
18    /// Create Connection
19    ///
20    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html)
21    ///
22    pub fn new(endpoint: String) -> Connection;
23
24    #[wasm_bindgen(constructor, js_namespace=["solanaWeb3"])]
25    /// Create Connection
26    ///
27    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html)
28    ///
29    pub fn new_with_commitment(endpoint: String, commitment: String) -> Connection;
30
31    #[wasm_bindgen(method, catch, js_name = "getLatestBlockhash")]
32    /// Fetch the latest blockhash from the cluster
33    ///
34    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getLatestBlockhash)
35    ///
36    pub async fn get_latest_block_hash_impl(this: &Connection) -> Result<JsValue>;
37
38    #[wasm_bindgen(method, catch, js_name = "getLatestBlockhash")]
39    /// Fetch the latest blockhash from the cluster
40    ///
41    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getLatestBlockhash)
42    ///
43    pub async fn get_latest_block_hash_with_commitment(
44        this: &Connection,
45        commitment: String,
46    ) -> Result<JsValue>;
47
48    #[wasm_bindgen(method, catch, js_name = "sendRawTransaction")]
49    /// Send a transaction that has already been signed and serialized into the wire format
50    ///
51    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#sendRawTransaction)
52    ///
53    pub async fn send_raw_transaction(this: &Connection, tx: JsValue) -> Result<JsValue>;
54
55    #[wasm_bindgen(method, catch, js_name = "sendRawTransaction")]
56    /// Send a transaction that has already been signed and serialized into the wire format
57    ///
58    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#sendRawTransaction)
59    ///
60    pub async fn send_raw_transaction_with_options_impl(
61        this: &Connection,
62        tx: JsValue,
63        options: JsValue,
64    ) -> Result<JsValue>;
65
66    #[wasm_bindgen(method, catch, js_name = "getAccountInfo")]
67    /// Fetch all the account info for the specified public key
68    ///
69    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getAccountInfo)
70    ///
71    pub async fn get_account_info_impl(this: &Connection, public_key: JsValue) -> Result<JsValue>;
72
73    #[wasm_bindgen(method, catch, js_name = "getAccountInfo")]
74    /// Fetch all the account info for the specified public key
75    ///
76    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getAccountInfo)
77    ///
78    pub async fn get_account_info_with_options_impl(
79        this: &Connection,
80        public_key: JsValue,
81        options: JsValue,
82    ) -> Result<JsValue>;
83
84    #[wasm_bindgen(method, catch, js_name = "getProgramAccounts")]
85    /// Fetch all the account info for the specified public key
86    ///
87    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getProgramAccounts)
88    ///
89    pub async fn get_program_accounts_with_config_impl(
90        this: &Connection,
91        public_key: JsValue,
92        config: RpcProgramAccountsConfig,
93    ) -> Result<JsValue>;
94
95    #[wasm_bindgen(extends = Object)]
96    #[derive(Debug, Clone, PartialEq, Eq)]
97    pub type LatestBlockhashInfo;
98
99    #[wasm_bindgen(getter, method, js_name = "blockhash")]
100    /// get blockhash
101    ///
102    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getLatestBlockhash)
103    ///
104    pub fn block_hash(this: &LatestBlockhashInfo) -> JsValue;
105
106    #[wasm_bindgen(getter, method, js_name = "lastValidBlockHeight")]
107    /// get lastValidBlockHeight
108    ///
109    /// ⧉ [Solana Documentation](https://solana-labs.github.io/solana-web3.js/classes/Connection.html#getLatestBlockhash)
110    ///
111    pub fn last_valid_block_height(this: &LatestBlockhashInfo) -> JsValue;
112
113    #[wasm_bindgen(extends = Object)]
114    #[derive(Debug, Clone, PartialEq, Eq)]
115    pub type SendRawTxOptions;
116
117}
118
119impl Connection {
120    pub async fn get_latest_block_hash(&self) -> Result<LatestBlockhashInfo> {
121        Ok(self.get_latest_block_hash_impl().await?.into())
122    }
123
124    pub async fn get_account_info(&self, pubkey: &Pubkey) -> Result<Account> {
125        let value = self
126            .get_account_info_impl(PublicKey::try_from(pubkey)?.into())
127            .await?;
128        if !value.is_object() {
129            return Err(JsValue::from(format!("Account not found: {pubkey:?}")).into());
130        }
131        let account: ProgramAccount = value.try_into().map_err(|err| {
132            JsValue::from(format!(
133                "Unable to convert account into to ProgramAccount: {err}"
134            ))
135        })?;
136        account.try_into()
137    }
138
139    pub async fn get_account_info_with_options(
140        &self,
141        pubkey: &Pubkey,
142        options: JsValue,
143    ) -> Result<JsValue> {
144        self.get_account_info_with_options_impl(PublicKey::try_from(pubkey)?.into(), options)
145            .await
146    }
147
148    pub async fn send_raw_transaction_with_options(
149        &self,
150        tx: JsValue,
151        options: SendRawTxOptions,
152    ) -> Result<JsValue> {
153        self.send_raw_transaction_with_options_impl(tx, options.into())
154            .await
155    }
156
157    pub async fn get_program_accounts_with_config(
158        &self,
159        pubkey: &Pubkey,
160        config: RpcProgramAccountsConfig,
161    ) -> Result<Vec<(Pubkey, Account)>> {
162        let res = self
163            .get_program_accounts_with_config_impl(pubkey_to_jsvalue(pubkey)?, config)
164            .await?;
165
166        //log_trace!("array: {res:#?}, is_array:{}", res.is_array());
167        let mut result = vec![];
168        if !res.is_array() {
169            return Ok(result);
170        }
171        let array = Array::from(&res);
172        let size = array.length();
173        for index in 0..size {
174            let item = array.get(index);
175            if !item.is_object() {
176                continue;
177            }
178            if let Ok(item) = ProgramAccountsResultItem::try_from(item) {
179                result.push((item.pubkey()?, item.account().try_into()?))
180            }
181        }
182
183        Ok(result)
184    }
185}
186
187impl OptionsTrait for SendRawTxOptions {}
188
189impl SendRawTxOptions {
190    /// set skipPreflight
191    pub fn skip_preflight(self, skip_preflight: bool) -> Self {
192        self.set("skipPreflight", JsValue::from(skip_preflight))
193    }
194}