你可以在web3j库的帮助下使用java轻松监听以太坊交易,但此库无法监听Erc20 Token交易。

要监听Erc20Token交易,你必须使用在合约(token)创建时的token封装类。我假设你已经使用最少的功能部署了合约,因此你的封装类看起来像这样:

package com.bolenum.util;

import java.io.IOException;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util...;

import org.web3j.abi.EventEncoder;

import org.web3j.abi...

import org.web3j.crypto.Credentials;

import org.web3j.protocol.Web3j;

import org.web3j.protocol...;

import org.web3j.tx.Contract;

import org.web3j.tx.TransactionManager;

import rx.Observable;

import rx.functions.Func1;

...

public final class Erc20TokenWrapper extends Contract {

private static final String BINARY = "contract binary key";

private Erc20TokenWrapper(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);

}

private Erc20TokenWrapper(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);

}

public List getTransferEvents(TransactionReceipt transactionReceipt) {

...

return responses;

}

public Observable transferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {

...

}

...

public Uint256 balanceOf(Address _owner) throws IOException {

Function function = new Function("balanceOf",

Arrays.asList(_owner),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

...

public TransactionReceipt transfer(Address _to, Uint256 _amount) throws IOException, TransactionException {

Function function = new Function("transfer", Arrays.asList(_to, _amount), Collections.>emptyList());

return executeTransaction(function);

}

...

public static Erc20TokenWrapper load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

return new Erc20TokenWrapper(contractAddress, web3j, transactionManager, gasPrice, gasLimit);

}

public static class TransferEventResponse {

public Address _from;

public Address _to;

public Uint256 _value;

public String _transactionHash;

}

...

}

现在你必须使用这个类函数来加载合约然后监听交易。使用下面的代码加载和监听交易:

Web3j web3j = Web3j.build(new HttpService("url of your ethereum blockchain"))

ClientTransactionManager transactionManager = new ClientTransactionManager(web3j,

"your deployed contract addess");

Erc20TokenWrapper token = Erc20TokenWrapper.load("your deployed contract addess", web3j, transactionManager,

Contract.GAS_PRICE, Contract.GAS_LIMIT);

token.transferEventObservable(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST)

.subscribe(tx -> {

String toAddress = tx._to.getValue();

String fromAddress = tx._from.getValue();

String txHash = tx._transactionHash.getValue();

}

如果你已经部署了合约,它由第三人部署,那么你可以直接使用我的包装类,只需更改你可以从https://etherscan.io/tokens很容易获得的二进制密钥。

结论:因此你可以将此代码用于任何token的监听交易。此代码为你提供address,fromAddress和transactionHash。所以这些东西你可以根据你的要求使用,你可以将它们保存在你的数据库中,或者你只保存地址是你的钱包地址的交易。

谢谢,我希望这会有所帮助。

如果希望快速进行web3j、java、以太坊开发,那请看我们精心打造的教程:

java以太坊开发教程,主要是针对java和android程序员进行区块链以太坊开发的web3j详解。

这里是原文

完整代码如下:

package com.bolenum.util;

import java.io.IOException;

import java.math.BigInteger;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

import java.util.concurrent.Future;

import org.web3j.abi.EventEncoder;

import org.web3j.abi.EventValues;

import org.web3j.abi.FunctionEncoder;

import org.web3j.abi.TypeReference;

import org.web3j.abi.datatypes.Address;

import org.web3j.abi.datatypes.Event;

import org.web3j.abi.datatypes.Function;

import org.web3j.abi.datatypes.Type;

import org.web3j.abi.datatypes.Utf8String;

import org.web3j.abi.datatypes.generated.Uint256;

import org.web3j.abi.datatypes.generated.Uint8;

import org.web3j.crypto.Credentials;

import org.web3j.protocol.Web3j;

import org.web3j.protocol.core.DefaultBlockParameter;

import org.web3j.protocol.core.RemoteCall;

import org.web3j.protocol.core.methods.request.EthFilter;

import org.web3j.protocol.core.methods.response.Log;

import org.web3j.protocol.core.methods.response.TransactionReceipt;

import org.web3j.protocol.exceptions.TransactionException;

import org.web3j.tx.Contract;

import org.web3j.tx.TransactionManager;

import rx.Observable;

import rx.functions.Func1;

/**

* Auto generated code.

* Do not modify!

* Please use the web3j command line tools, or {@link org.web3j.codegen.SolidityFunctionWrapperGenerator} to update.

*

*

Generated with web3j version 2.3.1.

*/

public final class Erc20TokenWrapper extends Contract {

private static final String BINARY = "contract binary key";

private Erc20TokenWrapper(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);

}

private Erc20TokenWrapper(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);

}

