您的位置:首页 > 其它

开发以太坊遇到的几个问题

2018-03-02 17:41 239 查看
1.数据同步(根据帐号查询历史交易信息) https://ethereum.stackexchange.com/questions/2531/common-useful-javascript-snippets-for-geth/3478#3478
function getTransactionsByAccount(myaccount, startBlockNumber, endBlockNumber) {
if (endBlockNumber == null) {
endBlockNumber = eth.blockNumber;
console.log("Using endBlockNumber: " + endBlockNumber);
}
if (startBlockNumber == null) {
startBlockNumber = endBlockNumber - 1000;
console.log("Using startBlockNumber: " + startBlockNumber);
}
console.log("Searching for transactions to/from account \"" + myaccount + "\" within blocks "  + startBlockNumber + " and " + endBlockNumber);

for (var i = startBlockNumber; i <= endBlockNumber; i++) {
if (i % 1000 == 0) {
console.log("Searching block " + i);
}
var block = eth.getBlock(i, true);
if (block != null && block.transactions != null) {
block.transactions.forEach( function(e) {
if (myaccount == "*" || myaccount == e.from || myaccount == e.to) {
console.log("  tx hash          : " + e.hash + "\n"
+ "   nonce           : " + e.nonce + "\n"
+ "   blockHash       : " + e.blockHash + "\n"
+ "   blockNumber     : " + e.blockNumber + "\n"
+ "   transactionIndex: " + e.transactionIndex + "\n"
+ "   from            : " + e.from + "\n"
+ "   to              : " + e.to + "\n"
+ "   value           : " + e.value + "\n"
+ "   time            : " + block.timestamp + " " + new Date(block.timestamp * 1000).toGMTString() + "\n"
+ "   gasPrice        : " + e.gasPrice + "\n"
+ "   gas             : " + e.gas + "\n"
+ "   input           : " + e.input);
}
})
}
}
}

Example

Find transactions to/from 
eth.accounts[0]
 address:
> getTransactionsByAccount(eth.accounts[0])
Using endBlockNumber: 1864
Using startBlockNumber: 864
Searching for transactions to/from account "0xa7857047907d53a2e494d5f311b4b586dc6a96d2" within blocks 864 and 1864
Searching block 1000
tx hash          : 0x3c3bc3c456a84e20cf0077f9aa5ce363d3b12bca18d01000a750288c2e76401e
nonce           : 44
blockHash       : 0xef2d15775908951fc61f9a83b53c00cf2cde4e0def93e20544f784441c6178db
blockNumber     : 1582
transactionIndex: 0
from            : 0xa7857047907d53a2e494d5f311b4b586dc6a96d2
to              : null
value           : 0
time            : 1470459255 Sat, 06 Aug 2016 04:54:15 GMT
gasPrice        : 20000000000
gas             : 24615
input           : 0x6060604052600a8060106000396000f360606040526008565b00
tx hash          : 0xc255cdbf477452eb8922d8230889f7cc08b9deed4695378aba3d97906071ce5f
nonce           : 45
blockHash       : 0x987a8214af96bb1530b97fe09da8f8168679e42c9efb4defee50800f2067d6d8
blockNumber     : 1587
transactionIndex: 0
from            : 0xa7857047907d53a2e494d5f311b4b586dc6a96d2
to              : null
value           : 0
time            : 1470459409 Sat, 06 Aug 2016 04:56:49 GMT
gasPrice        : 20000000000
gas             : 24615
input           : 0x6060604052600a8060106000396000f360606040526008565b00
...


2.根据交易hash查询raw数据 https://ethereum.stackexchange.com/questions/7473/get-raw-transaction-from-hash
There is an "undocumented" method 
eth_getRawTransactionByHash
 from JSON-RPC
curl -H "Content-Type: application/json" -X POST --data \
'{"jsonrpc":"2.0","method":"eth_getRawTransactionByHash","params":["<TX_HASH>"],"id":1}' http://localhost:8545
<TX_HASH> - transaction idcurl -H "Content-Type: application/json" -X POST --data \'{"jsonrpc":"2.0","method":"eth_getRawTransactionByHash","params":["<TX_HASH>"],"id":1}' http://localhost:8545 请查看我的另一篇文章
http://mp.blog.csdn.net/postedit/79428665
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  eth