-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathextension.ts
More file actions
262 lines (235 loc) · 7.61 KB
/
extension.ts
File metadata and controls
262 lines (235 loc) · 7.61 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
import { type InputBoxOptions, window, commands, type ExtensionContext, workspace, RelativePattern } from 'vscode'
import {
callContractMethod,
deployContract,
displayBalance,
setTransactionGas,
updateSelectedNetwork
} from './utils/networks'
import { logger } from './lib'
import {
createKeyPair,
deleteKeyPair,
selectAccount,
importKeyPair,
exportKeyPair
} from './utils/wallet'
import {
createERC4907Contract,
parseBatchCompiledJSON,
parseCompiledJSONPayload,
selectContract
} from './utils'
import { provider, status, wallet, contract } from './api'
import { events } from './api/events'
import { event } from './api/api'
import { type API } from './types'
// import path = require('path')
export async function activate (context: ExtensionContext): Promise<API | undefined> {
context.subscriptions.push(
// 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)
})
}),
// 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 () => {
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!')
})
)
// 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()
}
api.events.accountCreated.event((info) => {
if (info.success) logger.success(info.successMsg)
else {
logger.error(info.error)
}
})
const path_ = workspace.workspaceFolders
if (path_ === undefined) {
await window.showErrorMessage('No folder selected please open one.')
return
}
const watcher = workspace.createFileSystemWatcher(
new RelativePattern(path_[0].uri.fsPath, '{artifacts, build, out, cache}/**/*.json')
)
watcher.onDidCreate(async (uri) => {
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) => {
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) => {
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
}