xiaoyong931011
2022-02-25 89b7fb1d316cfce7eb98a27c8d668da493933f7f
src/main/java/com/xcong/excoin/rabbit/consumer/UsdtUpdateConsumer.java
@@ -1,14 +1,25 @@
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
@@ -16,18 +27,68 @@
 **/
@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("#---->{}#", content);
        log.info("#USDT同步---->{}#", content);
        EthUsdtChargeDto ethUsdtChargeDto = JSONObject.parseObject(content, EthUsdtChargeDto.class);
        // 更新这个用户的余额
        blockCoinService.updateEthUsdtNew(ethUsdtChargeDto);
        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();
        }
    }
}