solana_web3_sys/
connection.rs1use crate::account::ProgramAccount;
5use crate::api::*;
6use crate::imports::*;
7use crate::publickey::PublicKey;
8use solana_sdk::account::Account;
9#[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 pub fn new(endpoint: String) -> Connection;
23
24 #[wasm_bindgen(constructor, js_namespace=["solanaWeb3"])]
25 pub fn new_with_commitment(endpoint: String, commitment: String) -> Connection;
30
31 #[wasm_bindgen(method, catch, js_name = "getLatestBlockhash")]
32 pub async fn get_latest_block_hash_impl(this: &Connection) -> Result<JsValue>;
37
38 #[wasm_bindgen(method, catch, js_name = "getLatestBlockhash")]
39 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 pub async fn send_raw_transaction(this: &Connection, tx: JsValue) -> Result<JsValue>;
54
55 #[wasm_bindgen(method, catch, js_name = "sendRawTransaction")]
56 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 pub async fn get_account_info_impl(this: &Connection, public_key: JsValue) -> Result<JsValue>;
72
73 #[wasm_bindgen(method, catch, js_name = "getAccountInfo")]
74 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 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 pub fn block_hash(this: &LatestBlockhashInfo) -> JsValue;
105
106 #[wasm_bindgen(getter, method, js_name = "lastValidBlockHeight")]
107 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 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 pub fn skip_preflight(self, skip_preflight: bool) -> Self {
192 self.set("skipPreflight", JsValue::from(skip_preflight))
193 }
194}