问:写一个以太坊智能合约helloworld总共分几步?
答:三步,第一步创建以太坊服务环境;第二步撰写、编译、部署helloworld智能合约;第三步运行智能合约
部署Ethereum环境
本人使用docker搭建Ethereum环境,拉取ubuntu:20.04
镜像。
1 | docker pull --platform linux/amd64 ubuntu:20.04 |
然后,创建容器并开始安装Ethereum
1 | docker run -it --name eth_server --net host ubuntu:20.04 /bin/bash |
安装Ethereum
1 | apt update -y |
安装成功后,确认一下eth版本。
1 | geth version |
初始化
创建配置文件
创建一个genesis.json
的文件,填充如下内容。
1 | { |
初始化Ethereum数据
1 | geth --datadir ./eth-data --allow-insecure-unlock --http --http.addr 172.17.0.2 --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --http.corsdomain "*" --dev init genesis.json |
启动节点
1 | geth --datadir ./eth-data --networkid 110 --allow-insecure-unlock --http --http.addr 172.17.0.2 --http.api "admin,debug,web3,eth,txpool,personal,ethash,miner,net" --http.corsdomain "*" --dev |
networkid
--networkid
参数需要与genesis.json
配置文件中的chainId
值一致。
http.api
若--http.api
设置错误会出现如下错误1
ERROR[11-07|16:13:36.346] Unavailable modules in HTTP API list unavailable=[db] available="[admin debug web3 eth txpool personal ethash miner net]"
需要按照available
中规定的内容进行配置--http.api
参数。
attach交互
接下来需要attach到以太坊节点,在geth节点启动过程中有这样一条日志1
INFO [11-07|16:14:23.848] IPC endpoint opened url=/root/eth-data/geth.ipc
没错,你猜对了,就是要用这个endpoint进行attach
1 | geth attach ipc:/root/eth-data/geth.ipc |
接下来的创建用户
、挖矿
等操作都需要在attach状态下进行。
创建用户
使用personal.newAccout
创建用户,然后可以使用eth.accounts
查看用户列表。
1 | eth.accounts |
若不解锁用户,部署会提示错误creation of HelloWorld errored: authentication needed: password or unlock
。解锁用户
1 | personal.unlockAccount(eth.accounts[1]) |
挖矿
使用miner.start()
开始挖矿;使用miner.stop()
停止挖矿。
开始挖矿后,当出现如下日志信息时,说明挖到了。1
2
3
4
5
6
7
8
9
10
11
12INFO [11-07|17:59:54.021] Successfully sealed new block number=1 sealhash=84eaaa..f4d2c5 hash=925c9a..d8cb75 elapsed=1h30m51.894s
INFO [11-07|17:59:54.022] 🔨 mined potential block number=1 hash=925c9a..d8cb75
INFO [11-07|17:59:54.026] Commit new sealing work number=2 sealhash=865003..0e32da uncles=0 txs=0 gas=0 fees=0 elapsed=1.991ms
INFO [11-07|17:59:54.027] Commit new sealing work number=2 sealhash=865003..0e32da uncles=0 txs=0 gas=0 fees=0 elapsed=3.070ms
INFO [11-07|17:59:54.428] Generating DAG in progress epoch=1 percentage=0 elapsed=3.563s
INFO [11-07|17:59:56.581] Successfully sealed new block number=2 sealhash=865003..0e32da hash=f1224d..9dc4b8 elapsed=2.554s
INFO [11-07|17:59:56.582] 🔨 mined potential block number=2 hash=f1224d..9dc4b8
INFO [11-07|17:59:56.584] Commit new sealing work number=3 sealhash=8de6d6..c8886e uncles=0 txs=0 gas=0 fees=0 elapsed="906.417µs"
INFO [11-07|17:59:56.585] Commit new sealing work number=3 sealhash=8de6d6..c8886e uncles=0 txs=0 gas=0 fees=0 elapsed=2.271ms
INFO [11-07|17:59:57.730] Successfully sealed new block number=3 sealhash=8de6d6..c8886e hash=42a018..4e7ef3 elapsed=1.146s
INFO [11-07|17:59:57.731] 🔨 mined potential block number=3 hash=42a018..4e7ef3
INFO [11-07|17:59:57.733] Commit new sealing work number=4 sealhash=26af07..609e57 uncles=0 txs=0 gas=0 fees=0 elapsed=1.165ms
此时使用eth.blockNumber
可以查看到当前区块数量,使用eth.getBalance(eth.accounts[0])
查看默认用户余额,同理使用eth.getBalance(eth.accounts[1])
查看我们创建的用户的余额。
1 | eth.getBalance(eth.accounts[0]) |
挖到的奖励都进了默认账户了,我们新建的账户里木有哦。来给我转账吧,嘿嘿嘿….
1 | eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(10,'ether')}) |
哇哦,又挖到矿了。
智能合约
环境搭建好了,现在开始编写智能合约。
1 | // compiler version must be greater than or equal to 0.8.13 and less than 0.9.0 |
Remix
在这里获Remix
编译
部署
点击部署,然后选择外部http provider,并配置我们搭建好的ethereum服务。
接下中,选择我们创建好的账户,然后进行部署。
执行智能合约
执行智能合约可以选择在Remix中执行,也可选择attach到eth_server控制台执行。
1 | abi =[{"inputs":[],"name":"sayHelloWorld","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"nonpayable","type":"function"}] |