-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from OpenZeppelin/main
Release to wizard
- Loading branch information
Showing
13 changed files
with
215 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
export type CompilerInput = { | ||
/** | ||
* The language, currently only Solidity is supported. | ||
*/ | ||
language: string; | ||
/** | ||
* Name of the file and the content of the file. | ||
* e.g. { 'test.sol': { content: 'contract C { function f() public { L.f(); } }' } } | ||
*/ | ||
sources: ContractSources; | ||
/** | ||
* Settings for the compiler. | ||
* e.g. { outputSelection: { '*': { '*': ['*'] } }" | ||
*/ | ||
settings: { | ||
outputSelection: Record<string, Record<string, string[]>>; | ||
}; | ||
}; | ||
|
||
export type ContractSources = { | ||
[source: string]: { content: string }; | ||
}; | ||
|
||
export function buildCompilerInput(sources: ContractSources): CompilerInput { | ||
return { | ||
sources, | ||
language: 'Solidity', | ||
settings: { | ||
outputSelection: { | ||
'*': { '*': ['*'] } | ||
} | ||
} | ||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<script lang="ts"> | ||
import { API } from "$lib/api"; | ||
import { buildCompilerInput, type ContractSources } from "$lib/models/solc"; | ||
import { wizardState } from "../state.svelte"; | ||
let compilationResult: any; | ||
async function compile() { | ||
if (!wizardState.sources) return; | ||
compilationResult = await API.compile(buildCompilerInput(wizardState.sources)); | ||
} | ||
function getMainContractName(sources?: ContractSources) { | ||
if (!sources) return ''; | ||
// The first name that is not a dependency | ||
return Object.keys(sources).find(name => !name.startsWith('@')); | ||
} | ||
</script> | ||
|
||
<p> | ||
Contract to compile: {getMainContractName(wizardState.sources)} | ||
|
||
<button onclick={compile} > | ||
Compile | ||
</button> | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,20 @@ | ||
import type { ContractSources } from "../models/solc"; | ||
import { wizardState } from "./state.svelte"; | ||
|
||
export interface DefenderDeployMessage { | ||
kind: 'oz-wizard-defender-deploy'; | ||
sources: ContractSources; | ||
} | ||
|
||
export const initWizardPlugin = () => { | ||
// when users configure a contract, the plugin gets the results. | ||
// listenToContracts(); | ||
listenToContracts(); | ||
} | ||
|
||
function listenToContracts() { | ||
window.addEventListener('message', function (e: MessageEvent<DefenderDeployMessage>) { | ||
if (e.data.kind === 'oz-wizard-defender-deploy') { | ||
wizardState.sources = e.data.sources; | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import type { ContractSources } from "../models/solc"; | ||
|
||
export const wizardState = $state<{ sources: ContractSources | undefined }>({ | ||
sources: undefined, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import type { CompilerInput } from "$lib/models/solc"; | ||
import { json } from '@sveltejs/kit'; | ||
import { SolidityCompiler } from "./compiler"; | ||
import { attempt } from "$lib/utils/attempt"; | ||
|
||
|
||
export async function POST({ request }: { request: Request }) { | ||
const { input }: { input: CompilerInput } = await request.json(); | ||
|
||
const compiler = new SolidityCompiler(); | ||
|
||
const [output, error] = await attempt(() => JSON.parse(compiler.compile(input))); | ||
|
||
if (error) { | ||
return json({ success: false, error: error.msg }); | ||
} | ||
|
||
return json({ success: true, data: { output } }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import solc from 'solc'; | ||
|
||
import type { ContractSources } from "$lib/models/solc"; | ||
import type { CompilerInput } from "$lib/models/solc"; | ||
|
||
export class SolidityCompiler { | ||
getContent(path: string, contents: ContractSources) { | ||
if (contents[path]) { | ||
return contents[path]; | ||
} | ||
return { error: 'File not found' }; | ||
} | ||
|
||
compile(input: CompilerInput, contents?: ContractSources) { | ||
const shouldFindImports = contents !== undefined; | ||
const findImports = (path: string) => shouldFindImports ? this.getContent(path, contents) : undefined; | ||
const output = solc.compile(JSON.stringify(input), shouldFindImports ? { import: findImports } : undefined); | ||
return output; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
<script lang="ts"> | ||
import { initWizardPlugin } from "$lib/wizard"; | ||
import { onMount } from "svelte"; | ||
import Configure from "$lib/wizard/components/Configure.svelte"; | ||
onMount(initWizardPlugin); | ||
</script> | ||
|
||
<p>Defender Deploy in Wizard</p> | ||
<Configure /> | ||
|