Skip to content

Commit

Permalink
feat(frontend): use PostMessageDataResponseWallet for BTC wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysKarmazynDFINITY committed Sep 26, 2024
1 parent 31f5bdf commit 54a9afd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
10 changes: 6 additions & 4 deletions src/frontend/src/btc/schedulers/btc-wallet.scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WALLET_TIMER_INTERVAL_MILLIS } from '$lib/constants/app.constants';
import { SchedulerTimer, type Scheduler, type SchedulerJobData } from '$lib/schedulers/scheduler';
import type {
PostMessageDataRequestBtc,
PostMessageDataResponseBtcWallet
PostMessageDataResponseWallet
} from '$lib/types/post-message';
import { assertNonNullish } from '@dfinity/utils';

Expand Down Expand Up @@ -49,13 +49,15 @@ export class BtcWalletScheduler implements Scheduler<PostMessageDataRequestBtc>
balance: {
data: balance,
certified: true
}
},
// TODO: Send parsed transactions
newTransactions: ''
}
});
};

private postMessageWallet(data: PostMessageDataResponseBtcWallet) {
this.timer.postMsg<PostMessageDataResponseBtcWallet>({
private postMessageWallet(data: PostMessageDataResponseWallet) {
this.timer.postMsg<PostMessageDataResponseWallet>({
msg: 'syncBtcWallet',
data
});
Expand Down
4 changes: 2 additions & 2 deletions src/frontend/src/btc/services/btc-listener.services.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { balancesStore } from '$lib/stores/balances.store';
import type { PostMessageDataResponseBtcWallet } from '$lib/types/post-message';
import type { PostMessageDataResponseWallet } from '$lib/types/post-message';
import type { TokenId } from '$lib/types/token';
import { BigNumber } from '@ethersproject/bignumber';

export const syncWallet = ({
data,
tokenId
}: {
data: PostMessageDataResponseBtcWallet;
data: PostMessageDataResponseWallet;
tokenId: TokenId;
}) => {
const {
Expand Down
8 changes: 3 additions & 5 deletions src/frontend/src/btc/services/worker.btc-wallet.services.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { syncWallet } from '$btc/services/btc-listener.services';
import { isNetworkIdBTCRegtest, isNetworkIdBTCTestnet } from '$icp/utils/ic-send.utils';
import type { WalletWorker } from '$lib/types/listener';
import type { PostMessage, PostMessageDataResponseBtcWallet } from '$lib/types/post-message';
import type { PostMessage, PostMessageDataResponseWallet } from '$lib/types/post-message';
import type { Token } from '$lib/types/token';
import { mapToSignerBitcoinNetwork } from '$lib/utils/network.utils';

Expand All @@ -12,16 +12,14 @@ export const initBtcWalletWorker = async ({
const WalletWorker = await import('$btc/workers/btc-wallet.worker?worker');
const worker: Worker = new WalletWorker.default();

worker.onmessage = async ({
data
}: MessageEvent<PostMessage<PostMessageDataResponseBtcWallet>>) => {
worker.onmessage = async ({ data }: MessageEvent<PostMessage<PostMessageDataResponseWallet>>) => {
const { msg } = data;

switch (msg) {
case 'syncBtcWallet':
syncWallet({
tokenId,
data: data.data as PostMessageDataResponseBtcWallet
data: data.data as PostMessageDataResponseWallet
});
return;
}
Expand Down
9 changes: 2 additions & 7 deletions src/frontend/src/lib/types/post-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,15 @@ export interface PostMessageDataResponseExchangeError extends PostMessageDataRes
// Transactions & {certified: boolean}
type JsonTransactionsText = string;

type PostMessageWalletData<T = unknown> = Omit<T, 'transactions' | 'balance'> & {
type PostMessageWalletData<T> = Omit<T, 'transactions' | 'balance'> & {
balance: CertifiedData<bigint>;
newTransactions: JsonTransactionsText;
};

export interface PostMessageDataResponseWallet<T> extends PostMessageDataResponse {
export interface PostMessageDataResponseWallet<T = unknown> extends PostMessageDataResponse {
wallet: PostMessageWalletData<T>;
}

// TODO: use common PostMessageDataResponseWallet interface after BTC transactions added to the worker
export interface PostMessageDataResponseBtcWallet extends PostMessageDataResponse {
wallet: Omit<PostMessageWalletData, 'newTransactions'>;
}

export interface PostMessageDataResponseError extends PostMessageDataResponse {
error: unknown;
}
Expand Down

0 comments on commit 54a9afd

Please sign in to comment.