Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add revive-node version #3

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import tseslint from "typescript-eslint";
/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{ignores: ["**/resolc.js"]},
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
Expand Down
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
"dist"
],
"scripts": {
"build": "tsc",
"build": "tsc && cp src/resolc.wasm dist/ && cp src/resolc.js dist/",
"lint": "npx eslint 'src/**/*.{cjs,js,ts}' && npx prettier --check '**/*.{ts,json,yml,md}'",
"lint:fix": "npx prettier --write '**/*.{ts,json,yml,md}'",
"test": "npm run build && node ./dist/index.test.js"
"test": "npm run build && node ./dist/index.test.js",
"download:revive": "echo 'TODO: Download Revive release from github'"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about downloading the resolc.{js,wasm} and worker.js files from the Github Revive release instead of creating another one npm package with Revive compiler.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should end up having both? I.e. a JS release and an NPM package

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for the browser JS release, the point is that I don't see the reason to have two separate NPM packages — one for the output of the revive compilation and the other for the NPM js-revive package.

},
"devDependencies": {
"@eslint/js": "^9.14.0",
Expand Down
53 changes: 30 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import solc from 'solc'
import soljson from 'solc/soljson'
import path from 'path'
import createRevive from './resolc.js'
import { existsSync, readFileSync } from 'fs'

type SolcInput = {
Expand Down Expand Up @@ -88,36 +90,41 @@ export async function compile(sources: SolcInput): Promise<SolcOutput> {
// compile with solc to resolve all the imports
sources = resolveInputs(sources)

const body = {
cmd: '--standard-json',
input: JSON.stringify({
language: 'Solidity',
sources,
settings: {
optimizer: { enabled: false, runs: 200 },
outputSelection: {
'*': {
'*': ['abi'],
},
const input = JSON.stringify({
language: 'Solidity',
sources,
settings: {
optimizer: { enabled: false, runs: 200 },
outputSelection: {
'*': {
'*': ['abi'],
},
},
}),
}

const response = await fetch('https://remix-backend.polkadot.io/resolc', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
})

if (!response.ok) {
const text = await response.text().catch(() => '')
throw new Error(`${response.statusText}: ${text}`)
const revive = await createRevive()
revive.soljson = soljson
revive.setStdinData(input)

let stdout = ''
revive.setStdoutCallback(function (char: string) {
stdout += char
})

let stderr = ''
revive.setStderrCallback(function (char: string) {
stderr += char
})

// Compile the Solidity source code
const result = revive.callMain(['--standard-json'])

if (result) {
throw new Error(stderr)
}

return (await response.json()) as SolcOutput
return JSON.parse(stdout) as SolcOutput
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/resolc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export = createRevive
declare function createRevive(moduleArg?)
19 changes: 19 additions & 0 deletions src/resolc.js

Large diffs are not rendered by default.

Binary file added src/resolc.wasm
Binary file not shown.
4 changes: 4 additions & 0 deletions src/solc.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,7 @@ declare module 'solc' {
options?: { import: (path: string) => { contents: string } }
): string
}
declare module 'solc/soljson' {
const soljson: object
export default soljson
}