Skip to content

Commit

Permalink
refactor: get selected wallet and network by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ovitrif committed Feb 16, 2024
1 parent b945907 commit 238d40b
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 663 deletions.
49 changes: 8 additions & 41 deletions src/store/actions/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,22 +202,15 @@ export const updateWalletBalance = ({
* @returns {Result<string>}
*/
export const addUnconfirmedTransactions = ({
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
transactions,
}: {
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
transactions: IFormattedTransactions;
}): Result<string> => {
try {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}

let unconfirmedTransactions: IFormattedTransactions = {};
Object.keys(transactions).forEach((key) => {
const confirmations = blockHeightToConfirmations({
Expand Down Expand Up @@ -261,22 +254,15 @@ export const addUnconfirmedTransactions = ({
export const injectFakeTransaction = ({
id = 'fake-transaction',
fakeTx,
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
id?: string;
fakeTx?: IFormattedTransactions;
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
}): Result<string> => {
try {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}

const fakeTransaction = fakeTx ?? getFakeTransaction(id);

const payload = {
Expand Down Expand Up @@ -440,13 +426,10 @@ export const addBoostedTransaction = async ({
* This resets a given wallet to defaultWalletShape
*/
export const resetSelectedWallet = async ({
selectedWallet,
selectedWallet = getSelectedWallet(),
}: {
selectedWallet?: TWalletName;
}): Promise<void> => {
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}
dispatch({
type: actions.RESET_SELECTED_WALLET,
payload: { selectedWallet },
Expand Down Expand Up @@ -687,14 +670,11 @@ const txSpeedToFeeId = (txSpeed: ETransactionSpeed): EFeeId => {
*/
export const updateHeader = ({
header,
selectedNetwork,
selectedNetwork = getSelectedNetwork(),
}: {
header: IHeader;
selectedNetwork?: EAvailableNetwork;
}): void => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
const payload = {
header,
selectedNetwork,
Expand Down Expand Up @@ -724,22 +704,15 @@ export const resetExchangeRates = (): Result<string> => {
* @returns {Promise<Result<string>>}
*/
export const replaceImpactedAddresses = async ({
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
impactedAddresses,
}: {
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
impactedAddresses: TGetImpactedAddressesRes; // Retrieved from getImpactedAddresses
}): Promise<Result<string>> => {
try {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}

const { currentWallet } = getCurrentWallet({
selectedWallet,
selectedNetwork,
Expand All @@ -757,9 +730,6 @@ export const replaceImpactedAddresses = async ({
impactedAddresses.impactedChangeAddresses;

allImpactedAddresses.map(({ addressType, addresses }) => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
addresses.map((impactedAddress) => {
const invalidScriptHash = impactedAddress.storedAddress.scriptHash;
const validScriptHash = impactedAddress.generatedAddress.scriptHash;
Expand All @@ -780,9 +750,6 @@ export const replaceImpactedAddresses = async ({
});

allImpactedChangeAddresses.map(({ addressType, addresses }) => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
addresses.map((impactedAddress) => {
const invalidScriptHash = impactedAddress.storedAddress.scriptHash;
const validScriptHash = impactedAddress.generatedAddress.scriptHash;
Expand Down
11 changes: 2 additions & 9 deletions src/store/utils/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,11 @@ export enum EBackupCategories {

export const performLdkRestore = async ({
backupServerDetails,
selectedNetwork,
selectedNetwork = getSelectedNetwork(),
}: {
backupServerDetails: TBackupServerDetails;
selectedNetwork?: EAvailableNetwork;
}): Promise<Result<{ backupExists: boolean }>> => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}

const storageRes = await setLdkStoragePath();
if (storageRes.isErr()) {
return err(storageRes.error);
Expand Down Expand Up @@ -140,15 +136,12 @@ export const performLdkRestore = async ({

export const performLdkRestoreDeprecated = async ({
slashtag,
selectedNetwork,
selectedNetwork = getSelectedNetwork(),
}: {
slashtag: Slashtag;
selectedNetwork?: EAvailableNetwork;
}): Promise<Result<{ backupExists: boolean }>> => {
console.warn(`Restoring ${selectedNetwork} from deprecated backup server.`);
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
const res = await listBackups(
slashtag,
EBackupCategoriesOld.ldkComplete,
Expand Down
22 changes: 4 additions & 18 deletions src/store/utils/blocktank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ export const startChannelPurchase = async ({
couponCode,
turboChannel = true,
zeroConfPayment = false,
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
remoteBalance: number;
localBalance: number;
Expand All @@ -252,13 +252,6 @@ export const startChannelPurchase = async ({
transactionFeeEstimate: number;
}>
> => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}

const buyChannelResponse = await createOrder({
lspBalanceSat: localBalance,
channelExpiryWeeks: channelExpiry,
Expand Down Expand Up @@ -355,20 +348,13 @@ export const startChannelPurchase = async ({
*/
export const confirmChannelPurchase = async ({
order,
selectedNetwork,
selectedWallet,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
order: IBtOrder;
selectedNetwork?: EAvailableNetwork;
selectedWallet?: TWalletName;
}): Promise<Result<{ txid: string; useUnconfirmedInputs: boolean }>> => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}

const rawTx = await createTransaction();
if (rawTx.isErr()) {
showToast({
Expand Down
41 changes: 8 additions & 33 deletions src/store/utils/lightning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,12 @@ export const updateLightningNodeVersionThunk = async (): Promise<
* @param {EAvailableNetwork} [selectedNetwork]
*/
export const updateLightningChannelsThunk = async ({
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: {
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
}): Promise<Result<string>> => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}
const lightningChannelsRes = await getLightningChannels();
if (lightningChannelsRes.isErr()) {
return err(lightningChannelsRes.error.message);
Expand Down Expand Up @@ -194,15 +188,9 @@ export const createLightningInvoice = async ({
amountSats,
description,
expiryDeltaSeconds,
selectedNetwork,
selectedWallet,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
}: TCreateLightningInvoice): Promise<Result<TInvoice>> => {
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}
const invoice = await createPaymentRequest({
amountSats,
description,
Expand All @@ -224,21 +212,14 @@ export const createLightningInvoice = async ({
* @param {string} peer
*/
export const savePeer = ({
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
peer,
}: {
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
peer: string;
}): Result<string> => {
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}

if (!peer) {
return err('The peer data appears to be invalid.');
}
Expand Down Expand Up @@ -272,20 +253,14 @@ export const savePeer = ({
* @returns {Result<string>}
*/
export const removePeer = ({
selectedWallet,
selectedNetwork,
selectedWallet = getSelectedWallet(),
selectedNetwork = getSelectedNetwork(),
peer,
}: {
selectedWallet?: TWalletName;
selectedNetwork?: EAvailableNetwork;
peer: string;
}): Result<string> => {
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}
if (!selectedNetwork) {
selectedNetwork = getSelectedNetwork();
}
if (!peer) {
return err('The peer data appears to be invalid.');
}
Expand Down
6 changes: 1 addition & 5 deletions src/store/utils/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { TWalletName } from '../types/wallet';
* @return {Promise<Result<string>>}
*/
export const wipeApp = async ({
selectedWallet,
selectedWallet = getSelectedWallet(),
showNotification = true,
restartApp = true,
}: {
Expand All @@ -30,10 +30,6 @@ export const wipeApp = async ({
restartApp?: boolean;
} = {}): Promise<Result<string>> => {
try {
if (!selectedWallet) {
selectedWallet = getSelectedWallet();
}

// Reset Redux stores & persisted storage
dispatch({ type: actions.WIPE_APP });

Expand Down
Loading

0 comments on commit 238d40b

Please sign in to comment.