diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-04-19 04:22:39 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-05-07 16:23:08 +0800 |
commit | 72b2a1c66fa9fb85ea8515645b97332eee204550 (patch) | |
tree | 8a4504ea2fdbc554f5b62f2b46088bea3e5b6324 /packages/deployer/src/compiler.ts | |
parent | 69a6166b6a1d39afc24b8dd950ec5d8539a03420 (diff) | |
download | dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.tar dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.tar.gz dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.tar.bz2 dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.tar.lz dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.tar.xz dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.tar.zst dexon-sol-tools-72b2a1c66fa9fb85ea8515645b97332eee204550.zip |
Implement new artifacts format
Diffstat (limited to 'packages/deployer/src/compiler.ts')
-rw-r--r-- | packages/deployer/src/compiler.ts | 122 |
1 files changed, 57 insertions, 65 deletions
diff --git a/packages/deployer/src/compiler.ts b/packages/deployer/src/compiler.ts index a3c8004ec..673bc3186 100644 --- a/packages/deployer/src/compiler.ts +++ b/packages/deployer/src/compiler.ts @@ -6,6 +6,7 @@ import { FSResolver, NameResolver, NPMResolver, + RelativeFSResolver, Resolver, URLResolver, } from '@0xproject/sol-resolver'; @@ -38,11 +39,25 @@ import { ContractNetworks, ContractSourceData, ContractSpecificSourceData, + ContractVersionData, } from './utils/types'; import { utils } from './utils/utils'; const ALL_CONTRACTS_IDENTIFIER = '*'; const SOLC_BIN_DIR = path.join(__dirname, '..', '..', 'solc_bin'); +const DEFAULT_CONTRACTS_DIR = path.resolve('contracts'); +const DEFAULT_ARTIFACTS_DIR = path.resolve('artifacts'); +const DEFAULT_COMPILER_SETTINGS: solc.CompilerSettings = { + optimizer: { + enabled: false, + }, + outputSelection: { + '*': { + '*': ['abi', 'evm.bytecode.object'], + }, + }, +}; +const CONFIG_FILE = 'compiler.json'; /** * The Compiler facilitates compiling Solidity smart contracts and saves the results @@ -52,26 +67,27 @@ export class Compiler { private _resolver: Resolver; private _nameResolver: NameResolver; private _contractsDir: string; - private _networkId: number; - private _optimizerEnabled: boolean; + private _compilerSettings: solc.CompilerSettings; private _artifactsDir: string; - private _specifiedContracts: Set<string> = new Set(); + private _specifiedContracts: string[] | '*'; /** * Instantiates a new instance of the Compiler class. - * @param opts Options specifying directories, network, and optimization settings. * @return An instance of the Compiler class. */ constructor(opts: CompilerOptions) { - this._contractsDir = opts.contractsDir; - this._networkId = opts.networkId; - this._optimizerEnabled = opts.optimizerEnabled; - this._artifactsDir = opts.artifactsDir; - this._specifiedContracts = opts.specifiedContracts; + const config: CompilerOptions = fs.existsSync(CONFIG_FILE) + ? JSON.parse(fs.readFileSync(CONFIG_FILE).toString()) + : {}; + this._contractsDir = opts.contractsDir || config.contractsDir || DEFAULT_CONTRACTS_DIR; + this._compilerSettings = opts.compilerSettings || config.compilerSettings || DEFAULT_COMPILER_SETTINGS; + this._artifactsDir = opts.artifactsDir || config.artifactsDir || DEFAULT_ARTIFACTS_DIR; + this._specifiedContracts = opts.contracts || config.contracts || '*'; this._nameResolver = new NameResolver(path.resolve(this._contractsDir)); const resolver = new FallthroughResolver(); resolver.appendResolver(new URLResolver()); const packagePath = path.resolve(''); resolver.appendResolver(new NPMResolver(packagePath)); + resolver.appendResolver(new RelativeFSResolver(this._contractsDir)); resolver.appendResolver(new FSResolver()); resolver.appendResolver(this._nameResolver); this._resolver = resolver; @@ -83,13 +99,13 @@ export class Compiler { await createDirIfDoesNotExistAsync(this._artifactsDir); await createDirIfDoesNotExistAsync(SOLC_BIN_DIR); let contractNamesToCompile: string[] = []; - if (this._specifiedContracts.has(ALL_CONTRACTS_IDENTIFIER)) { + if (this._specifiedContracts === ALL_CONTRACTS_IDENTIFIER) { const allContracts = this._nameResolver.getAll(); contractNamesToCompile = _.map(allContracts, contractSource => path.basename(contractSource.path, constants.SOLIDITY_FILE_EXTENSION), ); } else { - contractNamesToCompile = Array.from(this._specifiedContracts.values()); + contractNamesToCompile = this._specifiedContracts; } for (const contractNameToCompile of contractNamesToCompile) { await this._compileContractAsync(contractNameToCompile); @@ -101,17 +117,18 @@ export class Compiler { */ private async _compileContractAsync(contractName: string): Promise<void> { const contractSource = this._resolver.resolve(contractName); + const absoluteContractPath = path.join(this._contractsDir, contractSource.path); const currentArtifactIfExists = await getContractArtifactIfExistsAsync(this._artifactsDir, contractName); - const sourceTreeHashHex = `0x${this._getSourceTreeHash(contractSource.path).toString('hex')}`; - + const sourceTreeHashHex = `0x${this._getSourceTreeHash(absoluteContractPath).toString('hex')}`; let shouldCompile = false; if (_.isUndefined(currentArtifactIfExists)) { shouldCompile = true; } else { const currentArtifact = currentArtifactIfExists as ContractArtifact; shouldCompile = - currentArtifact.networks[this._networkId].optimizer_enabled !== this._optimizerEnabled || - currentArtifact.networks[this._networkId].source_tree_hash !== sourceTreeHashHex; + currentArtifact.schemaVersion !== '2.0.0' || + !_.isEqual(currentArtifact.compiler.settings, this._compilerSettings) || + currentArtifact.sourceTreeHashHex !== sourceTreeHashHex; } if (!shouldCompile) { return; @@ -139,30 +156,14 @@ export class Compiler { logUtils.log(`Compiling ${contractName} with Solidity v${solcVersion}...`); const source = contractSource.source; - const absoluteFilePath = contractSource.path; const standardInput: solc.StandardInput = { language: 'Solidity', sources: { - [absoluteFilePath]: { - urls: [`file://${absoluteFilePath}`], - }, - }, - settings: { - optimizer: { - enabled: this._optimizerEnabled, - }, - outputSelection: { - '*': { - '*': [ - 'abi', - 'evm.bytecode.object', - 'evm.bytecode.sourceMap', - 'evm.deployedBytecode.object', - 'evm.deployedBytecode.sourceMap', - ], - }, + [contractSource.path]: { + content: contractSource.source, }, }, + settings: this._compilerSettings, }; const compiled: solc.StandardOutput = JSON.parse( solcInstance.compileStandardWrapper(JSON.stringify(standardInput), importPath => { @@ -188,34 +189,28 @@ export class Compiler { }); } } - const compiledData = compiled.contracts[absoluteFilePath][contractName]; + const compiledData = compiled.contracts[contractSource.path][contractName]; if (_.isUndefined(compiledData)) { throw new Error( - `Contract ${contractName} not found in ${absoluteFilePath}. Please make sure your contract has the same name as it's file name`, + `Contract ${contractName} not found in ${ + contractSource.path + }. Please make sure your contract has the same name as it's file name`, ); } - const abi: ContractAbi = compiledData.abi; - const bytecode = `0x${compiledData.evm.bytecode.object}`; - const runtimeBytecode = `0x${compiledData.evm.deployedBytecode.object}`; - const sourceMap = compiledData.evm.bytecode.sourceMap; - const sourceMapRuntime = compiledData.evm.deployedBytecode.sourceMap; - const unresolvedSourcePaths = _.keys(compiled.sources); - const sources = _.map( - unresolvedSourcePaths, - unresolvedSourcePath => this._resolver.resolve(unresolvedSourcePath).path, + const sourceCodes = _.mapValues( + compiled.sources, + (_1, sourceFilePath) => this._resolver.resolve(sourceFilePath).source, ); - const updated_at = Date.now(); - const contractNetworkData: ContractNetworkData = { - solc_version: solcVersion, - source_tree_hash: sourceTreeHashHex, - optimizer_enabled: this._optimizerEnabled, - abi, - bytecode, - runtime_bytecode: runtimeBytecode, - updated_at, - source_map: sourceMap, - source_map_runtime: sourceMapRuntime, - sources, + const contractVersion: ContractVersionData = { + compilerOutput: compiledData, + sources: compiled.sources, + sourceCodes, + sourceTreeHashHex, + compiler: { + name: 'solc', + version: solcVersion, + settings: this._compilerSettings, + }, }; let newArtifact: ContractArtifact; @@ -223,17 +218,14 @@ export class Compiler { const currentArtifact = currentArtifactIfExists as ContractArtifact; newArtifact = { ...currentArtifact, - networks: { - ...currentArtifact.networks, - [this._networkId]: contractNetworkData, - }, + ...contractVersion, }; } else { newArtifact = { - contract_name: contractName, - networks: { - [this._networkId]: contractNetworkData, - }, + schemaVersion: '2.0.0', + contractName, + ...contractVersion, + networks: {}, }; } |