solc-js – solc js公司区块链毕设代写

区块链毕设代写本文提供国外最新区块链项目源码下载,包括solidity,eth,fabric等blockchain区块链,solc-js – solc js公司区块链毕设代写 是一篇很好的国外资料

solc-js - solc js公司 solc-js - solc js公司 solc-js - solc js公司

solc-js

JavaScript bindings for the Solidity compiler.

Uses the Emscripten compiled Solidity found in the solc-bin repository.

Node.js Usage

To use the latest stable version of the Solidity compiler via Node.js you can install it via npm:

npm install solc

Usage on the Command-Line

If this package is installed globally (npm install -g solc), a command-line tool called solcjs will be available.

To see all the supported features, execute:

solcjs --help

To compile a contract that imports other contracts via relative paths:

solcjs --bin --base-path . ./MainContract.sol

The option --base-path enables automatic loading of imports from the filesystem and takes a path as argument that contains the source files.

Note: this commandline interface is not compatible with solc provided by the Solidity compiler package and thus cannot be used in combination with an Ethereum client via the eth.compile.solidity() RPC method. Please refer to the Solidity compiler documentation for instructions to install solc. Furthermore, the commandline interface to solc-js provides fewer features than the binary release.

Usage in Projects

There are two ways to use solc:

  1. Through a high-level API giving a uniform interface to all compiler versions
  2. Through a low-level API giving access to all the compiler interfaces, which depend on the version of the compiler

High-level API

The high-level API consists of a single method, compile, which expects the Compiler Standard Input and Output JSON.

It also accepts an optional set of callback functions, which include the import and the smtSolver callbacks. Starting 0.6.0 it only accepts an object in place of the callback to supply the callbacks.

The import callback function is used to resolve unmet dependencies. This callback receives a path and must synchronously return either an error or the content of the dependency as a string. It cannot be used together with callback-based, asynchronous, filesystem access. A workaround is to collect the names of dependencies, return an error, and keep re-running the compiler until all of them are resolved.

Example usage without the import callback

Example:

var solc = require('solc');  var input = {   language: 'Solidity',   sources: {     'test.sol': {       content: 'contract C { function f() public { } }'     }   },   settings: {     outputSelection: {       '*': {         '*': ['*']       }     }   } };  var output = JSON.parse(solc.compile(JSON.stringify(input)));  // `output` here contains the JSON output as specified in the documentation for (var contractName in output.contracts['test.sol']) {   console.log(     contractName +       ': ' +       output.contracts['test.sol'][contractName].evm.bytecode.object   ); }

Example usage with import callback

var solc = require('solc');  var input = {   language: 'Solidity',   sources: {     'test.sol': {       content: 'import "lib.sol"; contract C { function f() public { L.f(); } }'     }   },   settings: {     outputSelection: {       '*': {         '*': ['*']       }     }   } };  function findImports(path) {   if (path === 'lib.sol')     return {       contents:         'library L { function f() internal returns (uint) { return 7; } }'     };   else return { error: 'File not found' }; }  // New syntax (supported from 0.5.12, mandatory from 0.6.0) var output = JSON.parse(   solc.compile(JSON.stringify(input), { import: findImports }) );  // `output` here contains the JSON output as specified in the documentation for (var contractName in output.contracts['test.sol']) {   console.log(     contractName +       ': ' +       output.contracts['test.sol'][contractName].evm.bytecode.object   ); }

The smtSolver callback function is used to solve SMT queries generated by Solidity’s SMTChecker. If you have an SMT solver installed locally, it can be used to solve the given queries, where the callback must synchronously return either an error or the result from the solver. A default smtSolver callback is distributed by solc-js, which relies on either Z3 or CVC4 being installed locally.

Example usage with smtSolver callback

