Skip to content

Commit 532f751

Browse files
committed
feat(node/web): expose ledger accounts rpc
1 parent a2d9bf0 commit 532f751

File tree

4 files changed

+81
-1
lines changed

4 files changed

+81
-1
lines changed

node/common/src/service/rpc/ledger.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#[cfg(target_family = "wasm")]
2+
use gloo_utils::format::JsValueSerdeExt;
3+
use node::rpc::*;
4+
#[cfg(target_family = "wasm")]
5+
use wasm_bindgen::prelude::*;
6+
7+
use super::RpcSender;
8+
9+
#[derive(Clone)]
10+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
11+
pub struct Ledger {
12+
#[allow(unused)]
13+
sender: RpcSender,
14+
}
15+
16+
#[derive(Clone)]
17+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
18+
pub struct LedgerSelected {
19+
#[allow(unused)]
20+
sender: RpcSender,
21+
}
22+
23+
#[derive(Clone)]
24+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
25+
pub struct LedgerAccounts {
26+
#[allow(unused)]
27+
sender: RpcSender,
28+
}
29+
30+
impl Ledger {
31+
pub fn new(sender: RpcSender) -> Self {
32+
Self { sender }
33+
}
34+
35+
pub fn latest(&self) -> LedgerSelected {
36+
LedgerSelected {
37+
sender: self.sender.clone(),
38+
}
39+
}
40+
}
41+
42+
impl LedgerSelected {
43+
pub fn accounts(&self) -> LedgerAccounts {
44+
LedgerAccounts {
45+
sender: self.sender.clone(),
46+
}
47+
}
48+
}
49+
50+
impl LedgerAccounts {
51+
async fn _all(&self) -> Option<RpcLedgerSlimAccountsResponse> {
52+
self.sender
53+
.oneshot_request(RpcRequest::LedgerAccountsGet(AccountQuery::All))
54+
.await
55+
}
56+
}
57+
58+
#[cfg(not(target_family = "wasm"))]
59+
impl LedgerAccounts {
60+
pub async fn all(&self) -> Option<RpcLedgerSlimAccountsResponse> {
61+
self._all().await
62+
}
63+
}
64+
65+
#[cfg(target_family = "wasm")]
66+
#[cfg_attr(target_family = "wasm", wasm_bindgen)]
67+
impl LedgerAccounts {
68+
pub async fn all(&self) -> JsValue {
69+
JsValue::from_serde(&self._all().await).unwrap_or_default()
70+
}
71+
}

node/common/src/service/rpc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
mod sender;
22
pub use sender::RpcSender;
33

4+
pub mod ledger;
45
pub mod state;
56
pub mod stats;
67
pub mod transaction_pool;

node/common/src/service/rpc/sender.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use node::core::channels::{mpsc, oneshot};
88
use node::p2p::connection::outgoing::P2pConnectionOutgoingInitOpts;
99
use node::rpc::*;
1010

11+
use super::ledger::Ledger;
1112
use super::state::State;
1213
use super::stats::Stats;
1314
use super::transaction_pool::TransactionPool;
@@ -86,6 +87,10 @@ impl RpcSender {
8687
pub fn transition_frontier(&self) -> TransitionFrontier {
8788
TransitionFrontier::new(self.clone())
8889
}
90+
91+
pub fn ledger(&self) -> Ledger {
92+
Ledger::new(self.clone())
93+
}
8994
}
9095

9196
#[cfg(target_family = "wasm")]

node/native/src/http_server.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,10 @@ pub async fn run(port: u16, rpc_sender: RpcSender) {
468468

469469
async move {
470470
rpc_sender_clone
471-
.oneshot_request(RpcRequest::LedgerAccountsGet(AccountQuery::All))
471+
.ledger()
472+
.latest()
473+
.accounts()
474+
.all()
472475
.await
473476
.map_or_else(
474477
dropped_channel_response,

0 commit comments

Comments
 (0)