package com.xcong.excoin.modules.blackchain.service; import com.xcong.excoin.utils.RedisUtils; import org.springframework.stereotype.Service; import org.web3j.crypto.Credentials; import org.web3j.protocol.Web3j; import org.web3j.protocol.core.DefaultBlockParameterName; import org.web3j.protocol.core.DefaultBlockParameterNumber; import org.web3j.protocol.core.methods.request.EthFilter; import org.web3j.protocol.http.HttpService; import org.web3j.tx.gas.StaticGasProvider; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.BigInteger; import java.util.ArrayList; import java.util.List; @Service public class UsdtErc20UpdateService { public final static List ALL_ADDRESS_LIST = new ArrayList<>(); public final static String USDT_BLOCK_NUM = "USDT_BLOCK_NUM"; private final static BigInteger DIVIDE_USDT = new BigInteger("1000000"); private static Web3j web3; private static Web3j getInstance() { if (web3 == null) { HttpService httpService = new HttpService(blockchainNode); web3 = Web3j.build(httpService); } return web3; } public static BigInteger GAS_PRICE = BigInteger.valueOf(45000000000L); //public static final BigInteger GAS_LIMIT = BigInteger.valueOf(4300000L); public static final BigInteger GAS_LIMIT = BigInteger.valueOf(2000000L); private static StaticGasProvider getStaticGasProvider(){ return new StaticGasProvider(GAS_PRICE,GAS_LIMIT); } private static Web3j getInstanceScope() { HttpService httpService = new HttpService(blockchainNode); return Web3j.build(httpService); } private static String blockchainNode = "http://120.55.86.146:8545"; private static String contractAddr = "0xdac17f958d2ee523a2206206994597c13d831ec7"; // 操作账号 private static String privateKey = "4576fafdd215f52051c60e04618ef8997fbc5cee8413d3b34d210e296e6e9a3d"; @Resource private RedisUtils redisUtils; public void updateUsdt(){ // 获取最新区块 String string = redisUtils.getString(USDT_BLOCK_NUM); BigInteger blockNum = new BigInteger(string); Credentials credentials = Credentials.create(privateKey); EthUsdtContract contract = EthUsdtContract.load(contractAddr, web3, credentials, getStaticGasProvider()); EthFilter filter = getFilter(new BigInteger("10943021")); contract.transferEventFlowable(filter).subscribe(e->{ if(e!=null){ String transactionHash = e.log.getTransactionHash(); String toAddress = e.to; BigInteger tokenBalance = e.tokens; // 金额 BigInteger divide = tokenBalance.divide(DIVIDE_USDT); // 发送消息队列 TODO } }); } private static EthFilter getFilter(BigInteger startBlock) { if (startBlock != null) { EthFilter filter = new EthFilter(new DefaultBlockParameterNumber(startBlock), DefaultBlockParameterName.LATEST, contractAddr); return filter; } else { return new EthFilter(DefaultBlockParameterName.EARLIEST, DefaultBlockParameterName.LATEST, contractAddr); } } }