-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathextension.ts
More file actions
324 lines (284 loc) · 9.59 KB
/
extension.ts
File metadata and controls
324 lines (284 loc) · 9.59 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { type InputBoxOptions, window, commands, type ExtensionContext, workspace, RelativePattern } from 'vscode'
import {
callContractMethod,
deployContract,
displayBalance,
setTransactionGas,
updateSelectedNetwork,
getNetworkGasPrices
} from './utils/networks'
import { logger } from './lib'
import {
createKeyPair,
deleteKeyPair,
selectAccount,
importKeyPair,
exportKeyPair
} from './utils/wallet'
import {
createERC4907Contract,
parseBatchCompiledJSON,
parseFoundryCompiledJSON,
parseHardhatCompiledJSON,
parseCompiledJSONPayload,
selectContract,
parseFoundryConfig
} from './utils'
import { isFoundryProject, isHardhatProject } from './utils/functions'
import { provider, status, wallet, contract } from './api'
import { events } from './api/events'
import { event } from './api/api'
import { type API } from './types'
import * as toml from 'toml'
export async function activate (context: ExtensionContext): Promise<API | undefined> {
const disposables = [
// Create new account with password
commands.registerCommand('ethcode.account.create', async () => {
try {
const pwdInpOpt: InputBoxOptions = {
title: 'Password',
ignoreFocusOut: true,
password: true,
placeHolder: 'Password'
}
const password = await window.showInputBox(pwdInpOpt)
if (password === undefined) {
logger.log('Account not created')
return
}
createKeyPair(context, context.extensionPath, password ?? '')
} catch (error) {
logger.error(error)
}
}),
// Delete selected account with password
commands.registerCommand('ethcode.account.delete', async () => {
deleteKeyPair(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Deploy ContractcallContractMethod
commands.registerCommand('ethcode.contract.deploy', async () => {
deployContract(context)
.catch((error: any) => {
logger.error(error)
})
}),
// select ethereum networks
commands.registerCommand('ethcode.network.select', () => {
updateSelectedNetwork(context)
.catch((error: any) => {
logger.error(error)
})
}),
commands.registerCommand('ethcode.rental.create', () => {
createERC4907Contract(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Select Ethereum Account
commands.registerCommand('ethcode.account.select', () => {
selectAccount(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Get account balance
commands.registerCommand('ethcode.account.balance', async () => {
displayBalance(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Set gas strategy
commands.registerCommand('ethcode.transaction.gas.set', async () => {
setTransactionGas(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Get network gas prices
commands.registerCommand('ethcode.transaction.gas.prices', async () => {
await getNetworkGasPrices(context)
}),
// Load combined JSON output
commands.registerCommand('ethcode.compiled-json.load', () => {
const editorContent = (window.activeTextEditor != null)
? window.activeTextEditor.document.getText()
: undefined
parseCompiledJSONPayload(context, editorContent)
}),
// Load all combined JSON output
commands.registerCommand('ethcode.compiled-json.load.all', async () => {
await parseBatchCompiledJSON(context)
}),
// Select a compiled json from the list
commands.registerCommand('ethcode.compiled-json.select', () => {
selectContract(context)
}),
// Call contract method
commands.registerCommand('ethcode.contract.call', async () => {
callContractMethod(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Export Account
commands.registerCommand('ethcode.account.export', async () => {
exportKeyPair(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Import Key pair
commands.registerCommand('ethcode.account.import', async () => {
importKeyPair(context)
.catch((error: any) => {
logger.error(error)
})
}),
// Activate
commands.registerCommand('ethcode.activate', async () => {
logger.success('Welcome to Ethcode!')
}),
// Load Foundry contracts
commands.registerCommand('ethcode.foundry.load', async () => {
try {
await parseFoundryCompiledJSON(context)
} catch (error) {
logger.error(`Error loading Foundry contracts: ${error}`)
}
}),
// Load Hardhat contracts
commands.registerCommand('ethcode.hardhat.load', async () => {
try {
await parseHardhatCompiledJSON(context)
} catch (error) {
logger.error(`Error loading Hardhat contracts: ${error}`)
}
})
]
context.subscriptions.push(...disposables)
// API for extensions
// ref: https://code.visualstudio.com/api/references/vscode-api#extensions
/**
* Defines an API object with several endpoints for exporting functionality to other extensions.
*
* @remarks
* The API object is used to export functionality to other extensions.The API exports endpoints through the use of closures, allowing access to the API endpoint.
*
* @example
* ```typescript
* let ethcodeExtension: any = vscode.extensions.getExtension('7finney.ethcode');
* const api: any = ethcodeExtension.exports;
* const status: string = api.status();
* ```
*
* @returns {API} An API object with several endpoints for exporting functionality to other extensions.
*/
const api: API = {
/**
* STATUS
*
* Retrieves the current status of the API.
*
* @returns {string} The current status of the API.
*/
status: status(),
/**
* WALLET
*
* Provides wallet functionality to other extensions.
*
* @param {Context} context The context object used for managing the Ethcode Extension's state.
* @returns {WalletInterface} An object with several endpoints for exporting wallet functionality to other extensions.
*/
wallet: wallet(context),
/**
* PROVIDER
*
* Provides provider functionality to other extensions.
*
* @param {Context} context The context object used for managing the Ethcode Extension's state.
* @returns {ProviderInterface} An object with several endpoints for exporting provider functionality to other extensions.
*
*/
provider: provider(context),
/**
* CONTRACT
*
* Provides contract functionality to other extensions.
*
* @param {Context} context The context object used for managing the Ethcode Extension's state.
* @returns {ContractInterface} An object with several endpoints for exporting contract functionality to other extensions.
*
*/
contract: contract(context),
/**
* EVENTS
*
* Provides event functionality to other extensions.
*
* @returns {EventsInterface} An object with several endpoints for exporting event functionality to other extensions.
*/
events: events()
}
const path_ = workspace.workspaceFolders
if (path_ === undefined) {
await window.showErrorMessage('No folder selected please open one.')
return
}
// Create dynamic file watcher based on project type
const createDynamicWatcher = async () => {
const watchPatterns: string[] = []
try {
// Check for Foundry project using centralized parser
const foundryConfig = await parseFoundryConfig()
if (foundryConfig) {
const { outDir } = foundryConfig
watchPatterns.push(`${outDir}/**/*.json`)
logger.log(`Foundry project detected, watching: ${outDir}/**/*.json`)
}
// Check for Hardhat project
if (isHardhatProject(path_[0].uri.fsPath)) {
watchPatterns.push('artifacts/**/*.json')
logger.log('Hardhat project detected, watching: artifacts/**/*.json')
}
// Fallback patterns for other frameworks
if (watchPatterns.length === 0) {
watchPatterns.push('{artifacts, build, out, cache, out-*/**/*.json}')
logger.log('No specific framework detected, using fallback patterns')
}
} catch (error) {
logger.error(`Error setting up file watcher: ${error}`)
// Fallback to original pattern
watchPatterns.push('{artifacts, build, out, cache, out-*/**/*.json}')
}
// Create watcher with dynamic patterns
return workspace.createFileSystemWatcher(
new RelativePattern(path_[0].uri.fsPath, watchPatterns.join(','))
)
}
const watcher = await createDynamicWatcher()
watcher.onDidCreate(async (uri: any) => {
await parseBatchCompiledJSON(context)
const contracts = context.workspaceState.get('contracts') as string[]
if (contracts === undefined || contracts.length === 0) return []
event.contracts.fire(Object.keys(contracts))
})
watcher.onDidChange(async (uri: any) => {
await parseBatchCompiledJSON(context)
const contracts = context.workspaceState.get('contracts') as string[]
if (contracts === undefined || contracts.length === 0) return []
event.contracts.fire(Object.keys(contracts))
})
watcher.onDidDelete(async (uri: any) => {
const contracts = context.workspaceState.get('contracts') as string[]
if (contracts === undefined || contracts.length === 0) return []
event.contracts.fire(Object.keys(contracts))
})
context.subscriptions.push(watcher)
return api
}