ethereum/sourcify

"packagizing" the `useCompiler`

Opened this issue · 1 comments

We use a function useCompiler in various places to compile contracts. This function provides a small wrapper around the solc compiler in order to download it and invoke the compilation.

The code was duplicated across multiple places, namely in the tests of lib-sourcify, in serverless/compiler-lambda and in the server code. In order to minimize the effort for maintenance and synchronization of these code parts, they should be moved into a public package. Basically the content of services/server/src/server/services/compiler/local/solidityCompiler.ts should be the content of the new package.

Tasks

My proposal is to build this package with the idea of shipping multiple compilers in mind. For example doing something like this: https://github.com/lodash/lodash/tree/4.5.0-npm-packages. (We can actually do the same thing just by having multiple compilers under packages/)

The final objective should be being able to do something like this in nodejs

import { CheckedContract } from "@ethereum-sourcify/lib-sourcify"
import {initCompiler} from "@ethereum-sourcify/compilers/solidity/nodejs"

const compiler = initCompiler(...)
const contract = new CheckedContract(compiler, metadata, sources);

And something like this in the browser

import { CheckedContract } from "@ethereum-sourcify/lib-sourcify"
import {initCompiler} from "@ethereum-sourcify/compilers/solidity/browser"

const compiler = initCompiler(...)

const contract = new CheckedContract(compiler, metadata, sources);

And something like this in the lambda function

import {useCompiler} from "@ethereum-sourcify/compilers/solidity/nodejs"

exports.handler = async (event) => {
    return {
      success: true,
      body: await useCompiler(
        event.version,
        event.solcJsonInput,
        event.forceEmscripten
      ),
    }
};