Skip to content

Commit

Permalink
Merge branch 'main' into feat/integrate-wsteth-bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
JackHamer09 authored Apr 7, 2024
2 parents 2acf66b + 0dfc1ee commit f13f29b
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 26 deletions.
6 changes: 6 additions & 0 deletions composables/zksync/useWithdrawalFinalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ export default (transactionInfo: ComputedRef<TransactionInfo>) => {
},
});

trackEvent("withdrawal-finalized", {
token: transactionInfo.value!.token.symbol,
amount: transactionInfo.value!.token.amount,
to: transactionInfo.value!.to.address,
});

status.value = "done";
return receipt;
} catch (err) {
Expand Down
17 changes: 15 additions & 2 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ export default defineNuxtConfig({
src: "https://cdn.rudderlabs.com/v1.1/rudder-analytics.min.js",
defer: true,
},
{
hid: "MASA-JS",
src: "https://cdn.jsdelivr.net/npm/@masa-finance/analytics-sdk@latest/dist/browser/masa-analytics.min.js",
defer: true,
},
],
},
},
Expand Down Expand Up @@ -79,8 +84,16 @@ export default defineNuxtConfig({
public: {
ankrToken: process.env.ANKR_TOKEN,
screeningApiUrl: process.env.SCREENING_API_URL,
dataplaneUrl: process.env.DATAPLANE_URL,
rudderKey: process.env.RUDDER_KEY,
analytics: {
rudder: {
key: process.env.RUDDER_KEY,
dataplaneUrl: process.env.DATAPLANE_URL,
},
masa: {
clientId: process.env.MASA_KEY,
appId: process.env.MASA_APP_ID,
},
},
},
},
vite: {
Expand Down
1 change: 1 addition & 0 deletions store/onboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ export const useOnboardStore = defineStore("onboard", () => {
await identifyWalletName();
account.value = updatedAccount;
connectorName.value = updatedAccount.connector?.name;
identifyWallet(updatedAccount.address, walletName.value);
} catch (err) {
disconnect();
const error = formatError(err as Error);
Expand Down
12 changes: 11 additions & 1 deletion store/zksync/transactionStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ export const useZkSyncTransactionStatusStore = defineStore("zkSyncTransactionSta
const { account } = storeToRefs(onboardStore);
const { eraNetwork } = storeToRefs(providerStore);

const failedTransaction = useStorage<TransactionInfo[]>("zksync-bridge-failed-transaction", []);
const addFailedTransaction = (transaction: TransactionInfo) => {
if (failedTransaction.value.some((tx) => tx.transactionHash === transaction.transactionHash)) {
return;
}
failedTransaction.value = [...failedTransaction.value, transaction];
};

const storageSavedTransactions = useStorage<{ [networkKey: string]: TransactionInfo[] }>(
"zksync-bridge-transactions",
{}
Expand Down Expand Up @@ -88,6 +96,7 @@ export const useZkSyncTransactionStatusStore = defineStore("zkSyncTransactionSta
transaction.info.withdrawalFinalizationAvailable = false;
transaction.info.failed = true;
transaction.info.completed = true;
addFailedTransaction(transaction);
return transaction;
}
if (transactionDetails.status !== "verified") {
Expand All @@ -106,10 +115,11 @@ export const useZkSyncTransactionStatusStore = defineStore("zkSyncTransactionSta
const transactionReceipt = await providerStore.requestProvider().getTransactionReceipt(transaction.transactionHash);
if (!transactionReceipt) return transaction;
const transactionDetails = await providerStore.requestProvider().getTransactionDetails(transaction.transactionHash);
transaction.info.completed = true;
if (transactionDetails.status === "failed") {
transaction.info.failed = true;
addFailedTransaction(transaction);
}
transaction.info.completed = true;
return transaction;
};
const waitForCompletion = async (transaction: TransactionInfo) => {
Expand Down
11 changes: 11 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,16 @@ declare global {
track: (eventName: string, params?: unknown) => void;
initialized: boolean;
};
MA?: {
MasaAnalytics: {
new ({ clientId });
};
};
masaAnalytics?: {
trackCustomEvent: ({ eventName, additionalEventData }) => void;
firePageViewEvent: ({ page, additionalEventData }) => void;
fireConnectWalletEvent: ({ user_address, wallet_type, additionalEventData }) => void;
initialized: boolean;
};
}
}
80 changes: 57 additions & 23 deletions utils/analytics.ts
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
import type { Hash } from "@/types";

let analyticsLoaded = false;

const isReady = (): Promise<void> => {
return new Promise((resolve, reject) => {
if (!window.rudderanalytics) {
reject(new Error("Rudder not loaded"));
}
window.rudderanalytics?.ready(() => {
resolve();
});
async function loadRudder() {
if (!window.rudderanalytics) {
await new Promise((resolve) => setTimeout(resolve, 250));
throw new Error("Rudder not loaded");
}
const runtimeConfig = useRuntimeConfig();
window.rudderanalytics.load(
runtimeConfig.public.analytics.rudder.key,
runtimeConfig.public.analytics.rudder.dataplaneUrl
);
}

async function loadMasa() {
if (!window.MA) {
await new Promise((resolve) => setTimeout(resolve, 250));
throw new Error("Masa not loaded");
}
const runtimeConfig = useRuntimeConfig();
window.masaAnalytics = new window.MA.MasaAnalytics({
clientId: runtimeConfig.public.analytics.masa.clientId,
});
};
}

export async function initAnalytics(): Promise<boolean> {
if (analyticsLoaded) return true;

const runtimeConfig = useRuntimeConfig();
if (!runtimeConfig.public.rudderKey || !runtimeConfig.public.dataplaneUrl) {
const useRudder = Boolean(
runtimeConfig.public.analytics.rudder.key && runtimeConfig.public.analytics.rudder.dataplaneUrl
);
const useMasa = Boolean(runtimeConfig.public.analytics.masa.clientId && runtimeConfig.public.analytics.masa.appId);
if ((!useRudder && !useMasa) || analyticsLoaded) {
return false;
}

if (analyticsLoaded) {
await isReady();
return true;
}
await retry(async () => {
if (!window.rudderanalytics) {
await new Promise((resolve) => setTimeout(resolve, 250));
throw new Error("Rudder not loaded");
}
});
window.rudderanalytics?.load(runtimeConfig.public.rudderKey, runtimeConfig.public.dataplaneUrl);
const services = [];
if (useRudder) services.push(loadRudder());
if (useMasa) services.push(loadMasa());

await Promise.all(services);
analyticsLoaded = true;
await isReady();
return true;
}

export async function trackPage(): Promise<void> {
if (await initAnalytics()) {
const runtimeConfig = useRuntimeConfig();
window.rudderanalytics?.page();
window.masaAnalytics?.firePageViewEvent({
page: window.location.href,
additionalEventData: { appId: runtimeConfig.public.analytics.masa.appId },
});
}
}

export async function trackEvent(eventName: string, params?: unknown): Promise<void> {
export async function trackEvent(eventName: string, params?: object): Promise<void> {
if (await initAnalytics()) {
const runtimeConfig = useRuntimeConfig();
window.rudderanalytics?.track(eventName, params);
window.masaAnalytics?.trackCustomEvent({
eventName,
additionalEventData: { appId: runtimeConfig.public.analytics.masa.appId, ...params },
});
}
}

export async function identifyWallet(userAddress: Hash | undefined, walletType?: string): Promise<void> {
if (await initAnalytics()) {
const runtimeConfig = useRuntimeConfig();
window.masaAnalytics?.fireConnectWalletEvent({
user_address: userAddress,
wallet_type: walletType,
additionalEventData: { appId: runtimeConfig.public.analytics.masa.appId },
});
}
}
5 changes: 5 additions & 0 deletions views/transactions/Deposit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ const makeTransaction = async () => {
waitForCompletion(transactionInfo.value)
.then((completedTransaction) => {
transactionInfo.value = completedTransaction;
trackEvent("deposit", {
token: transaction.value!.token.symbol,
amount: transaction.value!.token.amount,
to: transaction.value!.to.address,
});
setTimeout(() => {
transfersHistoryStore.reloadRecentTransfers().catch(() => undefined);
eraWalletStore.requestBalance({ force: true }).catch(() => undefined);
Expand Down
5 changes: 5 additions & 0 deletions views/transactions/Transfer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,11 @@ const makeTransaction = async () => {
waitForCompletion(transactionInfo.value)
.then((completedTransaction) => {
transactionInfo.value = completedTransaction;
trackEvent(transaction.value!.type, {
token: transaction.value!.token.symbol,
amount: transaction.value!.token.amount,
to: transaction.value!.to.address,
});
setTimeout(() => {
transfersHistoryStore.reloadRecentTransfers().catch(() => undefined);
walletStore.requestBalance({ force: true }).catch(() => undefined);
Expand Down

0 comments on commit f13f29b

Please sign in to comment.