以太坊私有链搭建智能合约 mac
编译环境:
安装solidity编译器(智能合约编译器)
brew tap ethereum/ethereum
brew install solidity
这样会直接下载到solidity的最新版本。
如果你需要特定版本的 Solidity ,你需要从 Github 上安装一个 Homebrew formula。 你可查阅 solidity.rb commits on Github 的提交记录,去寻找包含 solidity.rb 文件改动的特殊提交。然后使用 brew 进行安装:
brew unlink solidity
# Install 0.4.8
brew install https://raw.githubusercontent.com/ethereum/homebrew-ethereum/77cce03da9f289e5a3ffe579840d3c5dc0a62717/solidity.rb
以下以solidity 0.7.0版本为例,如果版本不同,可能存在编译和语法上的区别
创建智能合约
1、在geth目录下新建一个contract文件夹。方便后面上链。这个geth目录是指创建初始块所在的目录。比如geth init ./genesis.json --datadir ./mychain。则目录则是datadir所指的mychain目录。
2、contract文件夹里新建一个demo.sol智能合约,内容如下:
pragma solidity ^0.7.0;
contract Demo {
string text = "Hello World!";
function say() view public returns (string memory){
return text;
}
}
3、编译
>solc -o . --bin --abi demo.sol
>ll
total 24
-rw-r--r-- 1 hpy staff 135B 8 13 11:10 Demo.abi
-rw-r--r-- 1 hpy staff 1.3K 8 13 11:10 Demo.bin
-rw-r--r-- 1 hpy staff 144B 8 13 11:09 demo.sol
生成Demo.abi和Demo.bin两个文件
4、修改生成的文件,使其在geth中加载简单化
Damo.abi文件内容修改为
var demoContract = eth.contract([{"inputs":[],"name":"say","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"}])
新添加var demoContract = eth.contract()
Demo.bin文件内容修改为
personal.unlockAccount(eth.accounts[0]) // 如果你的账号没有解锁的话,需要此步骤,否则忽略
var demo = demoContract.new({
from:eth.accounts[0],
data:"0x60806040526040518060400160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506000908051906020019061004f929190610062565b5034801561005c57600080fd5b506100ff565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b5b808211156100fb5760008160009055506001016100e3565b5090565b61018b8061010e6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063954ab4b214610030575b600080fd5b6100386100b3565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561007857808201518184015260208101905061005d565b50505050905090810190601f1680156100a55780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561014b5780601f106101205761010080835404028352916020019161014b565b820191906000526020600020905b81548152906001019060200180831161012e57829003601f168201915b505050505090509056fea26469706673582212202dc1950f91138cc1af1f78f6514e8712e7a370461184d5ebe1c5582f0615fac564736f6c63430007000033",
gas:500000
})
原来里面的内容加上0x放在data后
智能合约上私有链
进入创建的私有链中。geth命令下:
> loadScript("contract/Demo.abi")
undefined
> loadScript("contract/Demo.bin")
undefined
验证是否上链成功
> demo
{
abi: [{
inputs: [],
name: "say",
outputs: [{...}],
stateMutability: "view",
type: "function"
}],
address: undefined,
transactionHash: "0x7aeec8a912a384d5fd7f9fcbf218eb26341cdda2e6bd814e0027fe4867733265"
}
返回如上则成功
开始挖矿
> miner.start()
null
输入命令eth.blockNumber可知道块数是否增加,从而可判断是否在挖矿
> demo.say
function()
> demo.say.call()
"Hello World!"
则成功调用合约方法!!!happy
关闭挖矿
>miner.stop()
null
来源:oschina
链接:https://my.oschina.net/u/4346575/blog/4497082