以太坊(Ethereum)私链建立 、合约编译、部署完全教程(1)

当前位置:首页 > 币圈百科 > 以太坊(Ethereum)私链建立 、合约编译、部署完全教程(1)

以太坊(Ethereum)私链建立 、合约编译、部署完全教程(1)

2022-11-22币圈百科403

fg

开源工具和语言

一、brewMacOS包管理器

/usr/bin/ruby-e ' $(curl-fsSL https://raw。githubusercontent。com/home brew/install/master/install)'

二、坚固性以太坊智能合约语言

酿造安装可靠性

备注:安装时间可能有点长,请耐心等待…?备注:安装时间可能有点长,请耐心等待…?备注:安装时间可能有点长,请耐心等待…

如果碰见下面的错误,请移步:http://博客。csdn。net/sico 2 sico/article/details/71082130

MAC OS钥匙串中的开源代码库凭据可能无效。请使用:printf ' protocol=https \ n host=github。com \ n ' | git凭据-OSX钥匙链时代或创建个人访问令牌:https://github。com/settings/tokens/new?scopes=gist,public _ repo description=home brew

三、geth运行以太坊节点

下载源代码(焦油。gz)

李月春:下载李月春$ CD go-以太坊-1.5.9李月春:去-以太坊-1.5.9李月春$残疾人/用户/李月春/下载/去-以太坊-1 .5 .9李月春:去-以太坊-1 .5 .9李月春$ make geth

建立私链

1 .创建一个文件夹来存储你的私链数据

李月春:1015岳春丽$ mkdir privchanliyuechun:1015岳春丽$ pwd/Users/liyue chun/Desktop/1015李月春:1015岳春丽$ lsprivchanliyuechun:1015岳春丽$

2。使用geth来加载

geth-RPC-rpcaddr 127。0 .0 .1个RPC端口8545-dev-datadir特权链

执行上面的命令,你应该能看到下面的信息:

INFO [10-1503:14:50] IPC端点打开:/Users/李越春/桌面/1015/priv chain/geth。IPC信息[10-1503:14:50]HTTP端点打开:http://127.0.0.1:8545

如果你切换到私有链文件夹里面,你会看到geth?geth.ipc,和?密钥库。

李月春:1015岳春丽$ CD privchain/李月春:特权链岳春丽$ lsgethgeth。ipckeystoreliyuehun:priv链岳春丽$保持节点的运行,不要关闭终端,重新打开一个终端,使用获取附件连接节点,并且打开Geth控制台chun:priv chain岳$ Geth attach IPC:/Users/李越chun/Desktop/1015/priv chain/Geth。工业程序控制( industrial process control的缩写)欢迎来到Geth JavaScript控制台!实例:Geth/v 1。7 .1-稳定-05101641/达尔文-amd64/go 1。9 .一模块:admin:1.0 debug:1.0 eth:1.0 miner:1.0 net:1.0 personal:1.0 RPC:1.0 shh:1.0 tx pool:1.0 web 3:1.0

3 .相关美国石油学会(美国石油协会)命令

查看账户

个人。列表帐户[]

创建账户

个人。新帐户('李越春')' 0 XB 6d 7d 842 e 7 DC 9016 fa 6900 a 183 B2 be 26 fc 90 B2 d 8 '

PS:里面的李月春是你账户的密码,输入你自己喜欢的密码

查看账户

个人。列出帐户[' 0 XB 6d 7d 842 E7 DC 9016 fa 6900 a183 B2 be 26 fc 90 B2 D8 ']

4 . web 3命令

https://以太构建器。git书籍。io/guide/content/en/ether eum _ JavaScript _ API。html

web 3。eth。硬币基地' 0 XB 6d 7d 842 E7 DC 9016 fa 6900 a183 B2 be26 fc 90 B2 D8 '

5。编写智能合约代码

pragma solidity ^0.4.4;契约测试{函数乘法(单位a)返回(uint d){返回a * 7;}}

6 .获取智能合约字节码和abi

代码拷贝到https://remix.ethereum.org,编译,然后拷贝字节码。

6060604052341561000 f 576000080 FD 5b 60 ab 8061001 e 6000396000 f 30060604052600357 c 0100000000000000000000000000000000000000000000000000000000000000000。在贝森中转义成字符串

http://www。贝森。com

