From c79fc0c8798a2a59201999a8e59de4bdb1ca57bd Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 3 Dec 2024 08:17:36 -0300 Subject: [PATCH 1/7] feat: install compiler and setup --- package.json | 3 +- pnpm-lock.yaml | 57 ++++++++++++++++++++++++++++++++++++++ src/lib/models/solc.ts | 20 +++++++++++++ src/lib/wizard/compiler.ts | 23 +++++++++++++++ 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 src/lib/models/solc.ts create mode 100644 src/lib/wizard/compiler.ts diff --git a/package.json b/package.json index ed54db7..5cc57b4 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "@remixproject/plugin-utils": "^0.3.38", "@sveltejs/adapter-netlify": "^4.3.6", "bootstrap": "^5.3.3", - "ethers": "^6.13.4" + "ethers": "^6.13.4", + "solc": "^0.8.28" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index fbd3b7e..927600d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -32,6 +32,9 @@ importers: ethers: specifier: ^6.13.4 version: 6.13.4 + solc: + specifier: ^0.8.28 + version: 0.8.28 devDependencies: '@sveltejs/adapter-auto': specifier: ^3.0.0 @@ -660,6 +663,13 @@ packages: resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==} engines: {node: '>= 0.8'} + command-exists@1.2.9: + resolution: {integrity: sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w==} + + commander@8.3.0: + resolution: {integrity: sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==} + engines: {node: '>= 12'} + cookie@0.6.0: resolution: {integrity: sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==} engines: {node: '>= 0.6'} @@ -926,6 +936,10 @@ packages: magic-string@0.30.12: resolution: {integrity: sha512-Ea8I3sQMVXr8JhN4z+H/d8zwo+tYDgHE9+5G4Wnrwhs0gaK9fXTKx0Tw5Xwsd/bCPTTZNRAdpyzvoeORe9LYpw==} + memorystream@0.3.1: + resolution: {integrity: sha512-S3UwM3yj5mtUSEfP41UZmt/0SCoVYUcU1rkXv+BQ5Ig8ndL4sPoJNBUJERafdPb5jjHJGuMgytgKvKIf58XNBw==} + engines: {node: '>= 0.10.0'} + mime-db@1.52.0: resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} engines: {node: '>= 0.6'} @@ -973,6 +987,10 @@ packages: encoding: optional: true + os-tmpdir@1.0.2: + resolution: {integrity: sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==} + engines: {node: '>=0.10.0'} + package-json-from-dist@1.0.1: resolution: {integrity: sha512-UEZIS3/by4OC8vL3P2dTXRETpebLI2NiI5vIrjaD/5UtrkFX/tNbwjTSRAGC/+7CAo2pIcBaRgWmcBBHcsaCIw==} @@ -1027,6 +1045,10 @@ packages: safe-buffer@5.1.2: resolution: {integrity: sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==} + semver@5.7.2: + resolution: {integrity: sha512-cBznnQ9KjJqU67B52RMC65CMarK2600WFnbkcaiwWq3xy/5haFJlshgnpjovMVJ+Hff49d8GEn0b87C5pDQ10g==} + hasBin: true + set-cookie-parser@2.7.1: resolution: {integrity: sha512-IOc8uWeOZgnb3ptbCURJWNjWUPcO3ZnTTdzsurqERrP6nPyv+paC55vJM0LpOlT2ne+Ix+9+CRG1MNLlyZ4GjQ==} @@ -1053,6 +1075,11 @@ packages: resolution: {integrity: sha512-BPwJGUeDaDCHihkORDchNyyTvWFhcusy1XMmhEVTQTwGeybFbp8YEmB+njbPnth1FibULBSBVwCQni25XlCUDg==} engines: {node: '>=18'} + solc@0.8.28: + resolution: {integrity: sha512-AFCiJ+b4RosyyNhnfdVH4ZR1+TxiL91iluPjw0EJslIu4LXGM9NYqi2z5y8TqochC4tcH9QsHfwWhOIC9jPDKA==} + engines: {node: '>=10.0.0'} + hasBin: true + source-map-js@1.2.1: resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} engines: {node: '>=0.10.0'} @@ -1091,6 +1118,10 @@ packages: tiny-glob@0.2.9: resolution: {integrity: sha512-g/55ssRPUjShh+xkfx9UPDXqhckHEsHr4Vd9zX55oSdGZc/MD0m3sferOkwWtp98bv+kcVfEHtRJgBVJzelrzg==} + tmp@0.0.33: + resolution: {integrity: sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==} + engines: {node: '>=0.6.0'} + totalist@3.0.1: resolution: {integrity: sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==} engines: {node: '>=6'} @@ -2048,6 +2079,10 @@ snapshots: dependencies: delayed-stream: 1.0.0 + command-exists@1.2.9: {} + + commander@8.3.0: {} + cookie@0.6.0: {} core-util-is@1.0.3: {} @@ -2330,6 +2365,8 @@ snapshots: dependencies: '@jridgewell/sourcemap-codec': 1.5.0 + memorystream@0.3.1: {} + mime-db@1.52.0: {} mime-types@2.1.35: @@ -2358,6 +2395,8 @@ snapshots: dependencies: whatwg-url: 5.0.0 + os-tmpdir@1.0.2: {} + package-json-from-dist@1.0.1: {} pako@1.0.11: {} @@ -2427,6 +2466,8 @@ snapshots: safe-buffer@5.1.2: {} + semver@5.7.2: {} + set-cookie-parser@2.7.1: {} set-function-length@1.2.2: @@ -2454,6 +2495,18 @@ snapshots: mrmime: 2.0.0 totalist: 3.0.1 + solc@0.8.28: + dependencies: + command-exists: 1.2.9 + commander: 8.3.0 + follow-redirects: 1.15.9 + js-sha3: 0.8.0 + memorystream: 0.3.1 + semver: 5.7.2 + tmp: 0.0.33 + transitivePeerDependencies: + - debug + source-map-js@1.2.1: {} string-width@4.2.3: @@ -2513,6 +2566,10 @@ snapshots: globalyzer: 0.1.0 globrex: 0.1.2 + tmp@0.0.33: + dependencies: + os-tmpdir: 1.0.2 + totalist@3.0.1: {} tr46@0.0.3: {} diff --git a/src/lib/models/solc.ts b/src/lib/models/solc.ts new file mode 100644 index 0000000..f63b98d --- /dev/null +++ b/src/lib/models/solc.ts @@ -0,0 +1,20 @@ +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: Record; + /** + * Settings for the compiler. + * e.g. { outputSelection: { '*': { '*': ['*'] } }" + */ + settings: { + outputSelection: Record>; + }; +}; + +export type ImportContents = Record; \ No newline at end of file diff --git a/src/lib/wizard/compiler.ts b/src/lib/wizard/compiler.ts new file mode 100644 index 0000000..ed0f5cc --- /dev/null +++ b/src/lib/wizard/compiler.ts @@ -0,0 +1,23 @@ +const solc = require('solc'); + +import type { ImportContents } from "$lib/models/solc"; +import type { CompilerInput } from "$lib/models/solc"; + +export class SolidityCompiler { + getContent(path: string, contents: Record) { + if (contents[path]) { + return contents[path]; + } + return { error: 'File not found' }; + } + + compile(input: CompilerInput, contents: ImportContents) { + const findImports = (path: string) => this.getContent(path, contents); + const output = solc.compile(JSON.stringify(input), { import: findImports }); + return output; + } +} + +export const Compiler = new SolidityCompiler(); + + From 39bceb83d1c544271d4d5d0cd1ec02a7fc416770 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 3 Dec 2024 09:29:03 -0300 Subject: [PATCH 2/7] feat: add support to solidity compiler --- src/app.d.ts | 5 +++++ src/lib/api.ts | 11 +++++++++++ src/lib/wizard/compiler.ts | 23 ----------------------- src/lib/wizard/index.ts | 4 +--- src/routes/compiler/+server.ts | 19 +++++++++++++++++++ src/routes/compiler/compiler.ts | 20 ++++++++++++++++++++ src/routes/wizard.svelte | 6 +++++- 7 files changed, 61 insertions(+), 27 deletions(-) delete mode 100644 src/lib/wizard/compiler.ts create mode 100644 src/routes/compiler/+server.ts create mode 100644 src/routes/compiler/compiler.ts diff --git a/src/app.d.ts b/src/app.d.ts index ad92508..f122aeb 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -13,3 +13,8 @@ declare global { declare interface Window { ethereum?: import('ethers').Eip1193Provider & import('ethers').BrowserProvider; } + +// solc does not have a type definition file. +declare module 'solc' { + export function compile(input: string, opts?: any): any; +} \ No newline at end of file diff --git a/src/lib/api.ts b/src/lib/api.ts index 38a29c4..26180f0 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,6 +1,7 @@ import type { CreateApprovalProcessRequest } from "./models/approval-process"; import type { Credentials } from "./models/auth"; import type { DeployContractRequest, UpdateDeploymentRequest } from "./models/deploy"; +import type { CompilerInput } from "./models/solc"; class ApiClient { credentials: Credentials | null = null; @@ -72,6 +73,16 @@ class ApiClient { return response.json(); } + + async compile(input: CompilerInput) { + const response = await fetch("/compiler", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ input }), + }); + + return response.json(); + } } export const API = new ApiClient(); diff --git a/src/lib/wizard/compiler.ts b/src/lib/wizard/compiler.ts deleted file mode 100644 index ed0f5cc..0000000 --- a/src/lib/wizard/compiler.ts +++ /dev/null @@ -1,23 +0,0 @@ -const solc = require('solc'); - -import type { ImportContents } from "$lib/models/solc"; -import type { CompilerInput } from "$lib/models/solc"; - -export class SolidityCompiler { - getContent(path: string, contents: Record) { - if (contents[path]) { - return contents[path]; - } - return { error: 'File not found' }; - } - - compile(input: CompilerInput, contents: ImportContents) { - const findImports = (path: string) => this.getContent(path, contents); - const output = solc.compile(JSON.stringify(input), { import: findImports }); - return output; - } -} - -export const Compiler = new SolidityCompiler(); - - diff --git a/src/lib/wizard/index.ts b/src/lib/wizard/index.ts index eba0485..53ea232 100644 --- a/src/lib/wizard/index.ts +++ b/src/lib/wizard/index.ts @@ -1,4 +1,2 @@ export const initWizardPlugin = () => { - // when users configure a contract, the plugin gets the results. - // listenToContracts(); -} +} \ No newline at end of file diff --git a/src/routes/compiler/+server.ts b/src/routes/compiler/+server.ts new file mode 100644 index 0000000..09a1d47 --- /dev/null +++ b/src/routes/compiler/+server.ts @@ -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 } }); +} \ No newline at end of file diff --git a/src/routes/compiler/compiler.ts b/src/routes/compiler/compiler.ts new file mode 100644 index 0000000..55cc516 --- /dev/null +++ b/src/routes/compiler/compiler.ts @@ -0,0 +1,20 @@ +import solc from 'solc'; + +import type { ImportContents } from "$lib/models/solc"; +import type { CompilerInput } from "$lib/models/solc"; + +export class SolidityCompiler { + getContent(path: string, contents: Record) { + if (contents[path]) { + return contents[path]; + } + return { error: 'File not found' }; + } + + compile(input: CompilerInput, contents?: ImportContents) { + 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; + } +} diff --git a/src/routes/wizard.svelte b/src/routes/wizard.svelte index 105abb4..01cc9b5 100644 --- a/src/routes/wizard.svelte +++ b/src/routes/wizard.svelte @@ -1,4 +1,8 @@ -

Defender Deploy in Wizard

\ No newline at end of file +

Defender Deploy in Wizard

From ecd01cccff0bf99daf43c947c5a0b681d9d2d848 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 3 Dec 2024 09:32:10 -0300 Subject: [PATCH 3/7] fmt --- src/lib/models/solc.ts | 2 +- src/lib/wizard/index.ts | 2 +- src/routes/compiler/+server.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/models/solc.ts b/src/lib/models/solc.ts index f63b98d..d7a3c25 100644 --- a/src/lib/models/solc.ts +++ b/src/lib/models/solc.ts @@ -17,4 +17,4 @@ export type CompilerInput = { }; }; -export type ImportContents = Record; \ No newline at end of file +export type ImportContents = Record; diff --git a/src/lib/wizard/index.ts b/src/lib/wizard/index.ts index 53ea232..01cde03 100644 --- a/src/lib/wizard/index.ts +++ b/src/lib/wizard/index.ts @@ -1,2 +1,2 @@ export const initWizardPlugin = () => { -} \ No newline at end of file +} diff --git a/src/routes/compiler/+server.ts b/src/routes/compiler/+server.ts index 09a1d47..fcb0260 100644 --- a/src/routes/compiler/+server.ts +++ b/src/routes/compiler/+server.ts @@ -16,4 +16,4 @@ export async function POST({ request }: { request: Request }) { } return json({ success: true, data: { output } }); -} \ No newline at end of file +} From 9ce364f150b5853d54fb91e5a13186129a279ba5 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 3 Dec 2024 09:34:17 -0300 Subject: [PATCH 4/7] fmt --- src/app.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app.d.ts b/src/app.d.ts index f122aeb..e2c24df 100644 --- a/src/app.d.ts +++ b/src/app.d.ts @@ -17,4 +17,4 @@ declare interface Window { // solc does not have a type definition file. declare module 'solc' { export function compile(input: string, opts?: any): any; -} \ No newline at end of file +} From 847d7e96431540a63f41b4c31207ddb84457ebf3 Mon Sep 17 00:00:00 2001 From: Marcos Date: Tue, 3 Dec 2024 15:38:49 -0300 Subject: [PATCH 5/7] feat: listen to wizard changes --- src/lib/wizard/components/Configure.svelte | 9 +++ src/lib/wizard/index.ts | 13 +++- src/lib/wizard/models/contract.ts | 76 ++++++++++++++++++++++ src/lib/wizard/state.svelte.ts | 6 ++ src/routes/+page.svelte | 15 ++--- src/routes/remix.svelte | 4 ++ src/routes/wizard.svelte | 7 +- 7 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 src/lib/wizard/components/Configure.svelte create mode 100644 src/lib/wizard/models/contract.ts create mode 100644 src/lib/wizard/state.svelte.ts diff --git a/src/lib/wizard/components/Configure.svelte b/src/lib/wizard/components/Configure.svelte new file mode 100644 index 0000000..b62fc13 --- /dev/null +++ b/src/lib/wizard/components/Configure.svelte @@ -0,0 +1,9 @@ + + + +

+ Contract: {JSON.stringify(wizardState.contract, null, 2)} +

diff --git a/src/lib/wizard/index.ts b/src/lib/wizard/index.ts index eba0485..cf783fc 100644 --- a/src/lib/wizard/index.ts +++ b/src/lib/wizard/index.ts @@ -1,4 +1,15 @@ +import type { DefenderDeployMessage } from "./models/contract"; +import { globalState } from "./state.svelte"; + export const initWizardPlugin = () => { // when users configure a contract, the plugin gets the results. - // listenToContracts(); + listenToContracts(); +} + +function listenToContracts() { + window.addEventListener('message', function (e: MessageEvent) { + if (e.data.kind === 'oz-wizard-defender-deploy') { + globalState.contract = e.data.contract; + } + }); } diff --git a/src/lib/wizard/models/contract.ts b/src/lib/wizard/models/contract.ts new file mode 100644 index 0000000..bf524fc --- /dev/null +++ b/src/lib/wizard/models/contract.ts @@ -0,0 +1,76 @@ +/** + * Interfaces for the contract object returned by the wizard. + */ + +export interface Contract { + name: string; + license: string; + parents: Parent[]; + natspecTags: NatspecTag[]; + imports: ImportContract[]; + functions: ContractFunction[]; + constructorCode: string[]; + constructorArgs: FunctionArgument[]; + variables: string[]; + upgradeable: boolean; +} + +export interface NatspecTag { + key: string; + value: string; +} + +export type Value = string | number | { lit: string } | { note: string, value: Value }; + +export interface Parent { + contract: ImportContract; + params: Value[]; + importOnly?: boolean; +} + +export interface ImportContract extends ReferencedContract { + path: string; +} + +export interface ReferencedContract { + name: string; + transpiled?: boolean; +} + +export interface Using { + library: ImportContract; + usingFor: string; +} + +export interface BaseFunction { + name: string; + args: FunctionArgument[]; + returns?: string[]; + kind: FunctionKind; + mutability?: FunctionMutability; +} + +export interface ContractFunction extends BaseFunction { + override: Set; + modifiers: string[]; + code: string[]; + mutability: FunctionMutability; + final: boolean; + comments: string[]; +} + +export type FunctionKind = 'internal' | 'public'; +export type FunctionMutability = typeof mutabilityRank[number]; + +// Order is important +const mutabilityRank = ['pure', 'view', 'nonpayable', 'payable'] as const; + +export interface FunctionArgument { + type: string | ReferencedContract; + name: string; +} + +export interface DefenderDeployMessage { + kind: 'oz-wizard-defender-deploy'; + contract: Contract; +} diff --git a/src/lib/wizard/state.svelte.ts b/src/lib/wizard/state.svelte.ts new file mode 100644 index 0000000..3c60954 --- /dev/null +++ b/src/lib/wizard/state.svelte.ts @@ -0,0 +1,6 @@ + +import type { Contract } from "./models/contract"; + +export const wizardState = $state<{ contract: Contract | undefined }>({ + contract: undefined, +}); diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index 51c8760..9703914 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -20,20 +20,17 @@ // assumes that when in dev mode and // the ancestor origin is localhost, we are in the wizard if (dev && ancestorOrigin.includes("localhost")) { - parent = 'wizard'; - return; + return parent = 'wizard'; } - if (ancestorOrigin.includes("remix.ethereum")) { - parent = 'remix'; - return initRemixPlugin(); + if (ancestorOrigin.includes("wizard.openzeppelin")) { + return parent = 'wizard'; } - if (ancestorOrigin.includes("wizard.openzeppelin")) { - parent = 'wizard'; - // TODO: init wizard plugin - return; + if (ancestorOrigin.includes("remix.ethereum")) { + return parent = 'remix'; } + }); diff --git a/src/routes/remix.svelte b/src/routes/remix.svelte index a36362f..6e704fc 100644 --- a/src/routes/remix.svelte +++ b/src/routes/remix.svelte @@ -1,4 +1,6 @@

diff --git a/src/routes/wizard.svelte b/src/routes/wizard.svelte index 105abb4..d6faccd 100644 --- a/src/routes/wizard.svelte +++ b/src/routes/wizard.svelte @@ -1,4 +1,9 @@ -

Defender Deploy in Wizard

\ No newline at end of file + From 1505a17d0ba1dcdb2d6feb60aff497eb3867a281 Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 4 Dec 2024 10:15:11 -0300 Subject: [PATCH 6/7] change contract sources interface --- src/lib/api.ts | 1 + src/lib/wizard/components/Configure.svelte | 14 +++- src/lib/wizard/index.ts | 11 +++- src/lib/wizard/models/contract.ts | 76 ---------------------- src/lib/wizard/models/sources.ts | 12 ++++ src/lib/wizard/state.svelte.ts | 6 +- 6 files changed, 36 insertions(+), 84 deletions(-) delete mode 100644 src/lib/wizard/models/contract.ts create mode 100644 src/lib/wizard/models/sources.ts diff --git a/src/lib/api.ts b/src/lib/api.ts index 38a29c4..0ccf40a 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,6 +1,7 @@ import type { CreateApprovalProcessRequest } from "./models/approval-process"; import type { Credentials } from "./models/auth"; import type { DeployContractRequest, UpdateDeploymentRequest } from "./models/deploy"; +import type { ContractSources } from "./wizard/models/sources"; class ApiClient { credentials: Credentials | null = null; diff --git a/src/lib/wizard/components/Configure.svelte b/src/lib/wizard/components/Configure.svelte index b62fc13..97015bc 100644 --- a/src/lib/wizard/components/Configure.svelte +++ b/src/lib/wizard/components/Configure.svelte @@ -1,9 +1,19 @@ + async function compile() { + // if (!wizardState.sources) return; + // const result = await API.compile(wizardState.sources); + // console.log(result); + } +

- Contract: {JSON.stringify(wizardState.contract, null, 2)} + Contract to compile: {wizardState.sources?.target} + +

diff --git a/src/lib/wizard/index.ts b/src/lib/wizard/index.ts index cf783fc..c39e2da 100644 --- a/src/lib/wizard/index.ts +++ b/src/lib/wizard/index.ts @@ -1,5 +1,10 @@ -import type { DefenderDeployMessage } from "./models/contract"; -import { globalState } from "./state.svelte"; +import type { ContractSources } from "./models/sources"; +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. @@ -9,7 +14,7 @@ export const initWizardPlugin = () => { function listenToContracts() { window.addEventListener('message', function (e: MessageEvent) { if (e.data.kind === 'oz-wizard-defender-deploy') { - globalState.contract = e.data.contract; + wizardState.sources = e.data.sources; } }); } diff --git a/src/lib/wizard/models/contract.ts b/src/lib/wizard/models/contract.ts deleted file mode 100644 index bf524fc..0000000 --- a/src/lib/wizard/models/contract.ts +++ /dev/null @@ -1,76 +0,0 @@ -/** - * Interfaces for the contract object returned by the wizard. - */ - -export interface Contract { - name: string; - license: string; - parents: Parent[]; - natspecTags: NatspecTag[]; - imports: ImportContract[]; - functions: ContractFunction[]; - constructorCode: string[]; - constructorArgs: FunctionArgument[]; - variables: string[]; - upgradeable: boolean; -} - -export interface NatspecTag { - key: string; - value: string; -} - -export type Value = string | number | { lit: string } | { note: string, value: Value }; - -export interface Parent { - contract: ImportContract; - params: Value[]; - importOnly?: boolean; -} - -export interface ImportContract extends ReferencedContract { - path: string; -} - -export interface ReferencedContract { - name: string; - transpiled?: boolean; -} - -export interface Using { - library: ImportContract; - usingFor: string; -} - -export interface BaseFunction { - name: string; - args: FunctionArgument[]; - returns?: string[]; - kind: FunctionKind; - mutability?: FunctionMutability; -} - -export interface ContractFunction extends BaseFunction { - override: Set; - modifiers: string[]; - code: string[]; - mutability: FunctionMutability; - final: boolean; - comments: string[]; -} - -export type FunctionKind = 'internal' | 'public'; -export type FunctionMutability = typeof mutabilityRank[number]; - -// Order is important -const mutabilityRank = ['pure', 'view', 'nonpayable', 'payable'] as const; - -export interface FunctionArgument { - type: string | ReferencedContract; - name: string; -} - -export interface DefenderDeployMessage { - kind: 'oz-wizard-defender-deploy'; - contract: Contract; -} diff --git a/src/lib/wizard/models/sources.ts b/src/lib/wizard/models/sources.ts new file mode 100644 index 0000000..adde68f --- /dev/null +++ b/src/lib/wizard/models/sources.ts @@ -0,0 +1,12 @@ + + +export interface ContractSources { + target: string; + sources: SolcInputSources; +} + +export interface SolcInputSources { + [source: string]: { + content: string; + }; +} \ No newline at end of file diff --git a/src/lib/wizard/state.svelte.ts b/src/lib/wizard/state.svelte.ts index 3c60954..d5c9a40 100644 --- a/src/lib/wizard/state.svelte.ts +++ b/src/lib/wizard/state.svelte.ts @@ -1,6 +1,6 @@ -import type { Contract } from "./models/contract"; +import type { ContractSources } from "./models/sources"; -export const wizardState = $state<{ contract: Contract | undefined }>({ - contract: undefined, +export const wizardState = $state<{ sources: ContractSources | undefined }>({ + sources: undefined, }); From 492c6703dc26d8c59f70389e13786fc730c067da Mon Sep 17 00:00:00 2001 From: Marcos Date: Wed, 4 Dec 2024 11:58:32 -0300 Subject: [PATCH 7/7] fix: load contract to compile --- src/lib/wizard/components/Configure.svelte | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/lib/wizard/components/Configure.svelte b/src/lib/wizard/components/Configure.svelte index 5557dfb..e9eb6b9 100644 --- a/src/lib/wizard/components/Configure.svelte +++ b/src/lib/wizard/components/Configure.svelte @@ -1,24 +1,27 @@

- Contract to compile: {wizardState.sources?.target} + Contract to compile: {getMainContractName(wizardState.sources)} - - result: {JSON.stringify(result, null, 2)}