use alloc::string::String;
use alloc::vec::Vec;
use keetanetwork_bindings::permissions::{flag_names, offsets};
use keetanetwork_client::{
AccountInfo, AccountState, Acl, AclPrincipal, BlockEffects, Certificate, HistoryEntry, LedgerChecksum,
Representative, TokenBalance,
};
use serde::Serialize;
use tsify::Tsify;
use crate::convert::amount_to_string;
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct TokenBalanceView {
pub token: String,
pub balance: String,
}
impl From<&TokenBalance> for TokenBalanceView {
fn from(balance: &TokenBalance) -> Self {
Self { token: balance.token.to_string(), balance: amount_to_string(balance.balance.clone()) }
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct AccountInfoView {
pub name: Option<String>,
pub description: Option<String>,
pub metadata: Option<String>,
}
impl From<&AccountInfo> for AccountInfoView {
fn from(info: &AccountInfo) -> Self {
let name = info.name.clone();
let description = info.description.clone();
let metadata = info.metadata.clone();
Self { name, description, metadata }
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct AccountStateView {
pub representative: Option<String>,
pub head: Option<String>,
pub height: Option<String>,
pub info: Option<AccountInfoView>,
pub supply: Option<String>,
pub balances: Vec<TokenBalanceView>,
}
impl From<&AccountState> for AccountStateView {
fn from(state: &AccountState) -> Self {
Self {
representative: state
.representative
.as_ref()
.map(|representative| representative.to_string()),
head: state.head.map(|head| head.to_string()),
height: state.height.clone().map(amount_to_string),
info: state.info.as_ref().map(AccountInfoView::from),
supply: state.supply.clone().map(amount_to_string),
balances: state.balances.iter().map(TokenBalanceView::from).collect(),
}
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct BlockEffectsView {
pub block: String,
pub operation_indexes: Vec<usize>,
}
impl From<&BlockEffects> for BlockEffectsView {
fn from(effects: &BlockEffects) -> Self {
Self { block: hex::encode(effects.block.to_bytes()), operation_indexes: effects.operation_indexes.clone() }
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct StapleEffectsView {
pub id: String,
pub blocks: Vec<BlockEffectsView>,
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct HistoryEntryView {
pub staple: String,
pub id: Option<String>,
pub timestamp: Option<String>,
}
impl From<&HistoryEntry> for HistoryEntryView {
fn from(entry: &HistoryEntry) -> Self {
Self {
staple: hex::encode(entry.staple.as_bytes()),
id: entry.id.map(|id| id.to_string()),
timestamp: entry.timestamp.map(|moment| moment.to_string()),
}
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct RepresentativeView {
pub account: String,
pub weight: String,
pub api_url: Option<String>,
}
impl From<&Representative> for RepresentativeView {
fn from(rep: &Representative) -> Self {
Self {
account: rep.account.to_string(),
weight: amount_to_string(rep.weight.clone()),
api_url: rep.api_url.clone(),
}
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct LedgerChecksumView {
pub checksum: String,
pub moment: Option<String>,
pub moment_range: Option<f64>,
}
impl From<&LedgerChecksum> for LedgerChecksumView {
fn from(checksum: &LedgerChecksum) -> Self {
Self {
checksum: amount_to_string(checksum.checksum.clone()),
moment: checksum.moment.map(|moment| moment.to_string()),
moment_range: checksum.moment_range,
}
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct AclPrincipalView {
pub kind: String,
pub account: String,
pub certificate: Option<String>,
}
impl From<&AclPrincipal> for AclPrincipalView {
fn from(principal: &AclPrincipal) -> Self {
match principal {
AclPrincipal::Account(account) => {
Self { kind: String::from("account"), account: account.to_string(), certificate: None }
}
AclPrincipal::Certificate { hash, account } => Self {
kind: String::from("certificate"),
account: account.to_string(),
certificate: Some(hex::encode(hash)),
},
}
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct AclView {
pub principal: Option<AclPrincipalView>,
pub entity: Option<String>,
pub target: Option<String>,
pub permissions: Vec<String>,
pub external_permissions: Vec<u8>,
}
impl From<&Acl> for AclView {
fn from(acl: &Acl) -> Self {
Self {
principal: acl.principal.as_ref().map(AclPrincipalView::from),
entity: acl.entity.as_ref().map(|entity| entity.to_string()),
target: acl.target.as_ref().map(|target| target.to_string()),
permissions: flag_names(&acl.permissions),
external_permissions: offsets(&acl.permissions),
}
}
}
#[derive(Serialize, Tsify)]
#[tsify(into_wasm_abi)]
pub struct CertificateView {
pub certificate: String,
pub intermediates: Vec<String>,
}
impl From<&Certificate> for CertificateView {
fn from(certificate: &Certificate) -> Self {
Self { certificate: certificate.certificate.clone(), intermediates: certificate.intermediates.clone() }
}
}