Skip to content

Commit

Permalink
add performance check
Browse files Browse the repository at this point in the history
  • Loading branch information
lionellbriones committed Dec 19, 2024
1 parent 1377564 commit 52c7bdc
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 1 deletion.
70 changes: 69 additions & 1 deletion demo/vue-app-new/src/components/AppDashboard.vue
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<!-- eslint-disable no-console -->
<script setup lang="ts">
import { Button, Card } from "@toruslabs/vue-components";
import { CHAIN_NAMESPACES, IProvider, log, WALLET_ADAPTERS, WALLET_PLUGINS } from "@web3auth/base";
Expand All @@ -18,7 +19,14 @@ import {
signTypedMessage,
} from "../services/ethHandlers";
import { signAllTransactions, signAndSendTransaction, signMessage, signTransaction as signSolTransaction } from "../services/solHandlers";
import { walletSendEth, walletSignPersonalMessage, walletSignTypedMessage } from "../services/walletServiceHandlers";
import {
walletGetAccounts,
walletGetBalance,
walletSendEth,
walletSignEthMessage,
walletSignPersonalMessage,
walletSignTypedMessage,
} from "../services/walletServiceHandlers";
import { formDataStore } from "../store/form";

const { t } = useI18n({ useScope: "global" });
Expand Down Expand Up @@ -126,23 +134,63 @@ const onGetUserInfo = async () => {
};

const onSendEth = async () => {
const timeBefore = performance.now();
await sendEth(provider.value as IProvider, printToConsole);
const timeAfter = performance.now();
const timeTakenInSeconds = (timeAfter - timeBefore) / 1000;
console.log("check: onSendEth", timeTakenInSeconds);
const timeBeforeWalletServices = performance.now();
const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPlugin;
await walletSendEth(walletPlugin.wsEmbedInstance.provider, printToConsole);
const timeAfterWalletServices = performance.now();
const timeTakenInSecondsWalletServices = (timeAfterWalletServices - timeBeforeWalletServices) / 1000;
console.log("check: onSendEthWalletServices", timeTakenInSecondsWalletServices);
};

const onSignEthMessage = async () => {
const timeBefore = performance.now();
await signEthMessage(provider.value as IProvider, printToConsole);
const timeAfter = performance.now();
const timeTakenInSeconds = (timeAfter - timeBefore) / 1000;
console.log("check: onSignEthMessage", timeTakenInSeconds);
const timeBeforeWalletServices = performance.now();
const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPlugin;
await walletSignEthMessage(walletPlugin.wsEmbedInstance.provider, printToConsole);
const timeAfterWalletServices = performance.now();
const timeTakenInSecondsWalletServices = (timeAfterWalletServices - timeBeforeWalletServices) / 1000;
console.log("check: onSignEthMessageWalletServices", timeTakenInSecondsWalletServices);
};

const onGetAccounts = async () => {
const timeBefore = performance.now();
await getAccounts(provider.value as IProvider, printToConsole);
const timeAfter = performance.now();
const timeTakenInSeconds = (timeAfter - timeBefore) / 1000;
console.log("check: onGetAccounts", timeTakenInSeconds);
const timeBeforeWalletServices = performance.now();
const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPlugin;
await walletGetAccounts(walletPlugin.wsEmbedInstance.provider, printToConsole);
const timeAfterWalletServices = performance.now();
const timeTakenInSecondsWalletServices = (timeAfterWalletServices - timeBeforeWalletServices) / 1000;
console.log("check: onGetAccountsWalletServices", timeTakenInSecondsWalletServices);
};

const getConnectedChainId = async () => {
await getChainId(provider.value as IProvider, printToConsole);
};

const onGetBalance = async () => {
const timeBefore = performance.now();
await getBalance(provider.value as IProvider, printToConsole);
const timeAfter = performance.now();
const timeTakenInSeconds = (timeAfter - timeBefore) / 1000;
console.log("check: onGetBalance", timeTakenInSeconds);
const timeBeforeWalletServices = performance.now();
const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPlugin;
await walletGetBalance(walletPlugin.wsEmbedInstance.provider, printToConsole);
const timeAfterWalletServices = performance.now();
const timeTakenInSecondsWalletServices = (timeAfterWalletServices - timeBeforeWalletServices) / 1000;
console.log("check: onGetBalanceWalletServices", timeTakenInSecondsWalletServices);
};

const onSwitchChain = async () => {
Expand Down Expand Up @@ -203,11 +251,31 @@ const authenticateUser = async () => {
};

const onSignTypedData_v4 = async () => {
const timeBefore = performance.now();
await signTypedMessage(provider.value as IProvider, printToConsole);
const timeAfter = performance.now();
const timeTakenInSeconds = (timeAfter - timeBefore) / 1000;
console.log("check: onSignTypedData_v4", timeTakenInSeconds);
const timeBeforeWalletServices = performance.now();
const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPlugin;
await walletSignTypedMessage(walletPlugin.wsEmbedInstance.provider, printToConsole);
const timeAfterWalletServices = performance.now();
const timeTakenInSecondsWalletServices = (timeAfterWalletServices - timeBeforeWalletServices) / 1000;
console.log("check: onSignTypedData_v4WalletServices", timeTakenInSecondsWalletServices);
};

const onSignPersonalMsg = async () => {
const timeBefore = performance.now();
await signPersonalMessage(provider.value as IProvider, printToConsole);
const timeAfter = performance.now();
const timeTakenInSeconds = (timeAfter - timeBefore) / 1000;
console.log("check: onSignPersonalMsg", timeTakenInSeconds);
const timeBeforeWalletServices = performance.now();
const walletPlugin = web3Auth.value?.getPlugin(WALLET_PLUGINS.WALLET_SERVICES) as WalletServicesPlugin;
await walletSignPersonalMessage(walletPlugin.wsEmbedInstance.provider, printToConsole);
const timeAfterWalletServices = performance.now();
const timeTakenInSecondsWalletServices = (timeAfterWalletServices - timeBeforeWalletServices) / 1000;
console.log("check: onSignPersonalMsgWalletServices", timeTakenInSecondsWalletServices);
};
</script>

Expand Down
44 changes: 44 additions & 0 deletions demo/vue-app-new/src/services/walletServiceHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,50 @@ import { BrowserProvider, parseEther } from "ethers";

import { getV4TypedData } from "../config";

export const walletGetAccounts = async (provider: WalletServicesPlugin["provider"], uiConsole: any): Promise<string[] | undefined> => {
try {
const ethProvider = new BrowserProvider(provider);
const signer = await ethProvider.getSigner();
const account = await signer.getAddress();
uiConsole("accounts", account);
return [account];
} catch (error) {
log.error("Error", error);
uiConsole("error", error instanceof Error ? error.message : error);
return [];
}
};

export const walletGetBalance = async (provider: WalletServicesPlugin["provider"], uiConsole: any) => {
try {
const ethProvider = new BrowserProvider(provider);
const signer = await ethProvider.getSigner();
const account = await signer.getAddress();
const balance = await ethProvider.getBalance(account);
uiConsole("balance", balance.toString());
} catch (error) {
log.error("Error", error);
uiConsole("error", error instanceof Error ? error.message : error);
}
};

export const walletSignEthMessage = async (provider: WalletServicesPlugin["provider"], uiConsole: any) => {
try {
const ethProvider = new BrowserProvider(provider);
const signer = await ethProvider.getSigner();
const account = await signer.getAddress();
const fromAddress = account;
log.info("fromAddress", fromAddress);

const message = "Some string";
const sig = await ethProvider.send("eth_sign", [fromAddress, message]);
uiConsole("eth sign", sig);
} catch (error) {
log.error("error", error);
uiConsole("error", error instanceof Error ? error.message : error);
}
};

export const walletSignPersonalMessage = async (provider: WalletServicesPlugin["provider"], uiConsole: any) => {
try {
const ethProvider = new BrowserProvider(provider);
Expand Down
5 changes: 5 additions & 0 deletions packages/no-modal/src/noModal.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import type { AccountAbstractionProvider } from "@web3auth/account-abstraction-provider";
import { SafeEventEmitter, type SafeEventEmitterProvider } from "@web3auth/auth";
import { type AuthAdapter, LOGIN_PROVIDER, type LoginConfig } from "@web3auth/auth-adapter";
Expand Down Expand Up @@ -264,6 +265,7 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
async connectTo<T>(walletName: WALLET_ADAPTER_TYPE, loginParams?: T): Promise<IProvider | null> {
if (!this.walletAdapters[walletName] || !this.commonJRPCProvider)
throw WalletInitializationError.notFound(`Please add wallet adapter for ${walletName} wallet, before connecting`);
console.log("check: connectTo", performance.now());
return new Promise((resolve, reject) => {
this.once(ADAPTER_EVENTS.CONNECTED, (_) => {
resolve(this.provider);
Expand Down Expand Up @@ -344,9 +346,12 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
this.status = ADAPTER_STATUS.CONNECTED;
this.cacheWallet(data.adapter);
log.debug("connected", this.status, this.connectedAdapterName);

console.log("check: connectedToAdapter", performance.now());
this.connectToPlugins(data);
if (this.plugins[WALLET_PLUGINS.WALLET_SERVICES]) {
this.plugins[WALLET_PLUGINS.WALLET_SERVICES].on(PLUGIN_EVENTS.CONNECTED, () => {
console.log("check: connectedToWalletServices", performance.now());
(provider as CommonPrivateKeyProvider).updateProviderEngineProxy(
(this.plugins[WALLET_PLUGINS.WALLET_SERVICES] as WalletServicesPlugin).wsEmbedInstance.provider
);
Expand Down

0 comments on commit 52c7bdc

Please sign in to comment.