Skip to content

Commit

Permalink
Merge branch 'develop' into hm/migrate-snap-permissions-to-caveats
Browse files Browse the repository at this point in the history
  • Loading branch information
hmalik88 committed Mar 8, 2023
2 parents b1ff6c6 + 3a3e34a commit 0c7a772
Show file tree
Hide file tree
Showing 16 changed files with 885 additions and 66 deletions.
1 change: 1 addition & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
features: { buildStoriesJson: true },
stories: [
'../ui/**/*.stories.js',
'../ui/**/*.stories.tsx',
'../ui/**/*.stories.mdx',
'./*.stories.mdx',
],
Expand Down
75 changes: 68 additions & 7 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import {
AssetType,
TransactionStatus,
TransactionType,
TokenStandard,
} from '../../shared/constants/transaction';
import {
GAS_API_BASE_URL,
Expand Down Expand Up @@ -104,7 +105,10 @@ import {
} from '../../shared/constants/app';
import { EVENT, EVENT_NAMES } from '../../shared/constants/metametrics';

import { getTokenIdParam } from '../../shared/lib/token-util';
import {
getTokenIdParam,
fetchTokenBalance,
} from '../../shared/lib/token-util.ts';
import { isEqualCaseInsensitive } from '../../shared/modules/string-utils';
import { parseStandardTokenTransactionData } from '../../shared/modules/transaction.utils';
import { STATIC_MAINNET_TOKEN_LIST } from '../../shared/constants/tokens';
Expand Down Expand Up @@ -946,10 +950,7 @@ export default class MetamaskController extends EventEmitter {
this.getExternalPendingTransactions.bind(this),
getAccountType: this.getAccountType.bind(this),
getDeviceModel: this.getDeviceModel.bind(this),
getTokenStandardAndDetails:
this.assetsContractController.getTokenStandardAndDetails.bind(
this.assetsContractController,
),
getTokenStandardAndDetails: this.getTokenStandardAndDetails.bind(this),
securityProviderRequest: this.securityProviderRequest.bind(this),
});
this.txController.on('newUnapprovedTx', () => opts.showUserConfirmation());
Expand Down Expand Up @@ -2191,12 +2192,72 @@ export default class MetamaskController extends EventEmitter {
}

async getTokenStandardAndDetails(address, userAddress, tokenId) {
const details =
await this.assetsContractController.getTokenStandardAndDetails(
const { tokenList } = this.tokenListController.state;
const { tokens } = this.tokensController.state;

const staticTokenListDetails =
STATIC_MAINNET_TOKEN_LIST[address.toLowerCase()] || {};
const tokenListDetails = tokenList[address.toLowerCase()] || {};
const userDefinedTokenDetails =
tokens.find(({ address: _address }) =>
isEqualCaseInsensitive(_address, address),
) || {};

const tokenDetails = {
...staticTokenListDetails,
...tokenListDetails,
...userDefinedTokenDetails,
};
const tokenDetailsStandardIsERC20 =
isEqualCaseInsensitive(tokenDetails.standard, TokenStandard.ERC20) ||
tokenDetails.erc20 === true;
const noEvidenceThatTokenIsAnNFT =
!tokenId &&
!isEqualCaseInsensitive(tokenDetails.standard, TokenStandard.ERC1155) &&
!isEqualCaseInsensitive(tokenDetails.standard, TokenStandard.ERC721) &&
!tokenDetails.erc721;

const otherDetailsAreERC20Like =
tokenDetails.decimals !== undefined && tokenDetails.symbol;

const tokenCanBeTreatedAsAnERC20 =
tokenDetailsStandardIsERC20 ||
(noEvidenceThatTokenIsAnNFT && otherDetailsAreERC20Like);

let details;
if (tokenCanBeTreatedAsAnERC20) {
try {
const balance = await fetchTokenBalance(
address,
userAddress,
this.provider,
);

details = {
address,
balance,
standard: TokenStandard.ERC20,
decimals: tokenDetails.decimals,
symbol: tokenDetails.symbol,
};
} catch (e) {
// If the `fetchTokenBalance` call failed, `details` remains undefined, and we
// fall back to the below `assetsContractController.getTokenStandardAndDetails` call
log.warning(`Failed to get token balance. Error: ${e}`);
}
}

// `details`` will be undefined if `tokenCanBeTreatedAsAnERC20`` is false,
// or if it is true but the `fetchTokenBalance`` call failed. In either case, we should
// attempt to retrieve details from `assetsContractController.getTokenStandardAndDetails`
if (details === undefined) {
details = await this.assetsContractController.getTokenStandardAndDetails(
address,
userAddress,
tokenId,
);
}

return {
...details,
decimals: details?.decimals?.toString(10),
Expand Down
Loading

0 comments on commit 0c7a772

Please sign in to comment.