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

Refactor: Removes HathorWallet from Redux store #580

Merged
merged 10 commits into from
Apr 19, 2024
4 changes: 2 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ import EditSwap from './screens/atomic-swap/EditSwap';
import NewSwap from './screens/atomic-swap/NewSwap';
import ImportExisting from './screens/atomic-swap/ImportExisting';
import LOCAL_STORE from './storage';
import { getGlobalWallet } from "./services/wallet.singleton";

function Root() {
const {
ledgerClosed,
walletStartState,
isVersionAllowed,
wallet,
navigateTo,
} = useSelector((state) => {
return {
ledgerClosed: state.ledgerWasClosed,
walletStartState: state.walletStartState,
isVersionAllowed: state.isVersionAllowed,
wallet: state.wallet,
navigateTo: state.navigateTo,
};
});
const wallet = getGlobalWallet();
const dispatch = useDispatch();
const navigate = useNavigate();
const context = useContext(GlobalModalContext);
Expand Down
10 changes: 0 additions & 10 deletions src/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,16 +142,6 @@ export const updateRequestErrorStatusCode = data => ({ type: 'update_request_err
*/
export const updateHeight = (height, htrUpdatedBalance) => ({ type: 'update_height', payload: { height, htrUpdatedBalance } });

/**
* wallet {HathorWallet} wallet object
*/
export const setWallet = (wallet) => ({ type: 'set_wallet', payload: wallet });

/**
* Stop and clean wallet redux state
*/
export const resetWallet = () => ({ type: 'reset_wallet' });

/**
* tokens {Array} array of token uids the the wallet has
* currentAddress {Object} The current unused address
Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalAddManyTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import hathorLib from '@hathor/wallet-lib';
import { connect } from 'react-redux';
import tokens from '../utils/tokens';
import wallet from "../utils/wallet";
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return { storage: state.wallet.storage };
return { storage: getGlobalWallet().storage };
tuliomir marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalAddToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import tokens from '../utils/tokens';
import hathorLib from '@hathor/wallet-lib';
import { connect } from 'react-redux';
import wallet from "../utils/wallet";
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return { storage: state.wallet.storage };
return { storage: getGlobalWallet().storage };
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalBackupWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { updateWords } from '../actions/index';
import { connect } from 'react-redux';
import hathorLib from '@hathor/wallet-lib';
import { WORDS_VALIDATION } from '../constants';
import { getGlobalWallet } from "../services/wallet.singleton";


const mapDispatchToProps = dispatch => {
Expand All @@ -26,7 +27,7 @@ const mapDispatchToProps = dispatch => {
const mapStateToProps = (state) => {
return {
words: state.words,
wallet: state.wallet,
wallet: getGlobalWallet(),
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalPin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import $ from 'jquery';
import PinInput from './PinInput';
import { connect } from 'react-redux';
import PropTypes from "prop-types";
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalResetAllData.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import { CONFIRM_RESET_MESSAGE } from '../constants';
import { connect } from 'react-redux';
import SpanFmt from './SpanFmt';
import LOCAL_STORE from '../storage';
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalSendTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import PropTypes from "prop-types";
import SendTxHandler from '../components/SendTxHandler';
import ReactLoading from 'react-loading';
import { colors } from '../constants';
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
useWalletService: state.useWalletService,
};
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/ModalUnregisteredTokenInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import TokenGeneralInfo from '../components/TokenGeneralInfo';
import hathorLib from '@hathor/wallet-lib';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { getGlobalWallet } from "../services/wallet.singleton";

const mapStateToProps = (state) => {
return { storage: state.wallet.storage };
return { storage: getGlobalWallet().storage };
};

/**
Expand Down
3 changes: 2 additions & 1 deletion src/components/OutputsWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import { connect } from 'react-redux';
import hathorLib from '@hathor/wallet-lib';
import InputNumber from './InputNumber';
import LOCAL_STORE from '../storage';
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/TokenHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { colors } from '../constants';
import TokenPagination from './TokenPagination';
import HathorAlert from './HathorAlert';
import { TOKEN_DOWNLOAD_STATUS } from '../sagas/tokens';
import { getGlobalWallet } from "../services/wallet.singleton";

const mapStateToProps = (state, props) => {
const defaultTokenHistory = {
Expand All @@ -32,7 +33,7 @@ const mapStateToProps = (state, props) => {

return {
tokenHistory: history,
wallet: state.wallet,
wallet: getGlobalWallet(),
tokenMetadata: state.tokenMetadata,
};
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/TxData.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ import { MAX_GRAPH_LEVEL } from '../constants';
import helpers from '../utils/helpers';
import { GlobalModalContext, MODAL_TYPES } from '../components/GlobalModal';
import Loading from '../components/Loading';
import { getGlobalWallet } from "../services/wallet.singleton";


const mapStateToProps = (state) => {
return {
tokens: state.tokens,
tokenMetadata: state.tokenMetadata || {},
wallet: state.wallet,
wallet: getGlobalWallet(),
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/components/WalletAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { IPC_RENDERER } from '../constants';
import { sharedAddressUpdate } from '../actions/index';
import { GlobalModalContext, MODAL_TYPES } from './GlobalModal';
import LOCAL_STORE from '../storage';
import { getGlobalWallet } from "../services/wallet.singleton";

const mapDispatchToProps = dispatch => {
return {
Expand All @@ -26,7 +27,7 @@ const mapStateToProps = (state) => {
return {
lastSharedAddress: state.lastSharedAddress,
lastSharedIndex: state.lastSharedIndex,
wallet: state.wallet,
wallet: getGlobalWallet(),
};
};

Expand Down
4 changes: 2 additions & 2 deletions src/components/atomic-swap/ModalAtomicReceive.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import React, { useState, useRef, useEffect } from "react";
import { t } from "ttag";
import InputNumber from "../InputNumber";
import hathorLib, { Address } from "@hathor/wallet-lib";
import { useSelector } from "react-redux";
import walletUtils from '../../utils/wallet';
import { getGlobalWallet } from "../../services/wallet.singleton";

export function ModalAtomicReceive ({ sendClickHandler, receivableTokens, manageDomLifecycle, onClose }) {
/** @type HathorWallet */
const wallet = useSelector(state => state.wallet);
const wallet = getGlobalWallet();
const [selectedToken, setSelectedToken] = useState(receivableTokens[0]);
const [address, setAddress] = useState('');
let amountRef = useRef();
Expand Down
3 changes: 2 additions & 1 deletion src/components/tokens/TokenDelegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import { t } from 'ttag';
import TokenAction from './TokenAction';
import { connect } from "react-redux";
import hathorLib from '@hathor/wallet-lib';
import { getGlobalWallet } from "../../services/wallet.singleton";

const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
useWalletService: state.useWalletService,
};
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/tokens/TokenDestroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ import helpers from '../../utils/helpers';
import TokenAction from './TokenAction';
import SpanFmt from '../SpanFmt';
import { connect } from "react-redux";
import { getGlobalWallet } from "../../services/wallet.singleton";

const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
useWalletService: state.useWalletService,
};
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/tokens/TokenMelt.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import { connect } from 'react-redux';
import { get } from 'lodash';
import { TOKEN_DOWNLOAD_STATUS } from '../../sagas/tokens';
import { colors } from '../../constants';
import { getGlobalWallet } from "../../services/wallet.singleton";

const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
tokenMetadata: state.tokenMetadata,
useWalletService: state.useWalletService,
};
Expand Down
3 changes: 2 additions & 1 deletion src/components/tokens/TokenMint.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ import helpers from '../../utils/helpers';
import InputNumber from '../InputNumber';
import { connect } from 'react-redux';
import { get } from 'lodash';
import { getGlobalWallet } from "../../services/wallet.singleton";

