Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: paritytech/js-revive
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.0.7
Choose a base ref
...
head repository: paritytech/js-revive
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
  • 11 commits
  • 13 files changed
  • 1 contributor

Commits on Dec 20, 2024

  1. Add an option to compile with the wasm revive compiler

    pgherveou committed Dec 20, 2024
    Copy the full SHA
    8b3f2e7 View commit details
  2. dry it up

    pgherveou committed Dec 20, 2024
    Copy the full SHA
    cdc14cc View commit details
  3. rm comments

    pgherveou committed Dec 20, 2024
    Copy the full SHA
    7b061c7 View commit details
  4. fix lint

    pgherveou committed Dec 20, 2024
    Copy the full SHA
    a4f05ec View commit details
  5. Merge pull request #6 from paritytech/pg/wasm

    Add an option to compile with the wasm revive compiler
    pgherveou authored Dec 20, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    1aa20d9 View commit details

Commits on Jan 7, 2025

  1. Use wasm in main example

    pgherveou committed Jan 7, 2025
    Copy the full SHA
    eab3f07 View commit details

Commits on Jan 8, 2025

  1. Add option to build with binary

    pgherveou committed Jan 8, 2025
    Copy the full SHA
    0cc6c9f View commit details

Commits on Feb 13, 2025

  1. Auto update wasm with each release (#7)

    * update wasm
    
    * ignore resolc.wasm,js
    
    * fix
    
    * update
    
    * fix
    
    * use latest
    
    * update script
    
    * fix test
    pgherveou authored Feb 13, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    ce3e187 View commit details
  2. Update npm-release.yml

    pgherveou authored Feb 13, 2025

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    624c9f1 View commit details

Commits on Feb 18, 2025

  1. Fix script should not be a postinstall

    pgherveou committed Feb 18, 2025
    Copy the full SHA
    26e3c04 View commit details
  2. rename step

    pgherveou committed Feb 18, 2025
    Copy the full SHA
    5b9482d View commit details
Showing with 1,141 additions and 68 deletions.
  1. +3 −0 .github/workflows/checks.yml
  2. +4 −1 .github/workflows/npm-release.yml
  3. +2 −0 .gitignore
  4. +8 −9 eslint.config.mjs
  5. +3 −2 package.json
  6. +50 −0 scripts/update-resolc.js
  7. +27 −10 src/index.test.ts
  8. +50 −30 src/index.ts
  9. +0 −16 src/main.ts
  10. +11 −0 src/resolc.ts
  11. 0 src/resolc/.gitkeep
  12. +2 −0 src/solc.d.ts
  13. +981 −0 yarn.lock
3 changes: 3 additions & 0 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
@@ -29,6 +29,9 @@ jobs:
restore-keys: |
${{ runner.os }}-yarn-
- name: update-resolc
run: yarn update-resolc

- name: Install
run: yarn install --immutable

5 changes: 4 additions & 1 deletion .github/workflows/npm-release.yml
Original file line number Diff line number Diff line change
@@ -23,6 +23,9 @@ jobs:
- name: Install
run: yarn install --immutable

- name: update-resolc
run: yarn update-resolc

- name: Build
run: yarn build

@@ -37,7 +40,7 @@ jobs:
name: package
path: 'parity-revive-*.tgz'

- uses: octokit/request-action@v2.3.0
- uses: octokit/request-action@bbedc70b1981e610d89f1f8de88311a1fc02fb83
with:
route: POST /repos/paritytech/npm_publish_automation/actions/workflows/publish.yml/dispatches
ref: main
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
src/resolc/resolc.js
src/resolc/resolc.wasm

# Logs

17 changes: 8 additions & 9 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";

import globals from 'globals'
import pluginJs from '@eslint/js'
import tseslint from 'typescript-eslint'

/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["**/*.{js,mjs,cjs,ts}"]},
{languageOptions: { globals: globals.browser }},
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];
{ files: ['**/*.{mjs,ts}'] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
]
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -17,8 +17,9 @@
"dist"
],
"scripts": {
"build": "tsc",
"lint": "npx eslint 'src/**/*.{cjs,js,ts}' && npx prettier --check '**/*.{ts,json,yml,md}'",
"update-resolc": "node ./scripts/update-resolc.js",
"build": "tsc && cp src/resolc/** dist/resolc",
"lint": "npx eslint 'src/**/*.{cjs,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"
},
50 changes: 50 additions & 0 deletions scripts/update-resolc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require('fs')

