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 #4911

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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,14 @@
if (providerType === "DEX") {
const from = swapTransaction.swap.from;
const fromAccountId = from.parentAccount?.id || from.account?.id;
const customParams = {

Check failure on line 230 in apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx

View check run for this annotation

live-github-bot / @Desktop β€’ Test App

@typescript-eslint/no-unused-vars

'customParams' is assigned a value but never used.

Check failure on line 230 in apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx

View check run for this annotation

live-github-bot / @Desktop β€’ Test App

@typescript-eslint/no-unused-vars

'customParams' is assigned a value but never used.
provider,
providerURL,
} as {
provider: string;
providerURL?: string;
};
const customDappUrl = getCustomDappUrl(customParams);

Check failure on line 237 in apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx

View check run for this annotation

live-github-bot / @Desktop β€’ Test App

prettier/prettier

Delete `Β·Β·Β·Β·Β·Β·`

Check failure on line 237 in apps/ledger-live-desktop/src/renderer/screens/exchange/Swap2/Form/index.tsx

View check run for this annotation

live-github-bot / @Desktop β€’ Test App

prettier/prettier

Delete `Β·Β·Β·Β·Β·Β·`
const pathname = `/platform/${getProviderName(provider).toLowerCase()}`;
const getAccountId = ({
accountId,
Expand Down Expand Up @@ -262,7 +262,7 @@
// 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
5 changes: 1 addition & 4 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 { 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,7 +215,7 @@
if (providerType === "DEX") {
const from = swapTransaction.swap.from;
const fromAccountId = from.parentAccount?.id || from.account?.id;
const customParams = {

Check failure on line 218 in apps/ledger-live-mobile/src/screens/Swap/Form/index.tsx

View check run for this annotation

live-github-bot / @Mobile β€’ Test App

@typescript-eslint/no-unused-vars

'customParams' is assigned a value but never used.
provider,
providerURL,
} as {
Expand All @@ -224,8 +223,6 @@
providerURL?: string;
};

const customDappUrl = getCustomDappUrl(customParams);

const getAccountId = ({
accountId,
provider,
Expand Down Expand Up @@ -253,7 +250,7 @@
platform: getProviderName(provider).toLowerCase(),
name: getProviderName(provider),
accountId,
customDappURL: customDappUrl,
customDappURL: providerURL,
});
} else {
setConfirmed(true);
Expand Down
32 changes: 1 addition & 31 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 @@ -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");
});
});
});
69 changes: 0 additions & 69 deletions libs/ledger-live-common/src/exchange/swap/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
};
Loading