二维码

6.2 solcjs概述 - 数据结构 - 机器学习

1332 人阅读 | 时间:2021年01月15日 01:12
6.2 solcjs概述 - 数据结构 - 机器学习 #daohang ul li t,.reed .riqi,a.shangg,a.xiatt,a.shangg:hover,a.xiatt:hover,a.shang,a.xiat,a.shang:hover,a.xiat:hover,.reed-pinglun-anniu,span.now-page,#daohangs-around,#caidan-tubiao,#daohangs,#daohangs li,#btnPost{background-color:#D10B04;} .dinglanyou1 h3{border-bottom:3px solid #D10B04;} #dibuer{border-top:2px solid #D10B04;}.cebianlan .rongqi h3{border-bottom:1px solid #D10B04;} #edtSearch{border:1px solid #D10B04;} #daohang .zuo ul li{border-right:1px solid #;} #daohang ul li t a{border-top:1px solid #;border-right:1px solid #D10B04;} #daohang ul li t a:hover{border-right:1px solid #;} #daohang .you ul li a:hover,#daohang .zuo ul li a:hover,.reed-pinglun-anniu:hover{background-color:#;} a:hover,.reed h6 a:hover,#dibuer a:hover,.reed .riqiding,.cebianlan .rongqi li a:hover,#pinglun-liebiao ul.fubens li.depth-1 dl dd span.shu a,#pinglun-liebiao ul.fubens li.depth-1 dl dd span.huifuliuyan a:hover,.reed-biaoti h6 span{color:#D10B04;} .reed .kan a{color:#0A0AF5;}.reed .kan a:hover{color:#D10101;} @media screen and (max-width:1492px){a.shang,a.xiat{background:none;} a.xiat:hover,a.shang:hover{background-color:#f9f9f9;background-image:none;text-decoration:none;}} var _hmt = _hmt || [];(function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?b19db5ba3b437a9e8698d2bc8fc64334"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s);})(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?b19db5ba3b437a9e8698d2bc8fc64334"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?2d748c9763cfc72fb7d1ccab29f0770d"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f6d451f3f1be23f3abf240c64c469c1b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

当前位置:首页 » 区块链精品文章 » 正文

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646201", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646162", container: s }); })();

6.2 solcjs概述

2173 人参与  2018年09月15日 14:33  分类 : 区块链精品文章  评论

solcjs是用于编译solidity文件的node.js库和命令行工具。它不使用solc命令行编译器,而是纯粹使用JavaScript进行编译,因此它的安装比solc简单得多。

Solc是真实的solidity编译器,用C++编写。C++代码使用emscripten被编译成JavaScript。Solc的每一个版本都被编译成JavaScript。访问https://gIthub.com/ethereum/solc-bin/tree/gh-pages/bin ,可以发现每个solidity版本的以JavaScript为基础的编译器。solcjs仅使用这些编译器中的一种来编译solidity源代码。这些编译器在浏览器和node.js环境中都可以运行。

solidity的浏览器使用这些以JavaScript为基础的编译器编译solidity源代码。

6.2.1 安装solcjs

solcjs可以作为solc的npm包使用。和其他npm包一样,solcjs npm包可以在本地或者全局安装。如果包安装在全局,则命令行工具solcjs可用。因此,为了安装命令行工具,运行如下命令:

6.2 solcjs概述 - 数据结构 - 机器学习

接着运行如下命令,看看如何用命令行编译器编译solidity文件:

6.2 solcjs概述 - 数据结构 - 机器学习

我们不学习solcjs命令行工具,而是学习用solcjs API编译solidity文件。

solcjs默认使用的编译器版本与其自身版本匹配。例如,如果安装solcjs的0.4.8版本,则将默认使用0.4.8编译器版本进行编译。也可以配置solcjs,以使用一些其他的编译器版本。在写本书之时,solcjs的最新版本是0.4.8。

6.2.2 solcjs API

solcjs提供了compiler方法,用于编译solidity代码。根据源代码是否有import(引用),该 方法可以用于两种不同方法:如果源代码没有import,则需要两个实参,即第一个实参是字符串作solidity源代码,第二个实参是Boolean, 表示是否最优化字节码。如果源字符串包含多个合约,则将编译全部。示例如下:

6.2 solcjs概述 - 数据结构 - 机器学习

