Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): use PostMessageDataResponseWallet for BTC wallet [GIX-2795] #2549

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/frontend/src/btc/schedulers/btc-wallet.scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getBtcBalance } from '$lib/api/signer.api';
import { WALLET_TIMER_INTERVAL_MILLIS } from '$lib/constants/app.constants';
import { SchedulerTimer, type Scheduler, type SchedulerJobData } from '$lib/schedulers/scheduler';
import type { BitcoinTransaction } from '$lib/types/blockchain';
import type {
PostMessageDataRequestBtc,
PostMessageDataResponseBtcWallet
PostMessageDataResponseWallet
} from '$lib/types/post-message';
import { assertNonNullish } from '@dfinity/utils';
import { assertNonNullish, jsonReplacer } from '@dfinity/utils';

export class BtcWalletScheduler implements Scheduler<PostMessageDataRequestBtc> {
private timer = new SchedulerTimer('syncBtcWalletStatus');
Expand Down Expand Up @@ -44,18 +45,22 @@ export class BtcWalletScheduler implements Scheduler<PostMessageDataRequestBtc>
network: bitcoinNetwork
});

// TODO: Fetch and parse transactions
const transactions: BitcoinTransaction[] = [];

this.postMessageWallet({
wallet: {
balance: {
data: balance,
certified: true
}
},
newTransactions: JSON.stringify(transactions, jsonReplacer)
}
});
};

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