var solc = require('solc'); var smt = require('smtsolver'); // Note that this example only works via node and not in the browser.  var input = {   language: 'Solidity',   sources: {     'test.sol': {       content: 'pragma experimental SMTChecker; contract C { function f(uint x) public { assert(x > 0); } }'     }   } };  var output = JSON.parse(   solc.compile(JSON.stringify(input), { smtSolver: smt.smtSolver }) );

The assertion is clearly false, and an assertion failure warning should be returned.

Low-level API

The low-level API is as follows:

  • solc.lowlevel.compileSingle: the original entry point, supports only a single file
  • solc.lowlevel.compileMulti: this supports multiple files, introduced in 0.1.6
  • solc.lowlevel.compileCallback: this supports callbacks, introduced in 0.2.1
  • solc.lowlevel.compileStandard: this works just like compile above, but is only present in compilers after (and including) 0.4.11

For examples how to use them, please refer to the README of the above mentioned solc-js releases.

Using with Electron

Note: If you are using Electron, nodeIntegration is on for BrowserWindow by default. If it is on, Electron will provide a require method which will not behave as expected and this may cause calls, such as require('solc'), to fail.

To turn off nodeIntegration, use the following:

new BrowserWindow({   webPreferences: {     nodeIntegration: false   } });

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")).

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.

var solc = require('solc');  // getting the development snapshot solc.loadRemoteVersion('latest', function(err, solcSnapshot) {   if (err) {     // An error was encountered, display and quit   } else {     // NOTE: Use `solcSnapshot` here with the same interface `solc` has   } });

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');  bytecode = linker.linkBytecode(bytecode, { MyLibrary: '0x123456...' });

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:

var linker = require('solc/linker');  var linkReferences = linker.findLinkReferences(bytecode);

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:

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}]

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) 

Browser Usage

Add the version of solc you want to use into index.html:

<script   type="text/javascript"   src="https://solc-bin.ethereum.org/bin/{{ SOLC VERSION }}.js" ></script>

