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

(Feature) Automatic ui updates #84

Merged
merged 7 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/services/web3_wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ export const getWeb3WrapperOrThrow = async (): Promise<Web3Wrapper> => {

return web3WrapperOrNull;
};

/* Event to detect if user has changed the account */
export const accountChangedSubscription = (onChangeCb: () => void) => {
const { ethereum } = window;
if (!web3Wrapper || !onChangeCb) {
return;
}
ethereum.on('accountsChanged', async (accounts: []) => {
Agupane marked this conversation as resolved.
Show resolved Hide resolved
onChangeCb();
});
};

/* Event to detect if user has changed the network */
export const networkChangedSubscription = (onChangeCb: () => void) => {
const { ethereum } = window;
if (!web3Wrapper || !onChangeCb) {
return;
}
ethereum.on('networkChanged', async (network: number) => {
onChangeCb();
});
};
16 changes: 15 additions & 1 deletion src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import { getContractWrappers } from '../services/contract_wrappers';
import { cancelSignedOrder, getAllOrdersAsUIOrders, getUserOrdersAsUIOrders } from '../services/orders';
import { getRelayer } from '../services/relayer';
import { getTokenBalance, tokenToTokenBalance } from '../services/tokens';
import { getWeb3Wrapper, getWeb3WrapperOrThrow } from '../services/web3_wrapper';
import {
accountChangedSubscription,
getWeb3Wrapper,
getWeb3WrapperOrThrow,
networkChangedSubscription,
} from '../services/web3_wrapper';
import { getKnownTokens } from '../util/known_tokens';
import { buildOrder } from '../util/orders';
import { BlockchainState, OrderSide, RelayerState, Token, TokenBalance, UIOrder, Web3State } from '../util/types';
Expand Down Expand Up @@ -168,6 +173,15 @@ export const initWallet = () => {

const selectedToken = knownTokens.getTokenBySymbol('ZRX');

/* Subscriptions register **/
unjapones marked this conversation as resolved.
Show resolved Hide resolved
networkChangedSubscription(() => {
/* Performs a reload to avoid a bug with MetaMask: "MetaMask - RPC Error: Internal JSON-RPC" **/
window.location.reload();
});
accountChangedSubscription(() => {
window.location.reload();
});

dispatch(
initializeBlockchainData({
web3State: Web3State.Done,
Expand Down