1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

Fixing/extending type definitions for later usage (#17514)

This commit is contained in:
Brad Decker 2023-02-01 11:53:21 -06:00 committed by GitHub
parent c43b1b6191
commit d9275bb1c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 36 deletions

View File

@ -5,6 +5,10 @@ import { MINUTE } from '../../../shared/constants/time';
import { AUTO_LOCK_TIMEOUT_ALARM } from '../../../shared/constants/alarms';
import { isManifestV3 } from '../../../shared/modules/mv3.utils';
import { isBeta } from '../../../ui/helpers/utils/build-types';
import {
ENVIRONMENT_TYPE_BACKGROUND,
POLLING_TOKEN_ENVIRONMENT_TYPES,
} from '../../../shared/constants/app';
export default class AppStateController extends EventEmitter {
/**
@ -236,11 +240,16 @@ export default class AppStateController extends EventEmitter {
* @param pollingTokenType
*/
addPollingToken(pollingToken, pollingTokenType) {
if (
pollingTokenType !==
POLLING_TOKEN_ENVIRONMENT_TYPES[ENVIRONMENT_TYPE_BACKGROUND]
) {
const prevState = this.store.getState()[pollingTokenType];
this.store.updateState({
[pollingTokenType]: [...prevState, pollingToken],
});
}
}
/**
* removes a pollingToken for a given environmentType
@ -249,11 +258,16 @@ export default class AppStateController extends EventEmitter {
* @param pollingTokenType
*/
removePollingToken(pollingToken, pollingTokenType) {
if (
pollingTokenType !==
POLLING_TOKEN_ENVIRONMENT_TYPES[ENVIRONMENT_TYPE_BACKGROUND]
) {
const prevState = this.store.getState()[pollingTokenType];
this.store.updateState({
[pollingTokenType]: prevState.filter((token) => token !== pollingToken),
});
}
}
/**
* clears all pollingTokens

View File

@ -83,6 +83,7 @@ const VALID_UNAPPROVED_TRANSACTION_TYPES = [
/**
* @typedef {import('../../../../shared/constants/transaction').TransactionMeta} TransactionMeta
* @typedef {import('../../../../shared/constants/gas').TxGasFees} TxGasFees
*/
const METRICS_STATUS_FAILED = 'failed on-chain';
@ -508,18 +509,7 @@ export default class TransactionController extends EventEmitter {
* updates the gas fees of the transaction with id if the transaction state is unapproved
*
* @param {string} txId - transaction id
* @param {object} txGasFees - holds the gas fees parameters
* @param {string} txGasFees.gasLimit
* @param {string} txGasFees.gasPrice
* @param {string} txGasFees.maxPriorityFeePerGas
* @param {string} txGasFees.maxFeePerGas
* @param {string} txGasFees.estimateUsed
* @param {string} txGasFees.estimateSuggested
* @param {string} txGasFees.defaultGasEstimates
* @param {string} txGasFees.gas
* @param {string} txGasFees.originalGasEstimate
* @param {string} txGasFees.userEditedGasLimit
* @param {string} txGasFees.userFeeLevel
* @param {TxGasFees} txGasFees - holds the gas fees parameters
* @returns {TransactionMeta} the txMeta of the updated transaction
*/
updateTransactionGasFees(

View File

@ -1,5 +1,6 @@
import log from 'loglevel';
import { isNullOrUndefined } from '@metamask/utils';
import { SWAPS_API_V2_BASE_URL } from '../../../shared/constants/swaps';
import {
BUYABLE_CHAINS_MAP,
@ -29,7 +30,7 @@ const fetchWithTimeout = getFetchWithTimeout();
const createWyrePurchaseUrl = async (
walletAddress: string,
chainId: keyof typeof BUYABLE_CHAINS_MAP,
symbol: CurrencySymbol,
symbol?: CurrencySymbol,
): Promise<any> => {
const { wyre = {} as WyreChainSettings } = BUYABLE_CHAINS_MAP[chainId];
const { srn, currencyCode } = wyre;
@ -72,7 +73,7 @@ const createWyrePurchaseUrl = async (
const createTransakUrl = (
walletAddress: string,
chainId: keyof typeof BUYABLE_CHAINS_MAP,
symbol: CurrencySymbol,
symbol?: CurrencySymbol,
): string => {
const { nativeCurrency, network } = BUYABLE_CHAINS_MAP[chainId];
@ -98,15 +99,16 @@ const createTransakUrl = (
const createMoonPayUrl = async (
walletAddress: string,
chainId: keyof typeof BUYABLE_CHAINS_MAP,
symbol: CurrencySymbol,
symbol?: CurrencySymbol,
): Promise<string> => {
const { moonPay: { defaultCurrencyCode, showOnlyCurrencies } = {} as any } =
BUYABLE_CHAINS_MAP[chainId];
const moonPayQueryParams = new URLSearchParams({
apiKey: MOONPAY_API_KEY,
walletAddress,
defaultCurrencyCode:
formatMoonpaySymbol(symbol, chainId) || defaultCurrencyCode,
defaultCurrencyCode: symbol
? formatMoonpaySymbol(symbol, chainId)
: defaultCurrencyCode,
showOnlyCurrencies,
});
const queryParams = new URLSearchParams({
@ -144,7 +146,7 @@ const createMoonPayUrl = async (
const createCoinbasePayUrl = (
walletAddress: string,
chainId: keyof typeof BUYABLE_CHAINS_MAP,
symbol: CurrencySymbol,
symbol?: CurrencySymbol,
): string => {
// since coinbasePayCurrencies is going to be extended to include all tokens supported
// we now default to nativeCurrency instead of the 2 previous tokens + eth that we had before
@ -180,25 +182,40 @@ export default async function getBuyUrl({
symbol,
}: {
chainId: keyof typeof BUYABLE_CHAINS_MAP;
address: string;
service: string;
symbol: CurrencySymbol;
address?: string;
service?: string;
symbol?: CurrencySymbol;
}): Promise<string> {
let serviceToUse = service;
// default service by network if not specified
if (!service) {
if (isNullOrUndefined(service)) {
// eslint-disable-next-line no-param-reassign
service = getDefaultServiceForChain(chainId);
serviceToUse = getDefaultServiceForChain(chainId);
}
switch (service) {
switch (serviceToUse) {
case 'wyre':
return await createWyrePurchaseUrl(address, chainId, symbol);
if (address) {
return await createWyrePurchaseUrl(address as string, chainId, symbol);
}
throw new Error('Address is required when requesting url for Wyre');
case 'transak':
return createTransakUrl(address, chainId, symbol);
if (address) {
return createTransakUrl(address as string, chainId, symbol);
}
throw new Error('Address is required when requesting url for Transak');
case 'moonpay':
return createMoonPayUrl(address, chainId, symbol);
if (address) {
return createMoonPayUrl(address as string, chainId, symbol);
}
throw new Error('Address is required when requesting url for Moonpay');
case 'coinbase':
return createCoinbasePayUrl(address, chainId, symbol);
if (address) {
return createCoinbasePayUrl(address as string, chainId, symbol);
}
throw new Error(
'Address is required when requesting url for Coinbase Pay',
);
case 'metamask-faucet':
return 'https://faucet.metamask.io/';
case 'goerli-faucet':

View File

@ -82,6 +82,7 @@ export const POLLING_TOKEN_ENVIRONMENT_TYPES = {
[ENVIRONMENT_TYPE_POPUP]: 'popupGasPollTokens',
[ENVIRONMENT_TYPE_NOTIFICATION]: 'notificationGasPollTokens',
[ENVIRONMENT_TYPE_FULLSCREEN]: 'fullScreenGasPollTokens',
[ENVIRONMENT_TYPE_BACKGROUND]: 'none',
} as const;
export const ORIGIN_METAMASK = 'metamask';

View File

@ -83,3 +83,31 @@ export enum NetworkCongestionThresholds {
stable = 0.33,
busy = 0.66,
}
export interface TxGasFees {
/** Maxmimum number of units of gas to use for this transaction. */
gasLimit: string;
/** Price per gas for legacy txs */
gasPrice: string;
/**
* Maximum amount per gas to pay for the transaction, including the priority
* fee.
*/
maxFeePerGas: string;
/** Maximum amount per gas to give to validator as incentive. */
maxPriorityFeePerGas: string;
/** Which estimate level was used */
estimateUsed: string;
/** Which estimate level that the API suggested. */
estimateSuggested: string;
/** The default estimate for gas. */
defaultGasEstimates: string;
/** same as gasLimit? */
gas: string;
/** Original estimate for gas. */
originalGasEstimate: string;
/** The gas limit supplied by user. */
userEditedGasLimit: string;
/** Estimate level user selected. */
userFeeLevel: string;
}

View File

@ -46,6 +46,8 @@
* @typedef {object} MetaMetricsEventPayload
* @property {string} event - event name to track
* @property {string} category - category to associate event to
* @property {number} [actionId] - Action id to deduplicate event requests from
* the UI
* @property {string} [environmentType] - The type of environment this event
* occurred in. Defaults to the background process type
* @property {object} [properties] - object of custom values to track, keys