Skip to content

Commit

Permalink
refactor: removes globalwallet from local states
Browse files Browse the repository at this point in the history
  • Loading branch information
tuliomir committed Apr 16, 2024
1 parent 72b8250 commit b4224ea
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 99 deletions.
17 changes: 6 additions & 11 deletions src/components/ModalAddManyTokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,10 @@ import { t } from 'ttag';
import { get } from 'lodash';
import $ from 'jquery';
import hathorLib from '@hathor/wallet-lib';
import { connect } from 'react-redux';
import tokens from '../utils/tokens';
import wallet from "../utils/wallet";
import walletUtils from "../utils/wallet";
import { getGlobalWallet } from "../services/wallet.singleton";


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

/**
* Component that shows a modal to add many unknown tokens to the wallet (bulk import)
*
Expand Down Expand Up @@ -106,14 +100,15 @@ class ModalAddManyTokens extends React.Component {
// Preventing when the user forgets a comma in the end
if (config !== '') {
// Getting all validation promises
validations.push(hathorLib.tokensUtils.validateTokenToAddByConfigurationString(config, this.props.storage));
const storage = getGlobalWallet().storage;
validations.push(hathorLib.tokensUtils.validateTokenToAddByConfigurationString(config, storage));
}
}

try {
const toAdd = await Promise.all(validations)
const tokensBalance = this.props.tokensBalance;
const areZeroBalanceTokensHidden = wallet.areZeroBalanceTokensHidden();
const areZeroBalanceTokensHidden = walletUtils.areZeroBalanceTokensHidden();
const tokensWithoutBalance = [];
const tokensToAdd = [];

Expand Down Expand Up @@ -164,7 +159,7 @@ class ModalAddManyTokens extends React.Component {
// Adding the tokens to the wallet and returning with the success callback
for (const config of tokensToAdd) {
await tokens.addToken(config.uid, config.name, config.symbol);
wallet.setTokenAlwaysShow(config.uid, this.state.alwaysShow);
walletUtils.setTokenAlwaysShow(config.uid, this.state.alwaysShow);
}

this.props.success(toAdd.length);
Expand Down Expand Up @@ -241,4 +236,4 @@ class ModalAddManyTokens extends React.Component {
}
}

export default connect(mapStateToProps)(ModalAddManyTokens);
export default ModalAddManyTokens;
17 changes: 6 additions & 11 deletions src/components/ModalAddToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@ import { t } from 'ttag';
import $ from 'jquery';
import tokens from '../utils/tokens';
import hathorLib from '@hathor/wallet-lib';
import { connect } from 'react-redux';
import wallet from "../utils/wallet";
import walletUtils from "../utils/wallet";
import { getGlobalWallet } from "../services/wallet.singleton";


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

/**
* Component that shows a modal to add one specific unknown token to the wallet
*
Expand Down Expand Up @@ -89,7 +83,8 @@ class ModalAddToken extends React.Component {
}

try {
const tokenData = await hathorLib.tokensUtils.validateTokenToAddByConfigurationString(this.refs.config.value, this.props.storage);
const storage = getGlobalWallet().storage;
const tokenData = await hathorLib.tokensUtils.validateTokenToAddByConfigurationString(this.refs.config.value, storage);
const tokensBalance = this.props.tokensBalance;

const tokenUid = tokenData.uid;
Expand All @@ -103,7 +98,7 @@ class ModalAddToken extends React.Component {
* checkbox value as the user decision already.
*/
if (
wallet.areZeroBalanceTokensHidden()
walletUtils.areZeroBalanceTokensHidden()
&& tokenHasZeroBalance
&& !this.state.shouldExhibitAlwaysShowCheckbox
) {
Expand All @@ -116,7 +111,7 @@ class ModalAddToken extends React.Component {

// Adding the token to the wallet and returning with the success callback
tokens.addToken(tokenUid, tokenData.name, tokenData.symbol);
wallet.setTokenAlwaysShow(tokenUid, this.state.alwaysShow);
walletUtils.setTokenAlwaysShow(tokenUid, this.state.alwaysShow);

this.props.success();
} catch (e) {
Expand Down Expand Up @@ -196,4 +191,4 @@ class ModalAddToken extends React.Component {
}
}

export default connect(mapStateToProps)(ModalAddToken);
export default ModalAddToken;
4 changes: 2 additions & 2 deletions src/components/ModalBackupWords.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const mapDispatchToProps = dispatch => {
const mapStateToProps = (state) => {
return {
words: state.words,
wallet: getGlobalWallet(),
};
};

Expand Down Expand Up @@ -109,7 +108,8 @@ class ModalBackupWords extends React.Component {
this.setState({ passwordFormValidated: false });
const password = this.refs.password.value;
try {
const accessData = await this.props.wallet.storage.getAccessData();
const wallet = getGlobalWallet();
const accessData = await wallet.storage.getAccessData();
const words = hathorLib.cryptoUtils.decryptData(accessData.words, password);
this.props.updateWords(words);
this.setState({ passwordSuccess: true, errorMessage: '' });
Expand Down
13 changes: 3 additions & 10 deletions src/components/ModalPin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,9 @@ import React from 'react';
import { t } from 'ttag';
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: getGlobalWallet(),
};
};

/**
* Component that shows a modal with a form to ask for the user PIN
* and when the PIN succeeds, it invokes the callback function
Expand Down Expand Up @@ -79,7 +71,8 @@ export class ModalPin extends React.Component {
const pin = this.refs.pinInput.refs.pin.value;

// Incorrect PIN, show error message and do nothing else
if (!await this.props.wallet.checkPin(pin)) {
const wallet = getGlobalWallet();
if (!await wallet.checkPin(pin)) {
this.setState({ errorMessage: t`Invalid PIN` })
return;
}
Expand Down Expand Up @@ -137,7 +130,7 @@ export class ModalPin extends React.Component {
}
}

export default connect(mapStateToProps)(ModalPin);
export default ModalPin;


ModalPin.propTypes = {
Expand Down
15 changes: 4 additions & 11 deletions src/components/ModalResetAllData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,10 @@ import React from 'react';
import { t } from 'ttag';
import $ from 'jquery';
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: getGlobalWallet(),
};
};

/**
* Component that shows a modal to ask form confirmation data to reset the wallet
* Asks for the password and for the user to write a sentence saying that really wants to reset
Expand Down Expand Up @@ -76,8 +68,9 @@ class ModalResetAllData extends React.Component {
}

if (!forgotPassword) {
// Password was informed and it is incorrect
const correctPassword = await this.props.wallet.checkPassword(password);
// Password was informed and it is incorr
const wallet = getGlobalWallet();
const correctPassword = await wallet.checkPassword(password);
if (password && !correctPassword) {
this.setState({errorMessage: t`Invalid password`})
return
Expand Down Expand Up @@ -177,4 +170,4 @@ class ModalResetAllData extends React.Component {
}
}

export default connect(mapStateToProps)(ModalResetAllData);
export default ModalResetAllData;
4 changes: 2 additions & 2 deletions src/components/ModalSendTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { getGlobalWallet } from "../services/wallet.singleton";

const mapStateToProps = (state) => {
return {
wallet: getGlobalWallet(),
useWalletService: state.useWalletService,
};
};
Expand Down Expand Up @@ -82,7 +81,8 @@ export class ModalSendTx extends React.Component {
// If we are using the wallet service facade, we should avail of the validated PIN
// to renew the auth token.
if (this.props.useWalletService) {
await this.props.wallet.validateAndRenewAuthToken(pin);
const wallet = getGlobalWallet();
await wallet.validateAndRenewAuthToken(pin);
}

const preparedTx = await this.props.prepareSendTransaction(pin);
Expand Down
10 changes: 3 additions & 7 deletions src/components/ModalUnregisteredTokenInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,9 @@ import tokens from '../utils/tokens';
import SpanFmt from './SpanFmt';
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: getGlobalWallet().storage };
};

/**
* Component that shows a modal with information about an unregistered token
*
Expand Down Expand Up @@ -68,7 +63,8 @@ class ModalUnregisteredTokenInfo extends React.Component {
);

try {
const tokenData = await hathorLib.tokensUtils.validateTokenToAddByConfigurationString(configurationString, this.props.storage);
const storage = getGlobalWallet().storage;
const tokenData = await hathorLib.tokensUtils.validateTokenToAddByConfigurationString(configurationString, storage);
await tokens.addToken(tokenData.uid, tokenData.name, tokenData.symbol);
$('#unregisteredTokenInfoModal').modal('hide');
this.props.tokenRegistered(this.props.token);
Expand Down Expand Up @@ -159,4 +155,4 @@ ModalUnregisteredTokenInfo.propTypes = {
totalSupply: PropTypes.number,
};

export default connect(mapStateToProps)(ModalUnregisteredTokenInfo);
export default ModalUnregisteredTokenInfo;
11 changes: 1 addition & 10 deletions src/components/OutputsWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,9 @@ import React from 'react';
import { t } from 'ttag';
import $ from 'jquery';
import _ from 'lodash';
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: getGlobalWallet(),
};
};

/**
* Component that wraps the outputs of a token in the Send Tokens screen
Expand Down Expand Up @@ -86,4 +77,4 @@ class OutputsWrapper extends React.Component {
}
}

export default connect(mapStateToProps)(OutputsWrapper);
export default OutputsWrapper;
7 changes: 3 additions & 4 deletions src/components/TokenHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Link } from 'react-router-dom'
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { connect } from 'react-redux';
import { get } from 'lodash';
import wallet from '../utils/wallet';
import walletUtils from '../utils/wallet';
import helpers from '../utils/helpers';
import { colors } from '../constants';
import TokenPagination from './TokenPagination';
Expand All @@ -33,7 +33,6 @@ const mapStateToProps = (state, props) => {

return {
tokenHistory: history,
wallet: getGlobalWallet(),
tokenMetadata: state.tokenMetadata,
};
};
Expand Down Expand Up @@ -86,8 +85,8 @@ class TokenHistory extends React.Component {
*/
fetchMoreHistory = async () => {
if (this.state.shouldFetch) {
const newHistory = await wallet.fetchMoreHistory(
this.props.wallet,
const newHistory = await walletUtils.fetchMoreHistory(
getGlobalWallet(),
this.props.selectedToken,
this.props.tokenHistory.data
);
Expand Down
16 changes: 10 additions & 6 deletions src/components/TxData.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const mapStateToProps = (state) => {
return {
tokens: state.tokens,
tokenMetadata: state.tokenMetadata || {},
wallet: getGlobalWallet(),
};
};

Expand Down Expand Up @@ -117,7 +116,8 @@ class TxData extends React.Component {
});

try {
const fundsData = await this.props.wallet.graphvizNeighborsQuery(
const wallet = getGlobalWallet();
const fundsData = await wallet.graphvizNeighborsQuery(
this.props.transaction.hash,
'funds',
MAX_GRAPH_LEVEL,
Expand Down Expand Up @@ -147,7 +147,8 @@ class TxData extends React.Component {
});

try {
const verificationData = await this.props.wallet.graphvizNeighborsQuery(
const wallet = getGlobalWallet();
const verificationData = await wallet.graphvizNeighborsQuery(
this.props.transaction.hash,
'verification',
MAX_GRAPH_LEVEL,
Expand Down Expand Up @@ -185,7 +186,8 @@ class TxData extends React.Component {
];

try {
const walletAddressesMap = await this.props.wallet.checkAddressesMine(addresses);
const wallet = getGlobalWallet();
const walletAddressesMap = await wallet.checkAddressesMine(addresses);
this.setState({
walletAddressesMap,
});
Expand Down Expand Up @@ -247,7 +249,8 @@ class TxData extends React.Component {
}

calculateBalance = async () => {
const fullBalance = await hathorLib.transactionUtils.getTxBalance(this.props.transaction, this.props.wallet.storage);
const wallet = getGlobalWallet();
const fullBalance = await hathorLib.transactionUtils.getTxBalance(this.props.transaction, wallet.storage);
const balance = {};
for (const token of Object.keys(fullBalance)) {
const tokenBalance = fullBalance[token];
Expand Down Expand Up @@ -370,7 +373,8 @@ class TxData extends React.Component {
tokenClicked: token,
unregisteredLoading: true,
}, async () => {
const tokenDetails = await this.props.wallet.getTokenDetails(token.uid);
const wallet = getGlobalWallet();
const tokenDetails = await wallet.getTokenDetails(token.uid);

const { totalSupply, totalTransactions, authorities } = tokenDetails;

Expand Down
4 changes: 2 additions & 2 deletions src/components/WalletAddress.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const mapStateToProps = (state) => {
return {
lastSharedAddress: state.lastSharedAddress,
lastSharedIndex: state.lastSharedIndex,
wallet: getGlobalWallet(),
};
};

Expand Down Expand Up @@ -72,7 +71,8 @@ export class WalletAddress extends React.Component {
*/
generateNewAddress = async (e) => {
e.preventDefault();
const address = await this.props.wallet.getNextAddress();
const wallet = getGlobalWallet();
const address = await wallet.getNextAddress();

if (address.address === this.props.lastSharedAddress) {
this.alertErrorRef.current.show(3000);
Expand Down
Loading

0 comments on commit b4224ea

Please sign in to comment.