This repository was archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathDebuggerCommands.ts
More file actions
90 lines (78 loc) · 3.81 KB
/
Copy pathDebuggerCommands.ts
File metadata and controls
90 lines (78 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2022. Consensys Software Inc. All rights reserved.
// Licensed under the MIT license.
import {DEBUG_TYPE} from '@/debugAdapter/constants/debugAdapter';
import {DebugNetwork} from '@/debugAdapter/debugNetwork';
import {shortenHash} from '@/debugAdapter/functions';
import {TransactionProvider} from '@/debugAdapter/transaction/transactionProvider';
import {Web3Wrapper} from '@/debugAdapter/web3Wrapper';
import {getWorkspaceForUri} from '@/helpers/AbstractWorkspace';
import {showInputBox, showQuickPick} from '@/helpers/userInteraction';
import {getPathByPlatform} from '@/helpers/WorkspaceHelpers';
import {Telemetry} from '@/TelemetryClient';
import path from 'path';
import {debug, DebugConfiguration, QuickPickItem, workspace} from 'vscode';
export namespace DebuggerCommands {
export async function startSolidityDebugger() {
Telemetry.sendEvent('DebuggerCommands.startSolidityDebugger.commandStarted');
const workspaceUri = (await getWorkspaceForUri()).workspace;
const workingDirectory = getPathByPlatform(workspaceUri);
const debugNetwork = new DebugNetwork(workingDirectory);
await debugNetwork.load();
const contractBuildDir = debugNetwork.getTruffleConfiguration()!.contracts_build_directory;
const debugNetworkOptions = debugNetwork.getNetwork()!.options;
const web3 = new Web3Wrapper(debugNetworkOptions);
const providerUrl = web3.getProviderUrl();
const workspaceFolder = workspace.getWorkspaceFolder(workspaceUri);
if (debugNetwork.isLocalNetwork()) {
// if local service then provide last transactions to choose
const transactionProvider = new TransactionProvider(web3, contractBuildDir);
const txHashesAsQuickPickItems = await getQuickPickItems(transactionProvider);
const txHashSelection = await showQuickPick(txHashesAsQuickPickItems, {
ignoreFocusOut: true,
placeHolder: 'Enter the transaction hash to debug',
});
const txHash = txHashSelection.detail || txHashSelection.label;
const config = generateDebugAdapterConfig(txHash, workingDirectory, providerUrl);
debug.startDebugging(workspaceFolder, config).then(() => {
Telemetry.sendEvent('DebuggerCommands.startSolidityDebugger.commandFinished');
});
} else {
// if remote network then require txHash
const placeHolder = 'Type the transaction hash you want to debug (0x...)';
const txHash = await showInputBox({placeHolder});
if (txHash) {
const config = generateDebugAdapterConfig(txHash, workingDirectory, providerUrl);
debug.startDebugging(workspaceFolder, config).then(() => {
Telemetry.sendEvent('DebuggerCommands.startSolidityDebugger.commandFinished');
});
}
}
}
}
async function getQuickPickItems(txProvider: TransactionProvider) {
const txHashes = await txProvider.getLastTransactionHashes();
const txInfos = await txProvider.getTransactionsInfo(txHashes);
const lastTxsDeployed = txInfos.filter((tx) => tx.contractName !== '');
return lastTxsDeployed.map((txInfo) => {
const label = shortenHash(txInfo.hash);
const description = generateDescription(txInfo.contractName, txInfo.methodName);
return {alwaysShow: true, label, description, detail: txInfo.hash} as QuickPickItem;
});
}
function generateDebugAdapterConfig(txHash: string, workingDirectory: string, providerUrl: string): DebugConfiguration {
return {
files: [],
name: 'Debug Transactions',
providerUrl,
request: 'launch',
txHash,
type: DEBUG_TYPE,
workingDirectory,
timeout: 30000,
} as DebugConfiguration;
}
// Migration.json, setComplete => Migration.setComplete()
function generateDescription(contractName?: string, methodName?: string) {
const contractNameWithoutExt = path.basename(contractName || '', '.json');
return `${contractNameWithoutExt}.${methodName}()`;
}