{ \ ' contract _ name \ ':\ ' test \ ',\'abi\':[{\'constant\':false,\'inputs\':[{\'name\':\'a\ ',\'type\':\'uint256\'}],\'name\':\'multiply\ ',\'outputs\':[{\'name\':\'d\ ',\'type\ '通过abi创建合约对象

风险值合同信息=JSON。解析(' { \ ' contract _ name \ ':\ ' test \ ',\'abi\':[{\'constant\':false,\'inputs\':[{\'name\ ',\'type\':\'uint256\'}],\'name\':\'multiply\ ',\'outputs\':[{\'name\':\'d\ ',\'type\':\'uint256.}],名称:'乘法,输出:[{.}],应付款:false,type: 蓑衣网小编2022 'function' }],eth:{ accounts:[' 0 x2 abf 46d 8 b 0d 940 cdeedd 55872 BC 0648 add 40227d '],blockNumber: 384,币基:' 0 x2 abf 46d 8 b 0d 940 cdeedd 55872 BC 0648 add 40227d ',编译:lll: function(),serpent: function(),solidity: function() },defaultAccount: undefined,defaultBlock:'最新,汽油价格:0k), getBlockTransactionCount: function(), getBlockUncleCount: function(), getCode: function(), getCoinbase: function(callback), getCompilers: function(), getGasPrice: function(callback), getHashrate: function(callback), getMining: function(callback), getPendingTransactions: function(callback), getProtocolVersion: function(callback), getRawTransaction: function(), getRawTransactionFromBlock: function(), getStorageAt: function(), getSyncing: function(callback), getTransaction: function(), 蓑衣网小编2022 getTransactionCount: function(), getTransactionFromBlock: function(), getTransactionReceipt: function(), getUncle: function(), getWork: function(), iban: function(iban), icapNamereg: function(), isSyncing: function(callback), namereg: function(), resend: function(), sendIBANTransaction: function(), sendRawTransaction: function(), sendTransaction: function(), sign: function(), signTransaction: function(), submitTransaction: function(), submitWork: function() }, at: function(address, callback), getData: function(), new: function()}

8. 检查coinbase账号余额

> account1 = web3.eth.coinbase"0x2abf46d8b0d940cdeedd55872bc0648add40227d"> web3.eth.getBalance(account1)0>

如果余额大于0,继续,否则,开始挖矿。

> miner.start();null>

挖矿过程中,切换到节点终端,你会发现一直在挖矿。

蓑衣网小编2022

gif1如果你觉得差不多了,可以运行下面的命令停止挖矿。

miner.stop();

9. 停止挖矿,并且查余额

> miner.start();null> miner.stop();true> web3.eth.getBalance(account1)1.152e+21>

10. 解锁coinbase账号,我们通过coinbase账号来付费部署合约

liyuechun: 换成你的密码。

> personal.unlockAccount(account1, 'liyuechun') true>

11. 预估手续费

> bytecode = "6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a7230582067d7c851e14e862886b6f53dad6825135557fb3a4b691350c94ea5b80605f6770029""6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a7230582067d7c851e14e862886b6f53dad6825135557fb3a4b691350c94ea5b80605f6770029"> web3.eth.estimateGas({data: bytecode})Error: invalid argument 0: json: cannot unmarshal hex string without 0x prefix into Go struct field CallArgs.data of type hexutil.Bytes at web3.js:3104:20 at web3.js:6191:15 at web3.js:5004:36 at :1:1> bytecode = "0x6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a7230582067d7c851e14e862886b6f53dad6825135557fb3a4b691350c94ea5b80605f6770029""0x6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a7230582067d7c851e14e862886b6f53dad6825135557fb3a4b691350c94ea5b80605f6770029"> web3.eth.estimateGas({data: bytecode})98391>

备注:字节码前面需要添加0x。手续费大概为98391wei。

12. 部署合约,为了方便理解,设置一个回调函数

> contractInstance = myContract.new({data: bytecode gas: 1000000, from: account1}, function(e, contract){ if(!e){ if(!contract.address){ console.log("Contract transaction send: Transaction Hash: "+contract.transactionHash+" waiting to be mined..."); }else{ console.log("Contract mined! Address: "+contract.address); console.log(contract); } }else{ console.log(e) }})Contract transaction send: Transaction Hash: 0x5e2aebbf400d71a32e807dc3f11f1053b6ee3b2a81435ed8ace2fa54eebb9f3d waiting to be mined...{ abi: [{ constant: false, inputs: [{...}], name: "multiply", outputs: [{...}], payable: false, type: "function" }], address: undefined, transactionHash: "0x5e2aebbf400d71a32e807dc3f11f1053b6ee3b2a81435ed8ace2fa54eebb9f3d"}>

13. 你的合约等待挖矿,开始挖矿,等一会儿,停止

> miner.start()null> Contract mined! Address: 0xbf8b24283f2516360d3a4ba1db0df78ae74689db[object Object]> miner.stop()true>

wakuang114. 检查合约是否部署成功

> eth.getCode(contractInstance.address)"0x60606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a7230582067d7c851e14e862886b6f53dad6825135557fb3a4b691350c94ea5b80605f6770029">

15. 调用合约方法

> contractInstance.multiply.call(6)42>

PS: 这里添加call的原因是因为multiply函数没有添加constant。

pragma solidity ^0.4.4;contract test { function multiply(uint a) returns(uint d){ return a * 7; }}

Over Game!!!!

技术交流

区块链技术交流QQ群:348924182
以太坊(Ethereum)私链建立 、合约编译、部署完全教程(1) | 分享给朋友: