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
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"
通过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
部署智能合约