1use crate::imports::*;
6use js_sys::Reflect;
7use crate::account::ProgramAccount;
9use crate::publickey::PublicKey;
10use solana_sdk::commitment_config::CommitmentConfig;
11use solana_sdk::slot_history::Slot;
12
13#[wasm_bindgen]
14extern "C" {
15 #[wasm_bindgen(extends = Object)]
16 #[derive(Debug)]
17 pub type RpcProgramAccountsConfig;
18
19 #[wasm_bindgen(extends = Object)]
20 #[derive(Debug)]
21 pub type ProgramAccountsResultItem;
22
23 #[wasm_bindgen(getter, method)]
24 pub fn account(this: &ProgramAccountsResultItem) -> ProgramAccount;
25
26 #[wasm_bindgen(getter, method, js_name = "pubkey")]
27 pub fn pubkey_impl(this: &ProgramAccountsResultItem) -> PublicKey;
28
29}
30
31impl OptionsTrait for RpcProgramAccountsConfig {}
32
33impl RpcProgramAccountsConfig {
34 pub fn add_filters(self, filters: Array) -> Result<Self> {
35 Ok(self.set("filters", filters.into()))
36 }
37
38 pub fn encoding(self, encoding: RpcAccountEncoding) -> Result<Self> {
39 Ok(self.set("encoding", encoding.into()))
40 }
41
42 pub fn data_slice(self, data_slice: RpcDataSliceConfig) -> Result<Self> {
43 Ok(self.set("dataSlice", data_slice.try_into()?))
44 }
45
46 pub fn commitment(self, commitment: CommitmentConfig) -> Result<Self> {
47 Ok(self.set("commitment", commitment.commitment.to_string().into()))
48 }
49
50 pub fn min_context_slot(self, min_context_slot: Slot) -> Result<Self> {
51 Ok(self.set("minContextSlot", min_context_slot.into()))
52 }
53}
54
55pub enum RpcAccountEncoding {
56 Base58,
57 Base64,
58}
59
60impl From<RpcAccountEncoding> for String {
61 fn from(value: RpcAccountEncoding) -> Self {
62 match value {
63 RpcAccountEncoding::Base58 => "Base58".to_string(),
64 RpcAccountEncoding::Base64 => "Base64".to_string(),
65 }
66 }
67}
68impl From<RpcAccountEncoding> for JsValue {
69 fn from(value: RpcAccountEncoding) -> Self {
70 String::from(value).to_lowercase().into()
71 }
72}
73
74pub struct RpcDataSliceConfig {
75 pub offset: usize,
76 pub length: usize,
77}
78
79impl TryFrom<RpcDataSliceConfig> for JsValue {
80 type Error = crate::error::Error;
81
82 fn try_from(value: RpcDataSliceConfig) -> Result<Self> {
83 let obj = Object::new();
84 Reflect::set(&obj, &JsValue::from("offset"), &JsValue::from(value.offset))?;
85 Reflect::set(&obj, &JsValue::from("length"), &JsValue::from(value.length))?;
86 Ok(obj.into())
87 }
88}
89
90impl ProgramAccountsResultItem {
91 pub fn pubkey(&self) -> Result<Pubkey> {
92 self.pubkey_impl().try_into()
93 }
94}