|  |  | 
 |  |  | package com.xcong.excoin.modules.blackchain.service; | 
 |  |  |  | 
 |  |  | import cn.hutool.core.math.MathUtil; | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONArray; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | 
 |  |  | import java.util.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  *  TRX TRC20服务类 | 
 |  |  |  * TRX TRC20服务类 | 
 |  |  |  */ | 
 |  |  | @Slf4j | 
 |  |  | @Service | 
 |  |  | 
 |  |  |     private static String TRC20_CONTRACT_ADDRESS = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  手续费 | 
 |  |  |      * 手续费 | 
 |  |  |      */ | 
 |  |  |     private final static BigDecimal TRX_FEE = new BigDecimal("10"); | 
 |  |  |  | 
 |  |  | 
 |  |  |     RedisUtils redisUtils; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  扫块 同步充值USDT-TRC20和TRX | 
 |  |  |      * 扫块 同步充值USDT-TRC20和TRX | 
 |  |  |      */ | 
 |  |  |     public void monitorCoinListener(Long blockNum) { | 
 |  |  |         if (CollectionUtils.isEmpty(addressList)) { | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  解析区块数据 同步用户充值 | 
 |  |  |      * 解析区块数据 同步用户充值 | 
 |  |  |      * | 
 |  |  |      * @param addressList | 
 |  |  |      * @param num | 
 |  |  |      */ | 
 |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *   比对本地地址 同步TRX充值 | 
 |  |  |      * 比对本地地址 同步TRX充值 | 
 |  |  |      * | 
 |  |  |      * @param parseObject | 
 |  |  |      */ | 
 |  |  |     private void transferContract(JSONObject parseObject) { | 
 |  |  | 
 |  |  |         RestTemplate restTemplate = new RestTemplate(); | 
 |  |  |         HttpHeaders headers = new HttpHeaders(); | 
 |  |  |         headers.setContentType(MediaType.APPLICATION_JSON); | 
 |  |  |         headers.set("TRON-PRO-API-KEY",Trc20Service.API_KEY); | 
 |  |  |         HttpEntity<String> request = new HttpEntity<>(param,headers); | 
 |  |  |         headers.set("TRON-PRO-API-KEY", Trc20Service.API_KEY); | 
 |  |  |         HttpEntity<String> request = new HttpEntity<>(param, headers); | 
 |  |  |         ResponseEntity<String> result = restTemplate.postForEntity(url, request, String.class); | 
 |  |  | //        System.out.println("url:" + url + ",param:" + param + ",result:" + result.getBody()); | 
 |  |  |         return result; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  比对本地地址 同步充值USDT-TRC20 | 
 |  |  |      * 比对本地地址 同步充值USDT-TRC20 | 
 |  |  |      * | 
 |  |  |      * @param addressList | 
 |  |  |      * @param txId | 
 |  |  |      * @param parseObject | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  根据地址归集USDT-TRC20 | 
 |  |  |      * 根据地址归集USDT-TRC20 | 
 |  |  |      * | 
 |  |  |      * @param address | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  | 
 |  |  |             Trc20Service.sendTrc20(coinAddressEntity.getPrivateKey(), Trc20Service.POOL_ADDRESS, trc20Balance); | 
 |  |  |             // 需要将存在redis的待归集地址删除 | 
 |  |  |             Object trc20_pool = redisUtils.get("TRC20_POOL"); | 
 |  |  |             if(trc20_pool!=null){ | 
 |  |  |             if (trc20_pool != null) { | 
 |  |  |                 List<String> poolList = (List) trc20_pool; | 
 |  |  |                 Iterator<String> iterator = poolList.iterator(); | 
 |  |  |                 while (iterator.hasNext()){ | 
 |  |  |                 while (iterator.hasNext()) { | 
 |  |  |                     String next = iterator.next(); | 
 |  |  |                     if(address.equals(next)){ | 
 |  |  |                     if (address.equals(next)) { | 
 |  |  |                         iterator.remove(); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                if(CollectionUtils.isEmpty(poolList)) { | 
 |  |  |                    redisUtils.del("TRC20_POOL"); | 
 |  |  |                }else{ | 
 |  |  |                    redisUtils.set("TRC20_POOL", poolList); | 
 |  |  |                } | 
 |  |  |                 if (CollectionUtils.isEmpty(poolList)) { | 
 |  |  |                     redisUtils.del("TRC20_POOL"); | 
 |  |  |                 } else { | 
 |  |  |                     redisUtils.set("TRC20_POOL", poolList); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             return true; | 
 |  |  |         } else { | 
 |  |  | 
 |  |  |     // https://api.trongrid.io/wallet/getnowblock | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  获取最新区块 | 
 |  |  |      * 获取最新区块 | 
 |  |  |      * | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public long getnowblock(){ | 
 |  |  |         String url = http+"/wallet/getnowblock"; | 
 |  |  |     public long getnowblock() { | 
 |  |  |         String url = http + "/wallet/getnowblock"; | 
 |  |  |         RestTemplate restTemplate = new RestTemplate(); | 
 |  |  |         HttpHeaders headers = new HttpHeaders(); | 
 |  |  |         headers.setContentType(MediaType.APPLICATION_JSON); | 
 |  |  |         headers.set("TRON-PRO-API-KEY",Trc20Service.API_KEY); | 
 |  |  |         headers.set("TRON-PRO-API-KEY", Trc20Service.API_KEY); | 
 |  |  |         HttpEntity<String> request = new HttpEntity<>(headers); | 
 |  |  |         ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, request, String.class); | 
 |  |  |         String forObject = exchange.getBody(); | 
 |  |  | 
 |  |  |         return Long.valueOf(number); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      *  从tronscan.io查询最新区块 | 
 |  |  |      *  {"whole_block_count":29625671,"whole_pay":3392835760,"last_day_pay":460432,"last_day_block_count":28777} | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public  Long getnowblockFromTronScan() { | 
 |  |  |         String roundNum = Math.random() + ""; | 
 |  |  |         String url = "https://apiasia.tronscan.io:5566/api/block/statistic?randomNum=" + roundNum; | 
 |  |  |         RestTemplate restTemplate = new RestTemplate(); | 
 |  |  |         String forObject = restTemplate.getForObject(url, String.class); | 
 |  |  |         String wholeBlockCount = JSON.parseObject(forObject).getString("whole_block_count"); | 
 |  |  |         return Long.valueOf(wholeBlockCount); | 
 |  |  |     } | 
 |  |  | } |