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/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..e329aed21b35 100644 --- a/libs/ledger-live-common/src/hw/actions/startExchange.ts +++ b/libs/ledger-live-common/src/hw/actions/startExchange.ts @@ -129,38 +129,34 @@ 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 }); + } + + 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", + }); } - }, [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..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,6 +72,7 @@ type ExchangeStartParamsUiRequest = | { exchangeType: "SELL"; provider: string; + exchange: Partial | undefined; } | { exchangeType: "SWAP"; @@ -130,7 +131,7 @@ export const handlers = ({ 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,41 @@ function extractSwapStartParam( }; } -function extractSellStartParam(params: ExchangeStartSellParams): ExchangeStartParamsUiRequest { +function extractSellStartParam( + params: ExchangeStartSellParams, + accounts: AccountLike[], +): ExchangeStartParamsUiRequest { if (!("provider" in params)) { throw new ExchangeError(createWrongSellParams(params)); } + 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); + + if (!fromAccount) { + throw new ServerError(createAccountNotFound(params.fromAccountId)); + } + + const fromParentAccount = getParentAccount(fromAccount, accounts); + return { exchangeType: params.exchangeType, provider: params.provider, + exchange: { + fromAccount, + fromParentAccount, + }, }; }