Skip to content

Commit

Permalink
general refactoring mainly regarding MobX actions + fixed error state
Browse files Browse the repository at this point in the history
  • Loading branch information
myxmaster committed Dec 15, 2024
1 parent d80bfc9 commit 7a8f318
Show file tree
Hide file tree
Showing 22 changed files with 1,696 additions and 1,887 deletions.
25 changes: 11 additions & 14 deletions lndmobile/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,20 +337,17 @@ export const subscribeChannelEvents = async (): Promise<string> => {
/**
* @throws
*/
export const exportAllChannelBackups =
async (): Promise<lnrpc.ChanBackupSnapshot> => {
const response = await sendCommand<
lnrpc.IChanBackupExportRequest,
lnrpc.ChanBackupExportRequest,
lnrpc.ChanBackupSnapshot
>({
request: lnrpc.ChanBackupExportRequest,
response: lnrpc.ChanBackupSnapshot,
method: 'ExportAllChannelBackups',
options: {}
});
return response;
};
export const exportAllChannelBackups = (): Promise<lnrpc.ChanBackupSnapshot> =>
sendCommand<
lnrpc.IChanBackupExportRequest,
lnrpc.ChanBackupExportRequest,
lnrpc.ChanBackupSnapshot
>({
request: lnrpc.ChanBackupExportRequest,
response: lnrpc.ChanBackupSnapshot,
method: 'ExportAllChannelBackups',
options: {}
});

/**
* @throws
Expand Down
78 changes: 42 additions & 36 deletions stores/ActivityStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, observable } from 'mobx';
import { action, observable, runInAction } from 'mobx';
import EncryptedStorage from 'react-native-encrypted-storage';

// LN
Expand Down Expand Up @@ -75,7 +75,6 @@ export default class ActivityStore {
this.invoicesStore = invoicesStore;
}

@action
public resetFilters = async () => {
this.filters = DEFAULT_FILTERS;
await EncryptedStorage.setItem(
Expand All @@ -85,7 +84,6 @@ export default class ActivityStore {
this.setFilters(this.filters);
};

@action
public setFiltersPos = async () => {
this.filters = {
lightning: true,
Expand Down Expand Up @@ -137,7 +135,7 @@ export default class ActivityStore {
this.setFilters(this.filters);
};

getSortedActivity = () => {
private getSortedActivity = () => {
const activity: any[] = [];
const payments = this.paymentsStore.payments;
const transactions = this.transactionsStore.transactions;
Expand All @@ -158,47 +156,53 @@ export default class ActivityStore {
return sortedActivity;
};

@action
public getActivity = async () => {
this.loading = true;
this.activity = [];
private getActivity = async () => {
runInAction(() => {
this.loading = true;
this.activity = [];
});

await this.paymentsStore.getPayments();
if (BackendUtils.supportsOnchainSends())
await this.transactionsStore.getTransactions();
await this.invoicesStore.getInvoices();

this.activity = this.getSortedActivity();
this.filteredActivity = this.activity;

this.loading = false;
runInAction(() => {
this.activity = this.getSortedActivity();
this.filteredActivity = this.activity;
this.loading = false;
});
};

@action
public updateInvoices = async (locale: string | undefined) => {
await this.invoicesStore.getInvoices();
this.activity = this.getSortedActivity();
await this.setFilters(this.filters, locale);
await runInAction(async () => {
this.activity = this.getSortedActivity();
await this.setFilters(this.filters, locale);
});
};

@action
public updateTransactions = async (locale: string | undefined) => {
if (BackendUtils.supportsOnchainSends())
await this.transactionsStore.getTransactions();
this.activity = this.getSortedActivity();
await this.setFilters(this.filters, locale);
await runInAction(async () => {
this.activity = this.getSortedActivity();
await this.setFilters(this.filters, locale);
});
};

@action
public async getFilters() {
this.loading = true;
try {
const filters = await EncryptedStorage.getItem(STORAGE_KEY);
if (filters) {
this.filters = JSON.parse(filters, (key, value) =>
(key === 'startDate' || key === 'endDate') && value
? new Date(value)
: value
);
runInAction(() => {
this.filters = JSON.parse(filters, (key, value) =>
(key === 'startDate' || key === 'endDate') && value
? new Date(value)
: value
);
});
} else {
console.log('No activity filters stored');
}
Expand All @@ -211,24 +215,26 @@ export default class ActivityStore {
return this.filters;
}

@action
public setFilters = async (filters: Filter, locale?: string) => {
this.loading = true;
this.filters = filters;
this.filteredActivity = ActivityFilterUtils.filterActivities(
this.activity,
filters
);
this.filteredActivity.forEach((activity) => {
if (activity instanceof Invoice) {
activity.determineFormattedRemainingTimeUntilExpiry(locale);
}
runInAction(() => {
this.loading = true;
this.filters = filters;
this.filteredActivity = ActivityFilterUtils.filterActivities(
this.activity,
filters
);
this.filteredActivity.forEach((activity) => {
if (activity instanceof Invoice) {
activity.determineFormattedRemainingTimeUntilExpiry(locale);
}
});
});

await EncryptedStorage.setItem(STORAGE_KEY, JSON.stringify(filters));

this.loading = false;
};

@action
public getActivityAndFilter = async (
locale: string | undefined,
filters: Filter = this.filters
Expand Down
142 changes: 72 additions & 70 deletions stores/BalanceStore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, reaction, observable } from 'mobx';
import { action, reaction, observable, runInAction } from 'mobx';
import BigNumber from 'bignumber.js';

import SettingsStore from './SettingsStore';
Expand Down Expand Up @@ -32,7 +32,8 @@ export default class BalanceStore {
);
}

reset = () => {
@action
public reset = () => {
this.resetLightningBalance();
this.resetBlockchainBalance();
this.error = false;
Expand All @@ -47,49 +48,51 @@ export default class BalanceStore {
this.loadingBlockchainBalance = false;
};

resetLightningBalance = () => {
private resetLightningBalance = () => {
this.pendingOpenBalance = 0;
this.lightningBalance = 0;
this.loadingLightningBalance = false;
};

balanceError = () => {
@action
private balanceError = () => {
this.error = true;
this.loadingBlockchainBalance = false;
this.loadingLightningBalance = false;
};

@action
public getBlockchainBalance = (set: boolean, reset: boolean) => {
this.loadingBlockchainBalance = true;
public getBlockchainBalance = async (set: boolean, reset: boolean) => {
if (reset) this.resetBlockchainBalance();
return BackendUtils.getBlockchainBalance({})
.then((data: any) => {
// process external accounts
const accounts = data?.account_balance;

const unconfirmedBlockchainBalance = Number(
accounts?.default
? accounts.default.unconfirmed_balance || 0
: data.unconfirmed_balance || 0
);

const confirmedBlockchainBalance = Number(
accounts?.default
? accounts?.default.confirmed_balance || 0
: data.confirmed_balance || 0
);

const totalBlockchainBalance = new BigNumber(
unconfirmedBlockchainBalance
)
.plus(confirmedBlockchainBalance)
.toNumber();

const totalBlockchainBalanceAccounts = Number(
data.total_balance || 0
);

this.loadingBlockchainBalance = true;
try {
const data = await BackendUtils.getBlockchainBalance({});
// process external accounts
const accounts = data?.account_balance;

const unconfirmedBlockchainBalance = Number(
accounts?.default
? accounts.default.unconfirmed_balance || 0
: data.unconfirmed_balance || 0
);

const confirmedBlockchainBalance = Number(
accounts?.default
? accounts?.default.confirmed_balance || 0
: data.confirmed_balance || 0
);

const totalBlockchainBalance = new BigNumber(
unconfirmedBlockchainBalance
)
.plus(confirmedBlockchainBalance)
.toNumber();

const totalBlockchainBalanceAccounts = Number(
data.total_balance || 0
);

runInAction(() => {
if (set) {
if (accounts && accounts.default && data.confirmed_balance)
delete accounts.default;
Expand All @@ -104,44 +107,43 @@ export default class BalanceStore {
totalBlockchainBalanceAccounts;
}
this.loadingBlockchainBalance = false;
return {
unconfirmedBlockchainBalance,
confirmedBlockchainBalance,
totalBlockchainBalance,
accounts
};
})
.catch(() => {
this.balanceError();
});
return {
unconfirmedBlockchainBalance,
confirmedBlockchainBalance,
totalBlockchainBalance,
accounts
};
} catch {
this.balanceError();
}
};

@action
public getLightningBalance = (set: boolean, reset?: boolean) => {
this.loadingLightningBalance = true;
public getLightningBalance = async (set: boolean, reset?: boolean) => {
if (reset) this.resetLightningBalance();
return BackendUtils.getLightningBalance()
.then((data: any) => {
const pendingOpenBalance = Number(
data.pending_open_balance || 0
);
const lightningBalance = Number(data.balance || 0);
this.loadingLightningBalance = true;
try {
const data = await BackendUtils.getLightningBalance();
const pendingOpenBalance = Number(data.pending_open_balance || 0);
const lightningBalance = Number(data.balance || 0);

runInAction(() => {
if (set) {
this.pendingOpenBalance = pendingOpenBalance;
this.lightningBalance = lightningBalance;
}

this.loadingLightningBalance = false;

return {
pendingOpenBalance,
lightningBalance
};
})
.catch(() => {
this.balanceError();
});

return {
pendingOpenBalance,
lightningBalance
};
} catch {
this.balanceError();
}
};

@action
Expand All @@ -150,18 +152,18 @@ export default class BalanceStore {
const lightning = await this.getLightningBalance(false);
const onChain = await this.getBlockchainBalance(false, false);

// LN
this.pendingOpenBalance =
(lightning && lightning.pendingOpenBalance) || 0;
this.lightningBalance = (lightning && lightning.lightningBalance) || 0;
// on-chain
this.otherAccounts = (onChain && onChain.accounts) || [];
this.unconfirmedBlockchainBalance =
(onChain && onChain.unconfirmedBlockchainBalance) || 0;
this.confirmedBlockchainBalance =
(onChain && onChain.confirmedBlockchainBalance) || 0;
this.totalBlockchainBalance =
(onChain && onChain.totalBlockchainBalance) || 0;
runInAction(() => {
// LN
this.pendingOpenBalance = lightning?.pendingOpenBalance || 0;
this.lightningBalance = lightning?.lightningBalance || 0;
// on-chain
this.otherAccounts = onChain?.accounts || [];
this.unconfirmedBlockchainBalance =
onChain?.unconfirmedBlockchainBalance || 0;
this.confirmedBlockchainBalance =
onChain?.confirmedBlockchainBalance || 0;
this.totalBlockchainBalance = onChain?.totalBlockchainBalance || 0;
});

return {
onChain,
Expand Down
Loading

0 comments on commit 7a8f318

Please sign in to comment.