Skip to content

Commit

Permalink
fix: signedTransaction and submitTransaction (#1746)
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 authored Sep 15, 2023
1 parent 3adaa89 commit fb12117
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 87 deletions.
152 changes: 92 additions & 60 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
RPCService,
i18n,
swapService,
notificationService,
} from 'background/service';
import { notification } from 'background/webapi';
import { Session } from 'background/service/session';
Expand Down Expand Up @@ -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,
Expand All @@ -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) => {
Expand All @@ -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();
}
Expand Down Expand Up @@ -504,6 +526,7 @@ class ProviderController extends BaseController {
};
if (typeof signedTx === 'string') {
onTransactionSubmitted(signedTx);
notificationService.setStatsData(statsData);
return signedTx;
}

Expand All @@ -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,
});
Expand Down Expand Up @@ -588,6 +613,7 @@ class ProviderController extends BaseController {
);
}
onTransactionSubmitted(hash);
notificationService.setStatsData(statsData);
return hash;
} catch (e: any) {
if (
Expand All @@ -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,
Expand All @@ -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);
}
};
Expand Down
29 changes: 29 additions & 0 deletions src/background/controller/provider/rpcFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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
Expand Down
31 changes: 30 additions & 1 deletion src/background/service/notification.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand All @@ -50,6 +70,7 @@ class NotificationService extends Events {
notifiWindowId: null | number = null;
isLocked = false;
currentRequestDeferFn?: () => void;
statsData: StatsData | undefined;

get approvals() {
return this._approvals;
Expand Down Expand Up @@ -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();
50 changes: 24 additions & 26 deletions src/ui/views/Approval/components/WatchAddressWaiting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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);
Expand Down

0 comments on commit fb12117

Please sign in to comment.