Skip to content

Commit

Permalink
chore: use better typings
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Nov 15, 2024
1 parent 30eb9b7 commit ad2d8b5
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 34 deletions.
19 changes: 11 additions & 8 deletions apps/namadillo/src/App/Masp/ShieldedBalanceChart.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);

Expand Down Expand Up @@ -63,9 +66,9 @@ export const ShieldedBalanceChart = (): JSX.Element => {
}
<div className="absolute top-0 right-0 text-right">
Shieled sync progress: <br />
<Progress name="Scanned" {...shieledSyncProgress} />
<Progress name="Fetched" {...shieledSyncProgress2} />
<Progress name="Applied" {...shieledSyncProgress3} />
<Progress name="Scanned" {...shieledSyncProgressScanned} />
<Progress name="Fetched" {...shieledSyncProgressFetched} />
<Progress name="Applied" {...shieledSyncProgressApplied} />
</div>
</AtomErrorBoundary>
</div>
Expand Down
7 changes: 5 additions & 2 deletions apps/namadillo/src/atoms/balance/atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down
29 changes: 24 additions & 5 deletions apps/namadillo/src/atoms/balance/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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,
Expand All @@ -27,25 +44,27 @@ export const shieldedSync = async (
const worker = new ShieldedSyncWorker();
const shieldedSyncWorker = Comlink.wrap<ShieldedSyncWorkerApi>(worker);

worker.onmessage = (event) => {
worker.onmessage = (event: MessageEvent<Events>) => {
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), {
status: "loading",
current,
total,
});
} else if (SdkEvents.ProgressBarFinished === data.type) {
}
if (isFinished(data)) {
store.set(shieldedSyncProgressAtom(data.payload.name), {
status: "success",
current: 0,
Expand Down
15 changes: 9 additions & 6 deletions apps/namadillo/src/workers/ShieldedSyncWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -120,6 +116,7 @@ export class Sdk {

/**
* Return SDK Package version
* @returns SDK version
*/
getVersion(): string {
return packageJson.version;
Expand Down
35 changes: 32 additions & 3 deletions packages/shared/lib/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpClient>),
Indexer(IndexerMaspClient),
Expand Down Expand Up @@ -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()
Expand Down
31 changes: 28 additions & 3 deletions packages/shared/lib/src/sdk/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions packages/shared/lib/src/sdk/masp/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit ad2d8b5

Please sign in to comment.