mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-24 19:10:22 +01:00
efaaf4fab2
* Use tokenList to get token details, when available, in getTokenStandardAndDetails Previously, every call to getTokenStandardAndDetails would fetch data via the provider. This would result in at least 3 network requests whenever that method is called for an ERC20 token, contributing to unneccesary loading and lagging in multiple places. This commit takes advantage of stored data we already have available to avoid the unnecessary loading. * Lint fix * Fix build-quote test * bump coverage targets * Pass provider to token-util, for use in ethers Contract module * Check all possible sources of ERC20 token data before async call to assetsContractController * Add and update tests * Update app/scripts/metamask-controller.js Co-authored-by: Alex Donesky <adonesky@gmail.com> * Update app/scripts/metamask-controller.js Co-authored-by: Alex Donesky <adonesky@gmail.com> * Remove unnecessary this.ethQuery changes * Use metamask-eth-abis instead of human-standard-token-abi in token-util.ts * Add explanatory comments to getTokenStandardAndDetails * lint fix * Cleanup * fix test * Update app/scripts/metamask-controller.js Co-authored-by: Alex Donesky <adonesky@gmail.com> * update error message --------- Co-authored-by: Alex Donesky <adonesky@gmail.com>
38 lines
1.4 KiB
TypeScript
38 lines
1.4 KiB
TypeScript
import { abiERC20 } from '@metamask/metamask-eth-abis';
|
|
import { Contract } from '@ethersproject/contracts';
|
|
import { Web3Provider } from '@ethersproject/providers';
|
|
|
|
/**
|
|
* Gets the '_value' parameter of the given token transaction data
|
|
* (i.e function call) per the Human Standard Token ABI, if present.
|
|
*
|
|
* @param {object} tokenData - ethers Interface token data.
|
|
* @returns {string | undefined} A decimal string value.
|
|
*/
|
|
/**
|
|
* Gets either the '_tokenId' parameter or the 'id' param of the passed token transaction data.,
|
|
* These are the parsed tokenId values returned by `parseStandardTokenTransactionData` as defined
|
|
* in the ERC721 and ERC1155 ABIs from metamask-eth-abis (https://github.com/MetaMask/metamask-eth-abis/tree/main/src/abis)
|
|
*
|
|
* @param tokenData - ethers Interface token data.
|
|
* @returns A decimal string value.
|
|
*/
|
|
export function getTokenIdParam(tokenData: any = {}): string | undefined {
|
|
return (
|
|
tokenData?.args?._tokenId?.toString() ?? tokenData?.args?.id?.toString()
|
|
);
|
|
}
|
|
|
|
export async function fetchTokenBalance(
|
|
address: string,
|
|
userAddress: string,
|
|
provider: any,
|
|
): Promise<any> {
|
|
const ethersProvider = new Web3Provider(provider);
|
|
const tokenContract = new Contract(address, abiERC20, ethersProvider);
|
|
const tokenBalancePromise = tokenContract
|
|
? tokenContract.balanceOf(userAddress)
|
|
: Promise.resolve();
|
|
return await tokenBalancePromise;
|
|
}
|