如果源代码包含对其他合约的引用(imports),则第一个实参就是一个对象,它的键是文件名,值是文件内容。所以无论何时编译器看到一个import语句,它不会在文件系统中寻找文件,而是通过与文件名匹配的键在对象中寻找文件内容。示例如下:

6.2 solcjs概述 - 数据结构 - 机器学习

如果想在编译时从文件系统读取被引用的文件或者在编译时解析文件内容,则compiler方法支持第三个实参,即取文件名并返回文件内容的方法。示例如下:

6.2 solcjs概述 - 数据结构 - 机器学习

1.使用不同的编译器版本

为了使用不同的solidity版本编译合约,需要用useVersion方法去引用一个不同的编译器。useVersion用一个字符串,该字符串表示存储了编译器的JavaScript文件名,并在/node_modules/solc/bin目录中寻找该文件。

solcjs还提供另一种方式loadRemoteVersion,它用的编译器文件名与solc-bin文件库(https://github.com/etherum/solc-bin )中的solc-bin/bin目录下的文件名进行匹配,并下载和使用。

最后,solcjs还提供了另一个setupMethods方法,它与useVersion类似,但是可以从任意目录加载编译器。

下面用一个示例来演示这三个方法的用法:

6.2 solcjs概述 - 数据结构 - 机器学习

为了运行上述代码,首先需要从solc-bin repository下载v0.4.7.commit.822622cf.js文件,并将其保存在node_modules/solc/bin目录中。然 后需要下载编译器文件版本0.4.8,将其保存在文件系统中某处,并把setupMethods调用中的路径指向那个目录。

2.接入库

如果solidity源代码引用库,生成的字节码将包含被引用库真实地址的占位符。这些必须在部署合约之前,通过一个称为接入(linking)的程序更新。

solcjs提供了把库地址接入生成的字节码的linkByteCode方法。示例如下:

6.2 solcjs概述 - 数据结构 - 机器学习

3.更新ABI

合约ABI提供多种信息,这些信息不包括合约的实现。两种不同版本的编译器生成的ABI可能不匹配,因为较高版本比较 低版本支持更多的solidity功能,所以ABI中有一些额外信息。例如,回退函数是在Solidity 0.4.0版本时引入的,所以使用0.4.0以下版本编译器生成的ABI没有回退函数的信息,但这些智能合约的行为就像它们有回退函数一样,只不过是空的 函数体和应付修改器。所以应当更新ABI,以便让依赖于较新Solidity版本ABI的应用有关于合约的更佳信息。

solcjs提供了用于更新的API。示例如下:

6.2 solcjs概述 - 数据结构 - 机器学习

其中,0.3.6表示ABI是由0.3.6版本编译器生成的。因为我们正在使用solcjs版本0.4.8,将更新ABI以匹配编译器版本生成的ABI,但不能是更高的版本。

上述代码的输出如下:

6.2 solcjs概述 - 数据结构 - 机器学习


来源:我是码农,转载请保留出处和链接!

本文链接:http://www.54manong.com/?id=553

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646208", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646147", container: s }); })();
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
区块链是什么  

微信号:qq444848023    QQ号:444848023

加入【我是码农】QQ群:864689844(加群验证:我是码农)

<< 上一篇 下一篇 >>
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646186", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646175", container: s }); })();
搜索

网站分类

标签列表

最近发表

    (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https'){ bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else{ bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();

全站首页 | 数据结构 | 区块链| 大数据 | 机器学习 | 物联网和云计算 | 面试笔试

var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1276413723'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s23.cnzz.com/z_stat.php%3Fid%3D1276413723%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));本站资源大部分来自互联网,版权归原作者所有!

jQuery(document).ready(function($){ /* prepend menu icon */ $('#daohangs-around').prepend('
'); /* toggle nav */ $("#caidan-tubiao").on("click", function(){ $("#daohangs").slideToggle(); $(this).toggleClass("active"); }); });

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

©著作权归作者所有:来自ZhiKuGroup博客作者没文化的原创作品,如需转载,请注明出处,否则将追究法律责任 来源:ZhiKuGroup博客,欢迎分享。

评论专区
  • 昵 称必填
  • 邮 箱选填
  • 网 址选填
◎已有 0 人评论
搜索
作者介绍
30天热门
×
×
关闭广告
关闭广告
本站会员尊享VIP特权,现在就加入我们吧!登录注册×
»
会员登录
新用户注册
×
会员注册
已有账号登录
×