2021-06-23 23:35:25 +02:00
|
|
|
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
|
|
|
|
import BigNumber from 'bignumber.js';
|
2021-09-09 22:56:27 +02:00
|
|
|
import { addHexPrefix } from 'ethereumjs-util';
|
2023-02-23 19:16:55 +01:00
|
|
|
import { cloneDeep, debounce } from 'lodash';
|
2022-07-01 15:58:35 +02:00
|
|
|
import { v4 as uuidv4 } from 'uuid';
|
2021-06-23 23:35:25 +02:00
|
|
|
import {
|
2023-01-24 15:44:49 +01:00
|
|
|
decimalToHex,
|
2023-01-20 18:04:37 +01:00
|
|
|
getValueFromWeiHex,
|
2021-07-06 19:48:49 +02:00
|
|
|
} from '../../../shared/modules/conversion.utils';
|
2023-01-27 19:28:03 +01:00
|
|
|
import { GasEstimateTypes, GAS_LIMITS } from '../../../shared/constants/gas';
|
2021-06-23 23:35:25 +02:00
|
|
|
import {
|
|
|
|
CONTRACT_ADDRESS_ERROR,
|
|
|
|
INSUFFICIENT_FUNDS_ERROR,
|
2022-11-28 17:41:42 +01:00
|
|
|
INSUFFICIENT_FUNDS_FOR_GAS_ERROR,
|
2021-06-23 23:35:25 +02:00
|
|
|
INSUFFICIENT_TOKENS_ERROR,
|
|
|
|
INVALID_RECIPIENT_ADDRESS_ERROR,
|
|
|
|
INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR,
|
|
|
|
KNOWN_RECIPIENT_ADDRESS_WARNING,
|
|
|
|
NEGATIVE_ETH_ERROR,
|
2022-08-03 21:35:34 +02:00
|
|
|
RECIPIENT_TYPES,
|
2021-06-23 23:35:25 +02:00
|
|
|
} from '../../pages/send/send.constants';
|
|
|
|
|
|
|
|
import {
|
|
|
|
isBalanceSufficient,
|
|
|
|
isTokenBalanceSufficient,
|
|
|
|
} from '../../pages/send/send.utils';
|
|
|
|
import {
|
|
|
|
getAdvancedInlineGasShown,
|
|
|
|
getCurrentChainId,
|
|
|
|
getGasPriceInHexWei,
|
|
|
|
getIsMainnet,
|
|
|
|
getTargetAccount,
|
2021-07-01 03:54:47 +02:00
|
|
|
getIsNonStandardEthChain,
|
2021-08-03 00:52:18 +02:00
|
|
|
checkNetworkAndAccountSupports1559,
|
2021-09-09 22:56:27 +02:00
|
|
|
getUseTokenDetection,
|
|
|
|
getTokenList,
|
2021-11-04 07:58:32 +01:00
|
|
|
getAddressBookEntryOrAccountName,
|
2022-03-29 21:21:45 +02:00
|
|
|
getIsMultiLayerFeeNetwork,
|
2022-06-13 18:18:33 +02:00
|
|
|
getEnsResolutionByAddress,
|
2022-07-01 15:58:35 +02:00
|
|
|
getSelectedAccount,
|
|
|
|
getSelectedAddress,
|
2021-06-23 23:35:25 +02:00
|
|
|
} from '../../selectors';
|
|
|
|
import {
|
2021-07-08 22:23:00 +02:00
|
|
|
disconnectGasFeeEstimatePoller,
|
2021-06-23 23:35:25 +02:00
|
|
|
displayWarning,
|
2021-07-08 22:23:00 +02:00
|
|
|
getGasFeeEstimatesAndStartPolling,
|
2021-06-23 23:35:25 +02:00
|
|
|
hideLoadingIndication,
|
|
|
|
showLoadingIndication,
|
2022-03-07 20:14:48 +01:00
|
|
|
updateEditableParams,
|
|
|
|
updateTransactionGasFees,
|
2021-08-04 23:53:13 +02:00
|
|
|
addPollingTokenToAppState,
|
|
|
|
removePollingTokenFromAppState,
|
2022-11-15 19:49:42 +01:00
|
|
|
isNftOwner,
|
2022-01-19 19:42:41 +01:00
|
|
|
getTokenStandardAndDetails,
|
|
|
|
showModal,
|
2022-04-26 19:07:39 +02:00
|
|
|
addUnapprovedTransactionAndRouteToConfirmationPage,
|
2022-05-04 18:54:46 +02:00
|
|
|
updateTransactionSendFlowHistory,
|
2021-06-23 23:35:25 +02:00
|
|
|
} from '../../store/actions';
|
2021-07-16 18:06:32 +02:00
|
|
|
import { setCustomGasLimit } from '../gas/gas.duck';
|
2021-06-23 23:35:25 +02:00
|
|
|
import {
|
|
|
|
QR_CODE_DETECTED,
|
|
|
|
SELECTED_ACCOUNT_CHANGED,
|
|
|
|
ACCOUNT_CHANGED,
|
|
|
|
ADDRESS_BOOK_UPDATED,
|
2021-07-08 22:23:00 +02:00
|
|
|
GAS_FEE_ESTIMATES_UPDATED,
|
2021-06-23 23:35:25 +02:00
|
|
|
} from '../../store/actionConstants';
|
|
|
|
import {
|
|
|
|
getTokenAddressParam,
|
2022-07-14 00:15:38 +02:00
|
|
|
getTokenMetadata,
|
2022-07-23 16:37:31 +02:00
|
|
|
getTokenIdParam,
|
2021-06-23 23:35:25 +02:00
|
|
|
} from '../../helpers/utils/token-util';
|
|
|
|
import {
|
|
|
|
checkExistingAddresses,
|
|
|
|
isDefaultMetaMaskChain,
|
|
|
|
isOriginContractAddress,
|
|
|
|
isValidDomainName,
|
|
|
|
} from '../../helpers/utils/util';
|
2021-07-16 18:06:32 +02:00
|
|
|
import {
|
|
|
|
getGasEstimateType,
|
|
|
|
getTokens,
|
|
|
|
getUnapprovedTxs,
|
|
|
|
} from '../metamask/metamask';
|
2021-08-03 00:52:18 +02:00
|
|
|
|
2022-11-21 16:19:11 +01:00
|
|
|
import { resetDomainResolution } from '../domains';
|
2021-06-23 23:35:25 +02:00
|
|
|
import {
|
|
|
|
isBurnAddress,
|
|
|
|
isValidHexAddress,
|
2022-06-13 18:18:33 +02:00
|
|
|
toChecksumHexAddress,
|
2021-06-23 23:35:25 +02:00
|
|
|
} from '../../../shared/modules/hexstring-utils';
|
2023-01-20 18:04:37 +01:00
|
|
|
import { isSmartContractAddress } from '../../helpers/utils/transactions.util';
|
2022-03-29 21:21:45 +02:00
|
|
|
import fetchEstimatedL1Fee from '../../helpers/utils/optimism/fetchEstimatedL1Fee';
|
|
|
|
|
2022-03-16 23:15:03 +01:00
|
|
|
import {
|
2023-01-18 15:47:29 +01:00
|
|
|
AssetType,
|
|
|
|
TokenStandard,
|
|
|
|
TransactionEnvelopeType,
|
|
|
|
TransactionType,
|
2022-03-16 23:15:03 +01:00
|
|
|
} from '../../../shared/constants/transaction';
|
2022-01-19 19:42:41 +01:00
|
|
|
import { INVALID_ASSET_TYPE } from '../../helpers/constants/error-keys';
|
2022-03-07 19:54:36 +01:00
|
|
|
import { isEqualCaseInsensitive } from '../../../shared/modules/string-utils';
|
2022-07-01 15:58:35 +02:00
|
|
|
import { parseStandardTokenTransactionData } from '../../../shared/modules/transaction.utils';
|
2022-09-16 21:05:21 +02:00
|
|
|
import { getTokenValueParam } from '../../../shared/lib/metamask-controller-utils';
|
|
|
|
import {
|
|
|
|
calcGasTotal,
|
|
|
|
calcTokenAmount,
|
|
|
|
} from '../../../shared/lib/transactions-controller-utils';
|
2023-01-24 15:44:49 +01:00
|
|
|
import { Numeric } from '../../../shared/modules/Numeric';
|
2023-01-25 16:47:02 +01:00
|
|
|
import { EtherDenomination } from '../../../shared/constants/common';
|
2022-07-01 15:58:35 +02:00
|
|
|
import {
|
|
|
|
estimateGasLimitForSend,
|
|
|
|
generateTransactionParams,
|
|
|
|
getRoundedGasPrice,
|
|
|
|
} from './helpers';
|
2022-05-25 22:54:05 +02:00
|
|
|
// typedef import statements
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
2022-05-25 22:54:05 +02:00
|
|
|
* @typedef {(
|
|
|
|
* import('immer/dist/internal').WritableDraft<SendState>
|
|
|
|
* )} SendStateDraft
|
|
|
|
* @typedef {(
|
|
|
|
* import( '../../helpers/constants/common').TokenStandardStrings
|
|
|
|
* )} TokenStandardStrings
|
|
|
|
* @typedef {(
|
2022-07-01 15:58:35 +02:00
|
|
|
* import( '../../../shared/constants/tokens').TokenDetails
|
|
|
|
* )} TokenDetails
|
|
|
|
* @typedef {(
|
2022-11-24 20:59:07 +01:00
|
|
|
* import('@metamask/gas-fee-controller').LegacyGasPriceEstimate
|
2022-05-25 22:54:05 +02:00
|
|
|
* )} LegacyGasPriceEstimate
|
|
|
|
* @typedef {(
|
2022-11-24 20:59:07 +01:00
|
|
|
* import('@metamask/gas-fee-controller').GasFeeEstimates
|
2022-05-25 22:54:05 +02:00
|
|
|
* )} GasFeeEstimates
|
|
|
|
* @typedef {(
|
2022-11-24 20:59:07 +01:00
|
|
|
* import('@metamask/gas-fee-controller').EthGasPriceEstimate
|
2022-05-25 22:54:05 +02:00
|
|
|
* )} EthGasPriceEstimate
|
|
|
|
* @typedef {(
|
2022-11-24 20:59:07 +01:00
|
|
|
* import('@metamask/gas-fee-controller').GasEstimateType
|
2022-05-25 22:54:05 +02:00
|
|
|
* )} GasEstimateType
|
2022-07-01 15:58:35 +02:00
|
|
|
* @typedef {(
|
|
|
|
* import('redux').AnyAction
|
|
|
|
* )} AnyAction
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* @template R - Return type of the async function
|
|
|
|
* @typedef {(
|
|
|
|
* import('redux-thunk').ThunkAction<R, MetaMaskState, unknown, AnyAction>
|
|
|
|
* )} ThunkAction<R>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This type will take a typical constant string mapped object and turn it into
|
|
|
|
* a union type of the values.
|
|
|
|
*
|
|
|
|
* @template O - The object to make strings out of
|
|
|
|
* @typedef {O[keyof O]} MapValuesToUnion<O>
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-05-25 22:54:05 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} SendStateStages
|
2022-05-25 22:54:05 +02:00
|
|
|
* @property {'ADD_RECIPIENT'} ADD_RECIPIENT - The user is selecting which
|
|
|
|
* address to send an asset to.
|
|
|
|
* @property {'DRAFT'} DRAFT - The send form is shown for a transaction yet to
|
|
|
|
* be sent to the Transaction Controller.
|
|
|
|
* @property {'EDIT'} EDIT - The send form is shown for a transaction already
|
|
|
|
* submitted to the Transaction Controller but not yet confirmed. This happens
|
|
|
|
* when a confirmation is shown for a transaction and the 'edit' button in the
|
|
|
|
* header is clicked.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {'INACTIVE'} INACTIVE - The send state is idle, and hasn't yet
|
|
|
|
* fetched required data for gasPrice and gasLimit estimations, etc.
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
|
|
|
* The Stages that the send slice can be in
|
2022-05-25 22:54:05 +02:00
|
|
|
*
|
|
|
|
* @type {SendStateStages}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export const SEND_STAGES = {
|
|
|
|
ADD_RECIPIENT: 'ADD_RECIPIENT',
|
|
|
|
DRAFT: 'DRAFT',
|
|
|
|
EDIT: 'EDIT',
|
2022-07-01 15:58:35 +02:00
|
|
|
INACTIVE: 'INACTIVE',
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} DraftTxStatus
|
2022-05-25 22:54:05 +02:00
|
|
|
* @property {'INVALID'} INVALID - The transaction is invalid and cannot be
|
|
|
|
* submitted. There are a number of cases that would result in an invalid
|
|
|
|
* send state:
|
|
|
|
* 1. The recipient is not yet defined
|
|
|
|
* 2. The amount + gasTotal is greater than the user's balance when sending
|
|
|
|
* native currency
|
|
|
|
* 3. The gasTotal is greater than the user's *native* balance
|
|
|
|
* 4. The amount of sent asset is greater than the user's *asset* balance
|
|
|
|
* 5. Gas price estimates failed to load entirely
|
|
|
|
* 6. The gasLimit is less than 21000 (0x5208)
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {'VALID'} VALID - The transaction is valid and can be submitted.
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The status of the send slice
|
|
|
|
*
|
2022-07-01 15:58:35 +02:00
|
|
|
* @type {DraftTxStatus}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export const SEND_STATUSES = {
|
|
|
|
INVALID: 'INVALID',
|
2022-07-01 15:58:35 +02:00
|
|
|
VALID: 'VALID',
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
|
2022-05-25 22:54:05 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} SendStateGasModes
|
2022-05-25 22:54:05 +02:00
|
|
|
* @property {'BASIC'} BASIC - Shows the basic estimate slow/avg/fast buttons
|
|
|
|
* when on mainnet and the metaswaps API request is successful.
|
|
|
|
* @property {'CUSTOM'} CUSTOM - Shows GasFeeDisplay component that is a read
|
|
|
|
* only display of the values the user has set in the advanced gas modal
|
|
|
|
* (stored in the gas duck under the customData key).
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {'INLINE'} INLINE - Shows inline gasLimit/gasPrice fields when on
|
|
|
|
* any other network or metaswaps API fails and we use eth_gasPrice.
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
|
|
|
* Controls what is displayed in the send-gas-row component.
|
2022-05-25 22:54:05 +02:00
|
|
|
*
|
|
|
|
* @type {SendStateGasModes}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export const GAS_INPUT_MODES = {
|
|
|
|
BASIC: 'BASIC',
|
|
|
|
CUSTOM: 'CUSTOM',
|
2022-07-01 15:58:35 +02:00
|
|
|
INLINE: 'INLINE',
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
|
2022-05-25 22:54:05 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} SendStateAmountModes
|
2022-05-25 22:54:05 +02:00
|
|
|
* @property {'INPUT'} INPUT - the user provides the amount by typing in the
|
|
|
|
* field.
|
|
|
|
* @property {'MAX'} MAX - The user selects the MAX button and amount is
|
|
|
|
* calculated based on balance - (amount + gasTotal).
|
|
|
|
*/
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
|
|
|
* The modes that the amount field can be set by
|
2022-05-25 22:54:05 +02:00
|
|
|
*
|
|
|
|
* @type {SendStateAmountModes}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export const AMOUNT_MODES = {
|
|
|
|
INPUT: 'INPUT',
|
|
|
|
MAX: 'MAX',
|
|
|
|
};
|
|
|
|
|
2022-05-25 22:54:05 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} SendStateRecipientModes
|
2022-05-25 22:54:05 +02:00
|
|
|
* @property {'CONTACT_LIST'} CONTACT_LIST - The user is displayed a list of
|
|
|
|
* their contacts and addresses they have recently send to.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {'MY_ACCOUNTS'} MY_ACCOUNTS - the user is displayed a list of
|
|
|
|
* their own accounts to send to.
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The type of recipient list that is displayed to user
|
|
|
|
*
|
|
|
|
* @type {SendStateRecipientModes}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export const RECIPIENT_SEARCH_MODES = {
|
|
|
|
CONTACT_LIST: 'CONTACT_LIST',
|
2022-07-01 15:58:35 +02:00
|
|
|
MY_ACCOUNTS: 'MY_ACCOUNTS',
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} Account
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} address - The hex address of the account.
|
|
|
|
* @property {string} balance - Hex string representing the native asset
|
|
|
|
* balance of the account the transaction will be sent from.
|
|
|
|
*/
|
2021-07-16 18:06:32 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} Amount
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} [error] - Error to display for the amount field.
|
|
|
|
* @property {string} value - A hex string representing the amount of the
|
|
|
|
* selected currency to send.
|
|
|
|
*/
|
2022-01-10 17:23:53 +01:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} Asset
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} balance - A hex string representing the balance
|
|
|
|
* that the user holds of the asset that they are attempting to send.
|
|
|
|
* @property {TokenDetails} [details] - An object that describes the
|
|
|
|
* selected asset in the case that the user is sending a token or collectibe.
|
|
|
|
* Will be null when asset.type is 'NATIVE'.
|
|
|
|
* @property {string} [error] - Error to display when there is an issue
|
|
|
|
* with the asset.
|
2023-01-18 15:47:29 +01:00
|
|
|
* @property {AssetType} type - The type of asset that the user
|
2022-07-01 15:58:35 +02:00
|
|
|
* is attempting to send. Defaults to 'NATIVE' which represents the native
|
2022-11-15 19:49:42 +01:00
|
|
|
* asset of the chain. Can also be 'TOKEN' or 'NFT'.
|
2022-07-01 15:58:35 +02:00
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} GasFees
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} [error] - error to display for gas fields.
|
|
|
|
* @property {string} gasLimit - maximum gas needed for tx.
|
|
|
|
* @property {string} gasPrice - price in wei to pay per gas.
|
|
|
|
* @property {string} gasTotal - maximum total price in wei to pay.
|
|
|
|
* @property {string} maxFeePerGas - Maximum price in wei to pay per gas.
|
|
|
|
* @property {string} maxPriorityFeePerGas - Maximum priority fee in wei to pay
|
|
|
|
* per gas.
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* An object that describes the intended recipient of a transaction.
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} Recipient
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} address - The fully qualified address of the recipient.
|
|
|
|
* This is set after the recipient.userInput is validated, the userInput field
|
|
|
|
* is quickly updated to avoid delay between keystrokes and seeing the input
|
|
|
|
* field updated. After a debounce the address typed is validated and then the
|
|
|
|
* address field is updated. The address field is also set when the user
|
|
|
|
* selects a contact or account from the list, or an ENS resolution when
|
|
|
|
* typing ENS names.
|
|
|
|
* @property {string} [error] - Error to display on the address field.
|
|
|
|
* @property {string} nickname - The nickname that the user has added to their
|
|
|
|
* address book for the recipient.address.
|
|
|
|
* @property {string} [warning] - Warning to display on the address field.
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} DraftTransaction
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {Amount} amount - An object containing information about the
|
|
|
|
* amount of currency to send.
|
|
|
|
* @property {Asset} asset - An object that describes the asset that the user
|
|
|
|
* has selected to send.
|
|
|
|
* @property {Account} [fromAccount] - The send flow is usually only relative to
|
|
|
|
* the currently selected account. When editing a transaction, however, the
|
|
|
|
* account may differ. In that case, the details of that account will be
|
|
|
|
* stored in this object within the draftTransaction.
|
|
|
|
* @property {GasFees} gas - Details about the current gas settings
|
|
|
|
* @property {Array<{event: string, timestamp: number}>} history - An array of
|
|
|
|
* entries that describe the user's journey through the send flow. This is
|
|
|
|
* sent to the controller for attaching to state logs for troubleshooting and
|
|
|
|
* support.
|
|
|
|
* @property {string} [id] - If the transaction has already been added to the
|
|
|
|
* TransactionController this field will be populated with its id from the
|
|
|
|
* TransactionController state. This is required to be able to update the
|
|
|
|
* transaction in the controller.
|
|
|
|
* @property {Recipient} recipient - An object that describes the intended
|
|
|
|
* recipient of the transaction.
|
|
|
|
* @property {MapValuesToUnion<DraftTxStatus>} status - Describes the
|
|
|
|
* validity of the draft transaction, which will be either 'VALID' or
|
|
|
|
* 'INVALID', depending on our ability to generate a valid txParams object for
|
|
|
|
* submission.
|
|
|
|
* @property {string} transactionType - Determines type of transaction being
|
|
|
|
* sent, defaulted to 0x0 (legacy).
|
|
|
|
* @property {string} [userInputHexData] - When a user has enabled custom hex
|
|
|
|
* data field in advanced options, they can supply data to the field which is
|
|
|
|
* stored under this key.
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* @type {DraftTransaction}
|
|
|
|
*/
|
|
|
|
export const draftTransactionInitialState = {
|
|
|
|
amount: {
|
|
|
|
error: null,
|
|
|
|
value: '0x0',
|
|
|
|
},
|
|
|
|
asset: {
|
|
|
|
balance: '0x0',
|
|
|
|
details: null,
|
|
|
|
error: null,
|
2023-01-18 15:47:29 +01:00
|
|
|
type: AssetType.native,
|
2022-07-01 15:58:35 +02:00
|
|
|
},
|
|
|
|
fromAccount: null,
|
|
|
|
gas: {
|
|
|
|
error: null,
|
|
|
|
gasLimit: '0x0',
|
|
|
|
gasPrice: '0x0',
|
|
|
|
gasTotal: '0x0',
|
|
|
|
maxFeePerGas: '0x0',
|
|
|
|
maxPriorityFeePerGas: '0x0',
|
2022-08-18 18:03:23 +02:00
|
|
|
wasManuallyEdited: false,
|
2022-07-01 15:58:35 +02:00
|
|
|
},
|
|
|
|
history: [],
|
|
|
|
id: null,
|
|
|
|
recipient: {
|
|
|
|
address: '',
|
|
|
|
error: null,
|
|
|
|
nickname: '',
|
|
|
|
warning: null,
|
2022-08-03 21:35:34 +02:00
|
|
|
type: '',
|
2022-07-14 00:15:38 +02:00
|
|
|
recipientWarningAcknowledged: false,
|
2022-07-01 15:58:35 +02:00
|
|
|
},
|
|
|
|
status: SEND_STATUSES.VALID,
|
2023-01-18 15:47:29 +01:00
|
|
|
transactionType: TransactionEnvelopeType.legacy,
|
2022-07-01 15:58:35 +02:00
|
|
|
userInputHexData: null,
|
|
|
|
};
|
2021-07-01 03:54:47 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Describes the state tree of the send slice
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} SendState
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {MapValuesToUnion<SendStateAmountModes>} amountMode - Describe
|
|
|
|
* whether the user has manually input an amount or if they have selected max
|
|
|
|
* to send the maximum amount of the selected currency.
|
|
|
|
* @property {string} currentTransactionUUID - The UUID of the transaction
|
|
|
|
* currently being modified by the send flow. This UUID is generated upon
|
|
|
|
* initialization of the send flow, any previous UUIDs are discarded at
|
|
|
|
* clean up AND during initialization. When a transaction is edited a new UUID
|
|
|
|
* is generated for it and the state of that transaction is copied into a new
|
|
|
|
* entry in the draftTransactions object.
|
2022-07-26 20:10:51 +02:00
|
|
|
* @property {Object<string, DraftTransaction>} draftTransactions - An object keyed
|
2022-07-01 15:58:35 +02:00
|
|
|
* by UUID with draftTransactions as the values.
|
|
|
|
* @property {boolean} eip1559support - tracks whether the current network
|
|
|
|
* supports EIP 1559 transactions.
|
|
|
|
* @property {boolean} gasEstimateIsLoading - Indicates whether the gas
|
|
|
|
* estimate is loading.
|
|
|
|
* @property {string} [gasEstimatePollToken] - String token identifying a
|
|
|
|
* listener for polling on the gasFeeController
|
|
|
|
* @property {boolean} gasIsSetInModal - true if the user set custom gas in the
|
|
|
|
* custom gas modal
|
|
|
|
* @property {string} gasLimitMinimum - minimum supported gasLimit.
|
|
|
|
* @property {string} gasPriceEstimate - Expected price in wei necessary to
|
|
|
|
* pay per gas used for a transaction to be included in a reasonable timeframe.
|
|
|
|
* Comes from the GasFeeController.
|
|
|
|
* @property {string} gasTotalForLayer1 - Layer 1 gas fee total on multi-layer
|
|
|
|
* fee networks
|
|
|
|
* @property {string} recipientInput - The user input of the recipient
|
|
|
|
* which is updated quickly to avoid delays in the UI reflecting manual entry
|
|
|
|
* of addresses.
|
|
|
|
* @property {MapValuesToUnion<SendStateRecipientModes>} recipientMode -
|
|
|
|
* Describes which list of recipients the user is shown on the add recipient
|
|
|
|
* screen. When this key is set to 'MY_ACCOUNTS' the user is shown the list of
|
|
|
|
* accounts they own. When it is 'CONTACT_LIST' the user is shown the list of
|
|
|
|
* contacts they have saved in MetaMask and any addresses they have recently
|
|
|
|
* sent to.
|
|
|
|
* @property {Account} selectedAccount - The currently selected account in
|
|
|
|
* MetaMask. Native balance and address will be pulled from this account if a
|
|
|
|
* fromAccount is not specified in the draftTransaction object. During an edit
|
|
|
|
* the fromAccount is specified.
|
|
|
|
* @property {MapValuesToUnion<SendStateStages>} stage - The stage of the
|
|
|
|
* send flow that the user has progressed to. Defaults to 'INACTIVE' which
|
|
|
|
* results in the send screen not being shown.
|
|
|
|
*/
|
2021-07-01 03:54:47 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* @type {SendState}
|
|
|
|
*/
|
|
|
|
export const initialState = {
|
|
|
|
amountMode: AMOUNT_MODES.INPUT,
|
|
|
|
currentTransactionUUID: null,
|
|
|
|
draftTransactions: {},
|
|
|
|
eip1559support: false,
|
|
|
|
gasEstimateIsLoading: true,
|
|
|
|
gasEstimatePollToken: null,
|
|
|
|
gasIsSetInModal: false,
|
|
|
|
gasPriceEstimate: '0x0',
|
|
|
|
gasLimitMinimum: GAS_LIMITS.SIMPLE,
|
|
|
|
gasTotalForLayer1: '0x0',
|
|
|
|
recipientMode: RECIPIENT_SEARCH_MODES.CONTACT_LIST,
|
|
|
|
recipientInput: '',
|
|
|
|
selectedAccount: {
|
|
|
|
address: null,
|
|
|
|
balance: '0x0',
|
|
|
|
},
|
|
|
|
stage: SEND_STAGES.INACTIVE,
|
|
|
|
};
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* TODO: We really need to start creating the metamask state type, and the
|
|
|
|
* entire state tree of redux. Would be *extremely* valuable in future
|
|
|
|
* typescript conversions. The metamask key is typed as an object on purpose
|
|
|
|
* here because I cannot go so far in this work as to type that entire object.
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} MetaMaskState
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {SendState} send - The state of the send flow.
|
2022-07-27 15:28:05 +02:00
|
|
|
* @property {object} metamask - The state of the metamask store.
|
2022-07-01 15:58:35 +02:00
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
const name = 'send';
|
2021-06-23 23:35:25 +02:00
|
|
|
|
|
|
|
// After modification of specific fields in specific circumstances we must
|
|
|
|
// recompute the gasLimit estimate to be as accurate as possible. the cases
|
|
|
|
// that necessitate this logic are listed below:
|
|
|
|
// 1. when the amount sent changes when sending a token due to the amount being
|
|
|
|
// part of the hex encoded data property of the transaction.
|
|
|
|
// 2. when updating the data property while sending NATIVE currency (ex: ETH)
|
|
|
|
// because the data parameter defines function calls that the EVM will have
|
|
|
|
// to execute which is where a large chunk of gas is potentially consumed.
|
|
|
|
// 3. when the recipient changes while sending a token due to the recipient's
|
|
|
|
// address being included in the hex encoded data property of the
|
|
|
|
// transaction
|
|
|
|
// 4. when the asset being sent changes due to the contract address and details
|
|
|
|
// of the token being included in the hex encoded data property of the
|
|
|
|
// transaction. If switching to NATIVE currency (ex: ETH), the gasLimit will
|
|
|
|
// change due to hex data being removed (unless supplied by user).
|
|
|
|
// This method computes the gasLimit estimate which is written to state in an
|
|
|
|
// action handler in extraReducers.
|
|
|
|
export const computeEstimatedGasLimit = createAsyncThunk(
|
|
|
|
'send/computeEstimatedGasLimit',
|
|
|
|
async (_, thunkApi) => {
|
2021-07-01 03:54:47 +02:00
|
|
|
const state = thunkApi.getState();
|
|
|
|
const { send, metamask } = state;
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
send.draftTransactions[send.currentTransactionUUID];
|
2022-01-21 16:58:59 +01:00
|
|
|
const unapprovedTxs = getUnapprovedTxs(state);
|
2022-03-29 21:21:45 +02:00
|
|
|
const isMultiLayerFeeNetwork = getIsMultiLayerFeeNetwork(state);
|
2022-07-01 15:58:35 +02:00
|
|
|
const transaction = unapprovedTxs[draftTransaction.id];
|
2021-07-01 03:54:47 +02:00
|
|
|
const isNonStandardEthChain = getIsNonStandardEthChain(state);
|
2021-07-01 23:48:30 +02:00
|
|
|
const chainId = getCurrentChainId(state);
|
2022-03-29 21:21:45 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
let gasTotalForLayer1;
|
2022-03-29 21:21:45 +02:00
|
|
|
if (isMultiLayerFeeNetwork) {
|
2022-12-20 21:05:07 +01:00
|
|
|
gasTotalForLayer1 = await fetchEstimatedL1Fee({
|
2022-03-29 21:21:45 +02:00
|
|
|
txParams: {
|
2022-07-01 15:58:35 +02:00
|
|
|
gasPrice: draftTransaction.gas.gasPrice,
|
|
|
|
gas: draftTransaction.gas.gasLimit,
|
|
|
|
to: draftTransaction.recipient.address?.toLowerCase(),
|
2022-03-29 21:21:45 +02:00
|
|
|
value:
|
2022-07-01 15:58:35 +02:00
|
|
|
send.amountMode === AMOUNT_MODES.MAX
|
|
|
|
? send.selectedAccount.balance
|
2022-08-02 20:24:24 +02:00
|
|
|
: draftTransaction.amount.value,
|
2022-07-01 15:58:35 +02:00
|
|
|
from: send.selectedAccount.address,
|
|
|
|
data: draftTransaction.userInputHexData,
|
2022-03-29 21:21:45 +02:00
|
|
|
type: '0x0',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:58:59 +01:00
|
|
|
if (
|
|
|
|
send.stage !== SEND_STAGES.EDIT ||
|
|
|
|
!transaction.dappSuggestedGasFees?.gas ||
|
|
|
|
!transaction.userEditedGasLimit
|
|
|
|
) {
|
2021-06-23 23:35:25 +02:00
|
|
|
const gasLimit = await estimateGasLimitForSend({
|
2022-07-01 15:58:35 +02:00
|
|
|
gasPrice: draftTransaction.gas.gasPrice,
|
2021-07-16 18:06:32 +02:00
|
|
|
blockGasLimit: metamask.currentBlockGasLimit,
|
2021-06-23 23:35:25 +02:00
|
|
|
selectedAddress: metamask.selectedAddress,
|
2022-07-01 15:58:35 +02:00
|
|
|
sendToken: draftTransaction.asset.details,
|
|
|
|
to: draftTransaction.recipient.address?.toLowerCase(),
|
|
|
|
value: draftTransaction.amount.value,
|
|
|
|
data: draftTransaction.userInputHexData,
|
2021-07-01 03:54:47 +02:00
|
|
|
isNonStandardEthChain,
|
2021-07-01 23:48:30 +02:00
|
|
|
chainId,
|
2022-07-01 15:58:35 +02:00
|
|
|
gasLimit: draftTransaction.gas.gasLimit,
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
await thunkApi.dispatch(setCustomGasLimit(gasLimit));
|
|
|
|
return {
|
|
|
|
gasLimit,
|
2022-07-01 15:58:35 +02:00
|
|
|
gasTotalForLayer1,
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} Asset
|
2023-01-18 15:47:29 +01:00
|
|
|
* @property {AssetType} type - The type of asset that the user
|
2022-07-01 15:58:35 +02:00
|
|
|
* is attempting to send. Defaults to 'NATIVE' which represents the native
|
2022-11-15 19:49:42 +01:00
|
|
|
* asset of the chain. Can also be 'TOKEN' or 'NFT'.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} balance - A hex string representing the balance
|
|
|
|
* that the user holds of the asset that they are attempting to send.
|
|
|
|
* @property {TokenDetails} [details] - An object that describes the
|
|
|
|
* selected asset in the case that the user is sending a token or collectibe.
|
|
|
|
* Will be null when asset.type is 'NATIVE'.
|
|
|
|
* @property {string} [error] - Error to display when there is an issue
|
|
|
|
* with the asset.
|
|
|
|
*/
|
2021-07-16 18:06:32 +02:00
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
|
|
|
* Responsible for initializing required state for the send slice.
|
|
|
|
* This method is dispatched from the send page in the componentDidMount
|
|
|
|
* method. It is also dispatched anytime the network changes to ensure that
|
|
|
|
* the slice remains valid with changing token and account balances. To do so
|
|
|
|
* it keys into state to get necessary values and computes a starting point for
|
|
|
|
* the send slice. It returns the values that might change from this action and
|
|
|
|
* those values are written to the slice in the `initializeSendState.fulfilled`
|
|
|
|
* action handler.
|
2023-02-03 18:56:44 +01:00
|
|
|
*
|
|
|
|
* @type {import('@reduxjs/toolkit').AsyncThunk<any, { chainHasChanged: boolean }, {}>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export const initializeSendState = createAsyncThunk(
|
|
|
|
'send/initializeSendState',
|
2022-07-01 15:58:35 +02:00
|
|
|
async ({ chainHasChanged = false } = {}, thunkApi) => {
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} ReduxState
|
|
|
|
* @property {object} metamask - Half baked type for the MetaMask object
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {SendState} send - the send state
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @type {ReduxState}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
const state = thunkApi.getState();
|
2021-07-01 03:54:47 +02:00
|
|
|
const isNonStandardEthChain = getIsNonStandardEthChain(state);
|
2021-07-01 23:48:30 +02:00
|
|
|
const chainId = getCurrentChainId(state);
|
2021-08-03 00:52:18 +02:00
|
|
|
const eip1559support = checkNetworkAndAccountSupports1559(state);
|
2022-07-01 15:58:35 +02:00
|
|
|
const account = getSelectedAccount(state);
|
|
|
|
const { send: sendState, metamask } = state;
|
|
|
|
const draftTransaction =
|
|
|
|
sendState.draftTransactions[sendState.currentTransactionUUID];
|
|
|
|
|
|
|
|
// If the draft transaction is not present, then this action has been
|
|
|
|
// dispatched out of sync with the intended flow. This is not always a bug.
|
|
|
|
// For instance, in the actions.js file we dispatch this action anytime the
|
|
|
|
// chain changes.
|
|
|
|
if (!draftTransaction) {
|
2022-08-02 20:52:09 +02:00
|
|
|
return thunkApi.rejectWithValue(
|
2022-07-01 15:58:35 +02:00
|
|
|
'draftTransaction not found, possibly not on send flow',
|
|
|
|
);
|
|
|
|
}
|
2021-07-08 22:23:00 +02:00
|
|
|
|
2021-07-31 02:45:18 +02:00
|
|
|
// Default gasPrice to 1 gwei if all estimation fails, this is only used
|
|
|
|
// for gasLimit estimation and won't be set directly in state. Instead, we
|
|
|
|
// will return the gasFeeEstimates and gasEstimateType so that the reducer
|
|
|
|
// can set the appropriate gas fees in state.
|
2022-07-01 15:58:35 +02:00
|
|
|
let gasPrice =
|
|
|
|
sendState.stage === SEND_STAGES.EDIT
|
|
|
|
? draftTransaction.gas.gasPrice
|
|
|
|
: '0x1';
|
2021-07-08 22:23:00 +02:00
|
|
|
let gasEstimatePollToken = null;
|
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
// Instruct the background process that polling for gas prices should begin
|
|
|
|
gasEstimatePollToken = await getGasFeeEstimatesAndStartPolling();
|
2021-08-04 23:53:13 +02:00
|
|
|
|
|
|
|
addPollingTokenToAppState(gasEstimatePollToken);
|
|
|
|
|
2021-07-16 18:06:32 +02:00
|
|
|
const {
|
|
|
|
metamask: { gasFeeEstimates, gasEstimateType },
|
|
|
|
} = thunkApi.getState();
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
if (sendState.stage !== SEND_STAGES.EDIT) {
|
|
|
|
// Because we are only interested in getting a gasLimit estimation we only
|
|
|
|
// need to worry about gasPrice. So we use maxFeePerGas as gasPrice if we
|
|
|
|
// have a fee market estimation.
|
2023-01-27 19:28:03 +01:00
|
|
|
if (gasEstimateType === GasEstimateTypes.legacy) {
|
2022-07-01 15:58:35 +02:00
|
|
|
gasPrice = getGasPriceInHexWei(gasFeeEstimates.medium);
|
2023-01-27 19:28:03 +01:00
|
|
|
} else if (gasEstimateType === GasEstimateTypes.ethGasPrice) {
|
2022-07-01 15:58:35 +02:00
|
|
|
gasPrice = getRoundedGasPrice(gasFeeEstimates.gasPrice);
|
2023-01-27 19:28:03 +01:00
|
|
|
} else if (gasEstimateType === GasEstimateTypes.feeMarket) {
|
2022-07-01 15:58:35 +02:00
|
|
|
gasPrice = getGasPriceInHexWei(
|
|
|
|
gasFeeEstimates.medium.suggestedMaxFeePerGas,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
gasPrice = gasFeeEstimates.gasPrice
|
|
|
|
? getRoundedGasPrice(gasFeeEstimates.gasPrice)
|
|
|
|
: '0x0';
|
|
|
|
}
|
2021-07-08 22:23:00 +02:00
|
|
|
}
|
2021-07-16 18:06:32 +02:00
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
// Set a basic gasLimit in the event that other estimation fails
|
2022-07-01 15:58:35 +02:00
|
|
|
let { gasLimit } = draftTransaction.gas;
|
2021-06-23 23:35:25 +02:00
|
|
|
if (
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType !== GasEstimateTypes.none &&
|
2022-07-01 15:58:35 +02:00
|
|
|
sendState.stage !== SEND_STAGES.EDIT &&
|
|
|
|
draftTransaction.recipient.address
|
2021-06-23 23:35:25 +02:00
|
|
|
) {
|
2022-07-01 15:58:35 +02:00
|
|
|
gasLimit =
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type === AssetType.token ||
|
|
|
|
draftTransaction.asset.type === AssetType.NFT
|
2022-07-01 15:58:35 +02:00
|
|
|
? GAS_LIMITS.BASE_TOKEN_ESTIMATE
|
|
|
|
: GAS_LIMITS.SIMPLE;
|
2021-06-23 23:35:25 +02:00
|
|
|
// Run our estimateGasLimit logic to get a more accurate estimation of
|
|
|
|
// required gas. If this value isn't nullish, set it as the new gasLimit
|
|
|
|
const estimatedGasLimit = await estimateGasLimitForSend({
|
2021-07-08 22:23:00 +02:00
|
|
|
gasPrice,
|
2021-07-16 18:06:32 +02:00
|
|
|
blockGasLimit: metamask.currentBlockGasLimit,
|
2022-07-01 15:58:35 +02:00
|
|
|
selectedAddress:
|
|
|
|
draftTransaction.fromAccount?.address ??
|
|
|
|
sendState.selectedAccount.address,
|
|
|
|
sendToken: draftTransaction.asset.details,
|
|
|
|
to: draftTransaction.recipient.address.toLowerCase(),
|
|
|
|
value: draftTransaction.amount.value,
|
|
|
|
data: draftTransaction.userInputHexData,
|
2021-07-01 03:54:47 +02:00
|
|
|
isNonStandardEthChain,
|
2021-07-01 23:48:30 +02:00
|
|
|
chainId,
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
gasLimit = estimatedGasLimit || gasLimit;
|
|
|
|
}
|
2022-05-25 22:54:05 +02:00
|
|
|
// We have to keep the gas slice in sync with the send slice state
|
2021-06-23 23:35:25 +02:00
|
|
|
// so that it'll be initialized correctly if the gas modal is opened.
|
|
|
|
await thunkApi.dispatch(setCustomGasLimit(gasLimit));
|
2022-08-02 20:52:09 +02:00
|
|
|
|
|
|
|
// There may be a case where the send has been canceled by the user while
|
|
|
|
// the gas estimate is being computed. So we check again to make sure that
|
|
|
|
// a currentTransactionUUID exists and matches the previous tx.
|
|
|
|
const newState = thunkApi.getState();
|
|
|
|
if (
|
|
|
|
newState.send.currentTransactionUUID !== sendState.currentTransactionUUID
|
|
|
|
) {
|
|
|
|
return thunkApi.rejectWithValue(
|
|
|
|
`draftTransaction changed during initialization.
|
|
|
|
A new initializeSendState action must be dispatched.`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
return {
|
2022-07-01 15:58:35 +02:00
|
|
|
account,
|
2021-06-23 23:35:25 +02:00
|
|
|
chainId: getCurrentChainId(state),
|
|
|
|
tokens: getTokens(state),
|
2022-07-01 15:58:35 +02:00
|
|
|
chainHasChanged,
|
2021-07-31 02:45:18 +02:00
|
|
|
gasFeeEstimates,
|
|
|
|
gasEstimateType,
|
2021-06-23 23:35:25 +02:00
|
|
|
gasLimit,
|
|
|
|
gasTotal: addHexPrefix(calcGasTotal(gasLimit, gasPrice)),
|
2021-07-08 22:23:00 +02:00
|
|
|
gasEstimatePollToken,
|
2021-07-31 02:45:18 +02:00
|
|
|
eip1559support,
|
2021-09-09 22:56:27 +02:00
|
|
|
useTokenDetection: getUseTokenDetection(state),
|
|
|
|
tokenAddressList: Object.keys(getTokenList(state)),
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
// Action Payload Typedefs
|
2022-05-25 22:54:05 +02:00
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<string>
|
|
|
|
* )} SimpleStringPayload
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<MapValuesToUnion<SendStateAmountModes>>
|
|
|
|
* )} SendStateAmountModePayload
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<DraftTransaction['asset']>
|
|
|
|
* )} UpdateAssetPayload
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<Partial<
|
|
|
|
* Pick<DraftTransaction['recipient'], 'address' | 'nickname'>>
|
|
|
|
* >
|
|
|
|
* )} updateRecipientPayload
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<SendState['recipientMode']>
|
|
|
|
* )} UpdateRecipientModePayload
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} GasFeeUpdateParams
|
2023-01-18 15:47:29 +01:00
|
|
|
* @property {TransactionType} transactionType - The transaction type
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {string} [maxFeePerGas] - The maximum amount in hex wei to pay
|
|
|
|
* per gas on a FEE_MARKET transaction.
|
|
|
|
* @property {string} [maxPriorityFeePerGas] - The maximum amount in hex
|
|
|
|
* wei to pay per gas as an incentive to miners on a FEE_MARKET
|
|
|
|
* transaction.
|
|
|
|
* @property {string} [gasPrice] - The amount in hex wei to pay per gas on
|
|
|
|
* a LEGACY transaction.
|
|
|
|
* @property {boolean} [isAutomaticUpdate] - true if the update is the
|
|
|
|
* result of a gas estimate update from the controller.
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<GasFeeUpdateParams>
|
|
|
|
* )} GasFeeUpdatePayload
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-05-24 23:05:27 +02:00
|
|
|
/**
|
2022-07-27 15:28:05 +02:00
|
|
|
* @typedef {object} GasEstimateUpdateParams
|
2022-07-01 15:58:35 +02:00
|
|
|
* @property {GasEstimateType} gasEstimateType - The type of gas estimation
|
|
|
|
* provided by the controller.
|
|
|
|
* @property {(
|
|
|
|
* EthGasPriceEstimate | LegacyGasPriceEstimate | GasFeeEstimates
|
|
|
|
* )} gasFeeEstimates - The gas fee estimates provided by the controller.
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<GasEstimateUpdateParams>
|
|
|
|
* )} GasEstimateUpdatePayload
|
|
|
|
*/
|
2022-05-24 23:05:27 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<DraftTransaction['asset']>
|
|
|
|
* )} UpdateAssetPayload
|
|
|
|
* @typedef {(
|
|
|
|
* import('@reduxjs/toolkit').PayloadAction<DraftTransaction>
|
|
|
|
* )} DraftTransactionPayload
|
|
|
|
*/
|
2022-05-24 23:05:27 +02:00
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
const slice = createSlice({
|
|
|
|
name,
|
|
|
|
initialState,
|
|
|
|
reducers: {
|
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Adds a new draft transaction to state, first generating a new UUID for
|
|
|
|
* the transaction and setting that as the currentTransactionUUID. If the
|
|
|
|
* draft has an id property set, the stage is set to EDIT.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {DraftTransactionPayload} action - An action with payload that is
|
|
|
|
* a new draft transaction that will be added to state.
|
|
|
|
* @returns {void}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
addNewDraft: (state, action) => {
|
|
|
|
state.currentTransactionUUID = uuidv4();
|
|
|
|
state.draftTransactions[state.currentTransactionUUID] = action.payload;
|
|
|
|
if (action.payload.id) {
|
|
|
|
state.stage = SEND_STAGES.EDIT;
|
|
|
|
} else {
|
|
|
|
state.stage = SEND_STAGES.ADD_RECIPIENT;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Adds an entry, with timestamp, to the draftTransaction history.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {SimpleStringPayload} action - An action with payload that is
|
|
|
|
* a string to be added to the history of the draftTransaction
|
|
|
|
* @returns {void}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
addHistoryEntry: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
if (draftTransaction) {
|
|
|
|
draftTransaction.history.push({
|
|
|
|
entry: action.payload,
|
|
|
|
timestamp: Date.now(),
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* gasTotal is computed based on gasPrice and gasLimit and set in state
|
|
|
|
* recomputes the maximum amount if the current amount mode is 'MAX' and
|
|
|
|
* sending the native token. ERC20 assets max amount is unaffected by
|
|
|
|
* gasTotal so does not need to be recomputed. Finally, validates the gas
|
2022-05-25 22:54:05 +02:00
|
|
|
* field and send state.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {void}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
calculateGasTotal: (state) => {
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2021-07-31 02:45:18 +02:00
|
|
|
// use maxFeePerGas as the multiplier if working with a FEE_MARKET transaction
|
|
|
|
// otherwise use gasPrice
|
2022-07-01 15:58:35 +02:00
|
|
|
if (
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.transactionType === TransactionEnvelopeType.feeMarket
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
|
|
|
draftTransaction.gas.gasTotal = addHexPrefix(
|
|
|
|
calcGasTotal(
|
|
|
|
draftTransaction.gas.gasLimit,
|
|
|
|
draftTransaction.gas.maxFeePerGas,
|
|
|
|
),
|
2021-07-31 02:45:18 +02:00
|
|
|
);
|
|
|
|
} else {
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.gas.gasTotal = addHexPrefix(
|
|
|
|
calcGasTotal(
|
|
|
|
draftTransaction.gas.gasLimit,
|
|
|
|
draftTransaction.gas.gasPrice,
|
|
|
|
),
|
2021-07-31 02:45:18 +02:00
|
|
|
);
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
if (
|
2022-07-01 15:58:35 +02:00
|
|
|
state.amountMode === AMOUNT_MODES.MAX &&
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type === AssetType.native
|
2021-06-23 23:35:25 +02:00
|
|
|
) {
|
|
|
|
slice.caseReducers.updateAmountToMax(state);
|
|
|
|
}
|
|
|
|
slice.caseReducers.validateAmountField(state);
|
|
|
|
slice.caseReducers.validateGasField(state);
|
|
|
|
// validate send state
|
|
|
|
slice.caseReducers.validateSendState(state);
|
|
|
|
},
|
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Clears all drafts from send state and drops the currentTransactionUUID.
|
|
|
|
* This is an important first step before adding a new draft transaction to
|
|
|
|
* avoid possible collision.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {void}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
clearPreviousDrafts: (state) => {
|
|
|
|
state.currentTransactionUUID = null;
|
|
|
|
state.draftTransactions = {};
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2022-05-25 22:54:05 +02:00
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Clears the send state by setting it to the initial value
|
|
|
|
*
|
|
|
|
* @returns {SendState}
|
2022-05-25 22:54:05 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
resetSendState: () => initialState,
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* sets the amount mode to the provided value as long as it is one of the
|
|
|
|
* supported modes (MAX|INPUT)
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {SendStateAmountModePayload} action - The amount mode
|
|
|
|
* to set the state to.
|
|
|
|
* @returns {void}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
updateAmountMode: (state, action) => {
|
|
|
|
if (Object.values(AMOUNT_MODES).includes(action.payload)) {
|
|
|
|
state.amountMode = action.payload;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* computes the maximum amount of asset that can be sent and then calls
|
|
|
|
* the updateSendAmount action above with the computed value, which will
|
|
|
|
* revalidate the field and form.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateAmountToMax: (state) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
let amount = '0x0';
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction.asset.type === AssetType.token) {
|
2022-07-01 15:58:35 +02:00
|
|
|
const decimals = draftTransaction.asset.details?.decimals ?? 0;
|
2023-01-24 15:44:49 +01:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
const multiplier = Math.pow(10, Number(decimals));
|
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
amount = new Numeric(draftTransaction.asset.balance, 16)
|
|
|
|
.times(multiplier, 10)
|
|
|
|
.toString();
|
2022-07-01 15:58:35 +02:00
|
|
|
} else {
|
2023-01-24 15:44:49 +01:00
|
|
|
const _gasTotal = new Numeric(
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.gas.gasTotal || '0x0',
|
2023-01-24 15:44:49 +01:00
|
|
|
16,
|
|
|
|
).add(new Numeric(state.gasTotalForLayer1 || '0x0', 16));
|
|
|
|
|
|
|
|
amount = new Numeric(draftTransaction.asset.balance, 16)
|
|
|
|
.minus(_gasTotal)
|
|
|
|
.toString();
|
2022-07-01 15:58:35 +02:00
|
|
|
}
|
|
|
|
slice.caseReducers.updateSendAmount(state, {
|
|
|
|
payload: amount,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Updates the currently selected asset
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-18 19:20:02 +02:00
|
|
|
* @param {UpdateAssetPayload} action - The asset to set in the
|
2022-07-01 15:58:35 +02:00
|
|
|
* draftTransaction.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateAsset: (state, action) => {
|
2022-07-18 19:20:02 +02:00
|
|
|
const { asset, initialAssetSet } = action.payload;
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
|
2022-07-18 19:20:02 +02:00
|
|
|
draftTransaction.asset.type = asset.type;
|
|
|
|
draftTransaction.asset.balance = asset.balance;
|
|
|
|
draftTransaction.asset.error = asset.error;
|
2022-07-21 01:03:23 +02:00
|
|
|
|
2021-07-31 02:45:18 +02:00
|
|
|
if (
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type === AssetType.token ||
|
|
|
|
draftTransaction.asset.type === AssetType.NFT
|
2021-07-31 02:45:18 +02:00
|
|
|
) {
|
2022-07-18 19:20:02 +02:00
|
|
|
draftTransaction.asset.details = asset.details;
|
2021-07-31 02:45:18 +02:00
|
|
|
} else {
|
2022-07-01 15:58:35 +02:00
|
|
|
// clear the details object when sending native currency
|
|
|
|
draftTransaction.asset.details = null;
|
|
|
|
if (draftTransaction.recipient.error === CONTRACT_ADDRESS_ERROR) {
|
|
|
|
// Errors related to sending tokens to their own contract address
|
|
|
|
// are no longer valid when sending native currency.
|
|
|
|
draftTransaction.recipient.error = null;
|
|
|
|
}
|
2021-07-31 02:45:18 +02:00
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
// if amount mode is MAX update amount to max of new asset, otherwise set
|
|
|
|
// to zero. This will revalidate the send amount field.
|
|
|
|
if (state.amountMode === AMOUNT_MODES.MAX) {
|
|
|
|
slice.caseReducers.updateAmountToMax(state);
|
2022-07-18 19:20:02 +02:00
|
|
|
} else if (initialAssetSet === false) {
|
2022-07-01 15:58:35 +02:00
|
|
|
slice.caseReducers.updateSendAmount(state, { payload: '0x0' });
|
|
|
|
}
|
|
|
|
// validate send state
|
|
|
|
slice.caseReducers.validateSendState(state);
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2021-07-31 02:45:18 +02:00
|
|
|
/**
|
|
|
|
* Sets the appropriate gas fees in state after receiving new estimates.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {GasEstimateUpdatePayload)} action - The gas fee update payload
|
|
|
|
* @returns {void}
|
2021-07-31 02:45:18 +02:00
|
|
|
*/
|
|
|
|
updateGasFeeEstimates: (state, action) => {
|
|
|
|
const { gasFeeEstimates, gasEstimateType } = action.payload;
|
|
|
|
let gasPriceEstimate = '0x0';
|
|
|
|
switch (gasEstimateType) {
|
2023-01-27 19:28:03 +01:00
|
|
|
case GasEstimateTypes.feeMarket:
|
2021-07-31 02:45:18 +02:00
|
|
|
slice.caseReducers.updateGasFees(state, {
|
|
|
|
payload: {
|
2023-01-18 15:47:29 +01:00
|
|
|
transactionType: TransactionEnvelopeType.feeMarket,
|
2021-07-31 02:45:18 +02:00
|
|
|
maxFeePerGas: getGasPriceInHexWei(
|
|
|
|
gasFeeEstimates.medium.suggestedMaxFeePerGas,
|
|
|
|
),
|
|
|
|
maxPriorityFeePerGas: getGasPriceInHexWei(
|
|
|
|
gasFeeEstimates.medium.suggestedMaxPriorityFeePerGas,
|
|
|
|
),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
break;
|
2023-01-27 19:28:03 +01:00
|
|
|
case GasEstimateTypes.legacy:
|
2021-07-31 02:45:18 +02:00
|
|
|
gasPriceEstimate = getRoundedGasPrice(gasFeeEstimates.medium);
|
|
|
|
slice.caseReducers.updateGasFees(state, {
|
|
|
|
payload: {
|
|
|
|
gasPrice: gasPriceEstimate,
|
2023-01-18 15:47:29 +01:00
|
|
|
type: TransactionEnvelopeType.legacy,
|
2021-07-31 02:45:18 +02:00
|
|
|
isAutomaticUpdate: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
break;
|
2023-01-27 19:28:03 +01:00
|
|
|
case GasEstimateTypes.ethGasPrice:
|
2021-07-31 02:45:18 +02:00
|
|
|
gasPriceEstimate = getRoundedGasPrice(gasFeeEstimates.gasPrice);
|
|
|
|
slice.caseReducers.updateGasFees(state, {
|
|
|
|
payload: {
|
|
|
|
gasPrice: getRoundedGasPrice(gasFeeEstimates.gasPrice),
|
2023-01-18 15:47:29 +01:00
|
|
|
type: TransactionEnvelopeType.legacy,
|
2021-07-31 02:45:18 +02:00
|
|
|
isAutomaticUpdate: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
break;
|
2023-01-27 19:28:03 +01:00
|
|
|
case GasEstimateTypes.none:
|
2021-07-31 02:45:18 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Record the latest gasPriceEstimate for future comparisons
|
2022-07-01 15:58:35 +02:00
|
|
|
state.gasPriceEstimate = addHexPrefix(gasPriceEstimate);
|
2021-07-31 02:45:18 +02:00
|
|
|
},
|
2022-03-29 21:21:45 +02:00
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Sets the appropriate gas fees in state and determines and sets the
|
|
|
|
* appropriate transactionType based on gas fee fields received.
|
2022-03-29 21:21:45 +02:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {GasFeeUpdatePayload} action - The gas fees to update with
|
|
|
|
* @returns {void}
|
2022-03-29 21:21:45 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
updateGasFees: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
if (draftTransaction) {
|
|
|
|
if (
|
2023-01-18 15:47:29 +01:00
|
|
|
action.payload.transactionType === TransactionEnvelopeType.feeMarket
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
|
|
|
draftTransaction.gas.maxFeePerGas = addHexPrefix(
|
|
|
|
action.payload.maxFeePerGas,
|
|
|
|
);
|
|
|
|
draftTransaction.gas.maxPriorityFeePerGas = addHexPrefix(
|
|
|
|
action.payload.maxPriorityFeePerGas,
|
|
|
|
);
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.transactionType = TransactionEnvelopeType.feeMarket;
|
2022-07-01 15:58:35 +02:00
|
|
|
} else {
|
2022-08-18 18:03:23 +02:00
|
|
|
if (action.payload.manuallyEdited) {
|
|
|
|
draftTransaction.gas.wasManuallyEdited = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the gas price if it has not been manually edited,
|
|
|
|
// or if this current action is a manual edit.
|
2022-07-01 15:58:35 +02:00
|
|
|
if (
|
2022-08-18 18:03:23 +02:00
|
|
|
!draftTransaction.gas.wasManuallyEdited ||
|
|
|
|
action.payload.manuallyEdited
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
|
|
|
draftTransaction.gas.gasPrice = addHexPrefix(
|
|
|
|
action.payload.gasPrice,
|
|
|
|
);
|
|
|
|
}
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.transactionType = TransactionEnvelopeType.legacy;
|
2022-07-01 15:58:35 +02:00
|
|
|
}
|
|
|
|
slice.caseReducers.calculateGasTotal(state);
|
2022-03-29 21:21:45 +02:00
|
|
|
}
|
|
|
|
},
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* sets the provided gasLimit in state and then recomputes the gasTotal.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-05-25 22:54:05 +02:00
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {SimpleStringPayload} action - The
|
|
|
|
* gasLimit in hex to set in state.
|
|
|
|
* @returns {void}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
updateGasLimit: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2022-07-18 19:01:10 +02:00
|
|
|
if (draftTransaction) {
|
|
|
|
draftTransaction.gas.gasLimit = addHexPrefix(action.payload);
|
|
|
|
slice.caseReducers.calculateGasTotal(state);
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* sets the layer 1 fees total (for a multi-layer fee network)
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @param {SimpleStringPayload} action - the
|
|
|
|
* gasTotalForLayer1 to set in hex wei.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateLayer1Fees: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
state.gasTotalForLayer1 = action.payload;
|
2022-01-10 17:23:53 +01:00
|
|
|
if (
|
2022-07-01 15:58:35 +02:00
|
|
|
state.amountMode === AMOUNT_MODES.MAX &&
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type === AssetType.native
|
2022-01-10 17:23:53 +01:00
|
|
|
) {
|
2021-06-23 23:35:25 +02:00
|
|
|
slice.caseReducers.updateAmountToMax(state);
|
|
|
|
}
|
|
|
|
},
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Updates the recipient of the draftTransaction
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @param {updateRecipientPayload} action - The recipient to set in the
|
|
|
|
* draftTransaction.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
updateRecipient: (state, action) => {
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.recipient.error = null;
|
|
|
|
state.recipientInput = '';
|
|
|
|
draftTransaction.recipient.address = action.payload.address ?? '';
|
|
|
|
draftTransaction.recipient.nickname = action.payload.nickname ?? '';
|
|
|
|
|
|
|
|
if (draftTransaction.recipient.address === '') {
|
2021-06-23 23:35:25 +02:00
|
|
|
// If address is null we are clearing the recipient and must return
|
|
|
|
// to the ADD_RECIPIENT stage.
|
|
|
|
state.stage = SEND_STAGES.ADD_RECIPIENT;
|
|
|
|
} else {
|
2022-05-25 22:54:05 +02:00
|
|
|
// if an address is provided and an id exists, we progress to the EDIT
|
|
|
|
// stage, otherwise we progress to the DRAFT stage. We also reset the
|
|
|
|
// search mode for recipient search.
|
2022-07-01 15:58:35 +02:00
|
|
|
state.stage =
|
|
|
|
draftTransaction.id === null ? SEND_STAGES.DRAFT : SEND_STAGES.EDIT;
|
|
|
|
state.recipientMode = RECIPIENT_SEARCH_MODES.CONTACT_LIST;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// validate send state
|
|
|
|
slice.caseReducers.validateSendState(state);
|
|
|
|
},
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Clears the user input and changes the recipient search mode to the
|
|
|
|
* specified value
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @param {UpdateRecipientModePayload} action - The mode to set the
|
|
|
|
* recipient search to
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateRecipientSearchMode: (state, action) => {
|
|
|
|
state.recipientInput = '';
|
|
|
|
state.recipientMode = action.payload;
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2022-07-14 00:15:38 +02:00
|
|
|
|
|
|
|
updateRecipientWarning: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.recipient.warning = action.payload;
|
|
|
|
},
|
|
|
|
|
2022-08-03 21:35:34 +02:00
|
|
|
updateRecipientType: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.recipient.type = action.payload;
|
|
|
|
},
|
|
|
|
|
2022-07-14 00:15:38 +02:00
|
|
|
updateDraftTransactionStatus: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.status = action.payload;
|
|
|
|
},
|
|
|
|
|
|
|
|
acknowledgeRecipientWarning: (state) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.recipient.recipientWarningAcknowledged = true;
|
|
|
|
slice.caseReducers.validateSendState(state);
|
|
|
|
},
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Updates the value of the recipientInput key with what the user has
|
|
|
|
* typed into the recipient input field in the UI.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @param {SimpleStringPayload} action - the value the user has typed into
|
|
|
|
* the recipient field.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
updateRecipientUserInput: (state, action) => {
|
|
|
|
// Update the value in state to match what the user is typing into the
|
|
|
|
// input field
|
2022-07-01 15:58:35 +02:00
|
|
|
state.recipientInput = action.payload;
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* update current amount.value in state and run post update validation of
|
|
|
|
* the amount field and the send state.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @param {SimpleStringPayload} action - The hex string to be set as the
|
|
|
|
* amount value.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateSendAmount: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.amount.value = addHexPrefix(action.payload);
|
|
|
|
// Once amount has changed, validate the field
|
|
|
|
slice.caseReducers.validateAmountField(state);
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction.asset.type === AssetType.native) {
|
2022-07-01 15:58:35 +02:00
|
|
|
// if sending the native asset the amount being sent will impact the
|
|
|
|
// gas field as well because the gas validation takes into
|
|
|
|
// consideration the available balance minus amount sent before
|
|
|
|
// checking if there is enough left to cover the gas fee.
|
|
|
|
slice.caseReducers.validateGasField(state);
|
|
|
|
}
|
|
|
|
// validate send state
|
|
|
|
slice.caseReducers.validateSendState(state);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* updates the userInputHexData state key
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @param {SimpleStringPayload} action - The hex string to be set as the
|
|
|
|
* userInputHexData value.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
updateUserInputHexData: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
draftTransaction.userInputHexData = action.payload;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Updates the gasIsSetInModal property to true which results in showing
|
|
|
|
* the gas fees from the custom gas modal in the send page.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
useCustomGas: (state) => {
|
|
|
|
state.gasIsSetInModal = true;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Updates the gasIsSetInModal property to false which results in showing
|
|
|
|
* the default gas price/limit fields in the send page.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
useDefaultGas: (state) => {
|
|
|
|
state.gasIsSetInModal = false;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks for the validity of the draftTransactions selected amount to send
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
validateAmountField: (state) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2023-01-24 15:44:49 +01:00
|
|
|
|
|
|
|
const amountValue = new Numeric(draftTransaction.amount.value, 16);
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
switch (true) {
|
2022-11-28 17:41:42 +01:00
|
|
|
// set error to INSUFFICIENT_FUNDS_FOR_GAS_ERROR if the account balance is lower
|
2022-07-01 15:58:35 +02:00
|
|
|
// than the total price of the transaction inclusive of gas fees.
|
2023-01-18 15:47:29 +01:00
|
|
|
case draftTransaction.asset.type === AssetType.native &&
|
2022-07-01 15:58:35 +02:00
|
|
|
!isBalanceSufficient({
|
|
|
|
amount: draftTransaction.amount.value,
|
|
|
|
balance: draftTransaction.asset.balance,
|
|
|
|
gasTotal: draftTransaction.gas.gasTotal ?? '0x0',
|
|
|
|
}):
|
2022-11-28 17:41:42 +01:00
|
|
|
draftTransaction.amount.error = INSUFFICIENT_FUNDS_FOR_GAS_ERROR;
|
2022-07-01 15:58:35 +02:00
|
|
|
break;
|
2022-11-28 17:41:42 +01:00
|
|
|
// set error to INSUFFICIENT_TOKENS_ERROR if the token balance is lower
|
2022-07-01 15:58:35 +02:00
|
|
|
// than the amount of token the user is attempting to send.
|
2023-01-18 15:47:29 +01:00
|
|
|
case draftTransaction.asset.type === AssetType.token &&
|
2021-06-23 23:35:25 +02:00
|
|
|
!isTokenBalanceSufficient({
|
2022-07-01 15:58:35 +02:00
|
|
|
tokenBalance: draftTransaction.asset.balance ?? '0x0',
|
|
|
|
amount: draftTransaction.amount.value,
|
|
|
|
decimals: draftTransaction.asset.details.decimals,
|
2021-06-23 23:35:25 +02:00
|
|
|
}):
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.amount.error = INSUFFICIENT_TOKENS_ERROR;
|
2021-06-23 23:35:25 +02:00
|
|
|
break;
|
|
|
|
// if the amount is negative, set error to NEGATIVE_ETH_ERROR
|
|
|
|
// TODO: change this to NEGATIVE_ERROR and remove the currency bias.
|
2023-01-24 15:44:49 +01:00
|
|
|
case amountValue.isNegative():
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.amount.error = NEGATIVE_ETH_ERROR;
|
2021-06-23 23:35:25 +02:00
|
|
|
break;
|
|
|
|
// If none of the above are true, set error to null
|
|
|
|
default:
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.amount.error = null;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
},
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Checks if the user has enough funds to cover the cost of gas, always
|
|
|
|
* uses the native currency and does not take into account the amount
|
|
|
|
* being sent. If the user has enough to cover cost of gas but not gas
|
|
|
|
* + amount then the error will be displayed on the amount field.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
validateGasField: (state) => {
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2021-06-23 23:35:25 +02:00
|
|
|
const insufficientFunds = !isBalanceSufficient({
|
|
|
|
amount:
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type === AssetType.native
|
2022-07-01 15:58:35 +02:00
|
|
|
? draftTransaction.amount.value
|
|
|
|
: '0x0',
|
|
|
|
balance:
|
|
|
|
draftTransaction.fromAccount?.balance ??
|
|
|
|
state.selectedAccount.balance,
|
|
|
|
gasTotal: draftTransaction.gas.gasTotal ?? '0x0',
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.gas.error = insufficientFunds
|
|
|
|
? INSUFFICIENT_FUNDS_ERROR
|
|
|
|
: null;
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
2022-07-01 15:58:35 +02:00
|
|
|
validateRecipientUserInput: (state, action) => {
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
|
|
|
|
if (draftTransaction) {
|
|
|
|
if (
|
|
|
|
state.recipientMode === RECIPIENT_SEARCH_MODES.MY_ACCOUNTS ||
|
|
|
|
state.recipientInput === '' ||
|
|
|
|
state.recipientInput === null
|
|
|
|
) {
|
|
|
|
draftTransaction.recipient.error = null;
|
|
|
|
draftTransaction.recipient.warning = null;
|
|
|
|
} else {
|
2022-07-14 00:15:38 +02:00
|
|
|
const {
|
|
|
|
chainId,
|
|
|
|
tokens,
|
|
|
|
tokenAddressList,
|
|
|
|
isProbablyAnAssetContract,
|
|
|
|
} = action.payload;
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
if (
|
|
|
|
isBurnAddress(state.recipientInput) ||
|
|
|
|
(!isValidHexAddress(state.recipientInput, {
|
|
|
|
mixedCaseUseChecksum: true,
|
|
|
|
}) &&
|
|
|
|
!isValidDomainName(state.recipientInput))
|
|
|
|
) {
|
|
|
|
draftTransaction.recipient.error = isDefaultMetaMaskChain(chainId)
|
|
|
|
? INVALID_RECIPIENT_ADDRESS_ERROR
|
|
|
|
: INVALID_RECIPIENT_ADDRESS_NOT_ETH_NETWORK_ERROR;
|
|
|
|
} else if (
|
|
|
|
isOriginContractAddress(
|
|
|
|
state.recipientInput,
|
2022-07-14 00:15:38 +02:00
|
|
|
draftTransaction.asset?.details?.address,
|
2022-07-01 15:58:35 +02:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
draftTransaction.recipient.error = CONTRACT_ADDRESS_ERROR;
|
|
|
|
} else {
|
|
|
|
draftTransaction.recipient.error = null;
|
|
|
|
}
|
|
|
|
if (
|
2022-07-14 00:15:38 +02:00
|
|
|
(isValidHexAddress(state.recipientInput) &&
|
|
|
|
(tokenAddressList.find((address) =>
|
|
|
|
isEqualCaseInsensitive(address, state.recipientInput),
|
|
|
|
) ||
|
|
|
|
checkExistingAddresses(state.recipientInput, tokens))) ||
|
|
|
|
isProbablyAnAssetContract
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
2022-07-31 20:26:40 +02:00
|
|
|
draftTransaction.recipient.warning =
|
|
|
|
KNOWN_RECIPIENT_ADDRESS_WARNING;
|
2022-07-01 15:58:35 +02:00
|
|
|
} else {
|
|
|
|
draftTransaction.recipient.warning = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-14 00:15:38 +02:00
|
|
|
slice.caseReducers.validateSendState(state);
|
2022-07-01 15:58:35 +02:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Checks if the draftTransaction is currently valid. The following list of
|
|
|
|
* cases from the switch statement in this function describe when the
|
|
|
|
* transaction is invalid. Please keep this comment updated.
|
|
|
|
*
|
|
|
|
* case 1: State is invalid when amount field has an error.
|
|
|
|
* case 2: State is invalid when gas field has an error.
|
|
|
|
* case 3: State is invalid when asset field has an error.
|
|
|
|
* case 4: State is invalid if asset type is a token and the token details
|
|
|
|
* are unknown.
|
|
|
|
* case 5: State is invalid if no recipient has been added.
|
|
|
|
* case 6: State is invalid if the send state is uninitialized.
|
|
|
|
* case 7: State is invalid if gas estimates are loading.
|
|
|
|
* case 8: State is invalid if gasLimit is less than the gasLimitMinimum.
|
|
|
|
*
|
|
|
|
* @param {SendStateDraft} state - A writable draft of the send state to be
|
|
|
|
* updated.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
validateSendState: (state) => {
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: 'Begin validating send state',
|
|
|
|
});
|
2022-08-02 20:52:09 +02:00
|
|
|
if (draftTransaction) {
|
|
|
|
switch (true) {
|
|
|
|
case Boolean(draftTransaction.amount.error):
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Amount is in error ${draftTransaction.amount.error}`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case Boolean(draftTransaction.gas.error):
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Gas is in error ${draftTransaction.gas.error}`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case Boolean(draftTransaction.asset.error):
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Asset is in error ${draftTransaction.asset.error}`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2023-01-18 15:47:29 +01:00
|
|
|
case draftTransaction.asset.type === AssetType.token &&
|
2022-08-02 20:52:09 +02:00
|
|
|
draftTransaction.asset.details === null:
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Asset is TOKEN and token details is null`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case state.stage === SEND_STAGES.ADD_RECIPIENT:
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is invalid because stage is ADD_RECIPIENT`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case state.stage === SEND_STAGES.INACTIVE:
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is invalid because stage is INACTIVE`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case state.gasEstimateIsLoading:
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is invalid because gasEstimateIsLoading`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case new BigNumber(draftTransaction.gas.gasLimit, 16).lessThan(
|
|
|
|
new BigNumber(state.gasLimitMinimum),
|
|
|
|
):
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is invalid because ${draftTransaction.gas.gasLimit} is lessThan ${state.gasLimitMinimum}`,
|
|
|
|
});
|
|
|
|
|
2022-08-02 20:52:09 +02:00
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
|
|
|
case draftTransaction.recipient.warning === 'loading':
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is invalid because recipient warning is loading`,
|
|
|
|
});
|
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
2022-08-02 20:52:09 +02:00
|
|
|
case draftTransaction.recipient.warning ===
|
|
|
|
KNOWN_RECIPIENT_ADDRESS_WARNING &&
|
|
|
|
draftTransaction.recipient.recipientWarningAcknowledged === false:
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is invalid because recipient warning not acknolwedged`,
|
|
|
|
});
|
2022-08-02 20:52:09 +02:00
|
|
|
draftTransaction.status = SEND_STATUSES.INVALID;
|
|
|
|
break;
|
|
|
|
default:
|
2023-01-24 15:44:49 +01:00
|
|
|
slice.caseReducers.addHistoryEntry(state, {
|
|
|
|
payload: `Form is valid`,
|
|
|
|
});
|
2022-08-02 20:52:09 +02:00
|
|
|
draftTransaction.status = SEND_STATUSES.VALID;
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
extraReducers: (builder) => {
|
|
|
|
builder
|
|
|
|
.addCase(ACCOUNT_CHANGED, (state, action) => {
|
2022-08-03 21:59:02 +02:00
|
|
|
// This event occurs when the user's account details update due to
|
|
|
|
// background state changes. If the account that is being updated is
|
|
|
|
// the current from account on the edit flow we need to update
|
|
|
|
// the balance for the account and revalidate the send state.
|
|
|
|
if (state.stage === SEND_STAGES.EDIT && action.payload.account) {
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2022-08-03 21:59:02 +02:00
|
|
|
if (
|
|
|
|
draftTransaction &&
|
|
|
|
draftTransaction.fromAccount &&
|
|
|
|
draftTransaction.fromAccount.address ===
|
|
|
|
action.payload.account.address
|
|
|
|
) {
|
|
|
|
draftTransaction.fromAccount.balance =
|
|
|
|
action.payload.account.balance;
|
|
|
|
// We need to update the asset balance if the asset is the native
|
|
|
|
// network asset. Once we update the balance we recompute error state.
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction.asset.type === AssetType.native) {
|
2022-08-03 21:59:02 +02:00
|
|
|
draftTransaction.asset.balance = action.payload.account.balance;
|
|
|
|
}
|
|
|
|
slice.caseReducers.validateAmountField(state);
|
|
|
|
slice.caseReducers.validateGasField(state);
|
|
|
|
slice.caseReducers.validateSendState(state);
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.addCase(ADDRESS_BOOK_UPDATED, (state, action) => {
|
|
|
|
// When the address book updates from background state changes we need
|
|
|
|
// to check to see if an entry exists for the current address or if the
|
|
|
|
// entry changed.
|
|
|
|
const { addressBook } = action.payload;
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
if (
|
|
|
|
draftTransaction &&
|
|
|
|
addressBook[draftTransaction.recipient.address]?.name
|
|
|
|
) {
|
|
|
|
draftTransaction.recipient.nickname =
|
|
|
|
addressBook[draftTransaction.recipient.address].name;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.addCase(computeEstimatedGasLimit.pending, (state) => {
|
|
|
|
// When we begin to fetch gasLimit we should indicate we are loading
|
|
|
|
// a gas estimate.
|
|
|
|
state.gasEstimateIsLoading = true;
|
|
|
|
})
|
|
|
|
.addCase(computeEstimatedGasLimit.fulfilled, (state, action) => {
|
|
|
|
// When we receive a new gasLimit from the computeEstimatedGasLimit
|
|
|
|
// thunk we need to update our gasLimit in the slice. We call into the
|
|
|
|
// caseReducer updateGasLimit to tap into the appropriate follow up
|
|
|
|
// checks and gasTotal calculation. First set gasEstimateIsLoading to
|
|
|
|
// false.
|
|
|
|
state.gasEstimateIsLoading = false;
|
|
|
|
if (action.payload?.gasLimit) {
|
|
|
|
slice.caseReducers.updateGasLimit(state, {
|
|
|
|
payload: action.payload.gasLimit,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (action.payload?.gasTotalForLayer1) {
|
|
|
|
slice.caseReducers.updateLayer1Fees(state, {
|
|
|
|
payload: action.payload.gasTotalForLayer1,
|
|
|
|
});
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
})
|
2022-07-01 15:58:35 +02:00
|
|
|
.addCase(computeEstimatedGasLimit.rejected, (state) => {
|
|
|
|
// If gas estimation fails, we should set the loading state to false,
|
|
|
|
// because it is no longer loading
|
|
|
|
state.gasEstimateIsLoading = false;
|
|
|
|
})
|
|
|
|
.addCase(GAS_FEE_ESTIMATES_UPDATED, (state, action) => {
|
|
|
|
// When the gasFeeController updates its gas fee estimates we need to
|
|
|
|
// update and validate state based on those new values
|
|
|
|
slice.caseReducers.updateGasFeeEstimates(state, {
|
|
|
|
payload: action.payload,
|
|
|
|
});
|
|
|
|
})
|
2021-06-23 23:35:25 +02:00
|
|
|
.addCase(initializeSendState.pending, (state) => {
|
|
|
|
// when we begin initializing state, which can happen when switching
|
2022-07-01 15:58:35 +02:00
|
|
|
// chains even after loading the send flow, we set gasEstimateIsLoading
|
|
|
|
// as initialization will trigger a fetch for gasPrice estimates.
|
|
|
|
state.gasEstimateIsLoading = true;
|
2021-06-23 23:35:25 +02:00
|
|
|
})
|
|
|
|
.addCase(initializeSendState.fulfilled, (state, action) => {
|
|
|
|
// writes the computed initialized state values into the slice and then
|
|
|
|
// calculates slice validity using the caseReducers.
|
2021-07-31 02:45:18 +02:00
|
|
|
state.eip1559support = action.payload.eip1559support;
|
2022-07-01 15:58:35 +02:00
|
|
|
state.selectedAccount.address = action.payload.account.address;
|
|
|
|
state.selectedAccount.balance = action.payload.account.balance;
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
2022-08-02 20:52:09 +02:00
|
|
|
if (draftTransaction) {
|
|
|
|
draftTransaction.gas.gasLimit = action.payload.gasLimit;
|
|
|
|
draftTransaction.gas.gasTotal = action.payload.gasTotal;
|
|
|
|
if (action.payload.chainHasChanged) {
|
|
|
|
// If the state was reinitialized as a result of the user changing
|
|
|
|
// the network from the network dropdown, then the selected asset is
|
|
|
|
// no longer valid and should be set to the native asset for the
|
|
|
|
// network.
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type = AssetType.native;
|
2022-08-02 20:52:09 +02:00
|
|
|
draftTransaction.asset.balance =
|
|
|
|
draftTransaction.fromAccount?.balance ??
|
|
|
|
state.selectedAccount.balance;
|
|
|
|
draftTransaction.asset.details = null;
|
|
|
|
}
|
|
|
|
}
|
2021-07-31 02:45:18 +02:00
|
|
|
slice.caseReducers.updateGasFeeEstimates(state, {
|
|
|
|
payload: {
|
|
|
|
gasFeeEstimates: action.payload.gasFeeEstimates,
|
|
|
|
gasEstimateType: action.payload.gasEstimateType,
|
|
|
|
},
|
|
|
|
});
|
2022-07-01 15:58:35 +02:00
|
|
|
state.gasEstimatePollToken = action.payload.gasEstimatePollToken;
|
2021-07-08 22:23:00 +02:00
|
|
|
if (action.payload.gasEstimatePollToken) {
|
2022-07-01 15:58:35 +02:00
|
|
|
state.gasEstimateIsLoading = false;
|
2021-07-08 22:23:00 +02:00
|
|
|
}
|
2021-07-31 02:45:18 +02:00
|
|
|
if (state.stage !== SEND_STAGES.INACTIVE) {
|
2021-06-23 23:35:25 +02:00
|
|
|
slice.caseReducers.validateRecipientUserInput(state, {
|
|
|
|
payload: {
|
|
|
|
chainId: action.payload.chainId,
|
|
|
|
tokens: action.payload.tokens,
|
2021-09-09 22:56:27 +02:00
|
|
|
useTokenDetection: action.payload.useTokenDetection,
|
|
|
|
tokenAddressList: action.payload.tokenAddressList,
|
2021-06-23 23:35:25 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2022-08-03 18:54:35 +02:00
|
|
|
if (state.amountMode === AMOUNT_MODES.MAX) {
|
|
|
|
slice.caseReducers.updateAmountToMax(state);
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
slice.caseReducers.validateAmountField(state);
|
|
|
|
slice.caseReducers.validateGasField(state);
|
|
|
|
slice.caseReducers.validateSendState(state);
|
|
|
|
})
|
2022-07-01 15:58:35 +02:00
|
|
|
.addCase(SELECTED_ACCOUNT_CHANGED, (state, action) => {
|
2022-08-03 20:14:23 +02:00
|
|
|
// This event occurs when the user selects a new account from the
|
|
|
|
// account menu, or the currently active account's balance updates.
|
|
|
|
// We only care about new transactions, not edits, here, because we use
|
|
|
|
// the fromAccount and ACCOUNT_CHANGED action for that.
|
|
|
|
if (state.stage !== SEND_STAGES.EDIT && action.payload.account) {
|
2022-07-01 15:58:35 +02:00
|
|
|
state.selectedAccount.balance = action.payload.account.balance;
|
|
|
|
state.selectedAccount.address = action.payload.account.address;
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
// This action will occur even when we aren't on the send flow, which
|
|
|
|
// is okay as it keeps the selectedAccount details up to date. We do
|
|
|
|
// not need to validate anything if there isn't a current draft
|
|
|
|
// transaction. If there is, we need to update the asset balance if
|
|
|
|
// the asset is set to the native network asset, and then validate
|
|
|
|
// the transaction.
|
|
|
|
if (draftTransaction) {
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction?.asset.type === AssetType.native) {
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.asset.balance = action.payload.account.balance;
|
|
|
|
}
|
|
|
|
slice.caseReducers.validateAmountField(state);
|
|
|
|
slice.caseReducers.validateGasField(state);
|
|
|
|
slice.caseReducers.validateSendState(state);
|
|
|
|
}
|
2022-03-29 21:21:45 +02:00
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
})
|
2022-07-01 15:58:35 +02:00
|
|
|
.addCase(QR_CODE_DETECTED, (state, action) => {
|
|
|
|
// When data is received from the QR Code Scanner we set the recipient
|
|
|
|
// as long as a valid address can be pulled from the data. If an
|
|
|
|
// address is pulled but it is invalid, we display an error.
|
|
|
|
const qrCodeData = action.value;
|
|
|
|
const draftTransaction =
|
|
|
|
state.draftTransactions[state.currentTransactionUUID];
|
|
|
|
if (qrCodeData && draftTransaction) {
|
|
|
|
if (qrCodeData.type === 'address') {
|
|
|
|
const scannedAddress = qrCodeData.values.address.toLowerCase();
|
|
|
|
if (
|
|
|
|
isValidHexAddress(scannedAddress, { allowNonPrefixed: false })
|
|
|
|
) {
|
|
|
|
if (draftTransaction.recipient.address !== scannedAddress) {
|
|
|
|
slice.caseReducers.updateRecipient(state, {
|
|
|
|
payload: { address: scannedAddress },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-31 20:26:40 +02:00
|
|
|
draftTransaction.recipient.error =
|
|
|
|
INVALID_RECIPIENT_ADDRESS_ERROR;
|
2022-07-01 15:58:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { actions, reducer } = slice;
|
|
|
|
|
|
|
|
export default reducer;
|
|
|
|
|
|
|
|
const {
|
|
|
|
useDefaultGas,
|
|
|
|
useCustomGas,
|
|
|
|
updateGasLimit,
|
|
|
|
validateRecipientUserInput,
|
|
|
|
updateRecipientSearchMode,
|
2022-05-04 18:54:46 +02:00
|
|
|
addHistoryEntry,
|
2022-07-14 00:15:38 +02:00
|
|
|
acknowledgeRecipientWarning,
|
2021-06-23 23:35:25 +02:00
|
|
|
} = actions;
|
|
|
|
|
2022-07-14 00:15:38 +02:00
|
|
|
export {
|
|
|
|
useDefaultGas,
|
|
|
|
useCustomGas,
|
|
|
|
updateGasLimit,
|
|
|
|
addHistoryEntry,
|
|
|
|
acknowledgeRecipientWarning,
|
|
|
|
};
|
2021-06-23 23:35:25 +02:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* This method is for usage when validating user input so that validation
|
|
|
|
* is only run after a delay in typing of 300ms. Usage at callsites requires
|
|
|
|
* passing in both the dispatch method and the payload to dispatch, which makes
|
|
|
|
* it only applicable for use within action creators.
|
|
|
|
*/
|
2022-07-14 00:15:38 +02:00
|
|
|
const debouncedValidateRecipientUserInput = debounce(
|
|
|
|
(dispatch, payload, resolve) => {
|
|
|
|
dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user typed ${payload.userInput} into recipient input field`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
dispatch(validateRecipientUserInput(payload));
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
300,
|
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Begins a new draft transaction, derived from the txParams of an existing
|
|
|
|
* transaction in the TransactionController. This action will first clear out
|
|
|
|
* the previous draft transactions and currentTransactionUUID from state. This
|
|
|
|
* action is one of the two entry points into the send flow. NOTE: You must
|
|
|
|
* route to the send page *after* dispatching this action resolves to ensure
|
|
|
|
* that the draftTransaction is properly created.
|
|
|
|
*
|
2023-01-18 15:47:29 +01:00
|
|
|
* @param {AssetType} assetType - The type of asset the transaction
|
2022-07-01 15:58:35 +02:00
|
|
|
* being edited was sending. The details of the asset will be retrieved from
|
|
|
|
* the transaction data in state.
|
|
|
|
* @param {string} transactionId - The id of the transaction being edited.
|
|
|
|
* @returns {ThunkAction<void>}
|
|
|
|
*/
|
|
|
|
export function editExistingTransaction(assetType, transactionId) {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
await dispatch(actions.clearPreviousDrafts());
|
|
|
|
const state = getState();
|
|
|
|
const unapprovedTransactions = getUnapprovedTxs(state);
|
|
|
|
const transaction = unapprovedTransactions[transactionId];
|
|
|
|
const account = getTargetAccount(state, transaction.txParams.from);
|
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
if (assetType === AssetType.native) {
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
actions.addNewDraft({
|
|
|
|
...draftTransactionInitialState,
|
|
|
|
id: transactionId,
|
|
|
|
fromAccount: account,
|
|
|
|
gas: {
|
|
|
|
...draftTransactionInitialState.gas,
|
|
|
|
gasLimit: transaction.txParams.gas,
|
|
|
|
gasPrice: transaction.txParams.gasPrice,
|
|
|
|
},
|
|
|
|
userInputHexData: transaction.txParams.data,
|
|
|
|
recipient: {
|
|
|
|
...draftTransactionInitialState.recipient,
|
|
|
|
address: transaction.txParams.to,
|
|
|
|
nickname:
|
2022-09-19 18:09:12 +02:00
|
|
|
getAddressBookEntryOrAccountName(
|
|
|
|
state,
|
|
|
|
transaction.txParams.to,
|
|
|
|
) ?? '',
|
2022-07-01 15:58:35 +02:00
|
|
|
},
|
|
|
|
amount: {
|
|
|
|
...draftTransactionInitialState.amount,
|
|
|
|
value: transaction.txParams.value,
|
|
|
|
},
|
|
|
|
history: [
|
|
|
|
`sendFlow - user clicked edit on transaction with id ${transactionId}`,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await dispatch(
|
2023-01-18 15:47:29 +01:00
|
|
|
updateSendAsset({ type: AssetType.native }, { initialAssetSet: true }),
|
2022-07-01 15:58:35 +02:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
const tokenData = parseStandardTokenTransactionData(
|
|
|
|
transaction.txParams.data,
|
|
|
|
);
|
|
|
|
const tokenAmountInDec =
|
2023-01-18 15:47:29 +01:00
|
|
|
assetType === AssetType.token ? getTokenValueParam(tokenData) : '1';
|
2022-07-01 15:58:35 +02:00
|
|
|
const address = getTokenAddressParam(tokenData);
|
2022-09-19 18:09:12 +02:00
|
|
|
const nickname = getAddressBookEntryOrAccountName(state, address) ?? '';
|
2022-07-01 15:58:35 +02:00
|
|
|
|
2023-01-24 15:44:49 +01:00
|
|
|
const tokenAmountInHex = addHexPrefix(decimalToHex(tokenAmountInDec));
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
actions.addNewDraft({
|
|
|
|
...draftTransactionInitialState,
|
|
|
|
id: transactionId,
|
|
|
|
fromAccount: account,
|
|
|
|
gas: {
|
|
|
|
...draftTransactionInitialState.gas,
|
|
|
|
gasLimit: transaction.txParams.gas,
|
|
|
|
gasPrice: transaction.txParams.gasPrice,
|
|
|
|
},
|
|
|
|
userInputHexData: transaction.txParams.data,
|
|
|
|
recipient: {
|
|
|
|
...draftTransactionInitialState.recipient,
|
|
|
|
address,
|
|
|
|
nickname,
|
|
|
|
},
|
|
|
|
amount: {
|
|
|
|
...draftTransactionInitialState.amount,
|
|
|
|
value: tokenAmountInHex,
|
|
|
|
},
|
|
|
|
history: [
|
|
|
|
`sendFlow - user clicked edit on transaction with id ${transactionId}`,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
|
|
|
|
await dispatch(
|
|
|
|
updateSendAsset(
|
|
|
|
{
|
|
|
|
type: assetType,
|
|
|
|
details: {
|
|
|
|
address: transaction.txParams.to,
|
2023-01-18 15:47:29 +01:00
|
|
|
...(assetType === AssetType.NFT
|
2022-07-23 16:37:31 +02:00
|
|
|
? {
|
|
|
|
tokenId:
|
|
|
|
getTokenIdParam(tokenData) ??
|
|
|
|
getTokenValueParam(tokenData),
|
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
: {}),
|
|
|
|
},
|
|
|
|
},
|
2022-07-18 19:20:02 +02:00
|
|
|
{ initialAssetSet: true },
|
2022-07-01 15:58:35 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
await dispatch(initializeSendState());
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-07-31 02:45:18 +02:00
|
|
|
/**
|
|
|
|
* This method is a temporary placeholder to support the old UI in both the
|
|
|
|
* gas modal and the send flow. Soon we won't need to modify gasPrice from the
|
|
|
|
* send flow based on user input, it'll just be a shallow copy of the current
|
|
|
|
* estimate. This method is necessary because the internal structure of this
|
|
|
|
* slice has been changed such that it is agnostic to transaction envelope
|
|
|
|
* type, and this method calls into the new structure in the appropriate way.
|
|
|
|
*
|
|
|
|
* @deprecated - don't extend the usage of this temporary method
|
|
|
|
* @param {string} gasPrice - new gas price in hex wei
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {ThunkAction<void>}
|
2021-07-31 02:45:18 +02:00
|
|
|
*/
|
|
|
|
export function updateGasPrice(gasPrice) {
|
|
|
|
return (dispatch) => {
|
2022-05-04 18:54:46 +02:00
|
|
|
dispatch(
|
|
|
|
addHistoryEntry(`sendFlow - user set legacy gasPrice to ${gasPrice}`),
|
|
|
|
);
|
2021-07-31 02:45:18 +02:00
|
|
|
dispatch(
|
|
|
|
actions.updateGasFees({
|
|
|
|
gasPrice,
|
2023-01-18 15:47:29 +01:00
|
|
|
transactionType: TransactionEnvelopeType.legacy,
|
2022-08-18 18:03:23 +02:00
|
|
|
manuallyEdited: true,
|
2021-07-31 02:45:18 +02:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Updates the recipient in state based on the input provided, and then will
|
|
|
|
* recompute gas limit when sending a TOKEN asset type. Changing the recipient
|
|
|
|
* address results in hex data changing because the recipient address is
|
|
|
|
* encoded in the data instead of being in the 'to' field. The to field in a
|
|
|
|
* token send will always be the token contract address.
|
|
|
|
* If no nickname is provided, the address book state will be checked to see if
|
|
|
|
* a nickname for the passed address has already been saved. This ensures the
|
|
|
|
* (temporary) send state recipient nickname is consistent with the address book
|
|
|
|
* nickname which has already been persisted to state.
|
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} recipient - Recipient information
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {string} recipient.address - hex address to send the transaction to
|
|
|
|
* @param {string} [recipient.nickname] - Alias for the address to display
|
|
|
|
* to the user
|
|
|
|
* @returns {ThunkAction<void>}
|
|
|
|
*/
|
|
|
|
export function updateRecipient({ address, nickname }) {
|
2021-07-08 22:23:00 +02:00
|
|
|
return async (dispatch, getState) => {
|
2022-07-01 15:58:35 +02:00
|
|
|
// Do not addHistoryEntry here as this is called from a number of places
|
|
|
|
// each with significance to the user and transaction history.
|
2021-07-08 22:23:00 +02:00
|
|
|
const state = getState();
|
2022-07-01 15:58:35 +02:00
|
|
|
const nicknameFromAddressBookEntryOrAccountName =
|
|
|
|
getAddressBookEntryOrAccountName(state, address) ?? '';
|
|
|
|
await dispatch(
|
|
|
|
actions.updateRecipient({
|
|
|
|
address,
|
|
|
|
nickname: nickname || nicknameFromAddressBookEntryOrAccountName,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
await dispatch(computeEstimatedGasLimit());
|
|
|
|
};
|
|
|
|
}
|
2021-07-08 22:23:00 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* This method is called to update the user's input into the ENS input field.
|
|
|
|
* Once the field is updated, the field will be validated using a debounced
|
|
|
|
* version of the validateRecipientUserInput action. This way validation only
|
|
|
|
* occurs once the user has stopped typing.
|
|
|
|
*
|
|
|
|
* @param {string} userInput - the value that the user is typing into the field
|
|
|
|
*/
|
|
|
|
export function updateRecipientUserInput(userInput) {
|
|
|
|
return async (dispatch, getState) => {
|
2022-07-14 00:15:38 +02:00
|
|
|
dispatch(actions.updateRecipientWarning('loading'));
|
|
|
|
dispatch(actions.updateDraftTransactionStatus(SEND_STATUSES.INVALID));
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(actions.updateRecipientUserInput(userInput));
|
|
|
|
const state = getState();
|
2022-07-14 00:15:38 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state[name].draftTransactions[state[name].currentTransactionUUID];
|
|
|
|
const sendingAddress =
|
|
|
|
draftTransaction.fromAccount?.address ??
|
|
|
|
state[name].selectedAccount.address ??
|
|
|
|
getSelectedAddress(state);
|
2022-07-01 15:58:35 +02:00
|
|
|
const chainId = getCurrentChainId(state);
|
|
|
|
const tokens = getTokens(state);
|
|
|
|
const useTokenDetection = getUseTokenDetection(state);
|
2022-07-14 00:15:38 +02:00
|
|
|
const tokenMap = getTokenList(state);
|
|
|
|
const tokenAddressList = Object.keys(tokenMap);
|
|
|
|
|
|
|
|
const inputIsValidHexAddress = isValidHexAddress(userInput);
|
|
|
|
let isProbablyAnAssetContract = false;
|
|
|
|
if (inputIsValidHexAddress) {
|
2022-07-18 16:43:30 +02:00
|
|
|
const smartContractAddress = await isSmartContractAddress(userInput);
|
|
|
|
if (smartContractAddress) {
|
2022-08-03 21:35:34 +02:00
|
|
|
dispatch(actions.updateRecipientType(RECIPIENT_TYPES.SMART_CONTRACT));
|
2022-07-18 16:43:30 +02:00
|
|
|
const { symbol, decimals } =
|
|
|
|
getTokenMetadata(userInput, tokenMap) || {};
|
2022-07-14 00:15:38 +02:00
|
|
|
|
2022-07-18 16:43:30 +02:00
|
|
|
isProbablyAnAssetContract = symbol && decimals !== undefined;
|
2022-07-14 00:15:38 +02:00
|
|
|
|
2022-07-18 16:43:30 +02:00
|
|
|
if (!isProbablyAnAssetContract) {
|
|
|
|
try {
|
|
|
|
const { standard } = await getTokenStandardAndDetails(
|
|
|
|
userInput,
|
|
|
|
sendingAddress,
|
|
|
|
);
|
|
|
|
isProbablyAnAssetContract = Boolean(standard);
|
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
2022-07-14 00:15:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
debouncedValidateRecipientUserInput(
|
|
|
|
dispatch,
|
|
|
|
{
|
|
|
|
userInput,
|
|
|
|
chainId,
|
|
|
|
tokens,
|
|
|
|
useTokenDetection,
|
|
|
|
tokenAddressList,
|
|
|
|
isProbablyAnAssetContract,
|
|
|
|
},
|
|
|
|
resolve,
|
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
});
|
2021-07-08 22:23:00 +02:00
|
|
|
};
|
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
/**
|
|
|
|
* Updates the amount the user intends to send and performs side effects.
|
|
|
|
* 1. If the current mode is MAX change to INPUT
|
|
|
|
* 2. If sending a token, recompute the gasLimit estimate
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2021-06-23 23:35:25 +02:00
|
|
|
* @param {string} amount - hex string representing value
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export function updateSendAmount(amount) {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2022-05-17 18:57:23 +02:00
|
|
|
const { metamask } = state;
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state[name].draftTransactions[state[name].currentTransactionUUID];
|
2022-05-04 18:54:46 +02:00
|
|
|
let logAmount = amount;
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction.asset.type === AssetType.token) {
|
2022-05-04 18:54:46 +02:00
|
|
|
const multiplier = Math.pow(
|
|
|
|
10,
|
2022-07-01 15:58:35 +02:00
|
|
|
Number(draftTransaction.asset.details?.decimals || 0),
|
2022-05-04 18:54:46 +02:00
|
|
|
);
|
2023-01-24 15:44:49 +01:00
|
|
|
const decimalValueString = new Numeric(addHexPrefix(amount), 16)
|
|
|
|
.toBase(10)
|
|
|
|
.applyConversionRate(
|
|
|
|
draftTransaction.asset.details?.symbol ? multiplier : 1,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.toString();
|
2022-05-04 18:54:46 +02:00
|
|
|
logAmount = `${Number(decimalValueString) ? decimalValueString : ''} ${
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.asset.details?.symbol
|
2022-05-04 18:54:46 +02:00
|
|
|
}`;
|
|
|
|
} else {
|
|
|
|
const ethValue = getValueFromWeiHex({
|
|
|
|
value: amount,
|
2023-01-25 16:47:02 +01:00
|
|
|
toCurrency: EtherDenomination.ETH,
|
2022-05-04 18:54:46 +02:00
|
|
|
numberOfDecimals: 8,
|
|
|
|
});
|
2023-01-25 16:47:02 +01:00
|
|
|
logAmount = `${ethValue} ${
|
|
|
|
metamask?.provider?.ticker || EtherDenomination.ETH
|
|
|
|
}`;
|
2022-05-04 18:54:46 +02:00
|
|
|
}
|
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(`sendFlow - user set amount to ${logAmount}`),
|
|
|
|
);
|
|
|
|
await dispatch(actions.updateSendAmount(amount));
|
2022-07-01 15:58:35 +02:00
|
|
|
if (state[name].amountMode === AMOUNT_MODES.MAX) {
|
2021-06-23 23:35:25 +02:00
|
|
|
await dispatch(actions.updateAmountMode(AMOUNT_MODES.INPUT));
|
|
|
|
}
|
2021-07-24 02:40:40 +02:00
|
|
|
await dispatch(computeEstimatedGasLimit());
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* updates the asset to send to one of NATIVE or TOKEN and ensures that the
|
|
|
|
* asset balance is set. If sending a TOKEN also updates the asset details
|
|
|
|
* object with the appropriate ERC20 details including address, symbol and
|
|
|
|
* decimals.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-27 15:28:05 +02:00
|
|
|
* @param {object} payload - action payload
|
2021-06-23 23:35:25 +02:00
|
|
|
* @param {string} payload.type - type of asset to send
|
2022-01-07 16:57:33 +01:00
|
|
|
* @param {TokenDetails} [payload.details] - ERC20 details if sending TOKEN asset
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
export function updateSendAsset(
|
|
|
|
{ type, details: providedDetails },
|
2022-07-18 19:20:02 +02:00
|
|
|
{ initialAssetSet = false } = {},
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
2021-06-23 23:35:25 +02:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state[name].draftTransactions[state[name].currentTransactionUUID];
|
|
|
|
const sendingAddress =
|
|
|
|
draftTransaction.fromAccount?.address ??
|
|
|
|
state[name].selectedAccount.address ??
|
|
|
|
getSelectedAddress(state);
|
|
|
|
const account = getTargetAccount(state, sendingAddress);
|
2023-01-18 15:47:29 +01:00
|
|
|
if (type === AssetType.native) {
|
2022-07-21 01:03:23 +02:00
|
|
|
const unapprovedTxs = getUnapprovedTxs(state);
|
|
|
|
const unapprovedTx = unapprovedTxs?.[draftTransaction.id];
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(
|
2023-01-18 15:47:29 +01:00
|
|
|
`sendFlow - user set asset of type ${AssetType.native} with symbol ${
|
2023-01-25 16:47:02 +01:00
|
|
|
state.metamask.provider?.ticker ?? EtherDenomination.ETH
|
2023-01-18 15:47:29 +01:00
|
|
|
}`,
|
2022-07-01 15:58:35 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
await dispatch(
|
|
|
|
actions.updateAsset({
|
2022-07-18 19:20:02 +02:00
|
|
|
asset: {
|
|
|
|
type,
|
|
|
|
details: null,
|
|
|
|
balance: account.balance,
|
|
|
|
error: null,
|
|
|
|
},
|
|
|
|
initialAssetSet,
|
2022-07-01 15:58:35 +02:00
|
|
|
}),
|
|
|
|
);
|
2022-07-21 01:03:23 +02:00
|
|
|
|
|
|
|
// This is meant to handle cases where we are editing an unapprovedTx from the background state
|
|
|
|
// and its type is a token method. In such a case, the hex data will be the necessary hex data
|
|
|
|
// for calling the contract transfer method.
|
|
|
|
// Now that we are updating the transaction to be a send of a native asset type, we should
|
|
|
|
// set the hex data of the transaction being editing to be empty.
|
|
|
|
// then the user will not want to send any hex data now that they have change the
|
|
|
|
if (
|
2023-01-18 15:47:29 +01:00
|
|
|
unapprovedTx?.type === TransactionType.tokenMethodTransferFrom ||
|
|
|
|
unapprovedTx?.type === TransactionType.tokenMethodTransfer ||
|
|
|
|
unapprovedTx?.type === TransactionType.tokenMethodSafeTransferFrom
|
2022-07-21 01:03:23 +02:00
|
|
|
) {
|
|
|
|
await dispatch(actions.updateUserInputHexData(''));
|
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
} else {
|
|
|
|
await dispatch(showLoadingIndication());
|
|
|
|
const details = {
|
|
|
|
...providedDetails,
|
|
|
|
...(await getTokenStandardAndDetails(
|
|
|
|
providedDetails.address,
|
|
|
|
sendingAddress,
|
|
|
|
providedDetails.tokenId,
|
|
|
|
)),
|
|
|
|
};
|
|
|
|
await dispatch(hideLoadingIndication());
|
2022-07-18 16:43:30 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
const asset = {
|
|
|
|
type,
|
|
|
|
details,
|
|
|
|
error: null,
|
|
|
|
};
|
2022-07-18 16:43:30 +02:00
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
if (details.standard === TokenStandard.ERC20) {
|
2022-07-18 16:43:30 +02:00
|
|
|
asset.balance = addHexPrefix(
|
|
|
|
calcTokenAmount(details.balance, details.decimals).toString(16),
|
|
|
|
);
|
|
|
|
|
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user set asset to ERC20 token with symbol ${details.symbol} and address ${details.address}`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else if (
|
2023-01-18 15:47:29 +01:00
|
|
|
details.standard === TokenStandard.ERC1155 &&
|
|
|
|
type === AssetType.NFT
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
|
|
|
throw new Error('Sends of ERC1155 tokens are not currently supported');
|
|
|
|
} else if (
|
2023-01-18 15:47:29 +01:00
|
|
|
details.standard === TokenStandard.ERC1155 ||
|
|
|
|
details.standard === TokenStandard.ERC721
|
2022-07-01 15:58:35 +02:00
|
|
|
) {
|
2023-03-13 20:29:37 +01:00
|
|
|
if (type === AssetType.token) {
|
2022-07-01 15:58:35 +02:00
|
|
|
dispatch(
|
|
|
|
showModal({
|
|
|
|
name: 'CONVERT_TOKEN_TO_NFT',
|
|
|
|
tokenAddress: details.address,
|
|
|
|
}),
|
2022-01-19 19:42:41 +01:00
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
asset.error = INVALID_ASSET_TYPE;
|
|
|
|
throw new Error(INVALID_ASSET_TYPE);
|
|
|
|
} else {
|
|
|
|
let isCurrentOwner = true;
|
|
|
|
try {
|
2022-11-15 19:49:42 +01:00
|
|
|
isCurrentOwner = await isNftOwner(
|
2022-07-01 15:58:35 +02:00
|
|
|
sendingAddress,
|
|
|
|
details.address,
|
|
|
|
details.tokenId,
|
2022-01-19 19:42:41 +01:00
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
} catch (err) {
|
|
|
|
if (err.message.includes('Unable to verify ownership.')) {
|
|
|
|
// this would indicate that either our attempts to verify ownership failed because of network issues,
|
2023-02-16 20:23:29 +01:00
|
|
|
// or, somehow a token has been added to NFTs state with an incorrect chainId.
|
2022-07-01 15:58:35 +02:00
|
|
|
} else {
|
|
|
|
// Any other error is unexpected and should be surfaced.
|
|
|
|
dispatch(displayWarning(err.message));
|
|
|
|
}
|
2022-01-19 19:42:41 +01:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
if (isCurrentOwner) {
|
|
|
|
asset.error = null;
|
|
|
|
asset.balance = '0x1';
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
2023-02-16 20:23:29 +01:00
|
|
|
'Send slice initialized as NFT send with an NFT not currently owned by the select account',
|
2022-07-01 15:58:35 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user set asset to NFT with tokenId ${details.tokenId} and address ${details.address}`,
|
|
|
|
),
|
|
|
|
);
|
2022-01-10 17:23:53 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
|
2022-07-18 19:20:02 +02:00
|
|
|
await dispatch(actions.updateAsset({ asset, initialAssetSet }));
|
2022-07-01 15:58:35 +02:00
|
|
|
}
|
2022-07-18 19:20:02 +02:00
|
|
|
if (initialAssetSet === false) {
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(computeEstimatedGasLimit());
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* When a user has enabled hex data field in advanced settings they will be
|
|
|
|
* able to supply hex data on a transaction. This method updates the user
|
|
|
|
* supplied data. Note, when sending native assets this will result in
|
|
|
|
* recomputing estimated gasLimit. When sending a ERC20 asset this is not done
|
|
|
|
* because the data sent in the transaction will be determined by the asset,
|
|
|
|
* recipient and value, NOT what the user has supplied.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-01 15:58:35 +02:00
|
|
|
* @param {string} hexData - hex encoded string representing transaction data.
|
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
export function updateSendHexData(hexData) {
|
2021-06-23 23:35:25 +02:00
|
|
|
return async (dispatch, getState) => {
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(`sendFlow - user added custom hexData ${hexData}`),
|
|
|
|
);
|
2022-07-14 00:15:38 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(actions.updateUserInputHexData(hexData));
|
2021-06-23 23:35:25 +02:00
|
|
|
const state = getState();
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state[name].draftTransactions[state[name].currentTransactionUUID];
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction.asset.type === AssetType.native) {
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(computeEstimatedGasLimit());
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Sets the recipient search mode to show a list of the user's contacts and
|
|
|
|
* recently interacted with addresses.
|
|
|
|
*
|
|
|
|
* @returns {ThunkAction<void>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function useContactListForRecipientSearch() {
|
|
|
|
return (dispatch) => {
|
2022-05-04 18:54:46 +02:00
|
|
|
dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user selected back to all on recipient screen`,
|
|
|
|
),
|
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
dispatch(updateRecipientSearchMode(RECIPIENT_SEARCH_MODES.CONTACT_LIST));
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Sets the recipient search mode to show a list of the user's own accounts.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
export function useMyAccountsForRecipientSearch() {
|
|
|
|
return (dispatch) => {
|
|
|
|
dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user selected transfer to my accounts on recipient screen`,
|
|
|
|
),
|
2021-07-26 16:03:05 +02:00
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
dispatch(updateRecipientSearchMode(RECIPIENT_SEARCH_MODES.MY_ACCOUNTS));
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-01-07 16:57:33 +01:00
|
|
|
* Clears out the recipient user input, ENS resolution and recipient validation.
|
2022-07-01 15:58:35 +02:00
|
|
|
*
|
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export function resetRecipientInput() {
|
2022-09-14 17:35:33 +02:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const chainId = getCurrentChainId(state);
|
2022-05-04 18:54:46 +02:00
|
|
|
await dispatch(addHistoryEntry(`sendFlow - user cleared recipient input`));
|
2021-06-23 23:35:25 +02:00
|
|
|
await dispatch(updateRecipientUserInput(''));
|
|
|
|
await dispatch(updateRecipient({ address: '', nickname: '' }));
|
2022-11-21 16:19:11 +01:00
|
|
|
await dispatch(resetDomainResolution());
|
2022-09-14 17:35:33 +02:00
|
|
|
await dispatch(validateRecipientUserInput({ chainId }));
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-07-01 15:58:35 +02:00
|
|
|
* Resets the entire send state tree to the initial state. It also disconnects
|
|
|
|
* polling from the gas controller if the token is present in state.
|
2022-01-07 16:57:33 +01:00
|
|
|
*
|
2022-07-01 15:58:35 +02:00
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
2022-07-01 15:58:35 +02:00
|
|
|
export function resetSendState() {
|
2021-06-23 23:35:25 +02:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2022-07-01 15:58:35 +02:00
|
|
|
dispatch(actions.resetSendState());
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
if (state[name].gasEstimatePollToken) {
|
|
|
|
await disconnectGasFeeEstimatePoller(state[name].gasEstimatePollToken);
|
|
|
|
removePollingTokenFromAppState(state[name].gasEstimatePollToken);
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs a transaction or updates a transaction in state if editing.
|
|
|
|
* This method is called when a user clicks the next button in the footer of
|
|
|
|
* the send page, signaling that a transaction should be executed. This method
|
|
|
|
* will create the transaction in state (by way of the various global provider
|
|
|
|
* constructs) which will eventually (and fairly quickly from user perspective)
|
|
|
|
* result in a confirmation window being displayed for the transaction.
|
2022-07-01 15:58:35 +02:00
|
|
|
*
|
|
|
|
* @returns {ThunkAction<void>}
|
2021-06-23 23:35:25 +02:00
|
|
|
*/
|
|
|
|
export function signTransaction() {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2022-07-01 15:58:35 +02:00
|
|
|
const { stage, eip1559support } = state[name];
|
2022-05-24 23:05:27 +02:00
|
|
|
const txParams = generateTransactionParams(state[name]);
|
2022-07-01 15:58:35 +02:00
|
|
|
const draftTransaction =
|
|
|
|
state[name].draftTransactions[state[name].currentTransactionUUID];
|
2021-06-23 23:35:25 +02:00
|
|
|
if (stage === SEND_STAGES.EDIT) {
|
|
|
|
// When dealing with the edit flow there is already a transaction in
|
|
|
|
// state that we must update, this branch is responsible for that logic.
|
|
|
|
// We first must grab the previous transaction object from state and then
|
|
|
|
// merge in the modified txParams. Once the transaction has been modified
|
|
|
|
// we can send that to the background to update the transaction in state.
|
|
|
|
const unapprovedTxs = getUnapprovedTxs(state);
|
2023-02-23 19:16:55 +01:00
|
|
|
const unapprovedTx = cloneDeep(unapprovedTxs[draftTransaction.id]);
|
2021-08-02 16:47:42 +02:00
|
|
|
// We only update the tx params that can be changed via the edit flow UX
|
|
|
|
const eip1559OnlyTxParamsToUpdate = {
|
|
|
|
data: txParams.data,
|
|
|
|
from: txParams.from,
|
|
|
|
to: txParams.to,
|
|
|
|
value: txParams.value,
|
2022-01-21 16:58:59 +01:00
|
|
|
gas: unapprovedTx.userEditedGasLimit
|
|
|
|
? unapprovedTx.txParams.gas
|
|
|
|
: txParams.gas,
|
2021-08-02 16:47:42 +02:00
|
|
|
};
|
2022-01-21 16:58:59 +01:00
|
|
|
unapprovedTx.originalGasEstimate = eip1559OnlyTxParamsToUpdate.gas;
|
2021-06-23 23:35:25 +02:00
|
|
|
const editingTx = {
|
|
|
|
...unapprovedTx,
|
2021-08-02 16:47:42 +02:00
|
|
|
txParams: Object.assign(
|
|
|
|
unapprovedTx.txParams,
|
|
|
|
eip1559support ? eip1559OnlyTxParamsToUpdate : txParams,
|
|
|
|
),
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
2022-05-27 18:31:31 +02:00
|
|
|
|
2022-05-04 18:54:46 +02:00
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user clicked next and transaction should be updated in controller`,
|
|
|
|
),
|
|
|
|
);
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
updateTransactionSendFlowHistory(
|
|
|
|
draftTransaction.id,
|
2022-09-06 11:09:12 +02:00
|
|
|
unapprovedTx.sendFlowHistory?.length || 0,
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.history,
|
|
|
|
),
|
|
|
|
);
|
2022-07-21 01:03:23 +02:00
|
|
|
await dispatch(
|
|
|
|
updateEditableParams(draftTransaction.id, editingTx.txParams),
|
|
|
|
);
|
|
|
|
await dispatch(
|
2022-07-01 15:58:35 +02:00
|
|
|
updateTransactionGasFees(draftTransaction.id, editingTx.txParams),
|
|
|
|
);
|
2022-04-26 19:07:39 +02:00
|
|
|
} else {
|
2022-08-03 21:35:34 +02:00
|
|
|
let transactionType =
|
|
|
|
draftTransaction.recipient.type === RECIPIENT_TYPES.SMART_CONTRACT
|
2023-01-18 15:47:29 +01:00
|
|
|
? TransactionType.contractInteraction
|
|
|
|
: TransactionType.simpleSend;
|
2022-01-10 17:23:53 +01:00
|
|
|
|
2023-01-18 15:47:29 +01:00
|
|
|
if (draftTransaction.asset.type !== AssetType.native) {
|
2022-04-26 19:07:39 +02:00
|
|
|
transactionType =
|
2023-01-18 15:47:29 +01:00
|
|
|
draftTransaction.asset.type === AssetType.NFT
|
|
|
|
? TransactionType.tokenMethodTransferFrom
|
|
|
|
: TransactionType.tokenMethodTransfer;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
2022-05-04 18:54:46 +02:00
|
|
|
await dispatch(
|
|
|
|
addHistoryEntry(
|
|
|
|
`sendFlow - user clicked next and transaction should be added to controller`,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2022-04-26 19:07:39 +02:00
|
|
|
dispatch(
|
|
|
|
addUnapprovedTransactionAndRouteToConfirmationPage(
|
2023-01-23 15:32:01 +01:00
|
|
|
undefined,
|
2022-04-26 19:07:39 +02:00
|
|
|
txParams,
|
|
|
|
transactionType,
|
2022-07-01 15:58:35 +02:00
|
|
|
draftTransaction.history,
|
2022-04-26 19:07:39 +02:00
|
|
|
),
|
|
|
|
);
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Toggles the amount.mode between INPUT and MAX modes.
|
|
|
|
* As a result, the amount.value will change to either '0x0' when moving from
|
|
|
|
* MAX to INPUT, or to the maximum allowable amount based on current asset when
|
|
|
|
* moving from INPUT to MAX.
|
|
|
|
*
|
|
|
|
* @returns {ThunkAction<void>}
|
|
|
|
*/
|
|
|
|
export function toggleSendMaxMode() {
|
2021-06-23 23:35:25 +02:00
|
|
|
return async (dispatch, getState) => {
|
|
|
|
const state = getState();
|
2022-07-01 15:58:35 +02:00
|
|
|
if (state[name].amountMode === AMOUNT_MODES.MAX) {
|
|
|
|
await dispatch(actions.updateAmountMode(AMOUNT_MODES.INPUT));
|
|
|
|
await dispatch(actions.updateSendAmount('0x0'));
|
|
|
|
await dispatch(addHistoryEntry(`sendFlow - user toggled max mode off`));
|
|
|
|
} else {
|
|
|
|
await dispatch(actions.updateAmountMode(AMOUNT_MODES.MAX));
|
|
|
|
await dispatch(actions.updateAmountToMax());
|
|
|
|
await dispatch(addHistoryEntry(`sendFlow - user toggled max mode on`));
|
|
|
|
}
|
|
|
|
await dispatch(computeEstimatedGasLimit());
|
|
|
|
};
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Begins a new draft transaction, clearing out the previous draft transactions
|
|
|
|
* from state, and clearing the currentTransactionUUID. This action is one of
|
|
|
|
* the two entry points into the send flow. NOTE: You must route to the send
|
|
|
|
* page *after* dispatching this action resolves to ensure that the
|
|
|
|
* draftTransaction is properly created.
|
|
|
|
*
|
|
|
|
* @param {Pick<Asset, 'type' | 'details'>} asset - A partial asset
|
|
|
|
* object containing at least the asset type. If specifying a non-native asset
|
|
|
|
* then the asset details must be included with at least the address.
|
|
|
|
* @returns {ThunkAction<void>}
|
|
|
|
*/
|
|
|
|
export function startNewDraftTransaction(asset) {
|
|
|
|
return async (dispatch) => {
|
|
|
|
await dispatch(actions.clearPreviousDrafts());
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
actions.addNewDraft({
|
|
|
|
...draftTransactionInitialState,
|
|
|
|
history: [`sendFlow - User started new draft transaction`],
|
|
|
|
}),
|
|
|
|
);
|
2022-01-10 17:23:53 +01:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(
|
|
|
|
updateSendAsset({
|
2023-01-18 15:47:29 +01:00
|
|
|
type: asset.type ?? AssetType.native,
|
2022-07-01 15:58:35 +02:00
|
|
|
details: asset.details,
|
|
|
|
}),
|
|
|
|
);
|
2022-01-10 17:23:53 +01:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
await dispatch(initializeSendState());
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Selectors
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* The following typedef is a shortcut for typing selectors below. It uses a
|
|
|
|
* generic type, T, so that each selector can specify it's return type.
|
|
|
|
*
|
|
|
|
* @template T
|
|
|
|
* @typedef {(state: MetaMaskState) => T} Selector
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's UUID.
|
|
|
|
*
|
|
|
|
* @type {Selector<string>}
|
|
|
|
*/
|
|
|
|
export function getCurrentTransactionUUID(state) {
|
|
|
|
return state[name].currentTransactionUUID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction.
|
|
|
|
*
|
|
|
|
* @type {Selector<DraftTransaction>}
|
|
|
|
*/
|
|
|
|
export function getCurrentDraftTransaction(state) {
|
|
|
|
return state[name].draftTransactions[getCurrentTransactionUUID(state)] ?? {};
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-18 19:01:10 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns true if a draft transaction exists.
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
|
|
|
export function getDraftTransactionExists(state) {
|
|
|
|
const draftTransaction = getCurrentDraftTransaction(state);
|
|
|
|
if (Object.keys(draftTransaction).length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
// Gas selectors
|
2022-07-01 15:58:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's gasLimit.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getGasLimit(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).gas?.gasLimit;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's gasPrice.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getGasPrice(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).gas?.gasPrice;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's gasTotal.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getGasTotal(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).gas?.gasTotal;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the error, if present, for the gas fields.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function gasFeeIsInError(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return Boolean(getCurrentDraftTransaction(state).gas?.error);
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the minimum gasLimit for the current network.
|
|
|
|
*
|
|
|
|
* @type {Selector<string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getMinimumGasLimitForSend(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return state[name].gasLimitMinimum;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's gasLimit.
|
|
|
|
*
|
|
|
|
* @type {Selector<MapValuesToUnion<SendStateGasModes>>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getGasInputMode(state) {
|
|
|
|
const isMainnet = getIsMainnet(state);
|
2021-07-16 18:06:32 +02:00
|
|
|
const gasEstimateType = getGasEstimateType(state);
|
2021-06-23 23:35:25 +02:00
|
|
|
const showAdvancedGasFields = getAdvancedInlineGasShown(state);
|
2022-07-01 15:58:35 +02:00
|
|
|
if (state[name].gasIsSetInModal) {
|
2021-06-23 23:35:25 +02:00
|
|
|
return GAS_INPUT_MODES.CUSTOM;
|
|
|
|
}
|
|
|
|
if ((!isMainnet && !process.env.IN_TEST) || showAdvancedGasFields) {
|
|
|
|
return GAS_INPUT_MODES.INLINE;
|
|
|
|
}
|
2021-07-16 18:06:32 +02:00
|
|
|
|
|
|
|
// We get eth_gasPrice estimation if the legacy API fails but we need to
|
|
|
|
// instruct the UI to render the INLINE inputs in this case, only on
|
|
|
|
// mainnet or IN_TEST.
|
|
|
|
if (
|
|
|
|
(isMainnet || process.env.IN_TEST) &&
|
2023-01-27 19:28:03 +01:00
|
|
|
gasEstimateType === GasEstimateTypes.ethGasPrice
|
2021-07-16 18:06:32 +02:00
|
|
|
) {
|
|
|
|
return GAS_INPUT_MODES.INLINE;
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
return GAS_INPUT_MODES.BASIC;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Asset Selectors
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the asset the current draft transaction is sending.
|
|
|
|
*
|
|
|
|
* @type {Selector<?Asset>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendAsset(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).asset;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the contract address of the non-native asset that
|
|
|
|
* the current transaction is sending, if it exists.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendAssetAddress(state) {
|
|
|
|
return getSendAsset(state)?.details?.address;
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns a boolean value describing whether the currently
|
|
|
|
* selected asset is sendable, based upon the standard of the token.
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getIsAssetSendable(state) {
|
2023-01-18 15:47:29 +01:00
|
|
|
if (getSendAsset(state)?.type === AssetType.native) {
|
2021-06-23 23:35:25 +02:00
|
|
|
return true;
|
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
return getSendAsset(state)?.details?.isERC721 === false;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the asset error if it exists.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2022-01-19 19:42:41 +01:00
|
|
|
export function getAssetError(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getSendAsset(state).error;
|
2022-01-19 19:42:41 +01:00
|
|
|
}
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
// Amount Selectors
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the amount that current draft transaction is sending.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendAmount(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).amount?.value;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns true if the user has enough native asset balance to
|
|
|
|
* cover the cost of the transaction.
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getIsBalanceInsufficient(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return (
|
|
|
|
getCurrentDraftTransaction(state).gas?.error === INSUFFICIENT_FUNDS_ERROR
|
|
|
|
);
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
2022-07-01 15:58:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Selector that returns the amoung send mode, either MAX or INPUT.
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendMaxModeState(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return state[name].amountMode === AMOUNT_MODES.MAX;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's data field.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendHexData(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).userInputHexData;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's id, if present.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-09-17 06:37:50 +02:00
|
|
|
export function getDraftTransactionID(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getCurrentDraftTransaction(state).id;
|
2021-09-17 06:37:50 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns true if there is an error on the amount field.
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function sendAmountIsInError(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return Boolean(getCurrentDraftTransaction(state).amount?.error);
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Recipient Selectors
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current draft transaction's recipient.
|
|
|
|
*
|
|
|
|
* @type {Selector<DraftTransaction['recipient']>}
|
|
|
|
*/
|
|
|
|
export function getRecipient(state) {
|
|
|
|
const draft = getCurrentDraftTransaction(state);
|
|
|
|
if (!draft.recipient) {
|
|
|
|
return {
|
|
|
|
address: '',
|
|
|
|
nickname: '',
|
|
|
|
error: null,
|
|
|
|
warning: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const checksummedAddress = toChecksumHexAddress(draft.recipient.address);
|
|
|
|
if (state.metamask.ensResolutionsByAddress) {
|
|
|
|
return {
|
|
|
|
...draft.recipient,
|
|
|
|
nickname:
|
|
|
|
draft.recipient.nickname ||
|
|
|
|
getEnsResolutionByAddress(state, checksummedAddress),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return draft.recipient;
|
|
|
|
}
|
2021-06-23 23:35:25 +02:00
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the addres of the current draft transaction's
|
|
|
|
* recipient.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendTo(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return getRecipient(state)?.address;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns true if the current recipientMode is MY_ACCOUNTS
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getIsUsingMyAccountForRecipientSearch(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return state[name].recipientMode === RECIPIENT_SEARCH_MODES.MY_ACCOUNTS;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the value that the user has typed into the recipient
|
|
|
|
* input field.
|
|
|
|
*
|
|
|
|
* @type {Selector<?string>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getRecipientUserInput(state) {
|
2022-07-01 15:58:35 +02:00
|
|
|
return state[name].recipientInput;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-14 00:15:38 +02:00
|
|
|
export function getRecipientWarningAcknowledgement(state) {
|
|
|
|
return (
|
|
|
|
getCurrentDraftTransaction(state).recipient?.recipientWarningAcknowledged ??
|
|
|
|
false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-23 23:35:25 +02:00
|
|
|
// Overall validity and stage selectors
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the gasFee and amount errors, if they exist.
|
|
|
|
*
|
|
|
|
* @type {Selector<{ gasFee?: string, amount?: string}>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendErrors(state) {
|
|
|
|
return {
|
2022-07-01 15:58:35 +02:00
|
|
|
gasFee: getCurrentDraftTransaction(state).gas?.error,
|
|
|
|
amount: getCurrentDraftTransaction(state).amount?.error,
|
2021-06-23 23:35:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns true if the stage is anything except INACTIVE
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function isSendStateInitialized(state) {
|
2021-07-31 02:45:18 +02:00
|
|
|
return state[name].stage !== SEND_STAGES.INACTIVE;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns true if the current draft transaction is valid and in
|
|
|
|
* a sendable state.
|
|
|
|
*
|
|
|
|
* @type {Selector<boolean>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function isSendFormInvalid(state) {
|
2022-08-02 20:52:09 +02:00
|
|
|
const draftTransaction = getCurrentDraftTransaction(state);
|
|
|
|
if (!draftTransaction) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return draftTransaction.status === SEND_STATUSES.INVALID;
|
2021-06-23 23:35:25 +02:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:58:35 +02:00
|
|
|
/**
|
|
|
|
* Selector that returns the current stage of the send flow
|
|
|
|
*
|
|
|
|
* @type {Selector<MapValuesToUnion<SendStateStages>>}
|
|
|
|
*/
|
2021-06-23 23:35:25 +02:00
|
|
|
export function getSendStage(state) {
|
|
|
|
return state[name].stage;
|
|
|
|
}
|