public List getTransferEvents(TransactionReceipt transactionReceipt) {

final Event event = new Event("Transfer",

Arrays.>asList(new TypeReference

() {}, new TypeReference
() {}),

Arrays.>asList(new TypeReference() {}));

List valueList = extractEventParameters(event, transactionReceipt);

ArrayList responses = new ArrayList(valueList.size());

for (EventValues eventValues : valueList) {

TransferEventResponse typedResponse = new TransferEventResponse();

typedResponse._from = (Address) eventValues.getIndexedValues().get(0);

typedResponse._to = (Address) eventValues.getIndexedValues().get(1);

typedResponse._value = (Uint256) eventValues.getNonIndexedValues().get(0);

responses.add(typedResponse);

}

return responses;

}

public Observable transferEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {

final Event event = new Event("Transfer",

Arrays.>asList(new TypeReference

() {}, new TypeReference
() {}),

Arrays.>asList(new TypeReference() {}));

EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());

filter.addSingleTopic(EventEncoder.encode(event));

return web3j.ethLogObservable(filter).map(new Func1() {

@Override

public TransferEventResponse call(Log log) {

EventValues eventValues = extractEventParameters(event, log);

TransferEventResponse typedResponse = new TransferEventResponse();

typedResponse._from = (Address) eventValues.getIndexedValues().get(0);

typedResponse._to = (Address) eventValues.getIndexedValues().get(1);

typedResponse._value = (Uint256) eventValues.getNonIndexedValues().get(0);

typedResponse._transactionHash = log.getTransactionHash();

return typedResponse;

}

});

}

public List getApprovalEvents(TransactionReceipt transactionReceipt) {

final Event event = new Event("Approval",

Arrays.>asList(new TypeReference

() {}, new TypeReference
() {}),

Arrays.>asList(new TypeReference() {}));

List valueList = extractEventParameters(event, transactionReceipt);

ArrayList responses = new ArrayList(valueList.size());

for (EventValues eventValues : valueList) {

ApprovalEventResponse typedResponse = new ApprovalEventResponse();

typedResponse._owner = (Address) eventValues.getIndexedValues().get(0);

typedResponse._spender = (Address) eventValues.getIndexedValues().get(1);

typedResponse._value = (Uint256) eventValues.getNonIndexedValues().get(0);

responses.add(typedResponse);

}

return responses;

}

public Observable approvalEventObservable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {

final Event event = new Event("Approval",

Arrays.>asList(new TypeReference

() {}, new TypeReference
() {}),

Arrays.>asList(new TypeReference() {}));

EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());

filter.addSingleTopic(EventEncoder.encode(event));

return web3j.ethLogObservable(filter).map(new Func1() {

@Override

public ApprovalEventResponse call(Log log) {

EventValues eventValues = extractEventParameters(event, log);

ApprovalEventResponse typedResponse = new ApprovalEventResponse();

typedResponse._owner = (Address) eventValues.getIndexedValues().get(0);

typedResponse._spender = (Address) eventValues.getIndexedValues().get(1);

typedResponse._value = (Uint256) eventValues.getNonIndexedValues().get(0);

typedResponse._transactionHash = log.getTransactionHash();

return typedResponse;

}

});

}

