Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: signedTransaction and submitTransaction #1746

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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