From 43385a77303aa2295d902b5a2be9e28723d08cef Mon Sep 17 00:00:00 2001
From: zainali5120 <512061637@qq.com>
Date: Mon, 12 Oct 2020 16:00:21 +0800
Subject: [PATCH] Merge branches 'cpv' and 'feature/撮合交易' of https://gitee.com/chonggaoxiao/new_excoin into cpv

---
 src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtErc20UpdateService.java |   60 ++++++++++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 48 insertions(+), 12 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtErc20UpdateService.java b/src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtErc20UpdateService.java
index a67b220..2ad5e2c 100644
--- a/src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtErc20UpdateService.java
+++ b/src/main/java/com/xcong/excoin/modules/blackchain/service/UsdtErc20UpdateService.java
@@ -1,6 +1,12 @@
 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;
@@ -14,15 +20,24 @@
 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 = "USDT_BLOCK_NUM";
+    public final static String USDT_BLOCK_NUM_GOLDEN = "USDT_BLOCK_NUM_GOLDEN";
 
-    private final static BigInteger DIVIDE_USDT = new BigInteger("1000000");
+    private final static BigDecimal DIVIDE_USDT = new BigDecimal("1000000");
 
     private static Web3j web3;
 
@@ -58,26 +73,48 @@
 
     @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);
+        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, web3, credentials, getStaticGasProvider());
-        EthFilter filter = getFilter(new BigInteger("10943021"));
+        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){
+            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;
-                // 金额
-                BigInteger divide = tokenBalance.divide(DIVIDE_USDT);
-                // 发送消息队列 TODO
-
+                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) {
@@ -88,7 +125,6 @@
             return new EthFilter(DefaultBlockParameterName.EARLIEST,
                     DefaultBlockParameterName.LATEST, contractAddr);
         }
-
-
     }
+
 }

--
Gitblit v1.9.1