From f5e6133809c553cfd9fb28ee61019927c547c374 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Wed, 08 Dec 2021 15:58:33 +0800
Subject: [PATCH] 20211208  fish

---
 src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java |   89 +++++++++++++++++++++++++++++++++++++-------
 1 files changed, 74 insertions(+), 15 deletions(-)

diff --git a/src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java b/src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java
index fbe30a5..83b1e24 100644
--- a/src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java
+++ b/src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java
@@ -20,6 +20,9 @@
 import org.springframework.stereotype.Service;
 import org.springframework.web.client.RestTemplate;
 import org.tron.common.utils.ByteArray;
+import org.tron.trident.core.ApiWrapper;
+import org.tron.trident.core.contract.Contract;
+import org.tron.trident.core.contract.Trc20Contract;
 import org.tron.walletserver.WalletApi;
 
 import javax.annotation.Resource;
@@ -39,6 +42,10 @@
 
     private static String TRC20_CONTRACT_ADDRESS = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
 
+    /**
+     * 代币合约地址
+     */
+    public static String COIN_CONTRACT_ADDRESS = "TL2pea32CTtxJ48pJmnLQuoRHeoX79dLCT";
     /**
      * 手续费
      */
@@ -81,7 +88,7 @@
      * @param addressList
      * @param num
      */
-    private void httpTransactionInfo(List<String> addressList, Long num) {
+    public void httpTransactionInfo(List<String> addressList, Long num) {
         // 查询详情,包含了所有交易信息
         String transactionInfoByBlockNum = getblockbynum(BigInteger.valueOf(num));
         if (StringUtils.isBlank(transactionInfoByBlockNum)) {
@@ -259,14 +266,20 @@
         }
 
         BigDecimal amount = BigDecimal.ZERO;
-        //相匹配的合约地址
-        if (!TRC20_CONTRACT_ADDRESS.equals(contract_address)) {
+        // 相匹配的合约地址 USDT合约地址和代币合约地址
+        if (!TRC20_CONTRACT_ADDRESS.equals(contract_address) && !COIN_CONTRACT_ADDRESS.equals(contract_address)) {
             return;
         }
 
         //币种
         if (StringUtils.isNotEmpty(amountStr)) {
-            amount = new BigDecimal(amountStr).divide(new BigDecimal(1 + TransformUtil.getSeqNumByLong(0L, 6)));
+            if (TRC20_CONTRACT_ADDRESS.equals(contract_address)) {
+                // USDT精度为6
+                amount = new BigDecimal(amountStr).divide(new BigDecimal(1 + TransformUtil.getSeqNumByLong(0L, 6)));
+            } else {
+                // 代币精度为8
+                amount = new BigDecimal(amountStr).divide(new BigDecimal(1 + TransformUtil.getSeqNumByLong(0L, 8)));
+            }
         }
         for (String address : addressList) {
             if (address.equals(to_address)) {
@@ -274,7 +287,11 @@
                 // 金额
                 // 发送消息队列
                 EthUsdtChargeDto dto = new EthUsdtChargeDto(address, txId, amount);
-                dto.setSymbol(EthUsdtChargeDto.Symbol.USDT_TRC20);
+                if (TRC20_CONTRACT_ADDRESS.equals(contract_address)) {
+                    dto.setSymbol(EthUsdtChargeDto.Symbol.USDT_TRC20);
+                } else {
+                    dto.setSymbol(EthUsdtChargeDto.Symbol.COIN_TRC20);
+                }
                 usdtUpdateProducer.sendMsg(JSONObject.toJSONString(dto));
                 log.info("===to_address:" + to_address + "===amount:" + amount);
             }
@@ -295,16 +312,13 @@
             return false;
         }
         if (trxBalance.compareTo(TRX_FEE) >= 0) {
-            // 转
-            BigDecimal trc20Balance = Trc20Service.getTrc20Balance(address);
-            if (trc20Balance == null) {
+
+            boolean usdtResult = poolUsdt(address);
+            boolean coinResult = poolCoin(address);
+            if (usdtResult && coinResult) {
                 return false;
             }
-            MemberCoinAddressEntity coinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, "USDT", "TRC20");
-            if (coinAddressEntity == null) {
-                return false;
-            }
-            Trc20Service.sendTrc20(coinAddressEntity.getPrivateKey(), Trc20Service.POOL_ADDRESS, trc20Balance);
+
             // 需要将存在redis的待归集地址删除
             Object trc20_pool = redisUtils.get("TRC20_POOL");
             if (trc20_pool != null) {
@@ -331,16 +345,48 @@
             if (trc20_pool != null) {
                 poolList = (List) trc20_pool;
             }
-            poolList.add(address);
+
+            if (!poolList.contains(address)){
+                poolList.add(address);
+            }
             redisUtils.set("TRC20_POOL", poolList);
             return true;
         }
     }
 
-    // https://api.trongrid.io/wallet/getnowblock
+    public boolean poolUsdt(String address) {
+        // 转
+        BigDecimal trc20Balance = Trc20Service.getTrc20Balance(address);
+        if (trc20Balance == null || BigDecimal.ZERO.compareTo(trc20Balance) <= 0) {
+            return false;
+        }
+        MemberCoinAddressEntity coinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(address, "USDT", "TRC20");
+        if (coinAddressEntity == null) {
+            return false;
+        }
+        Trc20Service.sendTrc20(coinAddressEntity.getPrivateKey(), Trc20Service.POOL_ADDRESS, trc20Balance);
+        return true;
+    }
+
+    public boolean poolCoin(String address) {
+        MemberCoinAddressEntity coinAddress = memberCoinAddressDao.selectCoinAddressByAddressAndSymbol(address, CoinTypeEnum.XCC.name());
+        if (coinAddress == null) {
+            return false;
+        }
+
+        Trc20Contract token = contractToken(coinAddress.getAddress(), coinAddress.getPrivateKey());
+        BigInteger balance = token.balanceOf(address);
+        if (balance == null || balance.compareTo(BigInteger.ZERO) <= 0) {
+            return false;
+        }
+
+        token.transfer(Trc20Service.POOL_ADDRESS, balance.longValue(), 0, "memo", 100000000L);
+        return true;
+    }
 
     /**
      * 获取最新区块
+     * https://api.trongrid.io/wallet/getnowblock
      *
      * @return
      */
@@ -375,4 +421,17 @@
         String wholeBlockCount = JSON.parseObject(forObject).getString("whole_block_count");
         return Long.valueOf(wholeBlockCount);
     }
+
+    /**
+     * 获取对应代币合约token
+     *
+     * @param address 地址
+     * @param privateKey 密钥
+     * @return
+     */
+    public Trc20Contract contractToken(String address, String privateKey) {
+        ApiWrapper wrapper = ApiWrapper.ofMainnet(privateKey, Trc20Service.API_KEY);
+        Contract trc20Contract = wrapper.getContract(COIN_CONTRACT_ADDRESS);
+        return new Trc20Contract(trc20Contract, address, wrapper);
+    }
 }

--
Gitblit v1.9.1