Skip to content

Commit e489cee

Browse files
committed
refactor: getRequestParams util
1 parent 0540c62 commit e489cee

File tree

2 files changed

+26
-31
lines changed

2 files changed

+26
-31
lines changed

packages/bridge-controller/src/bridge-controller.ts

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import {
2929
type BridgeControllerMessenger,
3030
type FetchFunction,
3131
RequestStatus,
32-
ChainId,
3332
} from './types';
3433
import { getAssetIdsForToken, toExchangeRates } from './utils/assets';
3534
import { hasSufficientBalance } from './utils/balance';
@@ -70,7 +69,6 @@ import {
7069
import type {
7170
QuoteFetchData,
7271
RequestMetadata,
73-
RequestParams,
7472
RequiredEventContextFromClient,
7573
} from './utils/metrics/types';
7674
import { type CrossChainSwapsEventProperties } from './utils/metrics/types';
@@ -778,16 +776,6 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
778776
return networkClient;
779777
}
780778

781-
readonly #getRequestParams = (): Omit<
782-
RequestParams,
783-
'token_symbol_source' | 'token_symbol_destination'
784-
> => {
785-
const srcChainIdCaip = formatChainIdToCaip(
786-
this.state.quoteRequest.srcChainId ?? ChainId.ETH,
787-
);
788-
return getRequestParams(this.state.quoteRequest, srcChainIdCaip);
789-
};
790-
791779
readonly #getRequestMetadata = (): Omit<
792780
RequestMetadata,
793781
| 'stx_enabled'
@@ -830,18 +818,18 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
830818
case UnifiedSwapBridgeEventName.ButtonClicked:
831819
case UnifiedSwapBridgeEventName.PageViewed:
832820
return {
833-
...this.#getRequestParams(),
821+
...getRequestParams(this.state.quoteRequest),
834822
...baseProperties,
835823
};
836824
case UnifiedSwapBridgeEventName.QuotesValidationFailed:
837825
return {
838-
...this.#getRequestParams(),
826+
...getRequestParams(this.state.quoteRequest),
839827
refresh_count: this.state.quotesRefreshCount,
840828
...baseProperties,
841829
};
842830
case UnifiedSwapBridgeEventName.QuotesReceived:
843831
return {
844-
...this.#getRequestParams(),
832+
...getRequestParams(this.state.quoteRequest),
845833
...this.#getRequestMetadata(),
846834
...this.#getQuoteFetchData(),
847835
is_hardware_wallet: isHardwareWallet(
@@ -852,7 +840,7 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
852840
};
853841
case UnifiedSwapBridgeEventName.QuotesRequested:
854842
return {
855-
...this.#getRequestParams(),
843+
...getRequestParams(this.state.quoteRequest),
856844
...this.#getRequestMetadata(),
857845
is_hardware_wallet: isHardwareWallet(
858846
this.#getMultichainSelectedAccount(),
@@ -862,7 +850,7 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
862850
};
863851
case UnifiedSwapBridgeEventName.QuotesError:
864852
return {
865-
...this.#getRequestParams(),
853+
...getRequestParams(this.state.quoteRequest),
866854
...this.#getRequestMetadata(),
867855
is_hardware_wallet: isHardwareWallet(
868856
this.#getMultichainSelectedAccount(),
@@ -875,7 +863,7 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
875863
case UnifiedSwapBridgeEventName.AllQuotesSorted:
876864
case UnifiedSwapBridgeEventName.QuoteSelected:
877865
return {
878-
...this.#getRequestParams(),
866+
...getRequestParams(this.state.quoteRequest),
879867
...this.#getRequestMetadata(),
880868
...this.#getQuoteFetchData(),
881869
is_hardware_wallet: isHardwareWallet(
@@ -887,7 +875,7 @@ export class BridgeController extends StaticIntervalPollingController<BridgePoll
887875
// Populate the properties that the error occurred before the tx was submitted
888876
return {
889877
...baseProperties,
890-
...this.#getRequestParams(),
878+
...getRequestParams(this.state.quoteRequest),
891879
...this.#getRequestMetadata(),
892880
...this.#getQuoteFetchData(),
893881
...propertiesFromClient,

packages/bridge-controller/src/utils/metrics/properties.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import type { AccountsControllerState } from '@metamask/accounts-controller';
2-
import type { CaipChainId } from '@metamask/utils';
32

43
import { MetricsSwapType } from './constants';
5-
import type { InputKeys, InputValues } from './types';
4+
import type { InputKeys, InputValues, RequestParams } from './types';
65
import { DEFAULT_BRIDGE_CONTROLLER_STATE } from '../../constants/bridge';
7-
import type { BridgeControllerState, QuoteResponse, TxData } from '../../types';
8-
import { type GenericQuoteRequest, type QuoteRequest } from '../../types';
6+
import type { QuoteResponse, TxData } from '../../types';
7+
import {
8+
ChainId,
9+
type GenericQuoteRequest,
10+
type QuoteRequest,
11+
} from '../../types';
912
import { getNativeAssetForChainId, isCrossChain } from '../bridge';
1013
import {
1114
formatAddressToAssetId,
@@ -67,14 +70,18 @@ export const formatProviderLabel = ({
6770
}: QuoteResponse<TxData | string>['quote']): `${string}_${string}` =>
6871
`${bridgeId}_${bridges[0]}`;
6972

70-
export const getRequestParams = (
71-
{
72-
destChainId,
73-
srcTokenAddress,
74-
destTokenAddress,
75-
}: BridgeControllerState['quoteRequest'],
76-
srcChainIdCaip: CaipChainId,
77-
) => {
73+
export const getRequestParams = ({
74+
srcChainId,
75+
destChainId,
76+
srcTokenAddress,
77+
destTokenAddress,
78+
}: Partial<GenericQuoteRequest>): Omit<
79+
RequestParams,
80+
'token_symbol_source' | 'token_symbol_destination'
81+
> => {
82+
// Fallback to ETH if srcChainId is not defined. This is ok since the clients default to Ethereum as the source chain
83+
// This also doesn't happen at runtime since the quote request is validated before metrics are published
84+
const srcChainIdCaip = formatChainIdToCaip(srcChainId ?? ChainId.ETH);
7885
return {
7986
chain_id_source: srcChainIdCaip,
8087
chain_id_destination: destChainId ? formatChainIdToCaip(destChainId) : null,

0 commit comments

Comments
 (0)