Skip to content

Commit

Permalink
fix: network settings processing on wallet (#465)
Browse files Browse the repository at this point in the history
fix: wallet behavior consistency with custom network settings

chore: remove unused import
  • Loading branch information
alexruzenhack committed May 15, 2024
1 parent 1284d1f commit b88fa51
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,8 +913,8 @@ export const networkSettingsUpdateState = (customNetwork) => ({
* network: string,
* nodeUrl: string,
* explorerUrl: string,
* txMiningServiceUrl: string,
* explorerServiceUrl: string,
* txMiningServiceUrl: string,
* walletServiceUrl?: string
* walletServiceWsUrl?: string
* }} customNetwork Settings to persist
Expand Down
12 changes: 12 additions & 0 deletions src/reducers/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,18 @@ const initialState = {
featureToggles: {
...FEATURE_TOGGLE_DEFAULTS,
},
/**
* @param {{
* stage: string;
* network: string;
* nodeUrl: string;
* explorerUrl: string;
* explorerServiceUrl: string;
* txMiningServiceUrl: string;
* walletServiceUrl: string;
* walletServiceWsUrl: string;
* }}
*/
networkSettings: PRE_SETTINGS_MAINNET,
networkSettingsInvalid: {},
networkSettingsStatus: NETWORKSETTINGS_STATUS.READY,
Expand Down
26 changes: 11 additions & 15 deletions src/sagas/networkSettings.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
networkSettingsPersistStore,
networkSettingsUpdateInvalid,
networkSettingsUpdateFailure,
networkSettingsUpdateState,
networkSettingsUpdateSuccess,
networkSettingsUpdateWaiting,
types,
Expand Down Expand Up @@ -36,11 +35,6 @@ import { isWalletServiceEnabled } from './wallet';
* Initialize the network settings saga when the wallet starts successfully.
*/
export function* initNetworkSettings() {
const customNetwork = STORE.getItem(networkSettingsKeyMap.networkSettings);
if (customNetwork) {
yield put(networkSettingsUpdateState(customNetwork));
}

const status = yield select((state) => state.networkSettingsStatus);
if (status === NETWORKSETTINGS_STATUS.WAITING) {
// This branch completes the network update by delivering
Expand All @@ -60,13 +54,14 @@ export function* initNetworkSettings() {
*
* @param {{
* payload: {
* stage: string,
* network: string,
* nodeUrl: string,
* explorerUrl: string,
* explorerServiceUrl: string,
* walletServiceUrl?: string
* walletServiceWsUrl?: string
* stage: string;
* network: string;
* nodeUrl: string;
* explorerUrl: string;
* explorerServiceUrl: string;
* txMiningServiceUrl: string;
* walletServiceUrl?: string;
* walletServiceWsUrl?: string;
* }
* }} action contains the payload with the new
* network settings requested by the user to be processd.
Expand Down Expand Up @@ -148,15 +143,15 @@ export function* updateNetworkSettings(action) {
txMiningServiceUrl: networkSettings.txMiningServiceUrl,
};

config.setTxMiningUrl(txMiningServiceUrl);
config.setExplorerServiceBaseUrl(explorerServiceUrl);
config.setServerUrl(nodeUrl);
config.setTxMiningUrl(txMiningServiceUrl);

// - walletServiceUrl has precedence
// - nodeUrl as fallback
let potentialNetwork;
let network;
if (walletServiceUrl && useWalletService) {
if (useWalletService && !isEmpty(walletServiceUrl)) {
config.setWalletServiceBaseUrl(walletServiceUrl);
config.setWalletServiceBaseWsUrl(walletServiceWsUrl);

Expand Down Expand Up @@ -222,6 +217,7 @@ export function* updateNetworkSettings(action) {
nodeUrl,
explorerUrl,
explorerServiceUrl,
txMiningServiceUrl,
walletServiceUrl,
walletServiceWsUrl,
};
Expand Down
27 changes: 24 additions & 3 deletions src/sagas/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import {
} from 'redux-saga/effects';
import { eventChannel } from 'redux-saga';
import { getUniqueId } from 'react-native-device-info';
import { get } from 'lodash';
import { get, isEmpty } from 'lodash';
import {
DEFAULT_TOKEN,
WALLET_SERVICE_FEATURE_TOGGLE,
PUSH_NOTIFICATION_FEATURE_TOGGLE,
networkSettingsKeyMap,
} from '../constants';
import { STORE } from '../store';
import {
Expand Down Expand Up @@ -63,6 +64,7 @@ import {
resetWalletSuccess,
setTokens,
onExceptionCaptured,
networkSettingsUpdateState,
} from '../actions';
import { fetchTokenData } from './tokens';
import {
Expand Down Expand Up @@ -151,10 +153,29 @@ export function* startWallet(action) {
dispatch = _dispatch;
});

const networkSettings = yield select(getNetworkSettings);
// Network settings either from store or redux state
let networkSettings;
// Custom network settings are persisted in the app storage
const customNetwork = STORE.getItem(networkSettingsKeyMap.networkSettings);
if (customNetwork) {
networkSettings = customNetwork;
// On custom network settings one may use a different
// URL for the services from the ones registered by default
// for mainnet and testnet in the lib, and the wallet must
// behave consistently to the URLs set
config.setExplorerServiceBaseUrl(networkSettings.explorerServiceUrl);
config.setServerUrl(networkSettings.nodeUrl);
config.setTxMiningUrl(networkSettings.txMiningServiceUrl);

// If the wallet is initialized from quit state it must
// update the network settings on redux state
yield put(networkSettingsUpdateState(networkSettings));
} else {
networkSettings = yield select(getNetworkSettings);
}

let wallet;
if (useWalletService) {
if (useWalletService && !isEmpty(networkSettings.walletServiceUrl)) {
const network = new Network(networkSettings.network);

// Set urls for wallet service
Expand Down
13 changes: 10 additions & 3 deletions src/screens/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Text,
View,
} from 'react-native';
import { isEmpty } from 'lodash';
import OfflineBar from '../components/OfflineBar';
import Logo from '../components/Logo';
import { HathorList, ListItem, ListMenu } from '../components/HathorList';
Expand All @@ -32,9 +33,15 @@ import { isPushNotificationAvailableForUser } from '../utils';
* server {str} URL of server this wallet is connected to
*/
const mapStateToProps = (state) => {
const server = state.useWalletService
? state.wallet.storage.config.getWalletServiceBaseUrl()
: state.wallet.storage.config.getServerUrl();
let server;
const { walletServiceUrl } = state.networkSettings;
if (state.useWalletService && !isEmpty(walletServiceUrl)) {
server = walletServiceUrl;
}

if (!server) {
server = state.networkSettings.nodeUrl;
}

return {
selectedToken: state.selectedToken,
Expand Down

0 comments on commit b88fa51

Please sign in to comment.