solc-js
solc-js copied to clipboard
findImports fallback on webWorkers not working
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.
You're wrapping the solidity_compile
C function so your callback has to be second argument:
compile(data.input, getImport)
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.
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);
Finally got it to work! Thank you - turns out my real struggle was with the webpack stuff.
Closing this as the issue seems to have been resolved as confirmed by @ashwwwin.