Skip to content

Commit

Permalink
fix: remove deprecated dex urls patch
Browse files Browse the repository at this point in the history
  • Loading branch information
cpellizzari-ledger committed Oct 2, 2023
1 parent 173d7d6 commit 12deba9
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 107 deletions.
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 @@ -234,7 +234,7 @@ const SwapForm = () => {
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 @@ 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
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 { 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 @@ -224,8 +223,6 @@ export function SwapForm({
providerURL?: string;
};

const customDappUrl = getCustomDappUrl(customParams);

const getAccountId = ({
accountId,
provider,
Expand Down Expand Up @@ -253,7 +250,7 @@ export function SwapForm({
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 "";
};

0 comments on commit 12deba9

Please sign in to comment.