以下所有操作均在私有链及 geth js 交互控制台下
查询 coinbase
余额
>web3.fromWei(web3.eth.getBalance(eth.coinbase),"ether”)
240
coinbase 中的ether可以转给其他externally owned accounts(EOAs) 或者 accounts account
假设我们部署了一个合约,合约地址为:
0x2bda4364bb076187f0ef0067a61ccb95d636e383
现在通过 geth js 交互控制台从 coinbase 账户转 1 个 ether 给合约地址。
查看合约地址中 ether 数量:
> web3.fromWei(web3.eth.getBalance("0x2bda4364bb076187f0ef0067a61ccb95d636e383"),"ether")
0
首先解锁 coinbase 账号
personal.unlockAccount(eth.coinbase, "account_pwd", 10000000000)
转账:
> eth.sendTransaction({from:eth.coinbase,to:"0x2bda4364bb076187f0ef0067a61ccb95d636e383",value:web3.toWei(1,"ether")})
当 geth 中的地址发生交易时,geth 服务的日志(请查看Ethereum 私有链和 web3.js 使用如何设置 geth 日志) 会打印出类似下面这样的日志:
I1112 11:51:15.338680 eth/api.go:1185] Tx(0xb3515f0245f0f14f2756e45f402a8dd058b387e596ca50fb3ec9213390596c17) to: 0x2bda4364bb076187f0ef0067a61ccb95d636e383
此时说明交易已经发生了,等待被区块链确认状态(pending),在部署的私有测试链中可以开启挖矿 miner.start(1) 来确认这笔交易。在测试链中一般开启挖矿之后挖到一个块,发生的交易即可被确认,此时可以关闭挖矿了 miner.stop()
此时再查看合约地址上的 ether 数量,你就可以发现增加了一个。
写了一个脚本监控 geth 钱包中所有地址交易被确认。会打印出以下日志:
============ NEW DEPOSIT ============
TXID: 0x3102e122728fd768a58a9e2667dd6b05e3325768e63e69ff60349af41f15d6ec
RAW:
{"address":"0x2bda4364bb076187f0ef0067a61ccb95d636e383","blockHash":"0x32dc9feeff2f2dc4c6c72c30a175caee5ef25d8b1656044fab1494e6d189c52d","blockNumber":83,"logIndex":0,"transactionHash":"0x3102e122728fd768a58a9e2667dd6b05e3325768e63e69ff60349af41f15d6ec","transactionIndex":0,"event":"Deposit","args":{"_from":"0xbd2d69e3e68e1ab3944a865b3e566ca5c48740da","_band":"88","_value":"1000000000000000000"}}
PAYLOAD:
{"txid":"0x3102e122728fd768a58a9e2667dd6b05e3325768e63e69ff60349af41f15d6ec","block":83,"amount":"1","address":"0x2bda4364bb076187f0ef0067a61ccb95d636e383","band":"88","asset_key":"eth"}
PUBLISH
=====================================
以上就是从应用开发层面分析以太坊中交易的全部过程。
网友评论