日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java erc 2.0_java 监听 ERC20 Token 交易

發(fā)布時間:2023/12/20 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java erc 2.0_java 监听 ERC20 Token 交易 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

你可以在web3j庫的幫助下使用java輕松監(jiān)聽以太坊交易,但此庫無法監(jiān)聽Erc20 Token交易。

要監(jiān)聽Erc20Token交易,你必須使用在合約(token)創(chuàng)建時的token封裝類。我假設你已經(jīng)使用最少的功能部署了合約,因此你的封裝類看起來像這樣:

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;

}

...

}

現(xiàn)在你必須使用這個類函數(shù)來加載合約然后監(jiān)聽交易。使用下面的代碼加載和監(jiān)聽交易:

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();

}

如果你已經(jīng)部署了合約,它由第三人部署,那么你可以直接使用我的包裝類,只需更改你可以從https://etherscan.io/tokens很容易獲得的二進制密鑰。

結論:因此你可以將此代碼用于任何token的監(jiān)聽交易。此代碼為你提供address,fromAddress和transactionHash。所以這些東西你可以根據(jù)你的要求使用,你可以將它們保存在你的數(shù)據(jù)庫中,或者你只保存地址是你的錢包地址的交易。

謝謝,我希望這會有所幫助。

如果希望快速進行web3j、java、以太坊開發(fā),那請看我們精心打造的教程:

java以太坊開發(fā)教程,主要是針對java和android程序員進行區(qū)塊鏈以太坊開發(fā)的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 交易的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 青青草视频播放 | 另类小说婷婷 | 久久久二区 | 中字幕视频在线永久在线观看免费 | 黄免费在线观看 | 黑人日批视频 | 青青视频在线播放 | 国产精品一区二区av白丝下载 | 新香蕉视频 | 日韩中文字幕精品视频 | av地址在线观看 | 播色屋| 91社区视频 | 久久女人 | 精品人妻午夜一区二区三区四区 | 一区二区三区国产av | 在线看片你懂 | 亚洲精品欧美激情 | 亚洲一区久久 | 奶水旺盛的少妇在线播放 | 国产免费一区二区三区网站免费 | 亚洲图片88 | 欧美久久综合网 | 精品视频一二三 | 中年夫妇啪啪高潮 | 夜福利视频 | 少妇福利视频 | 91在线观看网站 | 国产福利小视频在线观看 | 亚洲国产免费 | 日韩欧美啪啪 | av在线资源网 | 夜夜嗨av禁果av粉嫩avhd | 超碰p | 亚洲精品白虎 | 天堂在线免费观看视频 | 中文字幕第一区综合 | 日韩一区不卡 | 国产精品一区二区免费看 | 欧美在线观看一区二区 | 亚洲激情久久 | 国产乱码精品一区二区三区中文 | 麻豆精品一区二区 | 美女视频黄色免费 | jizz少妇 | 免费九九视频 | 深夜福利国产精品 | 黄色成人在线播放 | 亚洲激情在线观看视频 | 依人在线视频 | 日韩一区二区在线观看视频 | 亚洲高清二区 | 国产成人无码专区 | 久久理论 | 午夜激情网 | 黄色精品一区二区 | 在线视频国产一区 | 欧美成人一区二区三区 | 国产18在线观看 | 激情四射av | 日韩免费成人av | 好吊操免费视频 | 久久久久久中文字幕 | 一级少妇精品久久久久久久 | youjizz日韩 | 国产页| 青青青在线视频免费观看 | 中文字幕在线观看二区 | 精品国产一区一区二区三亚瑟 | 欧美激情一区二区三区蜜桃视频 | 中文字幕人妻伦伦 | 小妹色播影院 | 蜜桃av噜噜一区二区三区网址 | 日韩欧美一区二区在线观看 | 久久久久成人网 | 美女扒开粉嫩的尿囗给男生桶 | 国产一级淫片a视频免费观看 | 无码人妻一区二区三区在线 | 日韩av大片 | 精品国产人妻一区二区三区 | 人人爱人人澡 | 一本大道综合伊人精品热热 | 黄色777 | 免费男女视频 | 伊人天堂网 | 日韩欧美久久精品 | 国产精品久久久精品三级 | 国产av无码专区亚洲av毛片搜 | 韩国19主播内部福利vip | 亚洲v国产| 91久久人澡人人添人人爽欧美 | 国产91精品一区二区 | 97人人澡人人爽人人模亚洲 | 美女一区 | 91免费网站视频 | 国产成人三级一区二区在线观看一 | 成人免费视频国产 | 97av超碰| 日本一区二区在线视频 |