### 前言和注意事项 本文章需要按照要求一步步安装,不能省下任意一个部件,下面文章内容会根据需求告诉你哪些部件可以直接下载,或者你可以选择自行编译。 Solidity,Abigen 都是windows版的,请自行更换版本,都差不多的。 文末有文件下载地址。 [scode type="red" size=""]以下文章和部件需要你最好处于科学上网的情况,挂着代理才能够政策安装,,否则容易下载失败和下载速度极慢。[/scode] --- #### 步骤 ##### 安装Solidity编译器 这里根据文章有两种方法,也有语言的不同。 [scode type="blue" size=""] 安装 `truffle`,进行开发,适用于Nodejs,即使你使用Golang等语言开发也强烈推荐安装。 [/scode] Golang可以直接先安装Solidity。 去Github下载相应系统的文件,如我是exe则是下载exe文件。  我下载的[solc-windows.exe](https://github.com/ethereum/solidity/releases),然后重命名为solc.exe,放置系统变量环境。 [collapse status="true" title="如何防止exe文件到系统变量后终端可以直接使用"] 1. 将exe文件放入一个目录 2. 电脑右键属性->选择高级系统设置->点击环境变量 3. 选择系统环境变量的Path 4. 编辑->新建->目录地址 5.  [/collapse] --- #### 安装Abigen [scode type="red" size=""]部分文章是叫你下载go-ethereum并make,这里建议直接go build[/scode] ``` go get -t -v github.com/ethereum/go-ethereum ``` 这里建议根据mod和放置到全局进行自主判断 下载完毕后进入目录,Go全局的话则是安装GoSDK地址的bin目录,如: D:\GO_SDK\bin\src\github.com\ethereum\go-ethereum 模块则是: ``` $GOPATH/src/github.com/ethereum/go-ethereum/ ``` 而后进入:github.com\ethereum\go-ethereum\cmd\abigen 目录 然后 go build 等待下载库和编译的时间,会生成一个exe的文件,然后按照防止solc文件的步骤也放进去。 --- ##### solc和abigen exe文件的压缩包 [点击链接的SolcAndAbigen.zip压缩包进行下载](https://github.com/StarYuhen/Ethereum_GolangStudy/tree/master/goethereumbook/) --- #### 智能合约编写及编译 ``` contract Store { event ItemSet(bytes32 key, bytes32 value); string public version; mapping(bytes32 => bytes32) public items; constructor(string memory _version) { version = _version; } function setItem(bytes32 key, bytes32 value) external { items[key] = value; emit ItemSet(key, value); } } ``` ***注意事项:*** **Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: ``" to e ach source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.** 解决方法: 第一行注释 ``` // SPDX-License-Identifier: MIT ``` --- **Error: Source file requires different compiler version (current compiler is 0.8.17+commit.8df45f5f.Windows.msvc) - note that nightly builds are considered to be str ictly less than the released version** 解决方法:更改为自己solc的系统版本  如这里是0.8.17,则是: ``` pragma solidity ^0.8.17; ``` --- **Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.** 解决方法:将方法的public删除,如: ``` constructor(string memory _version) public { version = _version; } ``` 改为: ``` constructor(string memory _version) { version = _version; } ``` --- **Error: Data location must be "storage" or "memory" for constructor parameter, but none was given.** 解决方法: ``` constructor(string _version) { version = _version; } ``` 协议要求需要声明内存,所以需要传参加上memory类型。 ``` constructor(string memory _version) { version = _version; } ``` --- #### solc编译bin和abi 文件 ``` solc --abi --bin test.sol ``` 若是想要输出文件则是: ``` solc --abi --bin -o ./ test.sol ``` [scode type="green" size=""]记住必须进入sol所在的目录[/scode] --- #### abigen 结合bin和abi编译Golang文件 ``` abigen --bin=Store.bin --abi=Store.abi --pkg=store --out=Store.go ``` --- [1]: https://api.yuhenm.top/api/file/source?name=21a473d8-cf59-48cd-9e87-d4a977847f74.jpg Loading... ### 前言和注意事项 本文章需要按照要求一步步安装,不能省下任意一个部件,下面文章内容会根据需求告诉你哪些部件可以直接下载,或者你可以选择自行编译。 Solidity,Abigen 都是windows版的,请自行更换版本,都差不多的。 文末有文件下载地址。 <div class="tip inlineBlock error"> 以下文章和部件需要你最好处于科学上网的情况,挂着代理才能够政策安装,,否则容易下载失败和下载速度极慢。 </div> --- #### 步骤 ##### 安装Solidity编译器 这里根据文章有两种方法,也有语言的不同。 <div class="tip inlineBlock info"> 安装 `truffle`,进行开发,适用于Nodejs,即使你使用Golang等语言开发也强烈推荐安装。 </div> Golang可以直接先安装Solidity。 去Github下载相应系统的文件,如我是exe则是下载exe文件。  我下载的[solc-windows.exe](https://github.com/ethereum/solidity/releases),然后重命名为solc.exe,放置系统变量环境。 <div class="panel panel-default collapse-panel box-shadow-wrap-lg"><div class="panel-heading panel-collapse" data-toggle="collapse" data-target="#collapse-3782b12967fd53d382c9f4cd645ffe2972" aria-expanded="true"><div class="accordion-toggle"><span style="">如何防止exe文件到系统变量后终端可以直接使用</span> <i class="pull-right fontello icon-fw fontello-angle-right"></i> </div> </div> <div class="panel-body collapse-panel-body"> <div id="collapse-3782b12967fd53d382c9f4cd645ffe2972" class="collapse in collapse-content"><p></p> 1. 将exe文件放入一个目录 2. 电脑右键属性->选择高级系统设置->点击环境变量 3. 选择系统环境变量的Path 4. 编辑->新建->目录地址 5.  <p></p></div></div></div> --- #### 安装Abigen <div class="tip inlineBlock error"> 部分文章是叫你下载go-ethereum并make,这里建议直接go build </div> ``` go get -t -v github.com/ethereum/go-ethereum ``` 这里建议根据mod和放置到全局进行自主判断 下载完毕后进入目录,Go全局的话则是安装GoSDK地址的bin目录,如: D:\GO_SDK\bin\src\github.com\ethereum\go-ethereum 模块则是: ``` $GOPATH/src/github.com/ethereum/go-ethereum/ ``` 而后进入:github.com\ethereum\go-ethereum\cmd\abigen 目录 然后 go build 等待下载库和编译的时间,会生成一个exe的文件,然后按照防止solc文件的步骤也放进去。 --- ##### solc和abigen exe文件的压缩包 [点击链接的SolcAndAbigen.zip压缩包进行下载](https://github.com/StarYuhen/Ethereum_GolangStudy/tree/master/goethereumbook/) --- #### 智能合约编写及编译 ``` contract Store { event ItemSet(bytes32 key, bytes32 value); string public version; mapping(bytes32 => bytes32) public items; constructor(string memory _version) { version = _version; } function setItem(bytes32 key, bytes32 value) external { items[key] = value; emit ItemSet(key, value); } } ``` ***注意事项:*** **Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: `<SPDX-License>`" to e ach source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.** 解决方法: 第一行注释 ``` // SPDX-License-Identifier: MIT ``` --- **Error: Source file requires different compiler version (current compiler is 0.8.17+commit.8df45f5f.Windows.msvc) - note that nightly builds are considered to be str ictly less than the released version** 解决方法:更改为自己solc的系统版本  如这里是0.8.17,则是: ``` pragma solidity ^0.8.17; ``` --- **Warning: Visibility for constructor is ignored. If you want the contract to be non-deployable, making it "abstract" is sufficient.** 解决方法:将方法的public删除,如: ``` constructor(string memory _version) public { version = _version; } ``` 改为: ``` constructor(string memory _version) { version = _version; } ``` --- **Error: Data location must be "storage" or "memory" for constructor parameter, but none was given.** 解决方法: ``` constructor(string _version) { version = _version; } ``` 协议要求需要声明内存,所以需要传参加上memory类型。 ``` constructor(string memory _version) { version = _version; } ``` --- #### solc编译bin和abi 文件 ``` solc --abi --bin test.sol ``` 若是想要输出文件则是: ``` solc --abi --bin -o ./ test.sol ``` <div class="tip inlineBlock success"> 记住必须进入sol所在的目录 </div> --- #### abigen 结合bin和abi编译Golang文件 ``` abigen --bin=Store.bin --abi=Store.abi --pkg=store --out=Store.go ``` --- [1]: https://api.yuhenm.top/api/file/source?name=21a473d8-cf59-48cd-9e87-d4a977847f74.jpg 最后修改:2023 年 03 月 26 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 8 如果觉得我的文章对你有用,请随意赞赏
3 条评论
神级教程,感恩博主
哇,万分感谢博主分享的abi编译exe文件,我快被这些东西折磨疯了,幸好有个现成的(´இ皿இ`)
博主真有品味,可以分享下歌单吗