Skip to content

Commit

Permalink
chore: apply changes from #9565
Browse files Browse the repository at this point in the history
  • Loading branch information
infiniteflower committed May 11, 2024
1 parent 92366ae commit 4ddcd0a
Show file tree
Hide file tree
Showing 35 changed files with 753 additions and 781 deletions.
44 changes: 17 additions & 27 deletions app/components/Nav/Main/RootRPCMethodsUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getTokenAddressParam,
calcTokenAmount,
getTokenValueParamAsHex,
getIsInSwapFlowTransaction,
getIsSwapApproveOrSwapTransaction,
} from '../../../util/transactions';
import { BN } from 'ethereumjs-util';
import Logger from '../../../util/Logger';
Expand Down Expand Up @@ -63,8 +63,9 @@ import { getLedgerKeyring } from '../../../core/Ledger/Ledger';
import { createLedgerTransactionModalNavDetails } from '../../UI/LedgerModals/LedgerTransactionModal';
import ExtendedKeyringTypes from '../../../constants/keyringTypes';
import { useMetrics } from '../../../components/hooks/useMetrics';
import { getIsSmartTransaction } from '../../../selectors/smartTransactionsController';
import { STX_NO_HASH_ERROR } from '../../../util/smart-transactions/smart-tx';
import { selectShouldUseSmartTransaction } from '../../../selectors/smartTransactionsController';
import { STX_NO_HASH_ERROR } from '../../../util/smart-transactions/smart-publish-hook';
import { getSmartTransactionMetricsProperties } from '../../../util/smart-transactions';

///: BEGIN:ONLY_INCLUDE_IF(snaps)
import InstallSnapApproval from '../../Approvals/InstallSnapApproval';
Expand Down Expand Up @@ -169,30 +170,20 @@ const RootRPCMethodsUI = (props) => {
delete newSwapsTransactions[transactionMeta.id].analytics;
delete newSwapsTransactions[transactionMeta.id].paramsForAnalytics;

let smartTransactionMetadata = {};
if (transactionMeta) {
const smartTransaction =
SmartTransactionsController.getSmartTransactionByMinedTxHash(
transactionMeta.transactionHash,
);

if (smartTransaction) {
smartTransactionMetadata = {
duplicated: smartTransaction.statusMetadata.duplicated,
timedOut: smartTransaction.statusMetadata.timedOut,
proxied: smartTransaction.statusMetadata.proxied,
};
}
}
const smartTransactionMetricsProperties =
getSmartTransactionMetricsProperties(
SmartTransactionsController,
transactionMeta,
);

const parameters = {
...analyticsParams,
time_to_mine: timeToMine,
estimated_vs_used_gasRatio: estimatedVsUsedGasRatio,
quote_vs_executionRatio: quoteVsExecutionRatio,
token_to_amount_received: tokenToAmountReceived.toString(),
is_smart_transaction: props.isSmartTransaction,
...smartTransactionMetadata,
is_smart_transaction: props.shouldUseSmartTransaction,
...smartTransactionMetricsProperties,
};

trackAnonymousEvent(event, parameters);
Expand All @@ -205,7 +196,7 @@ const RootRPCMethodsUI = (props) => {
},
[
props.selectedAddress,
props.isSmartTransaction,
props.shouldUseSmartTransaction,
trackEvent,
trackAnonymousEvent,
],
Expand All @@ -214,8 +205,7 @@ const RootRPCMethodsUI = (props) => {
const autoSign = useCallback(
async (transactionMeta) => {
const { TransactionController, KeyringController } = Engine.context;
const swapsTransactions =
Engine.context.TransactionController.state.swapsTransactions;
const swapsTransactions = TransactionController.state.swapsTransactions;
try {
TransactionController.hub.once(
`${transactionMeta.id}:finished`,
Expand Down Expand Up @@ -302,7 +292,7 @@ const RootRPCMethodsUI = (props) => {
const { data } = transactionMeta.txParams;

if (
getIsInSwapFlowTransaction(
getIsSwapApproveOrSwapTransaction(
data,
transactionMeta.origin,
to,
Expand Down Expand Up @@ -485,17 +475,17 @@ RootRPCMethodsUI.propTypes = {
*/
chainId: PropTypes.string,
/**
* If the transaction is a smart transaction
* If smart tranactions should be used
*/
isSmartTransaction: PropTypes.bool,
shouldUseSmartTransaction: PropTypes.bool,
};

const mapStateToProps = (state) => ({
selectedAddress: selectSelectedAddress(state),
chainId: selectChainId(state),
tokens: selectTokens(state),
providerType: selectProviderType(state),
isSmartTransaction: getIsSmartTransaction(state),
shouldUseSmartTransaction: selectShouldUseSmartTransaction(state),
});

const mapDispatchToProps = (dispatch) => ({
Expand Down
25 changes: 14 additions & 11 deletions app/components/UI/Notification/TransactionNotification/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ function TransactionNotification(props) {
// Don't show submitted notification for STX b/c we only know when it's confirmed,
// o/w a submitted notification will show up after it's confirmed, then a confirmed notification will show up immediately after
if (tx.status === 'submitted') {
const smartTx = smartTransactions.find(
(stx) => stx.transactionHash === tx.transactionHash,
);
const smartTx = smartTransactions.find((stx) => stx.txHash === tx.hash);
if (smartTx) {
return null;
}
Expand Down Expand Up @@ -435,15 +433,22 @@ TransactionNotification.propTypes = {

const mapStateToProps = (state) => {
const chainId = selectChainId(state);

const {
SmartTransactionsController,
TransactionController,
SwapsController,
} = state.engine.backgroundState;

const smartTransactions =
state.engine.backgroundState.SmartTransactionsController
?.smartTransactionsState?.smartTransactions?.[chainId] || [];
SmartTransactionsController?.smartTransactionsState?.smartTransactions?.[
chainId
] || [];

return {
accounts: selectAccounts(state),
selectedAddress: selectSelectedAddress(state),
transactions:
state.engine.backgroundState.TransactionController.transactions,
transactions: TransactionController.transactions,
ticker: selectTicker(state),
chainId,
tokens: selectTokensByAddress(state),
Expand All @@ -452,10 +457,8 @@ const mapStateToProps = (state) => {
conversionRate: selectConversionRate(state),
currentCurrency: selectCurrentCurrency(state),
primaryCurrency: state.settings.primaryCurrency,
swapsTransactions:
state.engine.backgroundState.TransactionController.swapsTransactions ||
{},
swapsTokens: state.engine.backgroundState.SwapsController.tokens,
swapsTransactions: TransactionController.swapsTransactions || {},
swapsTokens: SwapsController.tokens,
smartTransactions,
};
};
Expand Down
25 changes: 18 additions & 7 deletions app/components/UI/Swaps/QuotesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import { useMetrics } from '../../../components/hooks/useMetrics';
import { addTransaction } from '../../../util/transaction-controller';
import trackErrorAsAnalytics from '../../../util/metrics/TrackError/trackErrorAsAnalytics';
import { selectGasFeeEstimates } from '../../../selectors/confirmTransaction';
import { selectShouldUseSmartTransaction } from '../../../selectors/smartTransactionsController';

const POLLING_INTERVAL = 30000;
const SLIPPAGE_BUCKETS = {
Expand Down Expand Up @@ -388,6 +389,7 @@ function SwapsQuotesView({
usedCustomGas,
setRecipient,
resetTransaction,
shouldUseSmartTransaction,
}) {
const navigation = useNavigation();
/* Get params from navigation */
Expand Down Expand Up @@ -992,7 +994,7 @@ function SwapsQuotesView({
16,
).toString(10),
};
if (isHardwareAddress) {
if (isHardwareAddress || shouldUseSmartTransaction) {
TransactionController.hub.once(
`${transactionMeta.id}:confirmed`,
(transactionMeta) => {
Expand Down Expand Up @@ -1021,6 +1023,7 @@ function SwapsQuotesView({
selectedAddress,
setRecipient,
resetTransaction,
shouldUseSmartTransaction,
],
);

Expand Down Expand Up @@ -1053,12 +1056,17 @@ function SwapsQuotesView({
}
}

handleSwapTransaction(
TransactionController,
newSwapsTransactions,
approvalTransactionMetaId,
isHardwareAddress,
);
if (
!shouldUseSmartTransaction ||
(shouldUseSmartTransaction && !approvalTransaction)
) {
handleSwapTransaction(
TransactionController,
newSwapsTransactions,
approvalTransactionMetaId,
isHardwareAddress,
);
}

navigation.dangerouslyGetParent()?.pop();
}, [
Expand All @@ -1069,6 +1077,7 @@ function SwapsQuotesView({
handleApprovaltransaction,
handleSwapTransaction,
navigation,
shouldUseSmartTransaction,
]);

const onEditQuoteTransactionsGas = useCallback(() => {
Expand Down Expand Up @@ -2302,6 +2311,7 @@ SwapsQuotesView.propTypes = {
usedCustomGas: PropTypes.object,
setRecipient: PropTypes.func,
resetTransaction: PropTypes.func,
shouldUseSmartTransaction: PropTypes.bool,
};

const mapStateToProps = (state) => ({
Expand Down Expand Up @@ -2334,6 +2344,7 @@ const mapStateToProps = (state) => ({
usedCustomGas: state.engine.backgroundState.SwapsController.usedCustomGas,
primaryCurrency: state.settings.primaryCurrency,
swapsTokens: swapsTokensSelector(state),
shouldUseSmartTransaction: selectShouldUseSmartTransaction(state),
});

const mapDispatchToProps = (dispatch) => ({
Expand Down
11 changes: 5 additions & 6 deletions app/components/UI/Swaps/components/TokenSelectModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,11 @@ function TokenSelectModal({
const filteredInitialTokens = useMemo(
() =>
initialTokens?.length > 0
? initialTokens
.filter((token) => token !== undefined)
.filter(
(token) =>
!excludedAddresses.includes(token?.address?.toLowerCase()),
)
? initialTokens.filter(
(token) =>
typeof token !== 'undefined' &&
!excludedAddresses.includes(token?.address?.toLowerCase()),
)
: filteredTokens,
[excludedAddresses, filteredTokens, initialTokens],
);
Expand Down
2 changes: 1 addition & 1 deletion app/components/UI/TemplateRenderer/SafeComponentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export const safeComponentList = {
Button,
Icon,
SheetHeader,
SmartTransactionStatus,
Text,
View,
SmartTransactionStatus,
};

export type SafeComponentListValues = typeof safeComponentList;
16 changes: 9 additions & 7 deletions app/components/UI/TransactionElement/TransactionDetails/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { selectTokensByAddress } from '../../../../selectors/tokensController';
import { selectContractExchangeRates } from '../../../../selectors/tokenRatesController';
import { selectSelectedAddress } from '../../../../selectors/preferencesController';
import { regex } from '../../../../../app/util/regex';
import { getIsSmartTransaction } from '../../../../selectors/smartTransactionsController';
import { selectShouldUseSmartTransaction } from '../../../../selectors/smartTransactionsController';

const createStyles = (colors) =>
StyleSheet.create({
Expand Down Expand Up @@ -126,9 +126,9 @@ class TransactionDetails extends PureComponent {
primaryCurrency: PropTypes.string,

/**
* Boolean that indicates if the transaction is a smart transaction
* Boolean that indicates if smart transaction should be used
*/
isSmartTransaction: PropTypes.bool,
shouldUseSmartTransaction: PropTypes.bool,
};

state = {
Expand Down Expand Up @@ -306,12 +306,14 @@ class TransactionDetails extends PureComponent {
const {
chainId,
transactionObject: { status, time, txParams },
isSmartTransaction,
shouldUseSmartTransaction,
} = this.props;
const { updatedTransactionDetails } = this.state;
const styles = this.getStyles();

const renderTxActions = status === 'submitted' || status === 'approved';
const renderTxActions =
(status === 'submitted' || status === 'approved') &&
!shouldUseSmartTransaction;
const { rpcBlockExplorer } = this.state;

return updatedTransactionDetails ? (
Expand All @@ -322,7 +324,7 @@ class TransactionDetails extends PureComponent {
{strings('transactions.status')}
</DetailsModal.SectionTitle>
<StatusText status={status} />
{!!renderTxActions && !isSmartTransaction && (
{!!renderTxActions && (
<View style={styles.transactionActionsContainer}>
{this.renderSpeedUpButton()}
{this.renderCancelButton()}
Expand Down Expand Up @@ -432,7 +434,7 @@ const mapStateToProps = (state) => ({
swapsTransactions:
state.engine.backgroundState.TransactionController.swapsTransactions || {},
swapsTokens: state.engine.backgroundState.SwapsController.tokens,
isSmartTransaction: getIsSmartTransaction(state),
shouldUseSmartTransaction: selectShouldUseSmartTransaction(state),
});

TransactionDetails.contextType = ThemeContext;
Expand Down
7 changes: 4 additions & 3 deletions app/components/UI/TransactionElement/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,9 @@ class TransactionElement extends PureComponent {
const styles = createStyles(colors, typography);
const { value, fiatValue = false, actionKey } = transactionElement;
const renderNormalActions =
status === 'submitted' ||
(status === 'approved' && !isQRHardwareAccount && !isLedgerAccount);
(status === 'submitted' ||
(status === 'approved' && !isQRHardwareAccount && !isLedgerAccount)) &&
!isSmartTransaction;
const renderUnsignedQRActions =
status === 'approved' && isQRHardwareAccount;
const renderLedgerActions = status === 'approved' && isLedgerAccount;
Expand Down Expand Up @@ -359,7 +360,7 @@ class TransactionElement extends PureComponent {
</ListItem.Amounts>
)}
</ListItem.Content>
{renderNormalActions && !isSmartTransaction && (
{renderNormalActions && (
<ListItem.Actions>
{this.renderSpeedUpButton()}
{this.renderCancelButton()}
Expand Down
5 changes: 1 addition & 4 deletions app/components/UI/TransactionElement/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,7 @@ function decodeSwapsTx(args) {
contractExchangeRates,
assetSymbol,
} = args;
const swapTransaction =
(swapsTransactions &&
(swapsTransactions[id] || swapsTransactions[transactionHash])) ||
{};
const swapTransaction = swapsTransactions?.[id] || {};
const totalGas = calculateTotalGas({
...txParams,
gas: swapTransaction.gasUsed || gas,
Expand Down
2 changes: 2 additions & 0 deletions app/components/Views/Asset/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,9 @@ class Asset extends PureComponent {
};

const goToSwaps = () => {
// Pop asset screen first as it's very slow when trying to load the STX status modal if we don't
navigation.pop();

navigation.navigate(Routes.SWAPS, {
screen: 'SwapsAmountView',
params: {
Expand Down
5 changes: 3 additions & 2 deletions app/components/Views/Settings/AdvancedSettings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import {
selectSmartTransactionsOptInStatus,
selectUseTokenDetection,
} from '../../../../selectors/preferencesController';
import { getSmartTransactionsEnabled } from '../../../../selectors/smartTransactionsController';
import { selectSmartTransactionsEnabled } from '../../../../selectors/smartTransactionsController';
import Routes from '../../../../constants/navigation/Routes';

import { MetaMetricsEvents } from '../../../../core/Analytics';
Expand Down Expand Up @@ -369,6 +369,7 @@ class AdvancedSettings extends PureComponent {

this.props.metrics.trackEvent(MetaMetricsEvents.SMART_TRANSACTION_OPT_IN, {
stx_opt_in: smartTransactionsOptInStatus,
location: 'Advanced Settings',
});
};

Expand Down Expand Up @@ -620,7 +621,7 @@ const mapStateToProps = (state) => ({
isTokenDetectionEnabled: selectUseTokenDetection(state),
chainId: selectChainId(state),
smartTransactionsOptInStatus: selectSmartTransactionsOptInStatus(state),
smartTransactionsEnabled: getSmartTransactionsEnabled(state),
smartTransactionsEnabled: selectSmartTransactionsEnabled(state),
});

const mapDispatchToProps = (dispatch) => ({
Expand Down
Loading

0 comments on commit 4ddcd0a

Please sign in to comment.