UNPKG

tronbox

Version:

TronBox - Simple development framework for Tron

1 lines 8.56 kB
"use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");var _toConsumableArray2=_interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));var _defineProperty2=_interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));function ownKeys(e,r){var t=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);r&&(o=o.filter(function(r){return Object.getOwnPropertyDescriptor(e,r).enumerable})),t.push.apply(t,o)}return t}function _objectSpread(e){for(var r=1;r<arguments.length;r++){var t=null!=arguments[r]?arguments[r]:{};r%2?ownKeys(Object(t),!0).forEach(function(r){(0,_defineProperty2["default"])(e,r,t[r])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})}return e}var Profiler=require("./profiler");var OS=require("os");var path=require("path");var CompileError=require("./compileerror");var expect=require("@truffle/expect");var find_contracts=require("@truffle/contract-sources");var Config=require("../Config");var preReleaseCompilerWarning=require("./messages").preReleaseCompilerWarning;var compile=function compile(sources,options,callback){var _options$compilers;if(typeof options==="function"){callback=options;options={}}if(!options.logger){options.logger=console}expect.options(options,["contracts_directory","solc"]);var _require=require("../TronSolc"),getWrapper=_require.getWrapper;var solc=getWrapper(options);var listeners=process.listeners("uncaughtException");var solc_listener=listeners[listeners.length-1];if(solc_listener){process.removeListener("uncaughtException",solc_listener)}var operatingSystemIndependentSources={};var originalPathMappings={};Object.keys(sources).forEach(function(source){var replacement=source.replace(/\\/g,"/");if(replacement.length>=2&&replacement[1]===":"){replacement="/"+replacement;replacement=replacement.replace(":","")}operatingSystemIndependentSources[replacement]=sources[source];originalPathMappings[replacement]=source});var settings=Object.keys(options.solc).length?options.solc:((_options$compilers=options.compilers)===null||_options$compilers===void 0||(_options$compilers=_options$compilers.solc)===null||_options$compilers===void 0?void 0:_options$compilers.settings)||{};var solcStandardInput={language:"Solidity",sources:{},settings:_objectSpread(_objectSpread({},settings),{},{outputSelection:{"*":{"":["legacyAST","ast"],"*":["abi","evm.bytecode.object","evm.bytecode.sourceMap","evm.bytecode.linkReferences","evm.deployedBytecode.object","evm.deployedBytecode.sourceMap"]}}})};if(!Object.keys(sources).length){return callback(null,[],[])}Object.keys(operatingSystemIndependentSources).forEach(function(file_path){solcStandardInput.sources[file_path]={content:operatingSystemIndependentSources[file_path]}});var result=solc[solc.compileStandard?"compileStandard":"compile"](JSON.stringify(solcStandardInput));var standardOutput=JSON.parse(result);var errors=standardOutput.errors||[];var warnings=[];if(options.strict!==true){warnings=errors.filter(function(error){return error.severity==="warning"&&error.message!==preReleaseCompilerWarning});errors=errors.filter(function(error){return error.severity!=="warning"});if(options.quiet!==true&&warnings.length>0){options.logger.log(OS.EOL+"Compilation warnings encountered:"+OS.EOL);options.logger.log(warnings.map(function(warning){return warning.formattedMessage}).join())}}if(errors.length>0){options.logger.log("");return callback(new CompileError(standardOutput.errors.map(function(error){return error.formattedMessage}).join()))}var contracts=standardOutput.contracts;var files=[];Object.keys(standardOutput.sources).forEach(function(filename){var source=standardOutput.sources[filename];files[source.id]=originalPathMappings[filename]});var returnVal={};Object.keys(contracts).forEach(function(source_path){var files_contracts=contracts[source_path];Object.keys(files_contracts).forEach(function(contract_name){var contract=files_contracts[contract_name];var contract_definition={contract_name:contract_name,sourcePath:originalPathMappings[source_path],source:operatingSystemIndependentSources[source_path],sourceMap:contract.evm.bytecode.sourceMap,deployedSourceMap:contract.evm.deployedBytecode.sourceMap,legacyAST:standardOutput.sources[source_path].legacyAST,ast:standardOutput.sources[source_path].ast,abi:contract.abi,bytecode:"0x"+contract.evm.bytecode.object,deployedBytecode:"0x"+contract.evm.deployedBytecode.object,unlinked_binary:"0x"+contract.evm.bytecode.object,compiler:{name:"solc",version:solc.version()}};contract_definition.abi=orderABI(contract_definition);var bytecodeLinkRef=contract.evm.bytecode.linkReferences||{};Object.keys(bytecodeLinkRef).forEach(function(file_name){var fileLinks=bytecodeLinkRef[file_name];Object.keys(fileLinks).forEach(function(library_name){var linkReferences=fileLinks[library_name]||[];contract_definition.bytecode=replaceLinkReferences(contract_definition.bytecode,linkReferences,library_name);contract_definition.unlinked_binary=replaceLinkReferences(contract_definition.unlinked_binary,linkReferences,library_name)})});var deployedBytecodeLinkRef=contract.evm.deployedBytecode.linkReferences||{};Object.keys(deployedBytecodeLinkRef).forEach(function(file_name){var fileLinks=deployedBytecodeLinkRef[file_name];Object.keys(fileLinks).forEach(function(library_name){var linkReferences=fileLinks[library_name]||[];contract_definition.deployedBytecode=replaceLinkReferences(contract_definition.deployedBytecode,linkReferences,library_name)})});returnVal[contract_name]=contract_definition})});callback(null,returnVal,files)};function replaceLinkReferences(bytecode,linkReferences,libraryName){var linkId="__"+libraryName;while(linkId.length<40){linkId+="_"}linkReferences.forEach(function(ref){var start=ref.start*2+2;bytecode=bytecode.substring(0,start)+linkId+bytecode.substring(start+40)});return bytecode}function orderABI(contract){var abi=contract.abi,contractName=contract.contractName,ast=contract.ast;if(!abi){return[]}if(!ast||!ast.nodes){return abi}var contractDefinition=ast.nodes.find(function(_ref){var nodeType=_ref.nodeType,name=_ref.name;return nodeType==="ContractDefinition"&&name===contractName});if(!contractDefinition||!contractDefinition.nodes){return abi}var orderedFunctionNames=contractDefinition.nodes.filter(function(_ref2){var nodeType=_ref2.nodeType;return nodeType==="FunctionDefinition"}).map(function(_ref3){var functionName=_ref3.name;return functionName});var functionIndexes=orderedFunctionNames.map(function(functionName,index){return(0,_defineProperty2["default"])({},functionName,index)}).reduce(function(a,b){return Object.assign({},a,b)},{});return[].concat((0,_toConsumableArray2["default"])(abi.filter(function(_ref5){var name=_ref5.name;return functionIndexes[name]===undefined})),(0,_toConsumableArray2["default"])(abi.filter(function(_ref6){var name=_ref6.name;return functionIndexes[name]!==undefined}).sort(function(_ref7,_ref8){var a=_ref7.name;var b=_ref8.name;return functionIndexes[a]-functionIndexes[b]})))}compile.all=function(options,callback){find_contracts(options.contracts_directory,function(err,files){options.paths=files;compile.with_dependencies(options,callback)})};compile.necessary=function(options,callback){options.logger=options.logger||console;Profiler.updated(options,function(err,updated){if(err)return callback(err);if(updated.length===0&&options.quiet!==true){return callback(null,[],{})}options.paths=updated;compile.with_dependencies(options,callback)})};compile.with_dependencies=function(options,callback){options.logger=options.logger||console;options.contracts_directory=options.contracts_directory||process.cwd();expect.options(options,["paths","working_directory","contracts_directory","resolver"]);var config=Config["default"]().merge(options);Profiler.required_sources(config["with"]({paths:options.paths,base_path:options.contracts_directory,resolver:options.resolver}),function(err,result){if(err)return callback(err);if(!options.quiet){options.logger.log("Compiling your contracts...");options.logger.log("============================");Object.keys(result).sort().forEach(function(import_path){var display_path=import_path;if(path.isAbsolute(import_path)){display_path="."+path.sep+path.relative(options.working_directory,import_path)}options.logger.log("Compiling "+display_path+"...")})}compile(result,options,callback)})};module.exports=compile;