2021-02-04 19:15:23 +01:00
|
|
|
import { useState, useEffect, useRef, useCallback } from 'react';
|
|
|
|
import TokenTracker from '@metamask/eth-token-tracker';
|
|
|
|
import { useSelector } from 'react-redux';
|
2021-03-12 23:23:26 +01:00
|
|
|
import { getCurrentChainId, getSelectedAddress } from '../selectors';
|
2021-06-10 21:27:03 +02:00
|
|
|
import { SECOND } from '../../shared/constants/time';
|
2021-02-04 19:15:23 +01:00
|
|
|
import { useEqualityCheck } from './useEqualityCheck';
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2021-03-11 19:07:41 +01:00
|
|
|
export function useTokenTracker(
|
|
|
|
tokens,
|
|
|
|
includeFailedTokens = false,
|
|
|
|
hideZeroBalanceTokens = false,
|
|
|
|
) {
|
2021-03-12 23:23:26 +01:00
|
|
|
const chainId = useSelector(getCurrentChainId);
|
2021-02-04 19:15:23 +01:00
|
|
|
const userAddress = useSelector(getSelectedAddress);
|
|
|
|
const [loading, setLoading] = useState(() => tokens?.length >= 0);
|
|
|
|
const [tokensWithBalances, setTokensWithBalances] = useState([]);
|
|
|
|
const [error, setError] = useState(null);
|
|
|
|
const tokenTracker = useRef(null);
|
|
|
|
const memoizedTokens = useEqualityCheck(tokens);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2021-03-11 19:07:41 +01:00
|
|
|
const updateBalances = useCallback(
|
|
|
|
(tokenWithBalances) => {
|
|
|
|
const matchingTokens = hideZeroBalanceTokens
|
|
|
|
? tokenWithBalances.filter((token) => Number(token.balance) > 0)
|
|
|
|
: tokenWithBalances;
|
2021-06-22 19:39:44 +02:00
|
|
|
// TODO: improve this pattern for adding this field when we improve support for
|
|
|
|
// EIP721 tokens.
|
|
|
|
const matchingTokensWithIsERC721Flag = matchingTokens.map((token) => {
|
|
|
|
const additionalTokenData = memoizedTokens.find(
|
|
|
|
(t) => t.address === token.address,
|
|
|
|
);
|
|
|
|
return { ...token, isERC721: additionalTokenData?.isERC721 };
|
|
|
|
});
|
|
|
|
setTokensWithBalances(matchingTokensWithIsERC721Flag);
|
2021-03-11 19:07:41 +01:00
|
|
|
setLoading(false);
|
|
|
|
setError(null);
|
|
|
|
},
|
2021-06-22 19:39:44 +02:00
|
|
|
[hideZeroBalanceTokens, memoizedTokens],
|
2021-03-11 19:07:41 +01:00
|
|
|
);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2020-08-18 18:36:45 +02:00
|
|
|
const showError = useCallback((err) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
setError(err);
|
|
|
|
setLoading(false);
|
|
|
|
}, []);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
|
|
|
const teardownTracker = useCallback(() => {
|
|
|
|
if (tokenTracker.current) {
|
2021-02-04 19:15:23 +01:00
|
|
|
tokenTracker.current.stop();
|
|
|
|
tokenTracker.current.removeAllListeners('update');
|
|
|
|
tokenTracker.current.removeAllListeners('error');
|
|
|
|
tokenTracker.current = null;
|
2020-06-04 20:13:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, []);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
const buildTracker = useCallback(
|
|
|
|
(address, tokenList) => {
|
|
|
|
// clear out previous tracker, if it exists.
|
2021-02-04 19:15:23 +01:00
|
|
|
teardownTracker();
|
2020-11-03 00:41:28 +01:00
|
|
|
tokenTracker.current = new TokenTracker({
|
|
|
|
userAddress: address,
|
|
|
|
provider: global.ethereumProvider,
|
|
|
|
tokens: tokenList,
|
2020-11-18 23:13:28 +01:00
|
|
|
includeFailedTokens,
|
2021-06-10 21:27:03 +02:00
|
|
|
pollingInterval: SECOND * 8,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
tokenTracker.current.on('update', updateBalances);
|
|
|
|
tokenTracker.current.on('error', showError);
|
|
|
|
tokenTracker.current.updateBalances();
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
2020-11-18 23:13:28 +01:00
|
|
|
[updateBalances, includeFailedTokens, showError, teardownTracker],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
|
|
|
// Effect to remove the tracker when the component is removed from DOM
|
|
|
|
// Do not overload this effect with additional dependencies. teardownTracker
|
|
|
|
// is the only dependency here, which itself has no dependencies and will
|
|
|
|
// never update. The lack of dependencies that change is what confirms
|
|
|
|
// that this effect only runs on mount/unmount
|
|
|
|
useEffect(() => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return teardownTracker;
|
|
|
|
}, [teardownTracker]);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
|
|
|
// Effect to set loading state and initialize tracker when values change
|
|
|
|
useEffect(() => {
|
|
|
|
// This effect will only run initially and when:
|
2021-03-12 23:23:26 +01:00
|
|
|
// 1. chainId is updated,
|
2020-06-04 20:13:11 +02:00
|
|
|
// 2. userAddress is changed,
|
|
|
|
// 3. token list is updated and not equal to previous list
|
|
|
|
// in any of these scenarios, we should indicate to the user that their token
|
|
|
|
// values are in the process of updating by setting loading state.
|
2021-02-04 19:15:23 +01:00
|
|
|
setLoading(true);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2021-03-12 23:23:26 +01:00
|
|
|
if (!userAddress || chainId === undefined || !global.ethereumProvider) {
|
2020-06-04 20:13:11 +02:00
|
|
|
// If we do not have enough information to build a TokenTracker, we exit early
|
|
|
|
// When the values above change, the effect will be restarted. We also teardown
|
|
|
|
// tracker because inevitably this effect will run again momentarily.
|
2021-02-04 19:15:23 +01:00
|
|
|
teardownTracker();
|
|
|
|
return;
|
2020-06-04 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-09-02 21:19:04 +02:00
|
|
|
if (memoizedTokens.length === 0) {
|
2020-06-04 20:13:11 +02:00
|
|
|
// sets loading state to false and token list to empty
|
2021-02-04 19:15:23 +01:00
|
|
|
updateBalances([]);
|
2020-06-04 20:13:11 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
buildTracker(userAddress, memoizedTokens);
|
2020-11-03 00:41:28 +01:00
|
|
|
}, [
|
|
|
|
userAddress,
|
|
|
|
teardownTracker,
|
2021-03-12 23:23:26 +01:00
|
|
|
chainId,
|
2020-11-03 00:41:28 +01:00
|
|
|
memoizedTokens,
|
|
|
|
updateBalances,
|
|
|
|
buildTracker,
|
2021-02-04 19:15:23 +01:00
|
|
|
]);
|
2020-06-04 20:13:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return { loading, tokensWithBalances, error };
|
2020-06-04 20:13:11 +02:00
|
|
|
}
|