Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add account state getter to REST API
- Loading branch information
1 parent
870be55
commit 0366c14
Showing
4 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
Submodule cardano-deps
updated
from 50a648 to c1e96b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use crate::blockchain::BlockchainR; | ||
use actix_web::error::{Error, ErrorBadRequest, ErrorNotFound}; | ||
use actix_web::{App, Json, Path, Responder, State}; | ||
use chain_crypto::{bech32::Bech32, PublicKey}; | ||
use chain_impl_mockchain::account::{AccountAlg, Identifier}; | ||
use chain_impl_mockchain::accounting::account::AccountState; | ||
|
||
pub fn create_handler( | ||
blockchain: BlockchainR, | ||
) -> impl Fn(&str) -> App<BlockchainR> + Send + Sync + Clone + 'static { | ||
move |prefix: &str| { | ||
App::with_state(blockchain.clone()) | ||
.prefix(format!("{}/v0/account", prefix)) | ||
.resource("/{account_id}", |r| r.get().with(handle_request)) | ||
} | ||
} | ||
|
||
fn handle_request( | ||
blockchain: State<BlockchainR>, | ||
account_id_bech32: Path<String>, | ||
) -> Result<impl Responder, Error> { | ||
let account_id = parse_account_id(&account_id_bech32)?; | ||
let blockchain = blockchain.lock_read(); | ||
let state = blockchain | ||
.multiverse | ||
.get(&blockchain.get_tip().unwrap()) | ||
.unwrap() | ||
.accounts() | ||
.get_state(&account_id) | ||
.map_err(|e| ErrorNotFound(e))?; | ||
Ok(Json(AccountDto::from(state))) | ||
} | ||
|
||
fn parse_account_id(bech32: &str) -> Result<Identifier, Error> { | ||
PublicKey::<AccountAlg>::try_from_bech32_str(bech32) | ||
.map(Into::into) | ||
.map_err(|e| ErrorBadRequest(e)) | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct AccountDto { | ||
value: u64, | ||
counter: u32, | ||
} | ||
|
||
impl<'a> From<&'a AccountState> for AccountDto { | ||
fn from(state: &'a AccountState) -> Self { | ||
AccountDto { | ||
value: state.get_value().as_ref().clone(), | ||
counter: state.get_counter().into(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod account; | ||
pub mod block; | ||
pub mod message; | ||
pub mod node; | ||
|