Skip to content

Commit

Permalink
feat: tracking remake
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisduma-ledger committed Nov 29, 2024
1 parent 05b9543 commit 13e57ec
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 62 deletions.
90 changes: 51 additions & 39 deletions libs/ledger-live-common/src/wallet-api/Exchange/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,52 +114,61 @@ export const handlers = ({
uiHooks: ExchangeUiHooks;
}) =>
({
"custom.exchange.start": customWrapper<
ExchangeStartParams | ExchangeStartSwapParams | ExchangeStartSellParams,
ExchangeStartResult
>(async params => {
tracking.startExchangeRequested(manifest);

if (!params) {
tracking.startExchangeNoParams(manifest);
return { transactionId: "" };
}

let exchangeParams: ExchangeStartParamsUiRequest;

// 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, accounts);
} else {
exchangeParams = {
"custom.exchange.start": customWrapper<ExchangeStartParams, ExchangeStartResult>(
async params => {
if (!params) {
tracking.startExchangeNoParams(manifest);
return { transactionId: "" };
}

const trackingParams = {
// @ts-expect-error ExchangeStartFundParams does not yet have the provider. Will be added in another iteration after a bugfix is confirmed
// TODO: expect-error to be deleted after
provider: params.provider,
exchangeType: params.exchangeType,
};
}

return new Promise((resolve, reject) =>
uiExchangeStart({
exchangeParams,
onSuccess: (nonce: string, device) => {
tracking.startExchangeSuccess(manifest);
resolve({ transactionId: nonce, device });
},
onCancel: error => {
tracking.completeExchangeFail(manifest);
reject(error);
},
}),
);
}),
tracking.startExchangeRequested(trackingParams);

let exchangeParams: ExchangeStartParamsUiRequest;

// 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, accounts);
} else {
exchangeParams = {
exchangeType: params.exchangeType,
};
}

return new Promise((resolve, reject) =>
uiExchangeStart({
exchangeParams,
onSuccess: (nonce: string, device) => {
tracking.startExchangeSuccess(trackingParams);
resolve({ transactionId: nonce, device });
},
onCancel: error => {
tracking.startExchangeFail(trackingParams);
reject(error);
},
}),
);
},
),
"custom.exchange.complete": customWrapper<ExchangeCompleteParams, ExchangeCompleteResult>(
async params => {
tracking.completeExchangeRequested(manifest);

if (!params) {
tracking.completeExchangeNoParams(manifest);
return { transactionHash: "" };
}
const trackingParams = {
provider: params.provider,
exchangeType: params.exchangeType,
};
tracking.completeExchangeRequested(trackingParams);

const realFromAccountId = getAccountIdFromWalletAccountId(params.fromAccountId);
if (!realFromAccountId) {
Expand Down Expand Up @@ -286,11 +295,14 @@ export const handlers = ({
magnitudeAwareRate,
},
onSuccess: (transactionHash: string) => {
tracking.completeExchangeSuccess(manifest);
tracking.completeExchangeSuccess({
...trackingParams,
currency: params.rawTransaction.family,
});
resolve({ transactionHash });
},
onCancel: error => {
tracking.completeExchangeFail(manifest);
tracking.completeExchangeFail(trackingParams);
reject(error);
},
}),
Expand Down
51 changes: 28 additions & 23 deletions libs/ledger-live-common/src/wallet-api/Exchange/tracking.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LiveAppManifest } from "../../platform/types";
import type { AppManifest } from "../types";

/**
Expand All @@ -7,16 +8,15 @@ import type { AppManifest } from "../types";
*/
type TrackExchange = (
event: string,
properties: Record<string, any> | null,
properties: Record<string, string> | null,
mandatory: boolean | null,
) => void;

/**
* Obtain Event data from WalletAPI App manifest
*
* @param {AppManifest} manifest
* @returns Object - event data
*/
interface TrackEventPayload {
exchangeType: "SELL" | "FUND" | "SWAP";
provider: string;
}

function getEventData(manifest: AppManifest) {
return { walletAPI: manifest.name };
}
Expand All @@ -30,46 +30,51 @@ function getEventData(manifest: AppManifest) {
// in order to get the exact type matching the tracking wrapper API
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export default function trackingWrapper(trackCall: TrackExchange) {
const track = (event: string, properties: Record<string, any> | null) =>
trackCall(event, properties, null);
const track = (event: string, properties: Record<string, string> | null) => {
return trackCall(event, properties, null);
};

return {
// Generate Exchange nonce modal open
startExchangeRequested: (manifest: AppManifest) => {
track("WalletAPI start Exchange Nonce request", getEventData(manifest));
startExchangeRequested: ({ provider, exchangeType }: TrackEventPayload) => {
track("WalletAPI start Exchange Nonce request", { provider, exchangeType });
},

// Successfully generated an Exchange app nonce
startExchangeSuccess: (manifest: AppManifest) => {
track("WalletAPI start Exchange Nonce success", getEventData(manifest));
startExchangeSuccess: ({ provider, exchangeType }: TrackEventPayload) => {
track("WalletAPI start Exchange Nonce success", { provider, exchangeType });
},

// Failed to generate an Exchange app nonce
startExchangeFail: (manifest: AppManifest) => {
track("WalletAPI start Exchange Nonce fail", getEventData(manifest));
startExchangeFail: ({ provider, exchangeType }: TrackEventPayload) => {
track("WalletAPI start Exchange Nonce fail", { provider, exchangeType });
},

// No Params to generate an Exchange app nonce
startExchangeNoParams: (manifest: AppManifest) => {
startExchangeNoParams: (manifest: LiveAppManifest) => {
track("WalletAPI start Exchange no params", getEventData(manifest));
},

completeExchangeRequested: (manifest: AppManifest) => {
track("WalletAPI complete Exchange requested", getEventData(manifest));
completeExchangeRequested: ({ provider, exchangeType }: TrackEventPayload) => {
track("WalletAPI complete Exchange requested", { provider, exchangeType });
},

// Successfully completed an Exchange
completeExchangeSuccess: (manifest: AppManifest) => {
track("WalletAPI complete Exchange success", getEventData(manifest));
completeExchangeSuccess: ({
provider,
exchangeType,
currency,
}: TrackEventPayload & { currency: string }) => {
track("WalletAPI complete Exchange success", { provider, exchangeType, currency });
},

// Failed to complete an Exchange
completeExchangeFail: (manifest: AppManifest) => {
track("WalletAPI complete Exchange Nonce fail", getEventData(manifest));
completeExchangeFail: ({ provider, exchangeType }: TrackEventPayload) => {
track("WalletAPI complete Exchange Nonce fail", { provider, exchangeType });
},

// No Params to complete an Exchange
completeExchangeNoParams: (manifest: AppManifest) => {
completeExchangeNoParams: (manifest: LiveAppManifest) => {
track("WalletAPI complete Exchange no params", getEventData(manifest));
},
} as const;
Expand Down

0 comments on commit 13e57ec

Please sign in to comment.