Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

220 - Solidity deploy core POC #224

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
12,693 changes: 0 additions & 12,693 deletions .pnp.cjs

This file was deleted.

2,090 changes: 0 additions & 2,090 deletions .pnp.loader.mjs

This file was deleted.

893 changes: 893 additions & 0 deletions .yarn/releases/yarn-4.0.2.cjs

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
compressionLevel: mixed

enableGlobalCache: false

nodeLinker: node-modules
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
"lint": "echo 'linting...'",
"format": "echo 'formatting...'",
"test": "echo 'testing...'"
}
},
"packageManager": "[email protected]"
}
1 change: 1 addition & 0 deletions remove-me-d495e2d9927644ddbe0c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
d495e2d9927644ddbe0c
2 changes: 2 additions & 0 deletions toolchains/solidity/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,14 @@
"extension:publish": "yarn run package && vsce publish --no-dependencies"
},
"dependencies": {
"js-yaml": "^4.1.0",
"vscode-languageclient": "^8.1.0",
"vscode-languageserver": "^8.1.0",
"vscode-languageserver-textdocument": "^1.0.8"
},
"devDependencies": {
"@types/glob": "^8.1.0",
"@types/js-yaml": "^4.0.9",
"@types/mocha": "^10.0.1",
"@types/node": "20.2.5",
"@types/vscode": "^1.75.0",
Expand Down
121 changes: 121 additions & 0 deletions toolchains/solidity/extension/src/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { exec } from "child_process";
import * as yaml from 'js-yaml';
import * as path from 'path';
import { workspace } from "vscode";

export type Contract = {
name: string;
path: string;
abi: any[];
address?: string;
}

export type Script = {
path: string;
name: string;
}

async function getScriptFolder(): Promise<string> {
const foundryConfigContent = await workspace.fs.readFile(workspace.workspaceFolders![0].uri.with({ path: path.join(workspace.workspaceFolders![0].uri.path, 'foundry.toml') }));
const parsedFoundryConfig : any = yaml.load(foundryConfigContent.toString());

return parsedFoundryConfig.script ?? 'script';
}

async function getContractFolder(): Promise<string> {
const foundryConfigContent = await workspace.fs.readFile(workspace.workspaceFolders![0].uri.with({ path: path.join(workspace.workspaceFolders![0].uri.path, 'foundry.toml') }));
const parsedFoundryConfig : any = yaml.load(foundryConfigContent.toString());

return parsedFoundryConfig.contract ?? 'src';
}

async function getOutFolder(): Promise<string> {
const foundryConfigContent = await workspace.fs.readFile(workspace.workspaceFolders![0].uri.with({ path: path.join(workspace.workspaceFolders![0].uri.path, 'foundry.toml') }));
const parsedFoundryConfig : any = yaml.load(foundryConfigContent.toString());

return parsedFoundryConfig.out ?? 'out';
}

async function getAbiFile(contractName: string, outFolder: string): Promise<string> {
const abiFilePath = path.join(outFolder, contractName, `${contractName}.json`);
const abiFileBuffer = await workspace.fs.readFile(workspace.workspaceFolders![0].uri.with({ path: abiFilePath }));
const abiFileContent = Buffer.from(abiFileBuffer).toString('utf-8');

return abiFileContent;
}

async function getContracts(): Promise<Contract[]> {
const contracts: Contract[] = [];
const contractFolder = await getContractFolder();
const contractFiles = await workspace.findFiles(`**/${contractFolder}/*.sol`);
const outFolder = await getOutFolder();
for (const contractFile of contractFiles) {
const contractContentBuffer = await workspace.fs.readFile(contractFile);
const contractContent = Buffer.from(contractContentBuffer).toString('utf-8');
const contractNameRegex = /contract\s+(\w+)\s*\{/g;
let contractNameMatch;
while ((contractNameMatch = contractNameRegex.exec(contractContent)) !== null) {
const contractName = contractNameMatch[1];
const abi = await getAbiFile(contractName, outFolder);
const contract = {
name: contractName,
path: contractFile.path,
abi: JSON.parse(abi).abi,
};
contracts.push(contract);
}
}
return contracts;
}

async function getScripts(): Promise<Script[]> {
const scripts: Script[] = [];
const scriptFolder = await getScriptFolder();
const scriptFiles = await workspace.findFiles(`**/${scriptFolder}/*.sol`);
const outFolder = await getOutFolder();
for (const scriptFile of scriptFiles) {
const scriptContentBuffer = await workspace.fs.readFile(scriptFile);
const scriptContent = Buffer.from(scriptContentBuffer).toString('utf-8');
const contractNameRegex = /contract\s+(\w+)\s*\{/g;
let scriptNameMatch;
while ((scriptNameMatch = contractNameRegex.exec(scriptContent)) !== null) {
const scriptName = scriptNameMatch[1];
const abi = await getAbiFile(scriptName, outFolder);
const script = {
name: path.basename(scriptFile.path, '.sol'),
path: scriptFile.path,
abi: JSON.parse(abi).abi,
};
scripts.push(script);
}

}
return scripts;
}

async function deployContract(network: number, contract: Contract, verify: boolean, cstrArgs: string): Promise<void> {
const verifyStr = verify ? '--verify' : '';
exec(`forge create ${contract.path}:${contract.name} -c ${network} ${verifyStr} --contructor-args ${cstrArgs}`, (error, _stdout, _stderr) => {
if (error) {
throw error;
}
});
}

async function deployScript(network: number, script: Script, verify: boolean): Promise<void> {
const verifyStr = verify ? '--verify' : '';
exec(`forge script ${script.path}:${script.name} -c ${network} ${verifyStr}`, (error, _stdout, _stderr) => {
if (error) {
throw error;
}
});
}

async function verifyContract(network: number, contract: Contract): Promise<void> {
// TODO load the contructor args path from out
exec(`forge verify-contract ${contract.path} ${contract.address} -c ${network}`, (error, _stdout, _stderr) => {
if (error) {
throw error;
}
});
}
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ __metadata:
languageName: node
linkType: hard

"@types/js-yaml@npm:^4.0.9":
version: 4.0.9
resolution: "@types/js-yaml@npm:4.0.9"
checksum: a0ce595db8a987904badd21fc50f9f444cb73069f4b95a76cc222e0a17b3ff180669059c763ec314bc4c3ce284379177a9da80e83c5f650c6c1310cafbfaa8e6
languageName: node
linkType: hard

"@types/json-schema@npm:*, @types/json-schema@npm:^7.0.8, @types/json-schema@npm:^7.0.9":
version: 7.0.15
resolution: "@types/json-schema@npm:7.0.15"
Expand Down Expand Up @@ -3032,6 +3039,7 @@ __metadata:
resolution: "osmium-solidity-extension@workspace:toolchains/solidity/extension"
dependencies:
"@types/glob": "npm:^8.1.0"
"@types/js-yaml": "npm:^4.0.9"
"@types/mocha": "npm:^10.0.1"
"@types/node": "npm:20.2.5"
"@types/vscode": "npm:^1.75.0"
Expand All @@ -3041,6 +3049,7 @@ __metadata:
"@vscode/vsce": "npm:2.21.1"
eslint: "npm:^8.41.0"
glob: "npm:^8.1.0"
js-yaml: "npm:^4.1.0"
mocha: "npm:^10.2.0"
ts-loader: "npm:^9.4.3"
typescript: "npm:^5.1.3"
Expand Down