You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
const ResolverEngine = require('solc-resolver').ResolverEngine;
let resolverEngine = new ResolverEngine();
let resolveGithub = require('resolve-github');
resolverEngine.addResolver(resolveGithub);
let resolveIpfs = require('resolve-ipfs');
resolverEngine.addResolver(resolveIpfs);
const getImportContent = async function (path) {
return myDB.has(path) ? myDB.get(path) : await resolverEngine.require(path);
};
let output = await compiler(sourceCode, getImportContent);
The goal is:
to develop a nice "user interface" for developers who want to use a solidity compiler version, which means a standard output format and convert output of all solc-bin versions into this format
document the cross-compiler-version standard format in the modules README
The solidity compiler is developed in C++. There are autogenerated transpilations into Javascript in https://github.com/ethereum/solc-bin. Every time a new solidity compiler version is released, the soc-bin repository is updated. We maintain solc-js as a wrapper module around the autogenerated solidity compiler versions.
Updating the ABI
The ABI generated by Solidity versions can differ slightly, due to new features introduced. There is a tool included which aims to translate the ABI generated by an older Solidity version to conform to the latest standard.
There is a helper available to format old JSON assembly output into a text familiar to earlier users of Remix IDE.
var translate = require('solc/translate')
// assemblyJSON refers to the JSON of the given assembly and sourceCode is the source of which the assembly was generated from
var output = translate.prettyPrintLegacyAssemblyJSON(assemblyJSON, sourceCode)
Linking Bytecode
When using libraries, the resulting bytecode will contain placeholders for the real addresses of the referenced libraries. These have to be updated, via a process called linking, before deploying the contract.
The linker module (require('solc/linker')) offers helpers to accomplish this.
The linkBytecode method provides a simple helper for linking:
(Note: linkBytecode is also exposed via solc as solc.linkBytecode, but this usage is deprecated.)
As of Solidity 0.4.11 the compiler supports standard JSON input and output which outputs a link references map. This gives a map of library names to offsets in the bytecode to replace the addresses at. It also doesn't have the limitation on library file and contract name lengths.
There is a method available in the linker module called findLinkReferences which can find such link references in bytecode produced by an older compiler:
By default, the npm version is only created for releases. This prevents people from deploying contracts with non-release versions because they are less stable and harder to verify. If you would like to use the latest development snapshot (at your own risk!), you may use the following example code.
varsolc=require('solc')// getting the development snapshotsolc.loadRemoteVersion('latest',function(err,solcSnapshot){if(err){// An error was encountered, display and quit}varoutput=solcSnapshot.compile("contract t { function g() {} }",1)})
Using a Legacy Version
In order to compile contracts using a specific version of Solidity, the solc.loadRemoteVersion(version, callback) method is available. This returns a new solc object that uses a version of the compiler specified.
You can also load the "binary" manually and use setupMethods to create the familiar wrapper functions described above: var solc = solc.setupMethods(require("/my/local/soljson.js")).
From version 0.4.20
Starting from version 0.4.20 a Semver compatible version number can be retrieved on every compiler release, including old ones, using the semver() method.
From version 0.4.11
Starting from version 0.4.11 there is a new entry point named compileStandardWrapper() which supports Solidity's standard JSON input and output. It also maps old compiler output to it.
varsolc=require('solc')// 'input' is a JSON string corresponding to the "standard JSON input" as described in the link above// 'findImports' works as described abovevaroutput=solc.compileStandardWrapper(input,findImports)// Ouput is a JSON string corresponding to the "standard JSON output"
There is also a direct method, compileStandard, which is only present on recent compilers and works the same way. compileStandardWrapper is preferred however because it provides the same interface for old compilers.
From version 0.2.1
Starting from version 0.2.1, a callback is supported to resolve missing imports as follows:
varsolc=require('solc')varinput={'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'}functionfindImports(path){if(path==='lib.sol')return{contents: 'library L { function f() returns (uint) { return 7; } }'}elsereturn{error: 'File not found'}}varoutput=solc.compile({sources: input},1,findImports)for(varcontractNameinoutput.contracts)console.log(contractName+': '+output.contracts[contractName].bytecode)
The compile() method always returns an object, which can contain errors, sources and contracts fields. errors is a list of error mesages.
From version 0.1.6
Starting from version 0.1.6, multiple files are supported with automatic import resolution by the compiler as follows:
varsolc=require('solc')varinput={'lib.sol': 'library L { function f() returns (uint) { return 7; } }','cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'}varoutput=solc.compile({sources: input},1)for(varcontractNameinoutput.contracts)console.log(contractName+': '+output.contracts[contractName].bytecode)
Note that all input files that are imported have to be supplied, the compiler will not load any additional files on its own.
From early versions
It can also be included and used in other projects:
varsolc=require('solc')varinput='contract x { function g() {} }'// Setting 1 as second paramateractivates the optimiservaroutput=solc.compile(input,1)for(varcontractNameinoutput.contracts){// code and ABI that are needed by web3console.log(contractName+': '+output.contracts[contractName].bytecode)console.log(contractName+'; '+JSON.parse(output.contracts[contractName].interface))}
MORE
// /******************************************************************************
// VERSIONS
// ******************************************************************************/
//
// function v0_0_0_plus () {
// // From early versions
// // it can also be included and used in other projects
// var solc = require('solc')
// var input = 'contract x { function g() {} }'
// // Setting 1 as second paramateractivates the optimiser
// var output = solc.compile(input, 1)
// for (var contractName in output.contracts) {
// // code and ABI that are needed by web3
// console.log(contractName + ': ' + output.contracts[contractName].bytecode)
// console.log(contractName + '; ' + JSON.parse(output.contracts[contractName].interface))
// }
// }
// function v0_1_6_plus () {
// // From version 0.1.6
// // multiple files are supported with automatic import resolution
// // by the compiler as follows
// var input = {
// 'lib.sol': 'library L { function f() returns (uint) { return 7; } }',
// 'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
// }
// var output = solc.compile({ sources: input }, 1)
// for (var contractName in output.contracts)
// console.log(contractName + ': ' + output.contracts[contractName].bytecode)
// }
// function v0_2_1_plus () {
// // From version 0.2.1
// // a callback is supported to resolve missing imports as follows
// /*
// The compile() method always returns an object, which can contain errors,
// sources and contracts fields.
// errors is a list of error mesages
// */
// var solc = require('solc')
// var input = {
// 'cont.sol': 'import "lib.sol"; contract x { function g() { L.f(); } }'
// }
// function findImports (path) {
// if (path === 'lib.sol')
// return { contents: 'library L { function f() returns (uint) { return 7; } }' }
// else
// return { error: 'File not found' }
// }
// var output = solc.compile({ sources: input }, 1, findImports)
// for (var contractName in output.contracts)
// console.log(contractName + ': ' + output.contracts[contractName].bytecode)
// }
// function v0_4_11_plus () {
// // From version 0.4.11
// // Starting from version 0.4.11 there is a new entry point
// // named compileStandardWrapper() which supports
// // https://solidity.readthedocs.io/en/develop/using-the-compiler.html#compiler-input-and-output-json-description
// // => Solidity's standard JSON input and output.
// // It also maps old compiler output to it.
// // => outputs a link references map
// // => This gives a map of library names to offsets in the bytecode to replace the addresses at
// // It also doesn't have the limitation on library file and contract name lengths
// /*
// There is also a direct method, compileStandard, which is only present on
// recent compilers and works the same way. compileStandardWrapper
// is preferred however because it provides the same interface for old compilers.
// */
// var solc = require('solc')
//
// // 'input' is a JSON string corresponding to the "standard JSON input" as described in the link above
// // 'findImports' works as described above
// var output = solc.compileStandardWrapper(input, findImports)
// // Ouput is a JSON string corresponding to the "standard JSON output"
// }
// function v0_4_20_plus () {
// // From version 0.4.20
// /*
// Starting from version 0.4.20 a Semver compatible version number can be
// retrieved on every compiler release, including old ones,
// using the semver() method.
// */
// }
//
// /******************************************************************************
// LOAD COMPILER
// ******************************************************************************/
// function loadCompiler (version, next) {
// // API:
// // In order to compile contracts using a specific version of Solidity,
// // the solc.loadRemoteVersion(version, callback) method is available.
// // This returns a new solc object that uses a version of the compiler specified
// //
// // You can also load the "binary" manually and use setupMethods
// // to create the familiar wrapper functions described above:
// // var solc = solc.setupMethods(require("/my/local/soljson.js"))
//
// var solc = require('solc')
// // getting the development snapshot
// solc.loadRemoteVersion('latest', function (err, solcSnapshot) {
// if (err) {
// // An error was encountered, display and quit
// }
// var output = solcSnapshot.compile("contract t { function g() {} }", 1)
// next(null, solcSnapshot)
// })
// }
// /******************************************************************************
// LINKING BYTECODE
// ******************************************************************************/
// function linkByteCode ({ unlinkedByteCode, libAddressMap }, next) {
// // LINKING BYTECODE
// // When using libraries, the resulting bytecode will contain placeholders
// // for the real addresses of the referenced libraries.
// // These have to be updated, via a process called linking,
// // before deploying the contract.
//
// // The linker module
// // (require('solc/linker'))
// // offers helpers to accomplish this.
//
// // The linkBytecode method provides a simple helper for linking:
// var linker = require('solc/linker')
// libAddressMap = libAddressMap || { 'MyLibrary': '0x123456...' }
// bytecode = linker.linkBytecode(unlinkedByteCode, libAddressMap)
// // Note: linkBytecode is also exposed via solc as solc.linkBytecode,
// // but this usage is deprecated.
//
// /*
// There is a method available in the linker module called findLinkReferences
// which can find such link references in bytecode produced
// by an older compiler:
// */
// var linker = require('solc/linker')
// var linkReferences = linker.findLinkReferences(bytecode)
//
// const linkedByteCode = bytecode
// next(null, linkedByteCode)
// }
// /******************************************************************************
// FORMAT ASSEMBLY JSON
// ******************************************************************************/
// function formatJSON ({ assemblyJSON, sourceCode }, next) {
// // Formatting old JSON assembly output
// //
// // There is a helper available to format old JSON assembly output
// // into a text familiar to earlier users of Remix IDE
// var translate = require('solc/translate')
//
// // assemblyJSON refers to the JSON of the given assembly and sourceCode is the source of which the assembly was generated from
// var output = translate.prettyPrintLegacyAssemblyJSON(assemblyJSON, sourceCode)
//
// next(null, output)
// }
// /******************************************************************************
// UPDATE ABI
// ******************************************************************************/
// function updateABI ({ version, oldABI }, next) {
// // UPDATING THE ABI
// /*
// The ABI generated by Solidity versions can differ slightly,
// due to new features introduced.
// There is a tool included which aims to translate the ABI generated
// by an older Solidity version to conform to the latest standard
// */
// var abi = require('solc/abi')
//
// var inputABI = [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"}]
// var outputABI = abi.update('0.3.6', inputABI)
// // Output contains: [{"constant":false,"inputs":[],"name":"hello","outputs":[{"name":"","type":"string"}],"payable":true,"type":"function"},{"type":"fallback","payable":true}]
//
// const newABI = outputABI
// next(null, newABI)
// }
/src/solc-js.js
/src/lib/versions/index.js
/src/lib/versions/package.json
/src/lib/versions/versions2url.js
/src/lib/versions/versions.js
/src/lib/cache/index.js
/src/lib/ajax-cache/package.json
/src/lib/ajax-cache/cache.js
/src/lib/ajax-cache/node_modules/ajax/index.js
-----------
/src/node_modules/cache-ajax
/src/node_modules/cache-ajax/index.js
/src/node_modules/cache-ajax/test/test.js
/src/node_modules/cache-ajax/package.json
/src/node_modules/cache-ajax/
/solcjs/$>cd src/node_modules/cache-ajax/
// 100 LOC
/solcjs/src/node_modules/cache-ajax/$> npm run test
// MAIN FILE// ./src/solc-js.jsconstcache=require('ajax-cache')// ./src/node_modules/ajax-cache/**/*console.log(cache)
@todo
0.5
so thatplay-editor
works again0.5+
, because also remix is having issuesolcjs
module apisolc-js#README.md
to show all available properties and methodssolc-js#9
: use webworkersolc-js#3
: design compiler standard output formatsolc-js#README.md
to show the output JSONsolc-js#4
: re-use original solc test suitesolc-js#5
: enable hooks for fetching code behind solidity import statementssolc-js#6
: add solidity import resolverssmartcontract-ui
to accept the new output format and works with all compiler versionssolc-js#7
: make solc-js an "(un)installable" commandsolc-js#8
: add other solidity compilers as commandssolc-js#2
(add vyper compiler)review of refactoring
i like those:
https://www.npmjs.com/package/ajax-caching
https://www.npmjs.com/package/solc-version
not yet sure - need to see an example how they work with the compiler import callback
https://www.npmjs.com/package/solc-import
https://www.npmjs.com/package/solc-resolver
(also how they would look like if they would use many small resolver modules for each backend (e.g. swarm, github, ipfs, etc...)
i wish this one would be much more raw and minimal:
https://www.npmjs.com/package/solcjs-core
I am not yet sure about this one - depends on
solcjs-core
which would be cool if it was more minimalhttps://www.npmjs.com/package/solcjs-lightweight
implement a small resolver module
The goal is:
solc-bin
versions into this formatThe solidity compiler is developed in C++. There are autogenerated transpilations into Javascript in https://github.com/ethereum/solc-bin. Every time a new solidity compiler version is released, the
soc-bin
repository is updated. We maintainsolc-js
as a wrapper module around the autogenerated solidity compiler versions.Updating the ABI
The ABI generated by Solidity versions can differ slightly, due to new features introduced. There is a tool included which aims to translate the ABI generated by an older Solidity version to conform to the latest standard.
It can be used as:
Formatting old JSON assembly output
There is a helper available to format old JSON assembly output into a text familiar to earlier users of Remix IDE.
Linking Bytecode
When using libraries, the resulting bytecode will contain placeholders for the real addresses of the referenced libraries. These have to be updated, via a process called linking, before deploying the contract.
The
linker
module (require('solc/linker')
) offers helpers to accomplish this.The
linkBytecode
method provides a simple helper for linking:(Note:
linkBytecode
is also exposed viasolc
assolc.linkBytecode
, but this usage is deprecated.)As of Solidity 0.4.11 the compiler supports standard JSON input and output which outputs a link references map. This gives a map of library names to offsets in the bytecode to replace the addresses at. It also doesn't have the limitation on library file and contract name lengths.
There is a method available in the
linker
module calledfindLinkReferences
which can find such link references in bytecode produced by an older compiler:Using the Latest Development Snapshot
By default, the npm version is only created for releases. This prevents people from deploying contracts with non-release versions because they are less stable and harder to verify. If you would like to use the latest development snapshot (at your own risk!), you may use the following example code.
Using a Legacy Version
In order to compile contracts using a specific version of Solidity, the
solc.loadRemoteVersion(version, callback)
method is available. This returns a newsolc
object that uses a version of the compiler specified.You can also load the "binary" manually and use
setupMethods
to create the familiar wrapper functions described above:var solc = solc.setupMethods(require("/my/local/soljson.js"))
.From version 0.4.20
Starting from version 0.4.20 a Semver compatible version number can be retrieved on every compiler release, including old ones, using the
semver()
method.From version 0.4.11
Starting from version 0.4.11 there is a new entry point named
compileStandardWrapper()
which supports Solidity's standard JSON input and output. It also maps old compiler output to it.There is also a direct method,
compileStandard
, which is only present on recent compilers and works the same way.compileStandardWrapper
is preferred however because it provides the same interface for old compilers.From version 0.2.1
Starting from version 0.2.1, a callback is supported to resolve missing imports as follows:
The
compile()
method always returns an object, which can containerrors
,sources
andcontracts
fields.errors
is a list of error mesages.From version 0.1.6
Starting from version 0.1.6, multiple files are supported with automatic import resolution by the compiler as follows:
Note that all input files that are imported have to be supplied, the compiler will not load any additional files on its own.
From early versions
It can also be included and used in other projects:
MORE
Also:
These are all compiler tests the solidity developer team updates regularly and we should see that our compiler can pass them all: https://github.com/ethereum/solc-js/tree/master/test
write down a few example solidity contracts to use them for manual testing
The text was updated successfully, but these errors were encountered: