Skip to content

Commit

Permalink
feat: fix logic and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduma-ledger committed Nov 19, 2024
1 parent a6d347e commit 46a7eed
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 36 deletions.
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
42 changes: 12 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,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);

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
28 changes: 23 additions & 5 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>;
}
| {
exchangeType: "SWAP";
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
},
};
}

0 comments on commit 46a7eed

Please sign in to comment.