const mapStateToProps = (state) => {
return {
wallet: state.wallet,
wallet: getGlobalWallet(),
tokenMetadata: state.tokenMetadata,
useWalletService: state.useWalletService,
};
Expand Down
29 changes: 0 additions & 29 deletions src/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ const initialState = {
loadedData: { transactions: 0, addresses: 0 },
// Height of the best chain of the network arrived from ws data
height: 0,
wallet: null,
walletState: null,
// Metadata of tokens
tokenMetadata: {},
Expand Down Expand Up @@ -179,10 +178,6 @@ const rootReducer = (state = initialState, action) => {
return Object.assign({}, state, {requestErrorStatusCode: action.payload});
case 'update_height':
return onUpdateHeight(state, action);
case 'set_wallet':
return onSetWallet(state, action);
case 'reset_wallet':
return onResetWallet(state, action);
case 'load_wallet_success':
return onLoadWalletSuccess(state, action);
case 'update_tx':
Expand Down Expand Up @@ -277,30 +272,6 @@ const rootReducer = (state = initialState, action) => {
}
};

const onSetWallet = (state, action) => {
if (state.wallet && state.wallet.state !== hathorLib.HathorWallet.CLOSED) {
// Wallet was not closed
state.wallet.stop({ cleanStorage: false });
}

return {
...state,
wallet: action.payload
};
};

const onResetWallet = (state, action) => {
if (state.wallet) {
// Stop wallet
state.wallet.stop();
}

return {
...state,
wallet: null,
};
};

const getTxHistoryFromWSTx = (tx, tokenUid, tokenTxBalance) => {
return {
tx_id: tx.tx_id,
Expand Down
5 changes: 3 additions & 2 deletions src/sagas/atomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from "../utils/atomicSwap";
import { t } from "ttag";
import { swapService } from '@hathor/wallet-lib'
import { getGlobalWallet } from "../services/wallet.singleton";

const CONCURRENT_FETCH_REQUESTS = 5;

Expand Down Expand Up @@ -93,7 +94,7 @@ function* fetchProposalData(action) {
yield put(proposalFetchSuccess(proposalId, responseData));

// On success, build the proposal object locally and enrich it
const wallet = yield select((state) => state.wallet);
const wallet = getGlobalWallet();
const newData = generateReduxObjFromProposal(
proposalId,
password,
Expand Down Expand Up @@ -148,7 +149,7 @@ function* createProposalOnBackend(action) {
yield(put(importProposal(proposalId, password)));

// Enrich the PartialTx with exhibition metadata
const wallet = yield select((state) => state.wallet);
const wallet = getGlobalWallet();
const newProposalReduxObj = generateReduxObjFromProposal(
proposalId,
password,
Expand Down
9 changes: 5 additions & 4 deletions src/sagas/tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
proposalTokenFetchFailed,
} from '../actions';
import { t } from "ttag";
import { getGlobalWallet } from "../services/wallet.singleton";

const CONCURRENT_FETCH_REQUESTS = 5;
const METADATA_MAX_RETRIES = 3;
Expand Down Expand Up @@ -82,7 +83,7 @@ function* fetchTokenBalance(action) {
const { tokenId, force } = action;

try {
const wallet = yield select((state) => state.wallet);
const wallet = getGlobalWallet();
const tokenBalance = yield select((state) => get(state.tokensBalance, tokenId));

if (!force && tokenBalance && tokenBalance.oldStatus === TOKEN_DOWNLOAD_STATUS.READY) {
Expand Down Expand Up @@ -192,7 +193,7 @@ function* fetchTokenHistory(action) {
const { tokenId, force } = action;

try {
const wallet = yield select((state) => state.wallet);
const wallet = getGlobalWallet();
const tokenHistory = yield select((state) => get(state.tokensHistory, tokenId));

if (!force && tokenHistory && tokenHistory.oldStatus === TOKEN_DOWNLOAD_STATUS.READY) {
Expand All @@ -216,7 +217,7 @@ function* fetchTokenHistory(action) {
* for this token gets downloaded
*/
function* routeTokenChange(action) {
const wallet = yield select((state) => state.wallet);
const wallet = getGlobalWallet();

if (!wallet || !wallet.isReady()) {
return;
Expand Down Expand Up @@ -351,7 +352,7 @@ function* fetchProposalTokenData(action) {
return;
}

const wallet = yield select((state) => state.wallet);
const wallet = getGlobalWallet();

// Fetching name and symbol data from the fullnode
const updatedTokenDetails = yield wallet.getTokenDetails(tokenUid);
Expand Down
Loading
Loading