Skip to content

Commit

Permalink
fix(backup): add boosts & transfers to backup
Browse files Browse the repository at this point in the history
  • Loading branch information
pwltr committed Nov 26, 2024
1 parent 0fbc9c3 commit 0f96482
Show file tree
Hide file tree
Showing 16 changed files with 389 additions and 171 deletions.
72 changes: 72 additions & 0 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
reduceValue,
timeAgo,
isObjPartialMatch,
deepCompareStructure,
ellipsis,
generateCalendar,
getDurationForBlocks,
Expand Down Expand Up @@ -160,6 +161,77 @@ describe('isObjPartialMatch', () => {
});
});

describe('deepCompareStructure', () => {
it('can perform match', () => {
const f = deepCompareStructure;
expect(f({}, {})).toEqual(true);
expect(f({}, [])).toEqual(false);
expect(f({ a: 1 }, {})).toEqual(false);
expect(f({ a: 1 }, { a: 2 })).toEqual(true);
expect(f({ a: 1 }, { b: 1 })).toEqual(false);
expect(f({ a: { b: 1 } }, { a: { b: 2 } })).toEqual(true);
expect(f({ a: { b: 1 } }, { a: { c: 1 } })).toEqual(false);
expect(f({ a: 1 }, { a: [] })).toEqual(false);
expect(f({ a: { b: [] } }, { a: { b: [] } })).toEqual(true);
expect(f({ a: { b: 1 } }, { a: { b: [] } })).toEqual(false);

const received = {
boostedTransactions: {
bitcoin: {},
bitcoinTestnet: {},
bitcoinRegtest: {
fff9398e30329ab0d4ae227c017b9c11537d6fadede4df402d3ae9bb854816f5: {
parentTransactions: [
'fff9398e30329ab0d4ae227c017b9c11537d6fadede4df402d3ae9bb854816f5',
],
childTransaction:
'ee459c02101cad9dbab8d0fc2fe55026130e7db4d88ca8892b9003167c787fa1',
type: 'cpfp',
fee: 664,
},
'415098a69d7b1c93b31b14625c4b7663a4bdeee5f15c5982083ac1c4ec14717b': {
parentTransactions: [
'415098a69d7b1c93b31b14625c4b7663a4bdeee5f15c5982083ac1c4ec14717b',
],
childTransaction:
'f7f0d6184818a9588633be608dc4d8f3510708c5946bea330c663a0bf8c334a2',
type: 'cpfp',
fee: 664,
},
},
},
transfers: {
bitcoin: [],
bitcoinTestnet: [],
bitcoinRegtest: [
{
txId: '67a7108cd434d8580a0295517df0c740b59e84e875284ac139717e4dda4da0f8',
type: 'open',
status: 'pending',
orderId: '5f95e1f5-26f9-4fb2-82e6-9ae602764d3b',
amount: 17602,
},
],
},
};

const expected = {
boostedTransactions: {
bitcoin: {},
bitcoinTestnet: {},
bitcoinRegtest: {},
},
transfers: {
bitcoin: [],
bitcoinTestnet: [],
bitcoinRegtest: [],
},
};

expect(f(received, expected, 1)).toEqual(true);
});
});

