Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove deprecated dex urls patch #4914

Merged
merged 7 commits into from
Oct 13, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -227,14 +227,7 @@ const SwapForm = () => {
if (providerType === "DEX") {
const from = swapTransaction.swap.from;
const fromAccountId = from.parentAccount?.id || from.account?.id;
const customParams = {
provider,
providerURL,
} as {
provider: string;
providerURL?: string;
};
const customDappUrl = getCustomDappUrl(customParams);

const pathname = `/platform/${getProviderName(provider).toLowerCase()}`;
const getAccountId = ({
accountId,
Expand Down Expand Up @@ -262,7 +255,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",
Expand Down
12 changes: 1 addition & 11 deletions apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -216,15 +215,6 @@ export function SwapForm({
if (providerType === "DEX") {
const from = swapTransaction.swap.from;
const fromAccountId = from.parentAccount?.id || from.account?.id;
const customParams = {
provider,
providerURL,
} as {
provider: string;
providerURL?: string;
};

const customDappUrl = getCustomDappUrl(customParams);

const getAccountId = ({
accountId,
Expand Down Expand Up @@ -253,7 +243,7 @@ export function SwapForm({
platform: getProviderName(provider).toLowerCase(),
name: getProviderName(provider),
accountId,
customDappURL: customDappUrl,
customDappURL: providerURL,
});
} else {
setConfirmed(true);
Expand Down
30 changes: 0 additions & 30 deletions libs/ledger-live-common/src/exchange/swap/utils/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -221,32 +220,3 @@ 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");
});
});
70 changes: 0 additions & 70 deletions libs/ledger-live-common/src/exchange/swap/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,73 +84,3 @@ export const getNoticeType = (provider: string): { message: string; learnMore: b
return { message: "default", learnMore: true };
}
};

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 "";
};
Loading