前言
第一章 一.搭建私有以太坊网络,纯干货
操作系统:MacBook Pro (16-inch, 2019) Geth Version: 1.10.8-stable
Solc Version: 0.8.7+commit.e28d00a7.Darwin.appleclang
本篇文章以部署test.sol为列
创建test.sol
pragma solidity ^0.8.7;
contract test {
function multiply(uint a) public returns(uint d){
return a * 7;
}
}
Bytecode 字节码: 合约代码转换为16进制的字节数据
ABI : json格式数据
拷贝ABI在 Bejson中查看
[
{
"constant": false,
"inputs": [
{
"internalType": "uint256",
"name": "a",
"type": "uint256"
}
],
"name": "multiply",
"outputs": [
{
"internalType": "uint256",
"name": "d",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
安装solc
简介
solc 是 Solidity 源码库的构建目标之一,它是 Solidity 的命令行编译器。你可使用 solc --help 命令来查看它的所有选项的解释。该编译器可以生成各种输出,范围从简单的二进制文件、汇编文件到用于估计“gas”使用情况的抽象语法树(解析树)。如果你只想编译一个文件,你可以运行 solc --bin sourceFile.sol 来生成二进制文件。如果你想通过 solc 获得一些更高级的输出信息,可以通过 solc -o outputDirectory --bin --ast --asm sourceFile.sol 命令将所有的输出都保存到一个单独的文件夹中。
安装
npm install -g solc
安装后通过
solc --version
验证
jingbao@MacBook-Pro Ethereum % solc --version
solc, the solidity compiler commandline interface
Version: 0.8.7+commit.e28d00a7.Darwin.appleclang
部署test.sol
编译test.sol
cd /Users/jingbao/IdeaProjects/smart-contract/privateDemo
jingbao@MacBook-Pro privateDemo % ls -la
total 32
drwxr-xr-x 7 jingbao staff 224 9 15 15:26 .
drwxr-xr-x 8 jingbao staff 256 9 15 00:01 ..
-rw-r--r--@ 1 jingbao staff 6148 9 15 15:26 .DS_Store
drwx------ 5 jingbao staff 160 9 15 10:01 data1
drwx------ 5 jingbao staff 160 9 15 10:06 data2
-rw-r--r--@ 1 jingbao staff 813 9 15 00:40 genesis.json
-rw-r--r-- 1 jingbao staff 129 9 15 15:26 test.sol
通过solc 获取Binary字节码
获取test.sol Binary字节码
solc --bin test.sol
jingbao@MacBook-Pro privateDemo % solc --bin test.sol
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> test.sol
Warning: Function state mutability can be restricted to pure
--> test.sol:5:5:
|
5 | function multiply(uint a) public returns(uint d){
| ^ (Relevant source part starts here and spans across multiple lines).
======= test.sol:test =======
Binary:
608060405234801561001057600080fd5b506101c7806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c7565b60405180910390f35b600060078261006f91906100e2565b9050919050565b6000813590506100858161017a565b92915050565b6000602082840312156100a1576100a0610175565b5b60006100af84828501610076565b91505092915050565b6100c18161013c565b82525050565b60006020820190506100dc60008301846100b8565b92915050565b60006100ed8261013c565b91506100f88361013c565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561013157610130610146565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b6101838161013c565b811461018e57600080fd5b5056fea26469706673582212207a66b40b7ff47413a110d7be31c3f4417ed7afba21646d0bf1206ed0831379b664736f6c63430008070033
复制Binary到链接到geth.ipc的控制台上,切结Binary前要加0x 代表16进制
var code="0x608060405234801561001057600080fd5b506101c7806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c7565b60405180910390f35b600060078261006f91906100e2565b9050919050565b6000813590506100858161017a565b92915050565b6000602082840312156100a1576100a0610175565b5b60006100af84828501610076565b91505092915050565b6100c18161013c565b82525050565b60006020820190506100dc60008301846100b8565b92915050565b60006100ed8261013c565b91506100f88361013c565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561013157610130610146565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b6101838161013c565b811461018e57600080fd5b5056fea26469706673582212207a66b40b7ff47413a110d7be31c3f4417ed7afba21646d0bf1206ed0831379b664736f6c63430008070033"
> var code="0x608060405234801561001057600080fd5b506101c7806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c7565b60405180910390f35b600060078261006f91906100e2565b9050919050565b6000813590506100858161017a565b92915050565b6000602082840312156100a1576100a0610175565b5b60006100af84828501610076565b91505092915050565b6100c18161013c565b82525050565b60006020820190506100dc60008301846100b8565b92915050565b60006100ed8261013c565b91506100f88361013c565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561013157610130610146565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b6101838161013c565b811461018e57600080fd5b5056fea26469706673582212207a66b40b7ff47413a110d7be31c3f4417ed7afba21646d0bf1206ed0831379b664736f6c63430008070033"
undefined
> code
"0x608060405234801561001057600080fd5b506101c7806100206000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c7565b60405180910390f35b600060078261006f91906100e2565b9050919050565b6000813590506100858161017a565b92915050565b6000602082840312156100a1576100a0610175565b5b60006100af84828501610076565b91505092915050565b6100c18161013c565b82525050565b60006020820190506100dc60008301846100b8565b92915050565b60006100ed8261013c565b91506100f88361013c565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561013157610130610146565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b6101838161013c565b811461018e57600080fd5b5056fea26469706673582212207a66b40b7ff47413a110d7be31c3f4417ed7afba21646d0bf1206ed0831379b664736f6c63430008070033"
通过solc获取abi
切换原窗口获取abi
jingbao@MacBook-Pro privateDemo % solc --abi test.sol
Warning: SPDX license identifier not provided in source file. Before publishing, consider adding a comment containing "SPDX-License-Identifier: <SPDX-License>" to each source file. Use "SPDX-License-Identifier: UNLICENSED" for non-open-source code. Please see https://spdx.org for more information.
--> test.sol
Warning: Function state mutability can be restricted to pure
--> test.sol:5:5:
|
5 | function multiply(uint a) public returns(uint d){
| ^ (Relevant source part starts here and spans across multiple lines).
======= test.sol:test =======
Contract JSON ABI
[{"inputs":[{"internalType":"uint256","name":"a","type":"uint256"}],"name":"multiply","outputs":[{"internalType":"uint256","name":"d","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]
复制Binary到链接geth.ipc的控制台
> var abi = [{"inputs":[{"internalType":"uint256","name":"a","type":"uint256"}],"name":"multiply","outputs":[{"internalType":"uint256","name":"d","type":"uint256"}],"stateMutability":"nonpayable","type":"function"}]
undefined
> abi
[{
inputs: [{
internalType: "uint256",
name: "a",
type: "uint256"
}],
name: "multiply",
outputs: [{
internalType: "uint256",
name: "d",
type: "uint256"
}],
stateMutability: "nonpayable",
type: "function"
}]
查看余额是否够部署智能合约
> eth.getBalance(eth.accounts[0])
146000000000000000000
解锁账户
> personal.unlockAccount(eth.accounts[0])
Unlock account 0x88d2a3c6575dc35e9014fd13f530dd326b254399
Passphrase:
true
获取预估部署合约需要花费手续费
> web3.eth.estimateGas({data: code})
151081
151081gas
部署智能合约
创建合约实例
> var myContract = eth.contract(abi)
undefined
> var contract = myContract.new({from:eth.accounts[0],data:code,gas:170000})
undefined
参数讲解
from: 通过哪个账户部署合约
data: Binary字节码
gas: 愿意支付的gas费,应超过151081gas
如出现 Error: authentication needed: password or unlock
原因:刚才的解锁超时需要从新解锁
打印contract
> contract
{
abi: [{
inputs: [{...}],
name: "multiply",
outputs: [{...}],
stateMutability: "nonpayable",
type: "function"
}],
address: undefined,
transactionHash: "0xd1a10750c1f140303676538bc33bba9186f7fd512c4c53c32aa5093e7edf059e"
}
获取当前待办
> txpool.status
{
pending: 1,
queued: 0
}
pending: 1 说明有一个待办,还没有部署完
开始挖矿 8核
> miner.start(8)
null
停止挖矿
> miner.stop()
null
再次txpool.status查看
> txpool.status
{
pending: 0,
queued: 0
}
pending: 0,说明已经完成
验证是否成功
获取合约地址
> contract.address
"0x1b9ef78423727bd0893739e3d57bc9e33b0cb616"
通过合约地址获取字节码
> eth.getCode(contract.address)
"0x608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c6888fa114610030575b600080fd5b61004a6004803603810190610045919061008b565b610060565b60405161005791906100c7565b60405180910390f35b600060078261006f91906100e2565b9050919050565b6000813590506100858161017a565b92915050565b6000602082840312156100a1576100a0610175565b5b60006100af84828501610076565b91505092915050565b6100c18161013c565b82525050565b60006020820190506100dc60008301846100b8565b92915050565b60006100ed8261013c565b91506100f88361013c565b9250817fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff048311821515161561013157610130610146565b5b828202905092915050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b6101838161013c565b811461018e57600080fd5b5056fea26469706673582212207a66b40b7ff47413a110d7be31c3f4417ed7afba21646d0bf1206ed0831379b664736f6c63430008070033"
调用合约方法
> contract.multiply(6)
Error: invalid address
at web3.js:3940:11(47)
at web3.js:3756:41(16)
at web3.js:5035:37(8)
at web3.js:5034:12(13)
at web3.js:5060:34(20)
at web3.js:5085:39(15)
at web3.js:4147:41(57)
at web3.js:4233:75(24)
at <eval>:1:19(3)
报错原因是因为方法没有加view ,可以读取状态变量但是不能改,要加.call
> contract.multiply.call(6)
42
ok,大功告成
下一篇文章 三.以太坊多节点私链搭建
点赞关注订阅持续更新
点击关注订阅持续更新
点击关注订阅持续更新
有问题或文章有需要改正的地方请留言 或 pwn2ownyearn@gmail.com 邮箱
|