(Alternatively use https://solc-bin.ethereum.org/bin/soljson-latest.js to get the latests version.)

This will load solc into the global variable window.Module. Then use this inside Javascript as:

var wrapper = require('solc/wrapper'); var solc = wrapper(window.Module);

Or in ES6 syntax:

import * as wrapper from 'solc/wrapper'; const solc = wrapper(window.Module);

Alternatively, to iterate the releases, one can load list.js from solc-bin:

<script   type="text/javascript"   src="https://solc-bin.ethereum.org/bin/list.js" ></script>

This will result in two global variables, window.soljsonReleases listing all releases and window.soljsonSources listing all nightly builds and releases.


Solidity编译器的JavaScript绑定。

solc js

使用在solc bin存储库中找到的Emscripten编译的Solidity。

通过节点.js您可以通过npm安装它:

节点.js用法

如果这个软件包是全局安装的(npm install-g solc),则可以使用名为solcjs的命令行工具。

npm install solc

在命令行上的用法

要查看所有受支持的功能,请执行:

编译通过相对路径导入其他协定的协定:

solcjs --help

选项–base path启用从文件系统自动加载导入,并将路径作为包含源文件的参数。

solcjs --bin --base-path . ./MainContract.sol

注意:此命令行接口与Solidity编译器包提供的solc不兼容,因此不能通过eth.compile.solidity公司()RPC方法。有关安装solc的说明,请参阅Solidity编译器文档。此外,与二进制版本相比,solc js的命令行接口提供的功能更少。

使用solc有两种方法:

在项目中的用法

高级API由一个方法compile组成,该方法需要编译器标准的输入和输出JSON。

  1. solc.lowlevel.compileMulti:它支持0.1.6中引入的多个文件solc.lowlevel.compileCallback:这支持回调,在中介绍0.2.1
  2. solc.lowlevel.compiles标准:它的工作方式与上面的compile类似,但仅在0.4.11之后(包括0.4.11)的编译器中存在

使用最新的开发快照访问所有编译器接口,这取决于编译器的版本solc.lowlevel.compileSingle:原始入口点仅支持单个文件
  • solc.lowlevel.compileMulti:它支持0.1.6中引入的多个文件solc.lowlevel.compileCallback:这支持回调,在中介绍0.2.1
  • solc.lowlevel.compiles标准:它的工作方式与上面的compile类似,但仅在0.4.11之后(包括0.4.11)的编译器中存在
  • 它还接受一组可选的回调函数,包括import和smtSolver回调。从0.6.0开始,它只接受一个对象来代替回调来提供回调。

    导入回调函数用于解析未满足的依赖项。此回调接收路径,必须以字符串的形式同步返回错误或依赖项的内容。它不能与基于回调的异步文件系统访问一起使用。解决方法是收集依赖项的名称,返回错误,并继续重新运行编译器,直到所有依赖项都得到解决。

    示例:

    Example usage without the import callback

    smtSolver回调函数用于解决由Solidity的SMTChecker生成的SMT查询。如果本地安装了SMT解算器,则可以使用它来解决给定的查询,其中回调必须同步返回错误或解算器的结果。默认的smtSolver回调由solc js分发,它依赖于本地安装的Z3或CVC4。

    var solc = require('solc');  var input = {   language: 'Solidity',   sources: {     'test.sol': {       content: 'contract C { function f() public { } }'     }   },   settings: {     outputSelection: {       '*': {         '*': ['*']       }     }   } };  var output = JSON.parse(solc.compile(JSON.stringify(input)));  // `output` here contains the JSON output as specified in the documentation for (var contractName in output.contracts['test.sol']) {   console.log(     contractName +       ': ' +       output.contracts['test.sol'][contractName].evm.bytecode.object   ); }

    Example usage with import callback

    var solc = require('solc');  var input = {   language: 'Solidity',   sources: {     'test.sol': {       content: 'import "lib.sol"; contract C { function f() public { L.f(); } }'     }   },   settings: {     outputSelection: {       '*': {         '*': ['*']       }     }   } };  function findImports(path) {   if (path === 'lib.sol')     return {       contents:         'library L { function f() internal returns (uint) { return 7; } }'     };   else return { error: 'File not found' }; }  // New syntax (supported from 0.5.12, mandatory from 0.6.0) var output = JSON.parse(   solc.compile(JSON.stringify(input), { import: findImports }) );  // `output` here contains the JSON output as specified in the documentation for (var contractName in output.contracts['test.sol']) {   console.log(     contractName +       ': ' +       output.contracts['test.sol'][contractName].evm.bytecode.object   ); }

    断言显然是错误的,应该返回断言失败警告。

    Example usage with smtSolver callback

    var solc = require('solc'); var smt = require('smtsolver'); // Note that this example only works via node and not in the browser.  var input = {   language: 'Solidity',   sources: {     'test.sol': {       content: 'pragma experimental SMTChecker; contract C { function f(uint x) public { assert(x > 0); } }'     }   } };  var output = JSON.parse(   solc.compile(JSON.stringify(input), { smtSolver: smt.smtSolver }) );

    底层API如下:

    Low-level API

    有关如何使用它们的示例,请参阅上述solc js发行版的自述文件。

    • solc.lowlevel.compileSingle: the original entry point, supports only a single file
    • solc.lowlevel.compileMulti: this supports multiple files, introduced in 0.1.6
    • solc.lowlevel.compileCallback: this supports callbacks, introduced in 0.2.1
    • solc.lowlevel.compileStandard: this works just like compile above, but is only present in compilers after (and including) 0.4.11

    注意:如果您使用的是Electron,默认情况下BrowserWindow的nodeIntegration处于打开状态。如果该选项处于启用状态,Electron将提供一个require方法,该方法的行为将不符合预期,这可能导致require(“solc”)等调用失败。

    Using with Electron

    要关闭nodeIntegration,请使用以下命令:

    若要使用特定版本的Solidity编译合同,则solc.loadRemoteVersion方法(回调)可用。这将返回一个使用指定编译器版本的新solc对象。

    new BrowserWindow({   webPreferences: {     nodeIntegration: false   } });

    Using a Legacy Version

    您还可以手动加载“binary”,并使用setupMethods创建上面描述的熟悉的包装函数:var solc=解决方案设置方法(需要(“/my/local/soljson.js公司”)).

    默认情况下,npm版本仅为发行版创建。这会阻止人们部署带有非发布版本的合同,因为它们不太稳定,更难验证。如果您想使用最新的开发快照(风险自负!),您可以使用以下示例代码。

    Using the Latest Development Snapshot

    使用库时,生成的字节码将包含引用库的实际地址的占位符。在部署合同之前,必须通过一个称为链接的过程来更新这些内容。

    var solc = require('solc');  // getting the development snapshot solc.loadRemoteVersion('latest', function(err, solcSnapshot) {   if (err) {     // An error was encountered, display and quit   } else {     // NOTE: Use `solcSnapshot` here with the same interface `solc` has   } });

    Linking Bytecode

    链接器模块(require(’solc/linker’)提供帮助程序来完成此操作。

    linkBytecode方法为链接提供了一个简单的助手:

    从Solidity 0.4.11开始,编译器支持输出链接引用映射的标准JSON输入和输出。这将提供一个库名称映射到字节码中的偏移量,以替换位于的地址。它也没有对库文件和合同名长度的限制。

    var linker = require('solc/linker');  bytecode = linker.linkBytecode(bytecode, { MyLibrary: '0x123456...' });

    链接器模块中有一种方法叫做findLinkReferences,它可以在旧版编译器生成的字节码中找到这样的链接引用:

    由于引入了新功能,Solidity版本生成的ABI可能略有不同。有一个工具,旨在翻译由旧版Solidity生成的ABI,以符合最新标准。

    var linker = require('solc/linker');  var linkReferences = linker.findLinkReferences(bytecode);

    Updating the ABI

    它可以用作:

    有一个助手可以将旧的JSON程序集输出格式化为混音IDE早期用户熟悉的文本。

    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}]

    Formatting old JSON assembly output

    添加要使用的solc版本索引.html:

    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) 

    Browser Usage

    (或者使用https://solc-bin.ethereum.org/bin/soljson-latest.js这将把solc加载到全局变量中窗口模块. 然后在Javascript中使用它作为:

    <script   type="text/javascript"   src="https://solc-bin.ethereum.org/bin/{{ SOLC VERSION }}.js" ></script>

    或者在ES6语法中:

    或者,为了迭代发布,可以加载列表.js从solc bin:

    var wrapper = require('solc/wrapper'); var solc = wrapper(window.Module);

    这将产生两个全局变量,window.soljsonReleases列出所有版本和窗口.soljsonSources列出所有夜间的构建和发布。

    import * as wrapper from 'solc/wrapper'; const solc = wrapper(window.Module);

    Alternatively, to iterate the releases, one can load list.js from solc-bin:

    <script   type="text/javascript"   src="https://solc-bin.ethereum.org/bin/list.js" ></script>

    This will result in two global variables, window.soljsonReleases listing all releases and window.soljsonSources listing all nightly builds and releases.

    部分转自网络,侵权联系删除区块链源码网

    www.interchains.cc

    https://www.interchains.cc/16614.html

    区块链毕设网(www.interchains.cc)全网最靠谱的原创区块链毕设代做网站 部分资料来自网络,侵权联系删除! 最全最大的区块链源码站 !
    区块链知识分享网, 以太坊dapp资源网, 区块链教程, fabric教程下载, 区块链书籍下载, 区块链资料下载, 区块链视频教程下载, 区块链基础教程, 区块链入门教程, 区块链资源 » solc-js – solc js公司区块链毕设代写

    提供最优质的资源集合

    立即查看 了解详情