Skip to content

Commit e011e95

Browse files
authored
Merge pull request #11 from OpenZeppelin/add-compiler-to-wizard-plugin
Add solidity compiler to wizard plugin
2 parents e87118a + 9ce364f commit e011e95

File tree

9 files changed

+139
-4
lines changed

9 files changed

+139
-4
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"@remixproject/plugin-utils": "^0.3.38",
2727
"@sveltejs/adapter-netlify": "^4.3.6",
2828
"bootstrap": "^5.3.3",
29-
"ethers": "^6.13.4"
29+
"ethers": "^6.13.4",
30+
"solc": "^0.8.28"
3031
}
3132
}

pnpm-lock.yaml

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ declare global {
1313
declare interface Window {
1414
ethereum?: import('ethers').Eip1193Provider & import('ethers').BrowserProvider;
1515
}
16+
17+
// solc does not have a type definition file.
18+
declare module 'solc' {
19+
export function compile(input: string, opts?: any): any;
20+
}

src/lib/api.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { CreateApprovalProcessRequest } from "./models/approval-process";
22
import type { Credentials } from "./models/auth";
33
import type { DeployContractRequest, UpdateDeploymentRequest } from "./models/deploy";
4+
import type { CompilerInput } from "./models/solc";
45

56
class ApiClient {
67
credentials: Credentials | null = null;
@@ -72,6 +73,16 @@ class ApiClient {
7273

7374
return response.json();
7475
}
76+
77+
async compile(input: CompilerInput) {
78+
const response = await fetch("/compiler", {
79+
method: "POST",
80+
headers: { "Content-Type": "application/json" },
81+
body: JSON.stringify({ input }),
82+
});
83+
84+
return response.json();
85+
}
7586
}
7687

7788
export const API = new ApiClient();

src/lib/models/solc.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export type CompilerInput = {
2+
/**
3+
* The language, currently only Solidity is supported.
4+
*/
5+
language: string;
6+
/**
7+
* Name of the file and the content of the file.
8+
* e.g. { 'test.sol': { content: 'contract C { function f() public { L.f(); } }' } }
9+
*/
10+
sources: Record<string, { content: string }>;
11+
/**
12+
* Settings for the compiler.
13+
* e.g. { outputSelection: { '*': { '*': ['*'] } }"
14+
*/
15+
settings: {
16+
outputSelection: Record<string, Record<string, string[]>>;
17+
};
18+
};
19+
20+
export type ImportContents = Record<string, { contents: string }>;

src/lib/wizard/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
export const initWizardPlugin = () => {
2-
// when users configure a contract, the plugin gets the results.
3-
// listenToContracts();
42
}

src/routes/compiler/+server.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import type { CompilerInput } from "$lib/models/solc";
2+
import { json } from '@sveltejs/kit';
3+
import { SolidityCompiler } from "./compiler";
4+
import { attempt } from "$lib/utils/attempt";
5+
6+
7+
export async function POST({ request }: { request: Request }) {
8+
const { input }: { input: CompilerInput } = await request.json();
9+
10+
const compiler = new SolidityCompiler();
11+
12+
const [output, error] = await attempt(() => JSON.parse(compiler.compile(input)));
13+
14+
if (error) {
15+
return json({ success: false, error: error.msg });
16+
}
17+
18+
return json({ success: true, data: { output } });
19+
}

src/routes/compiler/compiler.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import solc from 'solc';
2+
3+
import type { ImportContents } from "$lib/models/solc";
4+
import type { CompilerInput } from "$lib/models/solc";
5+
6+
export class SolidityCompiler {
7+
getContent(path: string, contents: Record<string, { contents: string }>) {
8+
if (contents[path]) {
9+
return contents[path];
10+
}
11+
return { error: 'File not found' };
12+
}
13+
14+
compile(input: CompilerInput, contents?: ImportContents) {
15+
const shouldFindImports = contents !== undefined;
16+
const findImports = (path: string) => shouldFindImports ? this.getContent(path, contents) : undefined;
17+
const output = solc.compile(JSON.stringify(input), shouldFindImports ? { import: findImports } : undefined);
18+
return output;
19+
}
20+
}

src/routes/wizard.svelte

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<script lang="ts">
2+
import { onMount } from "svelte";
3+
import { initWizardPlugin } from "$lib/wizard";
4+
5+
let output = $state<any>(null);
26
</script>
37

4-
<p>Defender Deploy in Wizard</p>
8+
<p>Defender Deploy in Wizard</p>

0 commit comments

Comments
 (0)