fix
Helius
2021-11-23 9699d339f596847ac541cb4c1a370847c6e76b68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package com.xcong.excoin.modules.blackchain.service;
 
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.common.enumerates.CoinTypeEnum;
import com.xcong.excoin.modules.blackchain.model.EthUsdtChargeDto;
import com.xcong.excoin.modules.member.dao.MemberCoinAddressDao;
import com.xcong.excoin.rabbit.producer.UsdtUpdateProducer;
import com.xcong.excoin.utils.RedisUtils;
import org.apache.commons.lang.StringUtils;
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.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class UsdtErc20UpdateService {
 
    @Resource
    private UsdtUpdateProducer usdtUpdateProducer;
 
    @Resource
    private MemberCoinAddressDao  coinWalletDao;
 
    public final static List<String> ALL_ADDRESS_LIST = new ArrayList<>();
 
    public final static String USDT_BLOCK_NUM_GOLDEN = "USDT_BLOCK_NUM_GOLDEN";
 
    private final static BigDecimal DIVIDE_USDT = new BigDecimal("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://114.55.250.231:8545";
 
    private static String contractAddr = "0xdac17f958d2ee523a2206206994597c13d831ec7";
 
 
    // 操作账号
    private static String privateKey = "4576fafdd215f52051c60e04618ef8997fbc5cee8413d3b34d210e296e6e9a3d";
 
 
    @Resource
    private RedisUtils redisUtils;
 
 
    public void updateUsdt(){
        // 首先查询所有的钱包地址
        List<String> tdCoinWallets = coinWalletDao.selectAllSymbolAddress(CoinTypeEnum.USDT.toString(),"ERC20");
        if(tdCoinWallets!=null){
            ALL_ADDRESS_LIST.addAll(tdCoinWallets);
        }
        // 获取最新区块
        String string = redisUtils.getString(USDT_BLOCK_NUM_GOLDEN);
        if(string==null){
            string = "11014249";
        }
        BigInteger blockNum = new BigInteger(string);
        Credentials credentials = Credentials.create(privateKey);
        EthUsdtContract contract = EthUsdtContract.load(contractAddr, getInstance(), credentials, getStaticGasProvider());
        EthFilter filter = getFilter(blockNum);
        Map<String,BigInteger> map = new HashMap<String,BigInteger>();
        map.put("blockNum",blockNum);
        contract.transferEventFlowable(filter).subscribe(e->{
            if(e!=null && StringUtils.isNotBlank(e.to) &&  e.log.getBlockNumber()!=null){
                String transactionHash = e.log.getTransactionHash();
                BigInteger blockNumber1 = e.log.getBlockNumber();
                String toAddress = e.to;
                BigInteger tokenBalance = e.tokens;
                if(ALL_ADDRESS_LIST.contains(toAddress)){
                    System.out.println("存在本地的地址:"+toAddress);
                    // 金额
                    BigDecimal divide = new BigDecimal(tokenBalance.toString()).divide(DIVIDE_USDT);
                    // 发送消息队列
                    EthUsdtChargeDto dto = new EthUsdtChargeDto(toAddress,transactionHash,divide);
                    usdtUpdateProducer.sendMsg(JSONObject.toJSONString(dto));
                }
                if(map.get("blockNum").compareTo(blockNumber1)!=0){
                    redisUtils.set(USDT_BLOCK_NUM_GOLDEN,blockNumber1.toString());
                    map.put("blockNum",blockNumber1);
                }
            }
        });
 
    }
 
 
    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);
        }
    }
 
}