public Future name() throws IOException {

Function function = new Function("name",

Arrays.asList(),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

public TransactionReceipt approve(Address _spender, Uint256 _amount) throws IOException, TransactionException {

Function function = new Function("approve", Arrays.asList(_spender, _amount), Collections.>emptyList());

return executeTransaction(function);

}

public Future totalSupply() throws IOException {

Function function = new Function("totalSupply",

Arrays.asList(),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

public TransactionReceipt transferFrom(Address _from, Address _to, Uint256 _amount) throws IOException, TransactionException {

Function function = new Function("transferFrom", Arrays.asList(_from, _to, _amount), Collections.>emptyList());

return executeTransaction(function);

}

public Uint8 decimals() throws IOException {

Function function = new Function("decimals",

Arrays.asList(),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

public Uint256 balanceOf(Address _owner) throws IOException {

Function function = new Function("balanceOf",

Arrays.asList(_owner),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

public Future

owner() throws IOException {

Function function = new Function("owner",

Arrays.asList(),

Arrays.>asList(new TypeReference

() {}));

return executeCallSingleValueReturn(function);

}

public Future symbol() throws IOException {

Function function = new Function("symbol",

Arrays.asList(),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

public TransactionReceipt transfer(Address _to, Uint256 _amount) throws IOException, TransactionException {

Function function = new Function("transfer", Arrays.asList(_to, _amount), Collections.>emptyList());

return executeTransaction(function);

}

public Future allowance(Address _owner, Address _spender) throws IOException {

Function function = new Function("allowance",

Arrays.asList(_owner, _spender),

Arrays.>asList(new TypeReference() {}));

return executeCallSingleValueReturn(function);

}

public static RemoteCall deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue, Uint256 totalSupply, Utf8String tokenName, Uint8 decimalUnits, Utf8String tokenSymbol) {

String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(totalSupply, tokenName, decimalUnits, tokenSymbol));

return deployRemoteCall(Erc20TokenWrapper.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue);

}

public static RemoteCall deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, BigInteger initialWeiValue, Uint256 totalSupply, Utf8String tokenName, Uint8 decimalUnits, Utf8String tokenSymbol) {

String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.asList(totalSupply, tokenName, decimalUnits, tokenSymbol));

return deployRemoteCall(Erc20TokenWrapper.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor, initialWeiValue);

}

public static Erc20TokenWrapper load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {

return new Erc20TokenWrapper(contractAddress, web3j, credentials, gasPrice, gasLimit);

}

public static Erc20TokenWrapper load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {

return new Erc20TokenWrapper(contractAddress, web3j, transactionManager, gasPrice, gasLimit);

}

public static class TransferEventResponse {

public Address _from;

public Address _to;

public Uint256 _value;

public String _transactionHash;

}

public static class ApprovalEventResponse {

public Address _owner;

public Address _spender;

public Uint256 _value;

public String _transactionHash;

}

}

java erc 2.0_java 监听 ERC20 Token 交易相关推荐

  1. java如何监听以太坊交易

    2019独角兽企业重金招聘Python工程师标准>>> 你可以在web3j库的帮助下使用java轻松监听以太坊交易,但此库无法监听Erc20 Token交易. 要监听Erc20Tok ...

  2. 【java】画图和监听事件的应用

    [java]画图和监听事件的应用 (1)frame.getContentPane().add(new Change()); ***用getContentPane()方法获得JFrame的内容面板,再对 ...

  3. Java Swing 键盘事件监听

    Java Swing 键盘事件监听 开发工具与关键技术:java. elipse2019.jdk1.8 作者:Amewin 撰写时间:2019年9月16日 键盘事件的事件源一般丐组件相关,当一个组件处 ...

  4. java毕业设计——基于java+Winpcap的局域网监听软件设计与实现(毕业论文+程序源码)——局域网监听软件

    基于java+Winpcap的局域网监听软件设计与实现(毕业论文+程序源码) 大家好,今天给大家介绍基于java+Winpcap的局域网监听软件设计与实现,文章末尾附有本毕业设计的论文和源码下载地址哦 ...

  5. java实现全局键盘监听

    java实现全局键盘监听 Java本身是无法对桌面进行全局键盘监听的,无法设置全局快捷键,当焦点从java程序面板失去时,自带的监听器就无法监听了,但是比如一些用java写的截图程序是需要全局快捷键操 ...

  6. Java 实现日志文件监听并读取相关数据

    Java 实现日志文件监听并读取相关数据 项目需求 由于所在数据中台项目组需要实现监听文件夹或者日志文件并读取对应格式的脏数据的需求,以便在文件.文件夹发生变化时进行相应的业务流程:所以在这里记录下相 ...

  7. java 1.7 事件监听_17.7Listener监听器

    一.监听器介绍 1.1.监听器的概念 监听器 监听器是一个专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时,立即采取相应的行动.监听器其 实就是一个实现特定 ...

  8. java图形界面的监听_非专业码农 JAVA学习笔记 用户图形界面设计与实现-所有控件的监听事件...

    用户图形界面设计与实现-监听事件 System.applet.Applet (一)用户自定义成分 1.绘制图形 Public voit piant(Ghraphics g){  g.drawLine等 ...

  9. java怎么快速创建监听类_如何创建监听器

    /* 鄙视2楼的 */ 监听器对象有很多种 监听鼠标动作的,监听键盘动作的.监听器接口类都在java.awt.event包下面. 比如现在我要想监听键盘的动作 那么我们就可以这么做 我们可以实现一个K ...

最新文章

  1. dbgridview内操作粘贴,复制,等量复制,增量复制
  2. Unity3D中如何计算场景中的三角面和顶点数
  3. 【数字信号处理】傅里叶变换性质 ( 序列傅里叶变换共轭对称性质 | 序列实偶 傅里叶变换 实偶 | 序列实奇 傅里叶变换 虚奇 | 证明 “ 序列实奇 傅里叶变换 虚奇 “ )
  4. Haproxy全透明代理
  5. UA MATH564 概率论 依概率收敛的题目
  6. 解决不了“不可能三角”,火山抖音化只是个昏招
  7. Bootstrap全局css样式_按钮
  8. linux python3 mysql_Python3 MySQL 数据库连接 – PyMySQL 驱动
  9. kafka原理概念提炼
  10. 计算机视觉工作项目方案设计,机器视觉(项目方案设计案例)47.pdf
  11. Unity3D之NGUI基础6:UIButton按钮
  12. AI未来 - 李开复 - 未来8成的工作受影响 - 读后感
  13. 《HTTP 权威指南》—— 连接管理
  14. RS485_PTZ_云台控制
  15. Windows 在Windows中关闭/最小化窗口的几种快捷方法
  16. C#编写的AccessHelper
  17. java计算机毕业设计晨光文具店进销存系统设计与开发源码+数据库+系统+lw文档+部署
  18. 【C#.NET MVC】Deft框架简介与基本使用
  19. 淘宝直通车什么情况能退款?怎么退?
  20. 指纹图像方向图matlab,基于Matlab实现的指纹图像细节特征提取

热门文章

  1. HDU 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活
  2. 太原城市职业技术学院引入USB Server助力实训系统实现虚拟化
  3. 2020年数学建模国赛C题题目和解题思路
  4. AutoJs学习-实现文件深度搜索
  5. 谣言检测文献阅读三—The Future of False Information Detection on Social Media:New Perspectives and Trends
  6. rabbitmq基础2——rabbitmq二进制安装和docker安装、基础命令
  7. c++成员声明中的非法限定名_C++中作用域限定符
  8. macbookpro和macbookair哪个好些?
  9. STM32使用HAL库编写SHT2x温湿度传感器驱动
  10. vl53L0X传感器的编写,(未完待续)