Skip to content

Instantly share code, notes, and snippets.

@marcocastignoli
Last active April 14, 2025 15:49
Show Gist options
  • Select an option

  • Save marcocastignoli/f4baeb84636cd302d95e52e4d4a49550 to your computer and use it in GitHub Desktop.

Select an option

Save marcocastignoli/f4baeb84636cd302d95e52e4d4a49550 to your computer and use it in GitHub Desktop.
Sourcify in browser
import { fetchSolc } from "web-solc";
import "./App.css";
import {
ISolidityCompiler,
SolidityCompilation,
SolidityJsonInput,
} from "@ethereum-sourcify/lib-sourcify";
async function getCompilation() {
const sources = {
"project:/contracts/Storage.sol": {
content: `// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Storage
* @dev Store & retrieve value in a variable
*/
contract Storage {
uint256 number;
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
number = num;
}
/**
* @dev Return value
* @return value of 'number'
*/
function retrieve() public view returns (uint256){
return number;
}
}
`,
},
};
class Solc implements ISolidityCompiler {
async compile(
version: string,
solcJsonInput: SolidityJsonInput
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): Promise<any> {
const { compile } = await fetchSolc(version);
return await compile(solcJsonInput);
}
}
const solc = new Solc();
const compilation = new SolidityCompilation(
solc,
"0.8.4+commit.c7e474f2",
{
language: "Solidity",
sources,
settings: {
evmVersion: "istanbul",
libraries: {},
metadata: { bytecodeHash: "ipfs" },
optimizer: { enabled: false, runs: 200 },
remappings: [],
outputSelection: {
"*": ["abi", "evm.bytecode", "evm.deployedBytecode"],
},
},
},
{
path: "project:/contracts/Storage.sol",
name: "Storage",
}
);
await compilation.compile();
console.log(compilation.runtimeBytecode);
}
function App() {
return (
<>
<button onClick={() => getCompilation()}>Get Compilation</button>
</>
);
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment