Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/LIVE-12193 track swap cancel accept #6799

Merged
merged 15 commits into from
May 13, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { closePlatformAppDrawer, openExchangeDrawer } from "~/renderer/actions/U
import { WebviewProps } from "../Web3AppWebview/types";
import { context } from "~/renderer/drawers/Provider";
import WebviewErrorDrawer from "~/renderer/screens/exchange/Swap2/Form/WebviewErrorDrawer";
import { getCurrentDevice } from "~/renderer/reducers/devices";

export function usePTXCustomHandlers(manifest: WebviewProps["manifest"]) {
const dispatch = useDispatch();
const accounts = useSelector(flattenAccountsSelector);
const { setDrawer } = React.useContext(context);
const device = useSelector(getCurrentDevice);

const tracking = useMemo(
() =>
Expand Down Expand Up @@ -50,6 +52,10 @@ export function usePTXCustomHandlers(manifest: WebviewProps["manifest"]) {
tracking,
manifest,
uiHooks: {
"custom.device.get": () => ({
deviceId: device?.deviceId,
modelId: device?.modelId,
}),
andreicovaciu marked this conversation as resolved.
Show resolved Hide resolved
"custom.exchange.start": ({ exchangeParams, onSuccess, onCancel }) => {
dispatch(
openExchangeDrawer({
Expand Down Expand Up @@ -88,5 +94,5 @@ export function usePTXCustomHandlers(manifest: WebviewProps["manifest"]) {
},
}),
};
}, [accounts, dispatch, manifest, tracking, setDrawer]);
}, [accounts, tracking, manifest, device, dispatch, setDrawer]);
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ export function usePTXCustomHandlers(manifest: WebviewProps["manifest"]) {
"custom.exchange.error": () => {
// todo add screen for LLM
},
"custom.device.get": () => ({
deviceId: device?.deviceId,
modelId: device?.modelId,
}),
},
}),
};
Expand Down
6 changes: 6 additions & 0 deletions libs/exchange-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ export * from "./types";

// TODO maybe find a better way to type the available custom requests with correct types
export class ExchangeModule extends CustomModule {
async getDevice() {
return this.request<undefined, { deviceId?: string; modelId?: string }>(
"custom.device.get",
undefined,
);
}
/**
* Start the exchange process by generating a nonce on Ledger device
* @param exchangeType - used by the exchange transport to discern between swap/sell/fund
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ const testAppManifest = {
const mockUiStartExchange = jest.fn();
const mockUiCompleteExchange = jest.fn();
const mockUiError = jest.fn();
const mockDeviceGet = jest.fn();

const mockUiHooks = {
"custom.exchange.start": mockUiStartExchange,
"custom.exchange.complete": mockUiCompleteExchange,
"custom.exchange.error": mockUiError,
"custom.device.get": mockDeviceGet,
};

// Mock converter id to send back the id received in params.
Expand Down
9 changes: 9 additions & 0 deletions libs/ledger-live-common/src/wallet-api/Exchange/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type Handlers = {
>;
"custom.exchange.complete": RPCHandler<ExchangeCompleteResult, ExchangeCompleteParams>;
"custom.exchange.error": RPCHandler<void, SwapLiveError>;
"custom.device.get": RPCHandler<DeviceGetRequest, void>;
};

export type CompleteExchangeUiRequest = {
Expand Down Expand Up @@ -79,6 +80,11 @@ type ExchangeStartParamsUiRequest =
exchange: Exchange;
};

export type DeviceGetRequest = {
deviceId?: string;
modelId?: string;
};

type ExchangeUiHooks = {
"custom.exchange.start": (params: {
exchangeParams: ExchangeStartParamsUiRequest;
Expand All @@ -95,6 +101,7 @@ type ExchangeUiHooks = {
onSuccess: () => void;
onCancel: () => void;
}) => void;
"custom.device.get": () => DeviceGetRequest;
};

export const handlers = ({
Expand All @@ -105,6 +112,7 @@ export const handlers = ({
"custom.exchange.start": uiExchangeStart,
"custom.exchange.complete": uiExchangeComplete,
"custom.exchange.error": uiError,
"custom.device.get": deviceGet,
},
}: {
accounts: AccountLike[];
Expand Down Expand Up @@ -309,6 +317,7 @@ export const handlers = ({
}),
);
}),
"custom.device.get": customWrapper<void, DeviceGetRequest>(() => deviceGet()),
andreicovaciu marked this conversation as resolved.
Show resolved Hide resolved
}) as const satisfies Handlers;

function extractSwapStartParam(
Expand Down