Skip to content

Commit fb6ffbe

Browse files
authored
Merge pull request #12 from OpenZeppelin/listen-to-wizard-contracts
feat: listen to wizard contract changes
2 parents e011e95 + 492c670 commit fb6ffbe

File tree

8 files changed

+85
-17
lines changed

8 files changed

+85
-17
lines changed

src/lib/models/solc.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export type CompilerInput = {
77
* Name of the file and the content of the file.
88
* e.g. { 'test.sol': { content: 'contract C { function f() public { L.f(); } }' } }
99
*/
10-
sources: Record<string, { content: string }>;
10+
sources: ContractSources;
1111
/**
1212
* Settings for the compiler.
1313
* e.g. { outputSelection: { '*': { '*': ['*'] } }"
@@ -17,4 +17,19 @@ export type CompilerInput = {
1717
};
1818
};
1919

20-
export type ImportContents = Record<string, { contents: string }>;
20+
export type ContractSources = {
21+
[source: string]: { content: string };
22+
};
23+
24+
export function buildCompilerInput(sources: ContractSources): CompilerInput {
25+
return {
26+
sources,
27+
language: 'Solidity',
28+
settings: {
29+
outputSelection: {
30+
'*': { '*': ['*'] }
31+
}
32+
}
33+
};
34+
}
35+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<script lang="ts">
2+
import { API } from "$lib/api";
3+
import { buildCompilerInput, type ContractSources } from "$lib/models/solc";
4+
import { wizardState } from "../state.svelte";
5+
6+
let compilationResult: any;
7+
8+
async function compile() {
9+
if (!wizardState.sources) return;
10+
compilationResult = await API.compile(buildCompilerInput(wizardState.sources));
11+
}
12+
13+
function getMainContractName(sources?: ContractSources) {
14+
if (!sources) return '';
15+
// The first name that is not a dependency
16+
return Object.keys(sources).find(name => !name.startsWith('@'));
17+
}
18+
19+
</script>
20+
21+
<p>
22+
Contract to compile: {getMainContractName(wizardState.sources)}
23+
24+
<button onclick={compile} >
25+
Compile
26+
</button>
27+
</p>

src/lib/wizard/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,20 @@
1+
import type { ContractSources } from "../models/solc";
2+
import { wizardState } from "./state.svelte";
3+
4+
export interface DefenderDeployMessage {
5+
kind: 'oz-wizard-defender-deploy';
6+
sources: ContractSources;
7+
}
8+
19
export const initWizardPlugin = () => {
10+
// when users configure a contract, the plugin gets the results.
11+
listenToContracts();
12+
}
13+
14+
function listenToContracts() {
15+
window.addEventListener('message', function (e: MessageEvent<DefenderDeployMessage>) {
16+
if (e.data.kind === 'oz-wizard-defender-deploy') {
17+
wizardState.sources = e.data.sources;
18+
}
19+
});
220
}

src/lib/wizard/state.svelte.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { ContractSources } from "../models/solc";
2+
3+
export const wizardState = $state<{ sources: ContractSources | undefined }>({
4+
sources: undefined,
5+
});

src/routes/+page.svelte

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,17 @@
2020
// assumes that when in dev mode and
2121
// the ancestor origin is localhost, we are in the wizard
2222
if (dev && ancestorOrigin.includes("localhost")) {
23-
parent = 'wizard';
24-
return;
23+
return parent = 'wizard';
2524
}
2625
27-
if (ancestorOrigin.includes("remix.ethereum")) {
28-
parent = 'remix';
29-
return initRemixPlugin();
26+
if (ancestorOrigin.includes("wizard.openzeppelin")) {
27+
return parent = 'wizard';
3028
}
3129
32-
if (ancestorOrigin.includes("wizard.openzeppelin")) {
33-
parent = 'wizard';
34-
// TODO: init wizard plugin
35-
return;
30+
if (ancestorOrigin.includes("remix.ethereum")) {
31+
return parent = 'remix';
3632
}
33+
3734
});
3835
</script>
3936

src/routes/compiler/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import solc from 'solc';
22

3-
import type { ImportContents } from "$lib/models/solc";
3+
import type { ContractSources } from "$lib/models/solc";
44
import type { CompilerInput } from "$lib/models/solc";
55

66
export class SolidityCompiler {
7-
getContent(path: string, contents: Record<string, { contents: string }>) {
7+
getContent(path: string, contents: ContractSources) {
88
if (contents[path]) {
99
return contents[path];
1010
}
1111
return { error: 'File not found' };
1212
}
1313

14-
compile(input: CompilerInput, contents?: ImportContents) {
14+
compile(input: CompilerInput, contents?: ContractSources) {
1515
const shouldFindImports = contents !== undefined;
1616
const findImports = (path: string) => shouldFindImports ? this.getContent(path, contents) : undefined;
1717
const output = solc.compile(JSON.stringify(input), shouldFindImports ? { import: findImports } : undefined);

src/routes/remix.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<script lang="ts">
2+
import { onMount } from "svelte";
3+
import { initRemixPlugin } from "$lib/remix";
24
import { globalState } from "$lib/remix/state/state.svelte";
35
import Setup from "$lib/remix/components/Setup.svelte";
46
import Network from "$lib/remix/components/Network.svelte";
@@ -48,6 +50,8 @@
4850
4951
return false;
5052
});
53+
54+
onMount(initRemixPlugin);
5155
</script>
5256

5357
<p>

src/routes/wizard.svelte

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<script lang="ts">
2-
import { onMount } from "svelte";
32
import { initWizardPlugin } from "$lib/wizard";
3+
import { onMount } from "svelte";
4+
import Configure from "$lib/wizard/components/Configure.svelte";
45
5-
let output = $state<any>(null);
6+
onMount(initWizardPlugin);
67
</script>
78

8-
<p>Defender Deploy in Wizard</p>
9+
<Configure />
10+

0 commit comments

Comments
 (0)