diff --git a/apps/namadillo/src/App/Masp/ShieldedBalanceChart.tsx b/apps/namadillo/src/App/Masp/ShieldedBalanceChart.tsx index 0449280c6..a7bf47ee0 100644 --- a/apps/namadillo/src/App/Masp/ShieldedBalanceChart.tsx +++ b/apps/namadillo/src/App/Masp/ShieldedBalanceChart.tsx @@ -1,4 +1,5 @@ import { Heading, PieChart, SkeletonLoading } from "@namada/components"; +import { ProgressBarNames } from "@namada/shared"; import { AtomErrorBoundary } from "App/Common/AtomErrorBoundary"; import { FiatCurrency } from "App/Common/FiatCurrency"; import { @@ -11,12 +12,14 @@ import { colors } from "theme"; export const ShieldedBalanceChart = (): JSX.Element => { const shieldedTokensQuery = useAtomValue(shieldedTokensAtom); - const shieledSyncProgress = useAtomValue(shieldedSyncProgressAtom("scanned")); - const shieledSyncProgress2 = useAtomValue( - shieldedSyncProgressAtom("fetched") + const shieledSyncProgressScanned = useAtomValue( + shieldedSyncProgressAtom(ProgressBarNames.Scanned) ); - const shieledSyncProgress3 = useAtomValue( - shieldedSyncProgressAtom("applied") + const shieledSyncProgressFetched = useAtomValue( + shieldedSyncProgressAtom(ProgressBarNames.Fetched) + ); + const shieledSyncProgressApplied = useAtomValue( + shieldedSyncProgressAtom(ProgressBarNames.Applied) ); const shieldedDollars = getTotalDollar(shieldedTokensQuery.data); @@ -63,9 +66,9 @@ export const ShieldedBalanceChart = (): JSX.Element => { }
Shieled sync progress:
- - - + + +
diff --git a/apps/namadillo/src/atoms/balance/atoms.ts b/apps/namadillo/src/atoms/balance/atoms.ts index cb1b5b0ed..b78e9a572 100644 --- a/apps/namadillo/src/atoms/balance/atoms.ts +++ b/apps/namadillo/src/atoms/balance/atoms.ts @@ -56,8 +56,11 @@ export const viewingKeysAtom = atomWithQuery<[string, string[]]>((get) => { }); export const shieldedSyncProgressAtom = atomFamily((_name: string) => - // total is 0 so we do not get NaN - atom({ status: "pending", current: 0, total: 0 }) + atom<{ + status: "pending" | "loading" | "success"; + current: number; + total: number; + }>({ status: "pending", current: 0, total: 0 }) ); export const shieldedBalanceAtom = atomWithQuery< diff --git a/apps/namadillo/src/atoms/balance/services.ts b/apps/namadillo/src/atoms/balance/services.ts index bda148375..63268bac8 100644 --- a/apps/namadillo/src/atoms/balance/services.ts +++ b/apps/namadillo/src/atoms/balance/services.ts @@ -3,7 +3,12 @@ import * as Comlink from "comlink"; import { Balance, SdkEvents } from "@namada/sdk/web"; import { getDefaultStore } from "jotai"; import { getSdkInstance } from "utils/sdk"; -import { Worker as ShieldedSyncWorkerApi } from "workers/ShieldedSyncWorker"; +import { + Events, + ProgressBarIncremented, + ProgressBarStarted, + Worker as ShieldedSyncWorkerApi, +} from "workers/ShieldedSyncWorker"; import ShieldedSyncWorker from "workers/ShieldedSyncWorker?worker"; // TODO: circular dependency import { shieldedSyncProgressAtom } from "./atoms"; @@ -18,6 +23,18 @@ export const fetchCoinPrices = async ( `${sqsOsmosisApi}/tokens/prices?base=${assetBaseList.sort((a, b) => a.localeCompare(b)).join(",")}` ).then((res) => res.json()); +const isStarted = (event: Events): event is ProgressBarStarted => { + return event.type === SdkEvents.ProgressBarStarted; +}; + +const isIncremented = (event: Events): event is ProgressBarIncremented => { + return event.type === SdkEvents.ProgressBarIncremented; +}; + +const isFinished = (event: Events): event is ProgressBarStarted => { + return event.type === SdkEvents.ProgressBarFinished; +}; + export const shieldedSync = async ( rpcUrl: string, maspIndexerUrl: string, @@ -27,17 +44,18 @@ export const shieldedSync = async ( const worker = new ShieldedSyncWorker(); const shieldedSyncWorker = Comlink.wrap(worker); - worker.onmessage = (event) => { + worker.onmessage = (event: MessageEvent) => { const store = getDefaultStore(); const { data } = event; - if (SdkEvents.ProgressBarStarted === data.type) { + if (isStarted(data)) { store.set(shieldedSyncProgressAtom(data.payload.name), { status: "loading", current: 0, total: 0, }); - } else if (SdkEvents.ProgressBarIncremented === data.type) { + } + if (isIncremented(data)) { const { name, current, total } = data.payload; store.set(shieldedSyncProgressAtom(name), { @@ -45,7 +63,8 @@ export const shieldedSync = async ( current, total, }); - } else if (SdkEvents.ProgressBarFinished === data.type) { + } + if (isFinished(data)) { store.set(shieldedSyncProgressAtom(data.payload.name), { status: "success", current: 0, diff --git a/apps/namadillo/src/workers/ShieldedSyncWorker.ts b/apps/namadillo/src/workers/ShieldedSyncWorker.ts index 98dfc51f5..69f142d75 100644 --- a/apps/namadillo/src/workers/ShieldedSyncWorker.ts +++ b/apps/namadillo/src/workers/ShieldedSyncWorker.ts @@ -3,13 +3,16 @@ import { getSdk, Sdk, SdkEvents } from "@namada/sdk/web"; import * as Comlink from "comlink"; import { Init, InitDone, Sync, SyncDone } from "./ShieldedSyncMessages"; +export type ProgressBarStarted = { type: string; payload: { name: string } }; +export type ProgressBarIncremented = { + type: string; + payload: { name: string; current: number; total: number }; +}; +export type ProgressBarFinished = { type: string; payload: { name: string } }; export type Events = - | { type: SdkEvents.ProgressBarStarted; payload: { name: string } } - | { - type: SdkEvents.ProgressBarIncremented; - payload: { name: string; current: number; total: number }; - } - | { type: SdkEvents.ProgressBarFinished; payload: { name: string } }; + | ProgressBarStarted + | ProgressBarIncremented + | ProgressBarFinished; export class Worker { private sdk: Sdk | undefined; diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index 904efbb4e..1dfbc02d5 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -10,11 +10,7 @@ import { Rpc } from "./rpc"; import { Signing } from "./signing"; import { Tx } from "./tx"; -export enum SdkEvents { - ProgressBarStarted = "namada_sdk::progress_bar::started", - ProgressBarIncremented = "namada_sdk::progress_bar::incremented", - ProgressBarFinished = "namada_sdk::progress_bar::finished", -} +export { SdkEvents } from "@namada/shared"; /** * API for interacting with Namada SDK @@ -120,6 +116,7 @@ export class Sdk { /** * Return SDK Package version + * @returns SDK version */ getVersion(): string { return packageJson.version; diff --git a/packages/shared/lib/src/query.rs b/packages/shared/lib/src/query.rs index 104edb8fd..2bdabd091 100644 --- a/packages/shared/lib/src/query.rs +++ b/packages/shared/lib/src/query.rs @@ -51,6 +51,35 @@ use crate::sdk::{ use crate::types::query::{ProposalInfo, WasmHash}; use crate::utils::{set_panic_hook, to_js_result}; +/// Progress bar names +pub const SDK_SCANNED_PROGRESS_BAR: &str = "namada_sdk::progress_bar::scanned"; +pub const SDK_FETCHED_PROGRESS_BAR: &str = "namada_sdk::progress_bar::fetched"; +pub const SDK_APPLIED_PROGRESS_BAR: &str = "namada_sdk::progress_bar::applied"; + +#[wasm_bindgen] +pub struct ProgressBarNames {} + +#[wasm_bindgen] +impl ProgressBarNames { + #[allow(non_snake_case)] + #[wasm_bindgen(getter)] + pub fn Scanned() -> String { + SDK_SCANNED_PROGRESS_BAR.to_string() + } + + #[allow(non_snake_case)] + #[wasm_bindgen(getter)] + pub fn Fetched() -> String { + SDK_FETCHED_PROGRESS_BAR.to_string() + } + + #[allow(non_snake_case)] + #[wasm_bindgen(getter)] + pub fn Applied() -> String { + SDK_APPLIED_PROGRESS_BAR.to_string() + } +} + enum MaspClient { Ledger(LedgerMaspClient), Indexer(IndexerMaspClient), @@ -330,9 +359,9 @@ impl Query { where C: NamadaMaspClient + Send + Sync + Unpin + 'static, { - let progress_bar_scanned = sync::ProgressBarWeb::new("scanned".to_string()); - let progress_bar_fetched = sync::ProgressBarWeb::new("fetched".to_string()); - let progress_bar_applied = sync::ProgressBarWeb::new("applied".to_string()); + let progress_bar_scanned = sync::ProgressBarWeb::new(SDK_SCANNED_PROGRESS_BAR); + let progress_bar_fetched = sync::ProgressBarWeb::new(SDK_FETCHED_PROGRESS_BAR); + let progress_bar_applied = sync::ProgressBarWeb::new(SDK_APPLIED_PROGRESS_BAR); let shutdown_signal_web = sync::ShutdownSignalWeb {}; let config = ShieldedSyncConfig::builder() diff --git a/packages/shared/lib/src/sdk/events.rs b/packages/shared/lib/src/sdk/events.rs index ee5609993..2b6434fb8 100644 --- a/packages/shared/lib/src/sdk/events.rs +++ b/packages/shared/lib/src/sdk/events.rs @@ -2,6 +2,7 @@ use crate::utils::to_js_result; use serde::Serialize; use wasm_bindgen::prelude::*; use web_sys::{CustomEvent, CustomEventInit, Document, Event, WorkerGlobalScope}; +use js_sys::Object; #[wasm_bindgen] #[derive(Debug, Serialize)] @@ -45,9 +46,33 @@ impl ProgressIncrement { } /// Event constants for use in Rust -const SDK_EVENT_PROGRESS_BAR_STARTED: &str = "namada_sdk::progress_bar::started"; -const SDK_EVENT_PROGRESS_BAR_INCREMENTED: &str = "namada_sdk::progress_bar::incremented"; -const SDK_EVENT_PROGRESS_BAR_FINISHED: &str = "namada_sdk::progress_bar::finished"; +pub const SDK_EVENT_PROGRESS_BAR_STARTED: &str = "namada_sdk::progress_bar::started"; +pub const SDK_EVENT_PROGRESS_BAR_INCREMENTED: &str = "namada_sdk::progress_bar::incremented"; +pub const SDK_EVENT_PROGRESS_BAR_FINISHED: &str = "namada_sdk::progress_bar::finished"; + +#[wasm_bindgen] +pub struct SdkEvents {} + +#[wasm_bindgen] +impl SdkEvents { + #[allow(non_snake_case)] + #[wasm_bindgen(getter)] + pub fn ProgressBarStarted() -> String { + SDK_EVENT_PROGRESS_BAR_STARTED.to_string() + } + + #[allow(non_snake_case)] + #[wasm_bindgen(getter)] + pub fn ProgressBarIncremented() -> String { + SDK_EVENT_PROGRESS_BAR_INCREMENTED.to_string() + } + + #[allow(non_snake_case)] + #[wasm_bindgen(getter)] + pub fn ProgressBarFinished() -> String { + SDK_EVENT_PROGRESS_BAR_FINISHED.to_string() + } +} pub struct EventDispatcher { scope: WorkerGlobalScope, diff --git a/packages/shared/lib/src/sdk/masp/sync.rs b/packages/shared/lib/src/sdk/masp/sync.rs index a17a39918..266e492d2 100644 --- a/packages/shared/lib/src/sdk/masp/sync.rs +++ b/packages/shared/lib/src/sdk/masp/sync.rs @@ -93,9 +93,9 @@ pub struct ProgressBarWeb { } impl ProgressBarWeb { - pub fn new(name: String) -> Self { + pub fn new(name: &str) -> Self { Self { - name, + name: name.to_string(), total: 0, current: 0, }