Skip to content

Commit

Permalink
Use solc version defined in config
Browse files Browse the repository at this point in the history
  • Loading branch information
dafaqdhruv committed Apr 4, 2023
1 parent 3c64e0e commit aa3c984
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
22 changes: 20 additions & 2 deletions packages/codegen/src/artifacts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
Expand All @@ -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<Solc> {
return new Promise((resolve, reject) => {
solc.loadRemoteVersion(solcVersion, (err: any, solcInstance: Solc | Promise<any>) => {
if (err) {
reject(err);
} else {
resolve(solcInstance);
}
});
});
}
6 changes: 4 additions & 2 deletions packages/codegen/src/generate-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ const main = async (): Promise<void> => {
// 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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aa3c984

Please sign in to comment.