xiaoyong931011
2022-02-23 0c5bb71fdce5f677b6cc7320795713d599e6f04f
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
package com.xcong.excoin.rabbit.consumer;
 
import cn.hutool.http.HttpException;
import com.alibaba.fastjson.JSONObject;
import com.xcong.excoin.configurations.RabbitMqConfig;
import com.xcong.excoin.modules.blackchain.model.EthUsdtChargeDto;
import com.xcong.excoin.modules.blackchain.service.Trc20Service;
import com.xcong.excoin.modules.blackchain.service.TrxUsdtUpdateService;
import com.xcong.excoin.modules.blackchain.service.UsdtErc20UpdateService;
import com.xcong.excoin.modules.coin.service.BlockCoinService;
import com.xcong.excoin.quartz.job.BlockCoinUpdateJob;
import com.xcong.excoin.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClientException;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
 
 
/**
 * @author wzy
 * @date 2020-05-25
 **/
@Slf4j
@Component
@ConditionalOnProperty(prefix = "app", name = "block-job", havingValue = "true")
public class UsdtUpdateConsumer {
 
 
    @Resource
    private BlockCoinService blockCoinService;
 
    @Resource
    TrxUsdtUpdateService trxUsdtUpdateService;
 
    @RabbitListener(queues = RabbitMqConfig.QUEUE_USDT_UPDATE)
    public void doSomething(String content) {
        log.info("#USDT同步---->{}#", content);
        EthUsdtChargeDto ethUsdtChargeDto = JSONObject.parseObject(content, EthUsdtChargeDto.class);
        // 更新这个用户的余额
        if(EthUsdtChargeDto.Symbol.USDT_ERC20.equals(ethUsdtChargeDto.getSymbol())){
            blockCoinService.updateEthUsdtNew(ethUsdtChargeDto);
        }
        if(EthUsdtChargeDto.Symbol.USDT_TRC20.equals(ethUsdtChargeDto.getSymbol())){
            blockCoinService.updateTrc20(ethUsdtChargeDto);
            // 同步完直接归集
            trxUsdtUpdateService.poolByAddress(ethUsdtChargeDto.getAddress());
        }
 
    }
 
    @RabbitListener(queues = RabbitMqConfig.QUEUE_USDT_ADDRESS)
    public void addUsdtAddress(String content) {
        if(!UsdtErc20UpdateService.ALL_ADDRESS_LIST.contains(content)){
            log.info("#添加新地址---->{}#", content);
            if(StringUtils.isBlank(content)){
                return;
            }
            String[] split = content.split(",");
            if(split.length<2){
                return;
            }
            String address = split[0];
            String tag = split[1];
            if("ERC20".equals(tag)){
                UsdtErc20UpdateService.ALL_ADDRESS_LIST.add(address);
            }
            if("TRC20".equals(tag)){
                TrxUsdtUpdateService.addressList.add(address);
                // 此时还需要给这个地址转账用于激活及后续手续费
                Trc20Service.sendTrx(Trc20Service.TRX_PRIVATE_KEY,address,new BigDecimal(10));
            }
 
        }
    }
 
    @RabbitListener(queues = RabbitMqConfig.QUEUE_TRC20_BLOCK)
    public void trc20BlockMsg(String content) {
        Long blocnNum = Long.parseLong(content);
        try {
            trxUsdtUpdateService.monitorCoinListener(blocnNum);
        } catch (RestClientException | HttpException e) {
            //  此时是连接问题 这个块需要重新扫描
            log.info("查询区块超时:" + blocnNum);
            BlockCoinUpdateJob.TRC_BLOCK.add(blocnNum);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}