From c8aeffb395dc16ce916ec0f6541945fc53b6d7af Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:15:42 -0400 Subject: [PATCH 1/7] feat: add util to fetch global and chain feature flags at once --- src/swapsInterfaces.ts | 32 +++++++++++-- src/swapsUtil.test.ts | 100 +++++++++++++++++++++++++++++++++++++++++ src/swapsUtil.ts | 21 ++++++++- 3 files changed, 149 insertions(+), 4 deletions(-) diff --git a/src/swapsInterfaces.ts b/src/swapsInterfaces.ts index a0668919..5a2c8bbc 100644 --- a/src/swapsInterfaces.ts +++ b/src/swapsInterfaces.ts @@ -23,16 +23,42 @@ export type SwapsToken = { iconUrl?: string; } & SwapsAsset; -export type NetworkFeatureFlags = { +export interface NetworkFeatureFlags { mobile_active: boolean; extension_active: boolean; - fallback_to_v1?: boolean; -}; + fallback_to_v1: boolean; + fallbackToV1: boolean; + mobileActive: boolean; + extensionActive: boolean; + mobileActiveIOS: boolean; + mobileActiveAndroid: boolean; + + smartTransactions: { + expectedDeadline: number; + maxDeadline: number; + returnTxHashAsap: boolean; + }; +} export type NetworksFeatureStatus = { [network: string]: NetworkFeatureFlags; }; +export interface GlobalFeatureFlags { + smart_transactions: { + mobile_active: boolean; + extension_active: boolean; + }; + smartTransactions: { + mobileActive: boolean; + extensionActive: boolean; + mobileActiveIOS: boolean; + mobileActiveAndroid: boolean; + }; +} + +export type FeatureFlags = NetworksFeatureStatus & GlobalFeatureFlags; + /** * Metadata needed to fetch quotes * @interface APIFetchQuotesMetadata diff --git a/src/swapsUtil.test.ts b/src/swapsUtil.test.ts index 9c0bb718..330cc1c2 100644 --- a/src/swapsUtil.test.ts +++ b/src/swapsUtil.test.ts @@ -549,6 +549,106 @@ describe('SwapsUtil', () => { }); }); + describe('fetchSwapsFeatureFlags', () => { + it('should work', 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 everything on 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.fetchSwapsFeatureLiveness('0x1'), + ).rejects.toThrow(); + }); + }); + describe('fetchGasPrices', () => { it('should work', async () => { mockFetch({ diff --git a/src/swapsUtil.ts b/src/swapsUtil.ts index 951feaed..eec591ea 100644 --- a/src/swapsUtil.ts +++ b/src/swapsUtil.ts @@ -29,6 +29,7 @@ import { import type { APIAggregatorMetadata, APIFetchQuotesParams, + FeatureFlags, NetworkFeatureFlags, NetworksFeatureStatus, Quote, @@ -370,7 +371,7 @@ export async function fetchTopAssets( * Fetches 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]; } +/** + * + * @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. From 0577f200f5a5cb8e5b5236ea1d59a60e95713205 Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:20:14 -0400 Subject: [PATCH 2/7] fix: lint errors --- src/swapsInterfaces.ts | 4 ++-- src/swapsUtil.ts | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/swapsInterfaces.ts b/src/swapsInterfaces.ts index 5a2c8bbc..0642639d 100644 --- a/src/swapsInterfaces.ts +++ b/src/swapsInterfaces.ts @@ -23,7 +23,7 @@ export type SwapsToken = { iconUrl?: string; } & SwapsAsset; -export interface NetworkFeatureFlags { +export type NetworkFeatureFlags = { mobile_active: boolean; extension_active: boolean; fallback_to_v1: boolean; @@ -44,7 +44,7 @@ export type NetworksFeatureStatus = { [network: string]: NetworkFeatureFlags; }; -export interface GlobalFeatureFlags { +export type GlobalFeatureFlags = { smart_transactions: { mobile_active: boolean; extension_active: boolean; diff --git a/src/swapsUtil.ts b/src/swapsUtil.ts index eec591ea..b65e683e 100644 --- a/src/swapsUtil.ts +++ b/src/swapsUtil.ts @@ -368,7 +368,7 @@ 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 for the chainId. @@ -386,9 +386,9 @@ export async function fetchSwapsFeatureLiveness( } /** - * - * @param chainId Current chainId. - * @param clientId Client id. + * 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( From e5aabef6dd7c0d4591ef199de1cf1b988bb8c80c Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:24:08 -0400 Subject: [PATCH 3/7] fix: lint errors --- src/swapsInterfaces.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/swapsInterfaces.ts b/src/swapsInterfaces.ts index 0642639d..4e0dd471 100644 --- a/src/swapsInterfaces.ts +++ b/src/swapsInterfaces.ts @@ -38,7 +38,7 @@ export type NetworkFeatureFlags = { maxDeadline: number; returnTxHashAsap: boolean; }; -} +}; export type NetworksFeatureStatus = { [network: string]: NetworkFeatureFlags; @@ -55,7 +55,7 @@ export type GlobalFeatureFlags = { mobileActiveIOS: boolean; mobileActiveAndroid: boolean; }; -} +}; export type FeatureFlags = NetworksFeatureStatus & GlobalFeatureFlags; From fe21b1065169bf311c58f73d573da41cf4e9d848 Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Tue, 30 Apr 2024 13:25:06 -0400 Subject: [PATCH 4/7] fix: wrong test --- src/swapsUtil.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swapsUtil.test.ts b/src/swapsUtil.test.ts index 330cc1c2..6cd0d2c1 100644 --- a/src/swapsUtil.test.ts +++ b/src/swapsUtil.test.ts @@ -644,7 +644,7 @@ describe('SwapsUtil', () => { }); await expect(async () => - swapsUtil.fetchSwapsFeatureLiveness('0x1'), + swapsUtil.fetchSwapsFeatureFlags('0x1'), ).rejects.toThrow(); }); }); From 34c479407ba449efb00def012a1b10f56e1deb3f Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Thu, 9 May 2024 11:35:30 -0400 Subject: [PATCH 5/7] fix: make fallback_to_v1 optional to avoid breaking change --- src/swapsInterfaces.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swapsInterfaces.ts b/src/swapsInterfaces.ts index 4e0dd471..6396d81f 100644 --- a/src/swapsInterfaces.ts +++ b/src/swapsInterfaces.ts @@ -26,7 +26,7 @@ export type SwapsToken = { export type NetworkFeatureFlags = { mobile_active: boolean; extension_active: boolean; - fallback_to_v1: boolean; + fallback_to_v1?: boolean; fallbackToV1: boolean; mobileActive: boolean; extensionActive: boolean; From 6d7bfa728f9d3118b4de4a750697bc17269bd345 Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Thu, 9 May 2024 11:37:03 -0400 Subject: [PATCH 6/7] chore: improve test names --- src/swapsUtil.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/swapsUtil.test.ts b/src/swapsUtil.test.ts index 6cd0d2c1..5420e16d 100644 --- a/src/swapsUtil.test.ts +++ b/src/swapsUtil.test.ts @@ -550,7 +550,7 @@ describe('SwapsUtil', () => { }); describe('fetchSwapsFeatureFlags', () => { - it('should work', async () => { + it('should return network and global feature flags', async () => { const featureFlags = { bsc: { mobile_active: false, @@ -592,7 +592,7 @@ describe('SwapsUtil', () => { expect(featureLiveness).toEqual(featureFlags); }); - it('should return everything on unsupported networks', async () => { + it('should return network and global feature flags regardless of unsupported networks', async () => { const featureFlags = { bsc: { mobile_active: false, From 71f40f25e73578e063d4cd592e5b5b44502707a9 Mon Sep 17 00:00:00 2001 From: IF <139582705+infiniteflower@users.noreply.github.com> Date: Fri, 10 May 2024 10:22:05 -0400 Subject: [PATCH 7/7] chore: revert back to original interface for fetchSwapsFeatureLiveness and use new interfaces for fetchSwapsFeatureFlags --- src/swapsInterfaces.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/swapsInterfaces.ts b/src/swapsInterfaces.ts index 6396d81f..896ac606 100644 --- a/src/swapsInterfaces.ts +++ b/src/swapsInterfaces.ts @@ -27,6 +27,16 @@ export type NetworkFeatureFlags = { mobile_active: boolean; extension_active: boolean; fallback_to_v1?: boolean; +}; + +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; @@ -40,8 +50,8 @@ export type NetworkFeatureFlags = { }; }; -export type NetworksFeatureStatus = { - [network: string]: NetworkFeatureFlags; +export type NetworksFeatureStatusAll = { + [network: string]: NetworkFeatureFlagsAll; }; export type GlobalFeatureFlags = { @@ -57,7 +67,7 @@ export type GlobalFeatureFlags = { }; }; -export type FeatureFlags = NetworksFeatureStatus & GlobalFeatureFlags; +export type FeatureFlags = NetworksFeatureStatusAll & GlobalFeatureFlags; /** * Metadata needed to fetch quotes