async function main() {
const headers = { 'content-type': 'application/json' }

let asset_url = ''
try {
const response = await fetch(
'https://api.github.com/repos/paritytech/revive/releases/latest',
{ headers }
)

const json = await response.json()
console.log(`Using release ${json.name}`)

asset_url = json.assets_url
} catch (error) {
console.error(`Failed to get asset_url: ${error}`)
process.exit(1)
}

let assets = []
try {
const response = await fetch(asset_url)
assets = await response.json()
} catch (error) {
console.error(`Failed to get download_url: ${error}`)
process.exit(1)
}

try {
for (const assetName of ['resolc.wasm', 'resolc.js']) {
const asset = assets.find((asset) => asset.name == assetName)
const response = await fetch(asset.browser_download_url, {
headers: {
'content-type': 'application/octet-stream',
},
})
const data = await response.arrayBuffer()

fs.writeFileSync(`src/resolc/${assetName}`, Buffer.from(data))
console.log(`src/resolc/${assetName} updated`)
}
} catch (error) {
console.error(`Failed to download wasm: ${error}`)
process.exit(1)
}
}

main()
37 changes: 27 additions & 10 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -3,19 +3,36 @@ import { readFileSync } from 'node:fs'
import assert from 'node:assert'
import { compile, tryResolveImport } from '.'
import { resolve } from 'node:path'
import { execSync } from 'node:child_process'

test('check Ok output', async () => {
const contract = 'contracts/1_Storage.sol'
const sources = {
[contract]: {
content: readFileSync('fixtures/storage.sol', 'utf8'),
},
function isResolcInPath() {
try {
execSync('resolc --version', { stdio: 'ignore' })
return true
} catch {
return false
}
}

const out = await compile(sources)
assert(out.contracts[contract].Storage.abi != null)
assert(out.contracts[contract].Storage.evm.bytecode != null)
})
const compileOptions = [{}]
if (isResolcInPath()) {
compileOptions.push({ bin: 'resolc' })
}

for (const options of compileOptions) {
test(`check Ok output with option ${JSON.stringify(options)}`, async () => {
const contract = 'fixtures/token.sol'
const sources = {
[contract]: {
content: readFileSync('fixtures/storage.sol', 'utf8'),
},
}

const out = await compile(sources, options)
assert(out.contracts[contract].Storage.abi != null)
assert(out.contracts[contract].Storage.evm.bytecode != null)
})
}

test('check Err output', async () => {
const contract = 'contracts/1_Storage.sol'
80 changes: 50 additions & 30 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import solc from 'solc'
import { spawn } from 'child_process'
import { resolc } from './resolc'
import path from 'path'
import { existsSync, readFileSync } from 'fs'

@@ -22,7 +24,7 @@ type SolcError = {
type: string
}

type SolcOutput = {
export type SolcOutput = {
contracts: {
[contractPath: string]: {
[contractName: string]: {
@@ -84,42 +86,28 @@ export function resolveInputs(sources: SolcInput): SolcInput {
)
}

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: true, runs: 200 },
outputSelection: {
'*': {
'*': ['abi'],
},
export async function compile(
sources: SolcInput,
option: { bin?: string } = {}
): Promise<SolcOutput> {
const input = JSON.stringify({
language: 'Solidity',
sources: resolveInputs(sources),
settings: {
optimizer: { enabled: true, runs: 200 },
outputSelection: {
'*': {
'*': ['abi'],
},
},
}),
}

const BACKENDROOT_URL =
process.env.REMIX_BACKEND ?? 'https://remix-backend.polkadot.io'
const response = await fetch(`${BACKENDROOT_URL}/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}`)
if (option.bin) {
return compileWithBin(input, option.bin)
}

return (await response.json()) as SolcOutput
return resolc(input)
}

/**
@@ -175,3 +163,35 @@ export function tryResolveImport(importPath: string) {
throw new Error(`Resolved path ${resolvedPath} does not exist.`)
}
}
function compileWithBin(input: string, bin: string): PromiseLike<SolcOutput> {
return new Promise((resolve, reject) => {
const process = spawn(bin, ['--standard-json'])

let output = ''
let error = ''

process.stdin.write(input)
process.stdin.end()

process.stdout.on('data', (data) => {
output += data.toString()
})

process.stderr.on('data', (data) => {
error += data.toString()
})

process.on('close', (code) => {
if (code === 0) {
try {
const result: SolcOutput = JSON.parse(output)
resolve(result)
} catch {
reject(new Error(`Failed to parse output`))
}
} else {
reject(new Error(`Process exited with code ${code}: ${error}`))
}
})
})
}
16 changes: 0 additions & 16 deletions src/main.ts

This file was deleted.

11 changes: 11 additions & 0 deletions src/resolc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import soljson from 'solc/soljson'
import Resolc from './resolc/resolc'
import type { SolcOutput } from '.'

export function resolc(input: string): SolcOutput {
const m = Resolc() as any // eslint-disable-line @typescript-eslint/no-explicit-any
m.soljson = soljson
m.writeToStdin(input)
m.callMain(['--standard-json'])
return JSON.parse(m.readFromStdout()) as SolcOutput
}
Empty file added src/resolc/.gitkeep
Empty file.
2 changes: 2 additions & 0 deletions src/solc.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare module 'solc/soljson' {}

declare module 'solc' {
// Basic types for input/output handling
export interface CompileInput {
Loading