From aa3c984c3b20d3a8b2663bdd4a159347e8379894 Mon Sep 17 00:00:00 2001 From: Dhruv Srivastava Date: Tue, 4 Apr 2023 11:45:46 +0530 Subject: [PATCH] Use solc version defined in config --- packages/codegen/src/artifacts.ts | 22 ++++++++++++++++++++-- packages/codegen/src/generate-code.ts | 6 ++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/codegen/src/artifacts.ts b/packages/codegen/src/artifacts.ts index 178e84ff9..d23b989f1 100644 --- a/packages/codegen/src/artifacts.ts +++ b/packages/codegen/src/artifacts.ts @@ -4,13 +4,17 @@ import solc from 'solc'; +interface Solc { + compile(input: string): any; +} + /** * Compiles the given contract using solc and returns resultant artifacts. * @param contractContent Contents of the contract file to be compiled. * @param contractFileName Input contract file name. * @param contractName Name of the main contract in the contract file. */ -export function generateArtifacts (contractContent: string, contractFileName: string, contractName: string): { abi: any[], storageLayout: any } { +export async function generateArtifacts (contractContent: string, contractFileName: string, contractName: string, solcVersion: string): Promise<{ abi: any[], storageLayout: any }> { const input: any = { language: 'Solidity', sources: {}, @@ -27,6 +31,20 @@ export function generateArtifacts (contractContent: string, contractFileName: st content: contractContent }; + const solcInstance = (solcVersion === undefined) ? solc : await getSolcByVersion(solcVersion); + // Get artifacts for the required contract. - return JSON.parse(solc.compile(JSON.stringify(input))).contracts[contractFileName][contractName]; + return JSON.parse(solcInstance.compile(JSON.stringify(input))).contracts[contractFileName][contractName]; +} + +async function getSolcByVersion (solcVersion: string): Promise { + return new Promise((resolve, reject) => { + solc.loadRemoteVersion(solcVersion, (err: any, solcInstance: Solc | Promise) => { + if (err) { + reject(err); + } else { + resolve(solcInstance); + } + }); + }); } diff --git a/packages/codegen/src/generate-code.ts b/packages/codegen/src/generate-code.ts index c3e7248e3..cadc3f3d8 100644 --- a/packages/codegen/src/generate-code.ts +++ b/packages/codegen/src/generate-code.ts @@ -90,10 +90,11 @@ const main = async (): Promise => { // Generate artifacts from contract. const inputFileName = path.basename(inputFile, '.sol'); - const { abi, storageLayout } = generateArtifacts( + const { abi, storageLayout } = await generateArtifacts( contractData.contractString, `${inputFileName}.sol`, - contractData.contractName + contractData.contractName, + config.solc ); contractData.contractAbi = abi; @@ -387,6 +388,7 @@ function getConfig (configFile: string): any { mode: inputConfig.mode || MODE_ALL, kind: inputConfig.kind || KIND_ACTIVE, port: inputConfig.port || DEFAULT_PORT, + solc: inputConfig.solc, flatten, subgraphPath, subgraphConfig