diff --git a/src/background/controller/provider/controller.ts b/src/background/controller/provider/controller.ts index 3047f191758..d7a17482aa0 100644 --- a/src/background/controller/provider/controller.ts +++ b/src/background/controller/provider/controller.ts @@ -29,6 +29,7 @@ import { RPCService, i18n, swapService, + notificationService, } from 'background/service'; import { notification } from 'background/webapi'; import { Session } from 'background/service/session'; @@ -413,6 +414,23 @@ class ProviderController extends BaseController { ) { await new Promise((r) => setTimeout(r, 200)); } + + const statsData = { + signed: false, + signedSuccess: false, + submit: false, + submitSuccess: false, + type: currentAccount.brandName, + chainId: chainItem?.serverId || '', + category: KEYRING_CATEGORY_MAP[currentAccount.type], + preExecSuccess: cacheExplain + ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess + : true, + createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', + source: options?.data?.$ctx?.ga?.source || '', + trigger: options?.data?.$ctx?.ga?.trigger || '', + }; + try { const signedTx = await keyringService.signTransaction( keyring, @@ -425,18 +443,20 @@ class ProviderController extends BaseController { currentAccount.type === KEYRING_TYPE.CoboArgusKeyring ) { signedTransactionSuccess = true; - stats.report('signedTransaction', { - type: currentAccount.brandName, - chainId: chainItem?.serverId || '', - category: KEYRING_CATEGORY_MAP[currentAccount.type], - success: true, - preExecSuccess: cacheExplain - ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess - : true, - createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', - source: options?.data?.$ctx?.ga?.source || '', - trigger: options?.data?.$ctx?.ga?.trigger || '', - }); + // stats.report('signedTransaction', { + // type: currentAccount.brandName, + // chainId: chainItem?.serverId || '', + // category: KEYRING_CATEGORY_MAP[currentAccount.type], + // success: true, + // preExecSuccess: cacheExplain + // ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess + // : true, + // createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', + // source: options?.data?.$ctx?.ga?.source || '', + // trigger: options?.data?.$ctx?.ga?.trigger || '', + // }); + statsData.signed = true; + statsData.signedSuccess = true; return; } const onTransactionSubmitted = (hash: string) => { @@ -454,18 +474,20 @@ class ProviderController extends BaseController { const { r, s, v, ...other } = approvalRes; swapService.postSwap(chain, hash, other); - stats.report('submitTransaction', { - type: currentAccount.brandName, - chainId: chainItem?.serverId || '', - category: KEYRING_CATEGORY_MAP[currentAccount.type], - success: true, - preExecSuccess: cacheExplain - ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess - : true, - createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', - source: options?.data?.$ctx?.ga?.source || '', - trigger: options?.data?.$ctx?.ga?.trigger || '', - }); + // stats.report('submitTransaction', { + // type: currentAccount.brandName, + // chainId: chainItem?.serverId || '', + // category: KEYRING_CATEGORY_MAP[currentAccount.type], + // success: true, + // preExecSuccess: cacheExplain + // ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess + // : true, + // createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', + // source: options?.data?.$ctx?.ga?.source || '', + // trigger: options?.data?.$ctx?.ga?.trigger || '', + // }); + statsData.submit = true; + statsData.submitSuccess = true; if (isSend) { pageStateCacheService.clear(); } @@ -504,6 +526,7 @@ class ProviderController extends BaseController { }; if (typeof signedTx === 'string') { onTransactionSubmitted(signedTx); + notificationService.setStatsData(statsData); return signedTx; } @@ -530,18 +553,20 @@ class ProviderController extends BaseController { } } signedTransactionSuccess = true; - stats.report('signedTransaction', { - type: currentAccount.brandName, - chainId: chainItem?.serverId || '', - category: KEYRING_CATEGORY_MAP[currentAccount.type], - success: true, - preExecSuccess: cacheExplain - ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess - : true, - createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', - source: options?.data?.$ctx?.ga?.source || '', - trigger: options?.data?.$ctx?.ga?.trigger || '', - }); + // stats.report('signedTransaction', { + // type: currentAccount.brandName, + // chainId: chainItem?.serverId || '', + // category: KEYRING_CATEGORY_MAP[currentAccount.type], + // success: true, + // preExecSuccess: cacheExplain + // ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess + // : true, + // createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', + // source: options?.data?.$ctx?.ga?.source || '', + // trigger: options?.data?.$ctx?.ga?.trigger || '', + // }); + statsData.signed = true; + statsData.signedSuccess = true; eventBus.emit(EVENTS.broadcastToUI, { method: EVENTS.TX_SUBMITTING, }); @@ -588,6 +613,7 @@ class ProviderController extends BaseController { ); } onTransactionSubmitted(hash); + notificationService.setStatsData(statsData); return hash; } catch (e: any) { if ( @@ -601,18 +627,20 @@ class ProviderController extends BaseController { }); } - stats.report('submitTransaction', { - type: currentAccount.brandName, - chainId: chainItem?.serverId || '', - category: KEYRING_CATEGORY_MAP[currentAccount.type], - success: false, - preExecSuccess: cacheExplain - ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess - : true, - createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', - source: options?.data?.$ctx?.ga?.source || '', - trigger: options?.data?.$ctx?.ga?.trigger || '', - }); + // stats.report('submitTransaction', { + // type: currentAccount.brandName, + // chainId: chainItem?.serverId || '', + // category: KEYRING_CATEGORY_MAP[currentAccount.type], + // success: false, + // preExecSuccess: cacheExplain + // ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess + // : true, + // createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', + // source: options?.data?.$ctx?.ga?.source || '', + // trigger: options?.data?.$ctx?.ga?.trigger || '', + // }); + statsData.submit = true; + statsData.submitSuccess = false; if (!isSpeedUp && !isCancel) { // const cacheExplain = transactionHistoryService.getExplainCache({ // address: txParams.from, @@ -639,24 +667,28 @@ class ProviderController extends BaseController { // errMsg // ); // transactionHistoryService.removeSigningTx(signingTxId!); + notificationService.setStatsData(statsData); throw new Error(errMsg); } } catch (e) { if (!signedTransactionSuccess) { - stats.report('signedTransaction', { - type: currentAccount.brandName, - chainId: chainItem?.serverId || '', - category: KEYRING_CATEGORY_MAP[currentAccount.type], - success: false, - preExecSuccess: cacheExplain - ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess - : true, - createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', - source: options?.data?.$ctx?.ga?.source || '', - trigger: options?.data?.$ctx?.ga?.trigger || '', - }); + // stats.report('signedTransaction', { + // type: currentAccount.brandName, + // chainId: chainItem?.serverId || '', + // category: KEYRING_CATEGORY_MAP[currentAccount.type], + // success: false, + // preExecSuccess: cacheExplain + // ? cacheExplain.pre_exec.success && cacheExplain.calcSuccess + // : true, + // createBy: options?.data?.$ctx?.ga ? 'rabby' : 'dapp', + // source: options?.data?.$ctx?.ga?.source || '', + // trigger: options?.data?.$ctx?.ga?.trigger || '', + // }); + statsData.signed = true; + statsData.signedSuccess = false; } // transactionHistoryService.removeSigningTx(signingTxId!); + notificationService.setStatsData(statsData); throw new Error(e); } }; diff --git a/src/background/controller/provider/rpcFlow.ts b/src/background/controller/provider/rpcFlow.ts index 6862cb06e44..bc065a32f08 100644 --- a/src/background/controller/provider/rpcFlow.ts +++ b/src/background/controller/provider/rpcFlow.ts @@ -11,6 +11,7 @@ import eventBus from '@/eventBus'; import { resemblesETHAddress } from '@/utils'; import { ProviderRequest } from './type'; import * as Sentry from '@sentry/browser'; +import stats from '@/stats'; const isSignApproval = (type: string) => { const SIGN_APPROVALS = ['SignText', 'SignTypedData', 'SignTx']; @@ -282,7 +283,35 @@ const flowContext = flow export default (request: ProviderRequest) => { const ctx: any = { request: { ...request, requestedApproval: false } }; + notificationService.setStatsData(); return flowContext(ctx).finally(() => { + const statsData = notificationService.getStatsData(); + + if (statsData?.signed) { + stats.report('signedTransaction', { + type: statsData?.type, + chainId: statsData?.chainId, + category: statsData?.category, + success: statsData?.signedSuccess, + preExecSuccess: statsData?.preExecSuccess, + createBy: statsData?.createBy, + source: statsData?.source, + trigger: statsData?.trigger, + }); + } + if (statsData?.submit) { + stats.report('submitTransaction', { + type: statsData?.type, + chainId: statsData?.chainId, + category: statsData?.category, + success: statsData?.submitSuccess, + preExecSuccess: statsData?.preExecSuccess, + createBy: statsData?.createBy, + source: statsData?.source, + trigger: statsData?.trigger, + }); + } + if (ctx.request.requestedApproval) { flow.requestedApproval = false; // only unlock notification if current flow is an approval flow diff --git a/src/background/service/notification.ts b/src/background/service/notification.ts index aa0dc79f923..8d8127ecb7e 100644 --- a/src/background/service/notification.ts +++ b/src/background/service/notification.ts @@ -5,7 +5,13 @@ import { v4 as uuidv4 } from 'uuid'; import * as Sentry from '@sentry/browser'; import { EthereumProviderError } from 'eth-rpc-errors/dist/classes'; import { winMgr } from 'background/webapi'; -import { CHAINS, KEYRING_CATEGORY_MAP, IS_LINUX, IS_CHROME } from 'consts'; +import { + CHAINS, + KEYRING_CATEGORY_MAP, + IS_LINUX, + IS_CHROME, + KEYRING_CATEGORY, +} from 'consts'; import transactionHistoryService from './transactionHistory'; import preferenceService from './preference'; import stats from '@/stats'; @@ -42,6 +48,20 @@ const QUEUE_APPROVAL_COMPONENTS_WHITELIST = [ 'PrivatekeyWaiting', ]; +type StatsData = { + signed: boolean; + signedSuccess: boolean; + submit: boolean; + submitSuccess: boolean; + type: string; + chainId: string; + category: KEYRING_CATEGORY; + preExecSuccess: boolean; + createBy: string; + source: any; + trigger: any; +}; + // something need user approval in window // should only open one window, unfocus will close the current notification class NotificationService extends Events { @@ -50,6 +70,7 @@ class NotificationService extends Events { notifiWindowId: null | number = null; isLocked = false; currentRequestDeferFn?: () => void; + statsData: StatsData | undefined; get approvals() { return this._approvals; @@ -364,6 +385,14 @@ class NotificationService extends Events { callCurrentRequestDeferFn = () => { return this.currentRequestDeferFn?.(); }; + + setStatsData = (data?: StatsData) => { + this.statsData = data; + }; + + getStatsData = () => { + return this.statsData; + }; } export default new NotificationService(); diff --git a/src/ui/views/Approval/components/WatchAddressWaiting/index.tsx b/src/ui/views/Approval/components/WatchAddressWaiting/index.tsx index 2316f1dd251..5d05e96f06b 100644 --- a/src/ui/views/Approval/components/WatchAddressWaiting/index.tsx +++ b/src/ui/views/Approval/components/WatchAddressWaiting/index.tsx @@ -148,19 +148,18 @@ const WatchAddressWaiting = ({ params }: { params: ApprovalParams }) => { // address: from, // chainId: Number(chainId), // }); - - wallet.reportStats('signedTransaction', { - type: account.brandName, - chainId: findChainByEnum(chain)?.serverId || '', - category: KEYRING_CATEGORY_MAP[account.type], - success: true, - preExecSuccess: explain - ? explain?.calcSuccess && explain?.pre_exec.success - : true, - createBy: params?.$ctx?.ga ? 'rabby' : 'dapp', - source: params?.$ctx?.ga?.source || '', - trigger: params?.$ctx?.ga?.trigger || '', - }); + // wallet.reportStats('signedTransaction', { + // type: account.brandName, + // chainId: findChainByEnum(chain)?.serverId || '', + // category: KEYRING_CATEGORY_MAP[account.type], + // success: true, + // preExecSuccess: explain + // ? explain?.calcSuccess && explain?.pre_exec.success + // : true, + // createBy: params?.$ctx?.ga ? 'rabby' : 'dapp', + // source: params?.$ctx?.ga?.source || '', + // trigger: params?.$ctx?.ga?.trigger || '', + // }); } } setSignFinishedData({ @@ -178,19 +177,18 @@ const WatchAddressWaiting = ({ params }: { params: ApprovalParams }) => { // address: from, // chainId: Number(chainId), // }); - - wallet.reportStats('signedTransaction', { - type: account.brandName, - chainId: findChainByEnum(chain)?.serverId || '', - category: KEYRING_CATEGORY_MAP[account.type], - success: false, - preExecSuccess: explain - ? explain?.calcSuccess && explain?.pre_exec.success - : true, - createBy: params?.$ctx?.ga ? 'rabby' : 'dapp', - source: params?.$ctx?.ga?.source || '', - trigger: params?.$ctx?.ga?.trigger || '', - }); + // wallet.reportStats('signedTransaction', { + // type: account.brandName, + // chainId: findChainByEnum(chain)?.serverId || '', + // category: KEYRING_CATEGORY_MAP[account.type], + // success: false, + // preExecSuccess: explain + // ? explain?.calcSuccess && explain?.pre_exec.success + // : true, + // createBy: params?.$ctx?.ga ? 'rabby' : 'dapp', + // source: params?.$ctx?.ga?.source || '', + // trigger: params?.$ctx?.ga?.trigger || '', + // }); } } rejectApproval(data.errorMsg);