diff --git a/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx b/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx index 9ab312f51730..c3b60d9af282 100644 --- a/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx +++ b/apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx @@ -7,7 +7,7 @@ import { getCustomFeesPerFamily, convertToNonAtomicUnit, } from "@ledgerhq/live-common/exchange/swap/webApp/index"; -import { getProviderName, getCustomDappUrl } from "@ledgerhq/live-common/exchange/swap/utils/index"; +import { getProviderName } from "@ledgerhq/live-common/exchange/swap/utils/index"; import React, { useCallback, useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { useDispatch, useSelector } from "react-redux"; @@ -234,7 +234,7 @@ const SwapForm = () => { provider: string; providerURL?: string; }; - const customDappUrl = getCustomDappUrl(customParams); + const pathname = `/platform/${getProviderName(provider).toLowerCase()}`; const getAccountId = ({ accountId, @@ -262,7 +262,7 @@ const SwapForm = () => { // This looks like an issue, the proper signature is: push(path, [state]) - (function) Pushes a new entry onto the history stack // It seems possible to also pass a LocationDescriptorObject but it does not expect extra properties // @ts-expect-error so customDappUrl is not expected to be here - customDappUrl, + customDappUrl: providerURL, pathname, state: { returnTo: "/swap", diff --git a/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx b/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx index 1baad9af3347..51dc1bf42521 100644 --- a/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx +++ b/apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx @@ -4,7 +4,6 @@ import { KeyboardAwareScrollView } from "react-native-keyboard-aware-scroll-view import { Button, Flex } from "@ledgerhq/native-ui"; import { OnNoRatesCallback } from "@ledgerhq/live-common/exchange/swap/types"; import { useSwapTransaction, usePageState } from "@ledgerhq/live-common/exchange/swap/hooks/index"; -import { getCustomDappUrl } from "@ledgerhq/live-common/exchange/swap/utils/index"; import { useFeature } from "@ledgerhq/live-common/featureFlags/index"; import { useDispatch, useSelector } from "react-redux"; import { useTranslation } from "react-i18next"; @@ -224,8 +223,6 @@ export function SwapForm({ providerURL?: string; }; - const customDappUrl = getCustomDappUrl(customParams); - const getAccountId = ({ accountId, provider, @@ -253,7 +250,7 @@ export function SwapForm({ platform: getProviderName(provider).toLowerCase(), name: getProviderName(provider), accountId, - customDappURL: customDappUrl, + customDappURL: providerURL, }); } else { setConfirmed(true); diff --git a/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts b/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts index 0ecb5c0cd3bf..f5317d836639 100644 --- a/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts +++ b/libs/ledger-live-common/src/exchange/swap/utils/index.test.ts @@ -10,7 +10,6 @@ import { isRegistrationRequired, getProviderName, getNoticeType, - getCustomDappUrl, } from "./index"; /* TODO: Refacto these two function and move them to mock/account.ts if needed */ @@ -220,33 +219,4 @@ describe("swap/utils/getNoticeType", function () { expect(result).toEqual(expectedResult); }); -}); - -describe("swap/utils/getCustomDappUrl", () => { - it("should convert correct paraswap URL", async () => { - const customDappUrl = getCustomDappUrl({ - provider: "paraswap", - providerURL: - "/platform/paraswap/#/0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-0xdac17f958d2ee523a2206206994597c13d831ec7/0.004?network=1", - }); - expect(customDappUrl).toBe( - "https://embedded.paraswap.io?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false&network=1#/0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-0xdac17f958d2ee523a2206206994597c13d831ec7/0.004", - ); - }); - it("should convert correct 1inch URL", async () => { - const customDappUrl = getCustomDappUrl({ - provider: "oneinch", - providerURL: "/platform/1inch/#/1/unified/swap/eth/usdt?sourceTokenAmount=0.04", - }); - expect(customDappUrl).toBe( - "https://app.1inch.io/#/1/simple/swap/eth/usdt?ledgerLive=true&sourceTokenAmount=0.04", - ); - }); - it("should not update URL when complete URL is provided", async () => { - const customDappUrl = getCustomDappUrl({ - provider: "oneinch", - providerURL: "https://app.1inch.io/#/test", - }); - expect(customDappUrl).toBe("https://app.1inch.io/#/test"); - }); -}); +}); \ No newline at end of file diff --git a/libs/ledger-live-common/src/exchange/swap/utils/index.ts b/libs/ledger-live-common/src/exchange/swap/utils/index.ts index 33b4a8daf26f..5149d1590895 100644 --- a/libs/ledger-live-common/src/exchange/swap/utils/index.ts +++ b/libs/ledger-live-common/src/exchange/swap/utils/index.ts @@ -85,72 +85,3 @@ export const getNoticeType = (provider: string): { message: string; learnMore: b } }; -const isValidUrl = (urlString: string) => { - try { - return Boolean(new URL(urlString)); - } catch (e) { - return false; - } -}; - -/** - * Get complete DAPP URL - * @param provider - * @param providerURL - * - * This Func is to ensure a complete DAPP URL is generated if partial & incorrect path is provided. - * - * Example 1: - * actual: /platform/paraswap/#/0xdac17f958d2ee523a2206206994597c13d831ec7-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599/21.3?network=1 - * expected: https://embedded.paraswap.io?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false&network=1#/0xdac17f958d2ee523a2206206994597c13d831ec7-0x2260fac5e5542a773aa44fbcfedf7c193bc2c599/21.3 - * - * Example 2: - * expected: /platform/1inch/#/1/unified/swap/usdt/shib?sourceTokenAmount=24.6 - * actual: https://app.1inch.io/#/1/simple/swap/usdt/shib?ledgerLive=true&sourceTokenAmount=24.6 - */ -export const getCustomDappUrl = ({ - provider, - providerURL = "", -}: { - provider: string; - providerURL?: string; -}): string => { - if (isValidUrl(providerURL)) { - return providerURL; - } - - const dappUrl = - provider === "paraswap" - ? "https://embedded.paraswap.io/?referrer=ledger2&embed=true&enableStaking=false&displayMenu=false&enableNetworkSwitch=false" - : "https://app.1inch.io/?ledgerLive=true"; - const newUrl = `https://www.prefix.com/${providerURL}`; - const isValidNewdUrl = isValidUrl(newUrl); - - if (isValidNewdUrl) { - const { origin, search } = new URL(dappUrl); - const { hash: fragment, searchParams } = new URL(newUrl); - const [realFragment, query] = fragment.split("?"); - const urlSearchParams = new URLSearchParams(query); - const allParams = { - ...Object.fromEntries(new URLSearchParams(search)), - ...Object.fromEntries(urlSearchParams.entries()), - ...Object.fromEntries(searchParams), - }; - - /** - * Providers should use the standard structure: query + fragment - * - * 1inch is currently not using the standard (fragment + query). To be refactored once providers follow the standard structure. - * @see https://www.rfc-editor.org/rfc/rfc3986#section-4.2 - */ - const newDappUrl = - provider === "oneinch" - ? `${origin}/${realFragment}?${new URLSearchParams(allParams).toString()}`.replace( - "/unified/", - "/simple/", - ) - : `${origin}?${new URLSearchParams(allParams).toString()}${realFragment}`; - return newDappUrl; - } - return ""; -};