From 46a7eed41942a1461931b586ac228bb0239c31ee Mon Sep 17 00:00:00 2001 From: chrisduma-ledger Date: Tue, 19 Nov 2024 15:57:32 +0200 Subject: [PATCH 1/5] feat: fix logic and refactor --- libs/exchange-module/src/index.ts | 3 +- libs/exchange-module/src/types.ts | 1 + .../src/hw/actions/startExchange.ts | 42 ++++++------------- .../src/wallet-api/Exchange/server.test.ts | 1 + .../src/wallet-api/Exchange/server.ts | 28 ++++++++++--- 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/libs/exchange-module/src/index.ts b/libs/exchange-module/src/index.ts index 8ebe1a74c18f..4b98f9f951c7 100644 --- a/libs/exchange-module/src/index.ts +++ b/libs/exchange-module/src/index.ts @@ -36,12 +36,13 @@ export class ExchangeModule extends CustomModule { * * @returns - A transaction ID used to complete the exchange process */ - async startSell({ provider }: Omit) { + async startSell({ provider, fromAccountId }: Omit) { const result = await this.request( "custom.exchange.start", { exchangeType: "SELL", provider, + fromAccountId, }, ); diff --git a/libs/exchange-module/src/types.ts b/libs/exchange-module/src/types.ts index c7d47fcd1eb5..e5f131accd95 100644 --- a/libs/exchange-module/src/types.ts +++ b/libs/exchange-module/src/types.ts @@ -18,6 +18,7 @@ export type ExchangeStartFundParams = { export type ExchangeStartSellParams = { exchangeType: "SELL"; provider: string; + fromAccountId: string; }; export type ExchangeStartSwapParams = { diff --git a/libs/ledger-live-common/src/hw/actions/startExchange.ts b/libs/ledger-live-common/src/hw/actions/startExchange.ts index f5a022b7210a..513820da13a8 100644 --- a/libs/ledger-live-common/src/hw/actions/startExchange.ts +++ b/libs/ledger-live-common/src/hw/actions/startExchange.ts @@ -129,38 +129,20 @@ export const createAction = ( appName: "Exchange", }; } - if (!exchange || !mainFromAccount || !mainToAccount) { - return { - appName: "Exchange", - requireLatestFirmware, - }; - } else { - const shouldAddEthApp = - (mainFromAccount.currency.family === "evm" || mainToAccount.currency.family === "evm") && - mainFromAccount.currency.managerAppName !== "Ethereum" && - mainToAccount.currency.managerAppName !== "Ethereum"; - const dependencies: AppRequest["dependencies"] = [ - { - account: mainFromAccount, - }, - { - account: mainToAccount, - }, - ]; - - if (shouldAddEthApp) { - dependencies.push({ - appName: "Ethereum", - }); - } + const dependencies: AppRequest["dependencies"] = []; + if (mainFromAccount) { + dependencies.push({ appName: mainFromAccount?.currency?.managerAppName }); + } - return { - appName: "Exchange", - dependencies, - requireLatestFirmware, - }; + if (mainToAccount) { + dependencies.push({ appName: mainToAccount?.currency?.managerAppName }); } - }, [exchange, mainFromAccount, mainToAccount, requireLatestFirmware]); + return { + appName: "Exchange", + dependencies, + requireLatestFirmware, + }; + }, [mainFromAccount, mainToAccount, requireLatestFirmware]); const appState = createAppAction(connectAppExec).useHook(reduxDeviceFrozen, request); diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.test.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.test.ts index 6471b32a5b8b..e47699e2d9a6 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.test.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.test.ts @@ -119,6 +119,7 @@ describe("handlers", () => { const params: ExchangeStartSellParams = { exchangeType: "SELL", provider: "TestSellProvider", + fromAccountId: accounts[0].id, }; const { request, context, walletHandlers } = prepareSellRequest(params); diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts index dbd1d9a476ff..12014f2e17f4 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts @@ -72,6 +72,7 @@ type ExchangeStartParamsUiRequest = | { exchangeType: "SELL"; provider: string; + exchange: Partial; } | { exchangeType: "SWAP"; @@ -125,12 +126,12 @@ export const handlers = ({ } let exchangeParams: ExchangeStartParamsUiRequest; - + console.log("params", params); // Use `if else` instead of switch to leverage TS type narrowing and avoid `params` force cast. if (params.exchangeType == "SWAP") { exchangeParams = extractSwapStartParam(params, accounts); } else if (params.exchangeType == "SELL") { - exchangeParams = extractSellStartParam(params); + exchangeParams = extractSellStartParam(params, accounts); } else { exchangeParams = { exchangeType: params.exchangeType, @@ -362,13 +363,30 @@ function extractSwapStartParam( }; } -function extractSellStartParam(params: ExchangeStartSellParams): ExchangeStartParamsUiRequest { - if (!("provider" in params)) { - throw new ExchangeError(createWrongSellParams(params)); +function extractSellStartParam( + params: ExchangeStartSellParams, + accounts: AccountLike[], +): ExchangeStartParamsUiRequest { + const realFromAccountId = getAccountIdFromWalletAccountId(params.fromAccountId); + + if (!realFromAccountId) { + throw new ExchangeError(createAccounIdNotFound(params.fromAccountId)); } + const fromAccount = accounts.find(acc => acc.id === realFromAccountId); + + if (!fromAccount) { + throw new ServerError(createAccountNotFound(params.fromAccountId)); + } + + const fromParentAccount = getParentAccount(fromAccount, accounts); + return { exchangeType: params.exchangeType, provider: params.provider, + exchange: { + fromAccount, + fromParentAccount, + }, }; } From 5c4455f80b03991843e16c80c5af5a019db25227 Mon Sep 17 00:00:00 2001 From: chrisduma-ledger Date: Tue, 19 Nov 2024 16:00:36 +0200 Subject: [PATCH 2/5] feat: add changeset --- .changeset/young-coats-flash.md | 8 ++++++++ libs/ledger-live-common/src/wallet-api/Exchange/server.ts | 4 ++++ 2 files changed, 12 insertions(+) create mode 100644 .changeset/young-coats-flash.md diff --git a/.changeset/young-coats-flash.md b/.changeset/young-coats-flash.md new file mode 100644 index 000000000000..d28a74b61013 --- /dev/null +++ b/.changeset/young-coats-flash.md @@ -0,0 +1,8 @@ +--- +"ledger-live-desktop": minor +"live-mobile": minor +"@ledgerhq/live-common": minor +"@ledgerhq/wallet-api-exchange-module": minor +--- + +Fixes app install and refactors logic diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts index 12014f2e17f4..43eecab52fa1 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts @@ -367,6 +367,10 @@ function extractSellStartParam( params: ExchangeStartSellParams, accounts: AccountLike[], ): ExchangeStartParamsUiRequest { + if (!("provider" in params)) { + throw new ExchangeError(createWrongSellParams(params)); + } + const realFromAccountId = getAccountIdFromWalletAccountId(params.fromAccountId); if (!realFromAccountId) { From a6ac170440b229ccbf5680b11fdfedbc12982448 Mon Sep 17 00:00:00 2001 From: chrisduma-ledger Date: Tue, 19 Nov 2024 16:08:11 +0200 Subject: [PATCH 3/5] fix: rm clog --- libs/ledger-live-common/src/wallet-api/Exchange/server.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts index 43eecab52fa1..74f6ec5aa73d 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts @@ -126,7 +126,7 @@ export const handlers = ({ } let exchangeParams: ExchangeStartParamsUiRequest; - console.log("params", params); + // Use `if else` instead of switch to leverage TS type narrowing and avoid `params` force cast. if (params.exchangeType == "SWAP") { exchangeParams = extractSwapStartParam(params, accounts); From fa11bae135cbe8b8b8ab70f5cf292715f1ffb527 Mon Sep 17 00:00:00 2001 From: chrisduma-ledger Date: Wed, 20 Nov 2024 13:20:00 +0200 Subject: [PATCH 4/5] fix: add avax-like apps to use eth app --- .../src/hw/actions/startExchange.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libs/ledger-live-common/src/hw/actions/startExchange.ts b/libs/ledger-live-common/src/hw/actions/startExchange.ts index 513820da13a8..e329aed21b35 100644 --- a/libs/ledger-live-common/src/hw/actions/startExchange.ts +++ b/libs/ledger-live-common/src/hw/actions/startExchange.ts @@ -137,6 +137,20 @@ export const createAction = ( if (mainToAccount) { dependencies.push({ appName: mainToAccount?.currency?.managerAppName }); } + + const shouldAddEthApp = + (mainFromAccount?.currency?.family === "evm" || + mainToAccount?.currency?.family === "evm") && + mainFromAccount?.currency?.managerAppName !== "Ethereum" && + mainToAccount?.currency?.managerAppName !== "Ethereum"; + + // Check if we should add ETH app, for cases like when we want AVAX to use the ETH app. + if (shouldAddEthApp) { + dependencies.push({ + appName: "Ethereum", + }); + } + return { appName: "Exchange", dependencies, From 1fbfcc95c1d0504ca4f28a418d9684d4ddf25aed Mon Sep 17 00:00:00 2001 From: chrisduma-ledger Date: Fri, 22 Nov 2024 15:02:18 +0200 Subject: [PATCH 5/5] feat: make sure accountId is there --- .../src/wallet-api/Exchange/server.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts index 74f6ec5aa73d..06e3b4d16f9e 100644 --- a/libs/ledger-live-common/src/wallet-api/Exchange/server.ts +++ b/libs/ledger-live-common/src/wallet-api/Exchange/server.ts @@ -72,7 +72,7 @@ type ExchangeStartParamsUiRequest = | { exchangeType: "SELL"; provider: string; - exchange: Partial; + exchange: Partial | undefined; } | { exchangeType: "SWAP"; @@ -371,13 +371,20 @@ function extractSellStartParam( throw new ExchangeError(createWrongSellParams(params)); } - const realFromAccountId = getAccountIdFromWalletAccountId(params.fromAccountId); + if (!params.fromAccountId) { + return { + exchangeType: params.exchangeType, + provider: params.provider, + } as ExchangeStartParamsUiRequest; + } + + const realFromAccountId = getAccountIdFromWalletAccountId(params?.fromAccountId); if (!realFromAccountId) { throw new ExchangeError(createAccounIdNotFound(params.fromAccountId)); } - const fromAccount = accounts.find(acc => acc.id === realFromAccountId); + const fromAccount = accounts?.find(acc => acc.id === realFromAccountId); if (!fromAccount) { throw new ServerError(createAccountNotFound(params.fromAccountId));