Skip to content

Commit 6bc924c

Browse files
committed
contract execution successfull
1 parent 6654752 commit 6bc924c

File tree

5 files changed

+267
-30
lines changed

5 files changed

+267
-30
lines changed

package.json

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"name": "helloworld-sample",
3-
"displayName": "helloworld-sample",
4-
"description": "HelloWorld example for VS Code",
2+
"name": "Starkex",
3+
"displayName": "Starkex",
4+
"description": "Extension for execution of contracts written in cairo.",
55
"version": "0.0.1",
6-
"publisher": "vscode-samples",
7-
"repository": "https://github.com/Microsoft/vscode-extension-samples/helloworld-sample",
6+
"publisher": "Aniket6990",
7+
"repository": "https://github.com/Aniket6990/stark-extension.git",
88
"engines": {
99
"vscode": "^1.74.0"
1010
},
1111
"categories": [
12-
"Other"
12+
"Programming Languages"
1313
],
1414
"activationEvents": [
1515
"onCommand: starknet.activate"
@@ -19,42 +19,52 @@
1919
"commands": [
2020
{
2121
"command": "starknet.activate",
22-
"title": "Hello World",
22+
"title": "activate Starkex extension",
2323
"category": "Starknet"
2424
},
2525
{
2626
"command": "starknet.selectnetwork",
27-
"title": "Select Network",
27+
"title": "Select starknet network",
2828
"category": "Starknet"
2929
},
3030
{
3131
"command": "starknet.createaccount",
32-
"title": "Create new account",
32+
"title": "Create starkex new account",
3333
"category": "Starknet"
3434
},
3535
{
3636
"command": "starknet.unDeployedAccount",
37-
"title": "Select undeployed account",
37+
"title": "Select starkex undeployed account",
3838
"category": "Starknet"
3939
},
4040
{
4141
"command": "starknet.deployaccount",
42-
"title": "Deploy account",
42+
"title": "Deploy starkex new account",
4343
"category": "Starknet"
4444
},
4545
{
4646
"command": "starknet.selectaccount",
47-
"title": "Select account",
47+
"title": "Select starkex account",
4848
"category": "Starknet"
4949
},
5050
{
5151
"command": "starknet.selectContract",
52-
"title": "Select contract",
52+
"title": "Select cairo contract",
5353
"category": "Starknet"
5454
},
5555
{
5656
"command": "starknet.declareContract",
57-
"title": "Declare contract",
57+
"title": "Declare cairo contract",
58+
"category": "Starknet"
59+
},
60+
{
61+
"command": "starknet.deployContract",
62+
"title": "Deploy cairo contract",
63+
"category": "Starknet"
64+
},
65+
{
66+
"command": "starknet.callFunction",
67+
"title": "Call contact method",
5868
"category": "Starknet"
5969
}
6070
]

src/config/contract.ts

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as vscode from "vscode";
22
import * as fs from "fs";
3-
import path from "path";
3+
import path, { resolve } from "path";
44
import { logger } from "../lib";
5-
import { IContractQP } from "../types";
5+
import { ABIFragment, IContractQP, IFunctionQP } from "../types";
66
import { createABIFile, createAddressFile } from "../utils/functions";
77
import { getAccountInfo } from "./account";
8-
import { Account, ec, Provider } from "starknet";
8+
import { Account, Contract, ec, Provider } from "starknet";
99
import { getNetworkProvider } from "./network";
1010

1111
const loadAllCompiledContract = () => {
@@ -73,6 +73,16 @@ const getContractInfo = (path_: string, fileName: string) => {
7373
return parsedFileData;
7474
};
7575

76+
const getContractABI = (path_: string, fileName: string) => {
77+
const file = fileName.substring(0, fileName.length - 5);
78+
const fileData = fs.readFileSync(
79+
path.join(path_, "starkex", file, `${file}_abi.json`),
80+
{ encoding: "utf-8" }
81+
);
82+
const parsedFileData = JSON.parse(fileData);
83+
return parsedFileData;
84+
};
85+
7686
export const declareContract = async (context: vscode.ExtensionContext) => {
7787
try {
7888
if (vscode.workspace.workspaceFolders === undefined) {
@@ -117,3 +127,156 @@ export const declareContract = async (context: vscode.ExtensionContext) => {
117127
logger.log(`Error while contract declaration: ${error}`);
118128
}
119129
};
130+
131+
export const deployContract = async (context: vscode.ExtensionContext) => {
132+
try {
133+
if (vscode.workspace.workspaceFolders === undefined) {
134+
logger.error("Error: Please open your solidity project to vscode");
135+
return;
136+
}
137+
const path_ = vscode.workspace.workspaceFolders[0].uri.fsPath;
138+
const provider = getNetworkProvider(context) as Provider;
139+
const selectedContract: string = context.workspaceState.get(
140+
"selectedContract"
141+
) as string;
142+
const selectedAccount = context.workspaceState.get("account") as string;
143+
console.log(`selectedaAccount first: ${selectedAccount}`);
144+
if (selectedAccount === undefined) {
145+
logger.log("No account selected.");
146+
return;
147+
}
148+
const accountInfo = getAccountInfo(context, selectedAccount);
149+
const keyPair = ec.getKeyPair(accountInfo.privateKey);
150+
logger.log("Deploying contract...");
151+
const account = new Account(provider, accountInfo.accountAddress, keyPair);
152+
const contractInfo = getContractInfo(path_, selectedContract);
153+
if (contractInfo.classHash === "") {
154+
logger.log("No classHash available for selected contract.");
155+
return;
156+
}
157+
const deployResponse = await account.deployContract({
158+
classHash: contractInfo.classHash,
159+
});
160+
161+
logger.log(`transaction hash: ${deployResponse.transaction_hash}`);
162+
163+
logger.log("waiting for transaction success...");
164+
165+
await provider.waitForTransaction(deployResponse.transaction_hash);
166+
167+
const { abi: testAbi } = await provider.getClassAt(
168+
deployResponse.contract_address
169+
);
170+
if (testAbi === undefined) {
171+
throw new Error("no abi.");
172+
}
173+
const myTestContract = new Contract(
174+
testAbi,
175+
deployResponse.contract_address,
176+
provider
177+
);
178+
179+
await provider.waitForTransaction(myTestContract.transaction_hash);
180+
logger.log(`contract deployed successfully: ${myTestContract.address}`);
181+
} catch (error) {
182+
logger.log(`Error while contract deployment: ${error}`);
183+
}
184+
};
185+
186+
export const executeContractFunction = async (
187+
context: vscode.ExtensionContext
188+
) => {
189+
if (vscode.workspace.workspaceFolders === undefined) {
190+
logger.error("Error: Please open your solidity project to vscode");
191+
return;
192+
}
193+
const path_ = vscode.workspace.workspaceFolders[0].uri.fsPath;
194+
const provider = getNetworkProvider(context) as Provider;
195+
const selectedContract: string = context.workspaceState.get(
196+
"selectedContract"
197+
) as string;
198+
const selectedAccount = context.workspaceState.get("account") as string;
199+
if (selectedAccount === undefined) {
200+
logger.log("No account selected.");
201+
return;
202+
}
203+
const accountInfo = getAccountInfo(context, selectedAccount);
204+
const keyPair = ec.getKeyPair(accountInfo.privateKey);
205+
const account = new Account(provider, accountInfo.accountAddress, keyPair);
206+
const functionABI = await getSelectedFunction(path_, selectedContract);
207+
const contractInfo = getContractInfo(path_, selectedContract);
208+
209+
const params_: Array<string> = functionABI.inputs.map((e) => {
210+
return e.value;
211+
});
212+
213+
const params = params_ !== undefined ? params_ : [];
214+
215+
if (functionABI.stateMutability === "view") {
216+
const { abi: testAbi } = await provider.getClassAt(contractInfo.address);
217+
if (testAbi === undefined) {
218+
throw new Error("no abi.");
219+
}
220+
const contract = new Contract(testAbi, contractInfo.address, provider);
221+
const functionCall = await contract.call(`${functionABI.name}`);
222+
logger.log(`${functionCall.res.toString()}`);
223+
} else {
224+
const { abi: testAbi } = await provider.getClassAt(contractInfo.address);
225+
if (testAbi === undefined) {
226+
throw new Error("no abi.");
227+
}
228+
const contract = new Contract(testAbi, contractInfo.address, provider);
229+
contract.connect(account);
230+
const res = await contract.invoke(`${functionABI.name}`, params);
231+
logger.log(`transaction hash: ${res.transaction_hash}`);
232+
233+
logger.log("waiting for transaction success...");
234+
235+
await provider.waitForTransaction(res.transaction_hash);
236+
237+
logger.log("transaction successfull");
238+
}
239+
};
240+
241+
const getSelectedFunction = (
242+
path_: string,
243+
selectedContract: string
244+
): Promise<ABIFragment> => {
245+
return new Promise((resolve, reject) => {
246+
try {
247+
const contractInfo: Array<ABIFragment> = getContractABI(
248+
path_,
249+
selectedContract
250+
);
251+
252+
console.log(JSON.stringify(contractInfo));
253+
if (contractInfo === undefined) return;
254+
const quickPick = vscode.window.createQuickPick<IFunctionQP>();
255+
256+
quickPick.items = contractInfo.map((account: ABIFragment) => ({
257+
label: account.name,
258+
}));
259+
quickPick.onDidChangeActive(() => {
260+
quickPick.placeholder = "Select Function";
261+
});
262+
quickPick.onDidChangeSelection((selection: any) => {
263+
if (selection[0] != null) {
264+
const { label } = selection[0];
265+
quickPick.dispose();
266+
const functionItem = contractInfo.filter(
267+
(i: ABIFragment) => i.name === label
268+
);
269+
if (functionItem.length === 0)
270+
throw new Error("No function is selected");
271+
resolve(functionItem[0]);
272+
}
273+
});
274+
quickPick.onDidHide(() => {
275+
quickPick.dispose();
276+
});
277+
quickPick.show();
278+
} catch (error) {
279+
reject(error);
280+
}
281+
});
282+
};

src/extension.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ import {
77
selectDeployedAccount,
88
selectNotDeployedAccount,
99
} from "./config/account";
10-
import { declareContract, selectCompiledContract } from "./config/contract";
10+
import {
11+
declareContract,
12+
deployContract,
13+
executeContractFunction,
14+
selectCompiledContract,
15+
} from "./config/contract";
1116
import { updateSelectedNetwork } from "./config/network";
1217
import { logger } from "./lib";
1318

@@ -44,6 +49,12 @@ export function activate(context: vscode.ExtensionContext) {
4449
}),
4550
vscode.commands.registerCommand("starknet.declareContract", async () => {
4651
await declareContract(context);
52+
}),
53+
vscode.commands.registerCommand("starknet.deployContract", async () => {
54+
await deployContract(context);
55+
}),
56+
vscode.commands.registerCommand("starknet.callFunction", async () => {
57+
await executeContractFunction(context);
4758
})
4859
);
4960
}

src/types/index.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,24 @@ export interface IAccountQP extends QuickPickItem {
2020
export interface IContractQP extends QuickPickItem {
2121
label: string;
2222
}
23+
24+
export interface IFunctionQP extends QuickPickItem {
25+
label: string;
26+
}
27+
28+
interface inputType {
29+
name: string;
30+
type: string;
31+
value: string;
32+
}
33+
interface outputType {
34+
name: string;
35+
type: string;
36+
}
37+
export interface ABIFragment {
38+
inputs: Array<inputType>;
39+
name: string;
40+
stateMutability: string;
41+
type: string;
42+
outputs: Array<outputType>;
43+
}

0 commit comments

Comments
 (0)