solc-js icon indicating copy to clipboard operation
solc-js copied to clipboard

findImports fallback on webWorkers not working

Open ashwwwin opened this issue 1 year ago • 4 comments

Hey, I'm to trying to use the import fallback with web workers as follows but can't get it to work (the console.log doesn't get called either).

It compiles without issue if there are no imports or if compiling multiple contracts, I am using this method in order to get the main contract and imported contracts into a single compiled abi + bytecode.

My findImports function:

function getImport(path: any) {
      console.log('getImport called for', path);
      return {
        contents:
          "library L { function f() internal returns (uint) { return 7; } }",
      };
    }

Compilation:

 const compile = soljson.cwrap("solidity_compile", "string", [
        "string",
        "number",
        "function",
      ]);

const output = JSON.parse(compile(data.input, { import: getImport }));

Any leads would be much appreciated.

ashwwwin avatar Dec 19 '23 12:12 ashwwwin

You're wrapping the solidity_compile C function so your callback has to be second argument: compile(data.input, getImport)

dreadheadsic avatar Jan 04 '24 22:01 dreadheadsic

You're wrapping the solidity_compile C function so your callback has to be second argument: compile(data.input, getImport)

@dreadheadsic, thank you for the response - I tried this and it did not work.

It does seem like the C function, does not support compilations with imports - haven't been able to get it work. This has been a huge pain point, if you have any leads would appreciate.

ashwwwin avatar Jan 17 '24 02:01 ashwwwin

Use 'solc/wrapper' to wrap your worker

import wrapper from 'solc/wrapper';

self.importScripts(`https://binaries.soliditylang.org/wasm/${version}`);

const compiler = wrap(self);

then you can do

const result = await compiler.compile(input, {
    import: getImports,
});
const data = JSON.parse(result);

dreadheadsic avatar Jan 25 '24 16:01 dreadheadsic

Finally got it to work! Thank you - turns out my real struggle was with the webpack stuff.

ashwwwin avatar Jan 27 '24 00:01 ashwwwin

Closing this as the issue seems to have been resolved as confirmed by @ashwwwin.

mehtavishwa30 avatar Apr 17 '24 08:04 mehtavishwa30