Skip to content

Commit

Permalink
Merge pull request #8401 from LedgerHQ/feat/support-app-install
Browse files Browse the repository at this point in the history
feat: fix logic and refactor app install
  • Loading branch information
chrisduma-ledger authored Nov 25, 2024
2 parents 842b255 + b6d0d62 commit 38916d5
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 33 deletions.
8 changes: 8 additions & 0 deletions .changeset/young-coats-flash.md
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion libs/exchange-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ export class ExchangeModule extends CustomModule {
*
* @returns - A transaction ID used to complete the exchange process
*/
async startSell({ provider }: Omit<ExchangeStartSellParams, "exchangeType">) {
async startSell({ provider, fromAccountId }: Omit<ExchangeStartSellParams, "exchangeType">) {
const result = await this.request<ExchangeStartSellParams, ExchangeStartResult>(
"custom.exchange.start",
{
exchangeType: "SELL",
provider,
fromAccountId,
},
);

Expand Down
1 change: 1 addition & 0 deletions libs/exchange-module/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ExchangeStartFundParams = {
export type ExchangeStartSellParams = {
exchangeType: "SELL";
provider: string;
fromAccountId: string;
};

export type ExchangeStartSwapParams = {
Expand Down
56 changes: 26 additions & 30 deletions libs/ledger-live-common/src/hw/actions/startExchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ describe("handlers", () => {
const params: ExchangeStartSellParams = {
exchangeType: "SELL",
provider: "TestSellProvider",
fromAccountId: accounts[0].id,
};
const { request, context, walletHandlers } = prepareSellRequest(params);

Expand Down
33 changes: 31 additions & 2 deletions libs/ledger-live-common/src/wallet-api/Exchange/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type ExchangeStartParamsUiRequest =
| {
exchangeType: "SELL";
provider: string;
exchange: Partial<Exchange> | undefined;
}
| {
exchangeType: "SWAP";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
};
}

0 comments on commit 38916d5

Please sign in to comment.