Skip to content

Commit

Permalink
feat(router): CAN-596 add web-solc to compile on browser (#1481)
Browse files Browse the repository at this point in the history
  • Loading branch information
mjlescano authored Oct 21, 2024
1 parent e572934 commit 1b1c886
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"lint:fix:js": "prettier --write '{packages,examples}/**/*.{js,ts}' && eslint --fix '{packages,examples}/**/*.{js,ts}'",
"lint:fix:sol": "prettier --write '{packages,examples}/*/{contracts,src}/**/*.sol'",
"lint:fix": "pnpm run '/^lint:fix:(js|sol)/'",
"build": "pnpm -r --if-present run clean && pnpm -r --filter @usecannon/builder --filter @usecannon/cli --filter hardhat-cannon --filter @usecannon/api run build",
"build": "pnpm -r --if-present --filter @usecannon/builder --filter @usecannon/cli --filter hardhat-cannon --filter @usecannon/api run clean && pnpm -r --filter @usecannon/builder --filter @usecannon/cli --filter hardhat-cannon --filter @usecannon/api run build",
"watch": "pnpm -r --parallel --filter @usecannon/builder --filter @usecannon/cli run watch",
"version-alpha": "lerna version prerelease --no-private",
"version-patch": "lerna version patch --no-private",
Expand Down
3 changes: 2 additions & 1 deletion packages/builder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"typedoc-plugin-zod": "^1.2.0"
},
"dependencies": {
"@synthetixio/router": "^3.4.0",
"@usecannon/router": "^4.0.0",
"@usecannon/web-solc": "0.5.1",
"axios": "^1.7.2",
"axios-retry": "^4.4.2",
"buffer": "^6.0.3",
Expand Down
16 changes: 7 additions & 9 deletions packages/builder/src/steps/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Debug from 'debug';
import _ from 'lodash';
import * as viem from 'viem';
import { z } from 'zod';
import { generateRouter } from '@usecannon/router';
import { computeTemplateAccesses, mergeTemplateAccesses } from '../access-recorder';
import { ChainBuilderRuntime } from '../runtime';
import { routerSchema } from '../schemas';
Expand All @@ -13,6 +14,7 @@ import {
getMergedAbiFromContractPaths,
} from '../util';
import { template } from '../utils/template';
import { compileContract } from '../utils/compile';

const debug = Debug('cannon:builder:router');

Expand Down Expand Up @@ -101,8 +103,6 @@ const routerStep = {
config: Config,
packageState: PackageState
): Promise<ChainArtifacts> {
const { generateRouter, getCompileInput, compileContract } = await import('@synthetixio/router');

debug('exec', config);

const contracts = config.contracts.map((n) => {
Expand All @@ -111,16 +111,18 @@ const routerStep = {
throw new Error(`contract not found: ${n}`);
}

const contractName = n.replace('.', '_'); // Use step name, and replace '.' in case is pointing to an import.

return {
constructorArgs: contract.constructorArgs,
abi: contract.abi,
deployedAddress: contract.address ? viem.getAddress(contract.address) : contract.address, // Make sure address is checksum encoded
deployTxnHash: contract.deployTxnHash,
deployTxnBlockNumber: '',
deployTimestamp: '',
contractName: contract.contractName,
contractName: contractName,
sourceName: contract.sourceName,
contractFullyQualifiedName: `${contract.sourceName}:${contract.contractName}`,
contractFullyQualifiedName: `${contract.sourceName}:${contractName}`,
};
});

Expand All @@ -134,11 +136,7 @@ const routerStep = {

debug('router source code', sourceCode);

// On Mainnet, use default local solc evmVersion, for everything else, 'paris'
const evmVersion = [1, 5, 11155111].includes(runtime.chainId) ? undefined : 'paris';

const inputData = getCompileInput(contractName, sourceCode, evmVersion);
const solidityInfo = await compileContract(contractName, sourceCode, evmVersion);
const { input: inputData, output: solidityInfo } = await compileContract(contractName, sourceCode);

// the ABI is entirely based on the fallback call so we have to generate ABI here
const routableAbi = getMergedAbiFromContractPaths(ctx, config.contracts);
Expand Down
81 changes: 81 additions & 0 deletions packages/builder/src/utils/compile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import * as viem from 'viem';
import { fetchSolc } from '@usecannon/web-solc';

interface CompileResult {
sourceName: string;
contractName: string;
abi: viem.Abi;
metadata: string;
solcVersion: string;
assembly: string;
bytecode: string;
deployedBytecode: string;
gasEstimates: {
creation: {
codeDepositCost: string;
executionCost: string;
totalCost: string;
};
external: {
'': string;
};
};
}

export async function compileContract(
contractName: string,
sourceCode: string,
evmVersion = 'paris',
compilerVersion = '0.8.27'
) {
const { compile, stopWorker } = await fetchSolc(compilerVersion);

const sourceName = `${contractName}.sol`;

const input = {
language: 'Solidity',
sources: {
[sourceName]: {
content: sourceCode,
},
},
settings: {
outputSelection: {
'*': { '*': ['*'] },
},
evmVersion,
},
};

try {
const output = await compile(input);

if (output.errors) {
throw new Error(
[
`There was an error when compiling "${contractName}".`,
...output.errors.map((err: { message: string }) => err.message),
].join(' ')
);
}

const info = (output.contracts as any)[sourceName][contractName];
const metadata = JSON.parse(info.metadata);

const result = {
contractName,
sourceName,
abi: info.abi,
metadata: info.metadata,
solcVersion: metadata.compiler.version,
assembly: info.evm.assembly,
bytecode: info.evm.bytecode.object,
deployedBytecode: info.evm.deployedBytecode.object,
gasEstimates: info.evm.gasEstimates,
} satisfies CompileResult;

return { input, output: result };
} finally {
stopWorker();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ export default function QueueFromGitOps() {
!prevDeployLocation &&
tomlRequiresPrevPackage &&
!previousPackageInput) ||
canTomlBeDeployedUsingWebsite;
!canTomlBeDeployedUsingWebsite;

const PreviewButton = ({ message }: { message?: string }) => (
<Tooltip label={message}>
Expand Down
60 changes: 41 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1b1c886

Please sign in to comment.