|  |  |  | 
|---|
|  |  |  | import com.xcong.excoin.modules.member.entity.MemberCoinAddressEntity; | 
|---|
|  |  |  | import com.xcong.excoin.rabbit.producer.UsdtUpdateProducer; | 
|---|
|  |  |  | import com.xcong.excoin.utils.RedisUtils; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.collections.CollectionUtils; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.springframework.http.HttpEntity; | 
|---|
|  |  |  | import org.springframework.http.HttpHeaders; | 
|---|
|  |  |  | import org.springframework.http.MediaType; | 
|---|
|  |  |  | import org.springframework.http.ResponseEntity; | 
|---|
|  |  |  | import org.springframework.http.*; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.web.client.RestTemplate; | 
|---|
|  |  |  | import org.tron.common.utils.ByteArray; | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | *  TRX TRC20服务类 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @Service | 
|---|
|  |  |  | public class TrxUsdtUpdateService { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | *  扫块 同步充值USDT-TRC20和TRX | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public void monitorCoinListener() { | 
|---|
|  |  |  | public void monitorCoinListener(Long blockNum) { | 
|---|
|  |  |  | if (CollectionUtils.isEmpty(addressList)) { | 
|---|
|  |  |  | List<MemberCoinAddressEntity> coinAddressList = memberCoinAddressDao.selectAllBlockAddressBySymbolAndTag(CoinTypeEnum.USDT.name(), "TRC20"); | 
|---|
|  |  |  | if (CollectionUtils.isNotEmpty(coinAddressList)) { | 
|---|
|  |  |  | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // 扫块区块 | 
|---|
|  |  |  | Object trc20BlockNum = redisUtils.get("USDT_TRC20_BLOCK_NUM"); | 
|---|
|  |  |  | if(trc20BlockNum==null){ | 
|---|
|  |  |  | trc20BlockNum = 28471475L; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Long blockNum = Long.valueOf(trc20BlockNum.toString()); | 
|---|
|  |  |  | redisUtils.set("USDT_TRC20_BLOCK_NUM",blockNum); | 
|---|
|  |  |  | if (CollectionUtils.isEmpty(addressList)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | * @param num | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private void httpTransactionInfo(List<String> addressList, Long num) { | 
|---|
|  |  |  | String transactionInfoByBlockNum = getTransactionInfoByBlockNum(BigInteger.valueOf(num)); | 
|---|
|  |  |  | // 查询详情,包含了所有交易信息 | 
|---|
|  |  |  | String transactionInfoByBlockNum = getblockbynum(BigInteger.valueOf(num)); | 
|---|
|  |  |  | if (StringUtils.isBlank(transactionInfoByBlockNum)) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //        log.info("--->{}, {}", num, System.currentTimeMillis()); | 
|---|
|  |  |  | // 不用等到扫完再累加 只要进来就加 还有一个条件是必须查询出区块再加 否则当区块超过实际区块 | 
|---|
|  |  |  | redisUtils.set("USDT_TRC20_BLOCK_NUM", (num + 1L)); | 
|---|
|  |  |  | JSONArray parseArray = JSON.parseArray(transactionInfoByBlockNum); | 
|---|
|  |  |  | if (parseArray.size() > 0) { | 
|---|
|  |  |  | //        redisUtils.set("USDT_TRC20_BLOCK_NUM", (num + 1L)); | 
|---|
|  |  |  | JSONArray parseArray = JSON.parseObject(transactionInfoByBlockNum).getJSONArray("transactions"); | 
|---|
|  |  |  | if (parseArray != null && parseArray.size() > 0) { | 
|---|
|  |  |  | for (Object e : parseArray) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | String txId = JSON.parseObject(e.toString()).getString("id"); | 
|---|
|  |  |  | String contract_address = JSON.parseObject(e.toString()).getString("contract_address"); | 
|---|
|  |  |  | if(!"41a614f803b6fd780986a42c78ec9c7f77e6ded13c".equals(contract_address)){ | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //                    String txId = JSON.parseObject(e.toString()).getString("id"); | 
|---|
|  |  |  | //                    String contract_address = JSON.parseObject(e.toString()).getString("contract_address"); | 
|---|
|  |  |  | //                    if(!"41a614f803b6fd780986a42c78ec9c7f77e6ded13c".equals(contract_address)){ | 
|---|
|  |  |  | //                        continue; | 
|---|
|  |  |  | //                    } | 
|---|
|  |  |  | //判断 数据库 txId 有 就不用往下继续了 | 
|---|
|  |  |  | JSONObject parseObject = JSON.parseObject(getTransactionById(txId)); | 
|---|
|  |  |  | JSONObject parseObject = JSON.parseObject(e.toString()); | 
|---|
|  |  |  | String txId = parseObject.getString("txID"); | 
|---|
|  |  |  | String contractRet = parseObject.getJSONArray("ret").getJSONObject(0).getString("contractRet"); | 
|---|
|  |  |  | //交易成功 | 
|---|
|  |  |  | if ("SUCCESS".equals(contractRet)) { | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } else if ("TransferContract".equals(type)) { | 
|---|
|  |  |  | //trx 转账 | 
|---|
|  |  |  | transferContract(parseObject); | 
|---|
|  |  |  | //transferContract(parseObject); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception exception) { | 
|---|
|  |  |  | 
|---|
|  |  |  | return postForEntity(url, param).getBody(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取特定区块的所有交易 Info 信息 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param num 区块 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public static String getblockbynum(BigInteger num) { | 
|---|
|  |  |  | String url = http + "/wallet/getblockbynum"; | 
|---|
|  |  |  | Map<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | map.put("num", num); | 
|---|
|  |  |  | String param = JSON.toJSONString(map); | 
|---|
|  |  |  | return postForEntity(url, param).getBody(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * https://cn.developers.tron.network/docs/%E4%BA%A4%E6%98%9311#%E4%BA%A4%E6%98%93%E7%A1%AE%E8%AE%A4%E6%96%B9%E6%B3%95 | 
|---|
|  |  |  | 
|---|
|  |  |  | HttpHeaders headers = new HttpHeaders(); | 
|---|
|  |  |  | headers.setContentType(MediaType.APPLICATION_JSON); | 
|---|
|  |  |  | headers.set("TRON-PRO-API-KEY",Trc20Service.API_KEY); | 
|---|
|  |  |  | HttpEntity<String> request = new HttpEntity<>(param); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // https://api.trongrid.io/wallet/getnowblock | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | *  获取最新区块 | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | HttpEntity<String> request = new HttpEntity<>(headers); | 
|---|
|  |  |  | ResponseEntity<String> exchange = restTemplate.exchange(url, HttpMethod.GET, request, String.class); | 
|---|
|  |  |  | String forObject = exchange.getBody(); | 
|---|
|  |  |  | //System.out.println(forObject); | 
|---|
|  |  |  | // String forObject = restTemplate.getForObject(url, String.class); | 
|---|
|  |  |  | String number = JSON.parseObject(forObject).getJSONObject("block_header").getJSONObject("raw_data").getString("number"); | 
|---|
|  |  |  | return Long.valueOf(number); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|