describe('calendar', () => {
it('can generate calendar', () => {
const date = new Date(Date.UTC(2020, 11, 31, 23, 59, 59));
Expand Down
6 changes: 3 additions & 3 deletions src/screens/Settings/AppStatus/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
isOnlineSelector,
} from '../../../store/reselect/ui';
import { TBackupItem } from '../../../store/types/backup';
import { EBackupCategories } from '../../../store/utils/backup';
import { EBackupCategory } from '../../../store/utils/backup';
import { IColors } from '../../../styles/colors';
import { ScrollView, View as ThemedView } from '../../../styles/components';
import {
Expand Down Expand Up @@ -147,7 +147,7 @@ const AppStatus = ({}: SettingsScreenProps<'AppStatus'>): ReactElement => {
);
};

return Object.values(EBackupCategories).every((key) => {
return Object.values(EBackupCategory).every((key) => {
return isSyncOk(backup[key]);
});
}, [backup, now]);
Expand All @@ -157,7 +157,7 @@ const AppStatus = ({}: SettingsScreenProps<'AppStatus'>): ReactElement => {
if (!isBackupSyncOk) {
return { state: 'error' };
}
const syncTimes = Object.values(EBackupCategories).map((key) => {
const syncTimes = Object.values(EBackupCategory).map((key) => {
return backup[key].synced;
});
const max = Math.max(...syncTimes);
Expand Down
4 changes: 2 additions & 2 deletions src/screens/Settings/Backup/Metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useAppDispatch, useAppSelector } from '../../../hooks/redux';
import { closeSheet } from '../../../store/slices/ui';
import { backupSelector } from '../../../store/reselect/backup';
import { i18nTime } from '../../../utils/i18n';
import { EBackupCategories } from '../../../store/utils/backup';
import { EBackupCategory } from '../../../store/utils/backup';

const imageSrc = require('../../../assets/illustrations/card.png');

Expand All @@ -22,7 +22,7 @@ const Metadata = (): ReactElement => {
const backup = useAppSelector(backupSelector);

const max = Math.max(
...Object.values(EBackupCategories).map((key) => {
...Object.values(EBackupCategory).map((key) => {
return backup[key].synced;
}),
);
Expand Down
37 changes: 22 additions & 15 deletions src/screens/Settings/BackupSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { backupSelector } from '../../../store/reselect/backup';
import { lightningBackupSelector } from '../../../store/reselect/lightning';
import { forceBackup } from '../../../store/slices/backup';
import { TBackupItem } from '../../../store/types/backup';
import { EBackupCategories } from '../../../store/utils/backup';
import { EBackupCategory } from '../../../store/utils/backup';
import { toggleBottomSheet } from '../../../store/utils/ui';
import {
ArrowClockwise,
Expand All @@ -21,6 +21,7 @@ import {
RectanglesTwo,
SettingsIcon,
TagIcon,
TimerIconAlt,
TransferIcon,
UsersIcon,
} from '../../../styles/icons';
Expand All @@ -38,7 +39,7 @@ const Status = ({
Icon: React.FunctionComponent<any>;
title: ReactNode;
status: TBackupItem;
category?: EBackupCategories;
category?: EBackupCategory;
disableRetry?: boolean;
}): ReactElement => {
const { t } = useTranslation('settings');
Expand Down Expand Up @@ -124,7 +125,7 @@ const Status = ({
type TBackupCategory = {
Icon: React.FunctionComponent<any>;
title: string;
category?: EBackupCategories;
category?: EBackupCategory;
status: TBackupItem;
disableRetry?: boolean;
};
Expand Down Expand Up @@ -154,32 +155,38 @@ const BackupSettings = ({
{
Icon: NoteIcon,
title: t('backup.category_connection_receipts'),
category: EBackupCategories.blocktank,
status: backup[EBackupCategories.blocktank],
category: EBackupCategory.blocktank,
status: backup[EBackupCategory.blocktank],
},
{
Icon: TransferIcon,
title: t('backup.category_transaction_log'),
category: EBackupCategories.ldkActivity,
status: backup[EBackupCategories.ldkActivity],
category: EBackupCategory.ldkActivity,
status: backup[EBackupCategory.ldkActivity],
},
{
Icon: TimerIconAlt,
title: t('backup.category_wallet'),
category: EBackupCategory.wallet,
status: backup[EBackupCategory.wallet],
},
{
Icon: SettingsIcon,
title: t('backup.category_settings'),
category: EBackupCategories.settings,
status: backup[EBackupCategories.settings],
category: EBackupCategory.settings,
status: backup[EBackupCategory.settings],
},
{
Icon: RectanglesTwo,
title: t('backup.category_widgets'),
category: EBackupCategories.widgets,
status: backup[EBackupCategories.widgets],
category: EBackupCategory.widgets,
status: backup[EBackupCategory.widgets],
},
{
Icon: TagIcon,
title: t('backup.category_tags'),
category: EBackupCategories.metadata,
status: backup[EBackupCategories.metadata],
category: EBackupCategory.metadata,
status: backup[EBackupCategory.metadata],
},
// {
// Icon: UserRectangleIcon,
Expand All @@ -190,8 +197,8 @@ const BackupSettings = ({
{
Icon: UsersIcon,
title: t('backup.category_contacts'),
category: EBackupCategories.slashtags,
status: backup[EBackupCategories.slashtags],
category: EBackupCategory.slashtags,
status: backup[EBackupCategory.slashtags],
},
];

Expand Down
15 changes: 8 additions & 7 deletions src/store/shapes/backup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TBackupItem, TBackupState } from '../types/backup';
import { EBackupCategories } from '../utils/backup';
import { EBackupCategory } from '../utils/backup';

const item: TBackupItem = {
required: Date.now() - 1000,
Expand All @@ -8,10 +8,11 @@ const item: TBackupItem = {
};

export const initialBackupState: TBackupState = {
[EBackupCategories.widgets]: { ...item },
[EBackupCategories.settings]: { ...item },
[EBackupCategories.metadata]: { ...item },
[EBackupCategories.blocktank]: { ...item },
[EBackupCategories.slashtags]: { ...item },
[EBackupCategories.ldkActivity]: { ...item },
[EBackupCategory.wallet]: { ...item },
[EBackupCategory.widgets]: { ...item },
[EBackupCategory.settings]: { ...item },
[EBackupCategory.metadata]: { ...item },
[EBackupCategory.blocktank]: { ...item },
[EBackupCategory.slashtags]: { ...item },
[EBackupCategory.ldkActivity]: { ...item },
};
45 changes: 34 additions & 11 deletions src/store/slices/backup.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PayloadAction, createSlice } from '@reduxjs/toolkit';

import { initialBackupState } from '../shapes/backup';
import { EBackupCategories } from '../utils/backup';
import { EBackupCategory } from '../utils/backup';
import { addPaidBlocktankOrder, updateBlocktankOrder } from './blocktank';
import {
addLastUsedTag,
Expand All @@ -17,6 +17,7 @@ import {
} from './metadata';
import { updateSettings } from './settings';
import { addContact, addContacts, deleteContact } from './slashtags';
import { addTransfer, removeTransfer, updateTransfer } from './wallet';
import { setFeedWidget, deleteWidget } from './widgets';
import { updateActivityItems } from './activity';
import { EActivityType } from '../types/activity';
Expand All @@ -26,40 +27,58 @@ export const backupSlice = createSlice({
initialState: initialBackupState,
reducers: {
resetBackupState: () => initialBackupState,
backupStart: (state, action: PayloadAction<{ category: string }>) => {
requireBackup: (state, action: PayloadAction<EBackupCategory>) => {
state[action.payload].required = Date.now();
},
backupStart: (
state,
action: PayloadAction<{ category: EBackupCategory }>,
) => {
const { category } = action.payload;
state[category].running = true;
},
backupSuccess: (state, action: PayloadAction<{ category: string }>) => {
backupSuccess: (
state,
action: PayloadAction<{ category: EBackupCategory }>,
) => {
const { category } = action.payload;
state[category].running = false;
state[category].synced = Date.now();
},
backupError: (state, action: PayloadAction<{ category: string }>) => {
backupError: (
state,
action: PayloadAction<{ category: EBackupCategory }>,
) => {
const { category } = action.payload;
state[category].running = false;
},
forceBackup: (state, action: PayloadAction<{ category: string }>) => {
forceBackup: (
state,
action: PayloadAction<{ category: EBackupCategory }>,
) => {
const { category } = action.payload;
state[category].required = Date.now();
state[category].running = true;
},
},
extraReducers: (builder) => {
const blocktankReducer = (state): void => {
state[EBackupCategories.blocktank].required = Date.now();
state[EBackupCategory.blocktank].required = Date.now();
};
const metadataReducer = (state): void => {
state[EBackupCategories.metadata].required = Date.now();
state[EBackupCategory.metadata].required = Date.now();
};
const settingsReducer = (state): void => {
state[EBackupCategories.settings].required = Date.now();
state[EBackupCategory.settings].required = Date.now();
};
const slashtagsReducer = (state): void => {
state[EBackupCategories.slashtags].required = Date.now();
state[EBackupCategory.slashtags].required = Date.now();
};
const walletReducer = (state): void => {
state[EBackupCategory.wallet].required = Date.now();
};
const widgetsReducer = (state): void => {
state[EBackupCategories.widgets].required = Date.now();
state[EBackupCategory.widgets].required = Date.now();
};

builder
Expand All @@ -79,6 +98,9 @@ export const backupSlice = createSlice({
.addCase(addContact, slashtagsReducer)
.addCase(addContacts, slashtagsReducer)
.addCase(deleteContact, slashtagsReducer)
.addCase(addTransfer, walletReducer)
.addCase(updateTransfer, walletReducer)
.addCase(removeTransfer, walletReducer)
.addCase(setFeedWidget, widgetsReducer)
.addCase(deleteWidget, widgetsReducer)
.addCase(updateActivityItems, (state, action) => {
Expand All @@ -87,7 +109,7 @@ export const backupSlice = createSlice({
(item) => item.activityType === EActivityType.lightning,
);
if (hasLnActivity) {
state[EBackupCategories.ldkActivity].required = Date.now();
state[EBackupCategory.ldkActivity].required = Date.now();
}
});
},
Expand All @@ -97,6 +119,7 @@ const { actions, reducer } = backupSlice;

export const {
resetBackupState,
requireBackup,
backupStart,
backupSuccess,
backupError,
Expand Down
Loading

0 comments on commit 0f96482

Please sign in to comment.