ETH(实现Etherscan的eventlog)
2018-09-27 本文已影响0人
坠叶飘香
例如:
https://etherscan.io/tx/0xc9ed903d7e5295e973b0c7b58d98a6c2ff06b0b8edef721f6d702a8bf7c4864e#eventlog
1.获取一笔交易的logs
public void getTransactionReceipt(View view) {
new Thread(new Runnable() {
@Override
public void run() {
String hash = "0xc9ed903d7e5295e973b0c7b58d98a6c2ff06b0b8edef721f6d702a8bf7c4864e";
try {
EthGetTransactionReceipt ethGetTransactionReceipt = mWeb3j.ethGetTransactionReceipt(hash).sendAsync().get();
TransactionReceipt receipt = ethGetTransactionReceipt.getTransactionReceipt();
String raw = ethGetTransactionReceipt.getRawResponse();
List<org.web3j.protocol.core.methods.response.Log> list = receipt.getLogs();
for(org.web3j.protocol.core.methods.response.Log log : list){
Log.e("test", "MainActivity getTransactionReceipt log = " + log);
}
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
其中一个log输出
{
removed=false,
logIndex='0x1a', //26
transactionIndex='0x32',
transactionHash='0xc9ed903d7e5295e973b0c7b58d98a6c2ff06b0b8edef721f6d702a8bf7c4864e',
blockHash='0xcbc4ec88129a9a851f4067ef37f079275ae7765a0e7b2c17a33b4c5009df2b77',
blockNumber='0x619c16',
address='0x0e0989b1f9b8a38983c2ba8053269ca62ec9b195',
data='0x000000000000000000000000000000000000000000000000000003ce683d7122',
type='null',
topics=[0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef,
0x0000000000000000000000001adeed31039138b6197b7da8fe8acbc1885c936a,
0x000000000000000000000000f20b9e713a33f61fa38792d2afaf1cd30339126a]
}
2.解析abi,通过第一步的topics[0]获取event的name
代码:https://github.com/plaichat1986/Ett_Demo/blob/master/src/com/laurel/eth/abi/Test.java
event.nameprivate void handleEventEntry(Abi.Entry entry, String signagure) {
if(entry.type == Abi.Entry.Type.event) {
List<Abi.Entry.Param> paramList = entry.inputs;
for(Abi.Entry.Param param : paramList) {
System.out.println("param.name = " + param.name);
}
System.out.println("entry.formatSignature = " + entry.formatSignature()); //对应topics[0]
System.out.println("test entry.name = " + entry.name + ", signagure = " + signagure);
System.out.println(" ");
}
}
3.topics[0]的解释
event的Keccak-256哈希值
fingerprintSignature