diff --git a/src/swapsInterfaces.ts b/src/swapsInterfaces.ts index a0668919..896ac606 100644 --- a/src/swapsInterfaces.ts +++ b/src/swapsInterfaces.ts @@ -33,6 +33,42 @@ export type NetworksFeatureStatus = { [network: string]: NetworkFeatureFlags; }; +export type NetworkFeatureFlagsAll = { + mobile_active: boolean; + extension_active: boolean; + fallback_to_v1?: boolean; + fallbackToV1: boolean; + mobileActive: boolean; + extensionActive: boolean; + mobileActiveIOS: boolean; + mobileActiveAndroid: boolean; + + smartTransactions: { + expectedDeadline: number; + maxDeadline: number; + returnTxHashAsap: boolean; + }; +}; + +export type NetworksFeatureStatusAll = { + [network: string]: NetworkFeatureFlagsAll; +}; + +export type GlobalFeatureFlags = { + smart_transactions: { + mobile_active: boolean; + extension_active: boolean; + }; + smartTransactions: { + mobileActive: boolean; + extensionActive: boolean; + mobileActiveIOS: boolean; + mobileActiveAndroid: boolean; + }; +}; + +export type FeatureFlags = NetworksFeatureStatusAll & GlobalFeatureFlags; + /** * Metadata needed to fetch quotes * @interface APIFetchQuotesMetadata diff --git a/src/swapsUtil.test.ts b/src/swapsUtil.test.ts index 9c0bb718..5420e16d 100644 --- a/src/swapsUtil.test.ts +++ b/src/swapsUtil.test.ts @@ -549,6 +549,106 @@ describe('SwapsUtil', () => { }); }); + describe('fetchSwapsFeatureFlags', () => { + it('should return network and global feature flags', async () => { + const featureFlags = { + bsc: { + mobile_active: false, + extension_active: true, + fallback_to_v1: true, + }, + ethereum: { + mobile_active: false, + extension_active: true, + fallback_to_v1: true, + }, + polygon: { + mobile_active: false, + extension_active: true, + fallback_to_v1: false, + }, + smart_transactions: { + mobile_active: true, + extension_active: true + }, + smartTransactions: { + mobileActive: true, + extensionActive: true, + mobileActiveIOS: false, + mobileActiveAndroid: false + }, + swapRedesign: { + mobileActive: false, + extensionActive: true + } + } + + mockFetch({ + 'https://swap.metaswap.codefi.network/featureFlags': { + body: featureFlags, + }, + }); + const featureLiveness = await swapsUtil.fetchSwapsFeatureFlags('0x1'); + expect(featureLiveness).toEqual(featureFlags); + }); + + it('should return network and global feature flags regardless of unsupported networks', async () => { + const featureFlags = { + bsc: { + mobile_active: false, + extension_active: true, + fallback_to_v1: true, + }, + ethereum: { + mobile_active: false, + extension_active: true, + fallback_to_v1: true, + }, + polygon: { + mobile_active: false, + extension_active: true, + fallback_to_v1: false, + }, + smart_transactions: { + mobile_active: true, + extension_active: true + }, + smartTransactions: { + mobileActive: true, + extensionActive: true, + mobileActiveIOS: false, + mobileActiveAndroid: false + }, + swapRedesign: { + mobileActive: false, + extensionActive: true + } + } + + mockFetch({ + 'https://swap.metaswap.codefi.network/featureFlags': { + body: featureFlags, + }, + }); + const featureLiveness = await swapsUtil.fetchSwapsFeatureFlags( + '0x321', + ); + expect(featureLiveness).toEqual(featureFlags); + }); + + it('should throw on exception', async () => { + mockFetch({ + 'https://swap.metaswap.codefi.network/featureFlags': { + throws: true, + }, + }); + + await expect(async () => + swapsUtil.fetchSwapsFeatureFlags('0x1'), + ).rejects.toThrow(); + }); + }); + describe('fetchGasPrices', () => { it('should work', async () => { mockFetch({ diff --git a/src/swapsUtil.ts b/src/swapsUtil.ts index 951feaed..b65e683e 100644 --- a/src/swapsUtil.ts +++ b/src/swapsUtil.ts @@ -29,6 +29,7 @@ import { import type { APIAggregatorMetadata, APIFetchQuotesParams, + FeatureFlags, NetworkFeatureFlags, NetworksFeatureStatus, Quote, @@ -367,10 +368,10 @@ export async function fetchTopAssets( } /** - * Fetches feature flags from API URL. + * Fetches chainId specific feature flags from API URL. * @param chainId - Current chainId. * @param clientId - Client id. - * @returns Promise resolving to an object containing feature flags. + * @returns Promise resolving to an object containing feature flags for the chainId. */ export async function fetchSwapsFeatureLiveness( chainId: Hex, @@ -384,6 +385,24 @@ export async function fetchSwapsFeatureLiveness( return status[networkName]; } +/** + * Fetches global and chainId specific feature flags from API URL. + * @param chainId - Current chainId. + * @param clientId - Client id. + * @returns Promise resolving to an object containing global and chainId specific feature flags. + */ +export async function fetchSwapsFeatureFlags( + chainId: Hex, + clientId?: string, +): Promise { + const status: FeatureFlags = await handleFetch( + getBaseApiURL(APIType.FEATURE_FLAG, chainId), + { method: 'GET', headers: getClientIdHeader(clientId) }, + ); + + return status; +} + /** * Fetches gas prices from API URL. * @param chainId - Current chainId.