wzy
2021-04-15 02e320c6dcb715a969af3d80e363a671e914b10e
src/main/java/com/xcong/excoin/modules/blackchain/service/TrxUsdtUpdateService.java
@@ -9,6 +9,7 @@
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;
@@ -28,6 +29,7 @@
/**
 *  TRX TRC20服务类
 */
@Slf4j
@Service
public class TrxUsdtUpdateService {
@@ -63,13 +65,21 @@
            }
        }
        // 扫块区块
        Object trc20BlockNum = redisUtils.get("USDT_TRC20_BLOCK_NUM");
       Object trc20BlockNum = redisUtils.get("USDT_TRC20_BLOCK_NUM");
        if(trc20BlockNum==null){
            trc20BlockNum = 27805917L;
            trc20BlockNum = 28471475L;
        }
        Long blockNum = Long.valueOf(trc20BlockNum.toString());
        redisUtils.set("USDT_TRC20_BLOCK_NUM",blockNum);
        redisUtils.set("USDT_TRC20_BLOCK_NUM",blockNum + 1L);
//        log.info("---{}",blockNum);
        // 查询一下最新区块
        long getnowblock = getnowblock()-20L;
//        log.info("--->{}, {}", blockNum, System.currentTimeMillis());
        if(getnowblock<blockNum){
            // 如果当前区块比最新已确认区块还大,则不继续执行
            return;
        }
        if (CollectionUtils.isEmpty(addressList)) {
            return;
        }
@@ -77,7 +87,7 @@
        //  解析区块
        httpTransactionInfo(addressList, blockNum);
        redisUtils.set("USDT_TRC20_BLOCK_NUM", (blockNum + 1L));
    }
    /**
@@ -86,17 +96,26 @@
     * @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;
        }
        JSONArray parseArray = JSON.parseArray(transactionInfoByBlockNum);
//        log.info("--->{}, {}", num, System.currentTimeMillis());
        // 不用等到扫完再累加 只要进来就加 还有一个条件是必须查询出区块再加 否则当区块超过实际区块
//        redisUtils.set("USDT_TRC20_BLOCK_NUM", (num + 1L));
        JSONArray parseArray = JSON.parseObject(transactionInfoByBlockNum).getJSONArray("transactions");
        if (parseArray.size() > 0) {
            for (Object e : parseArray) {
                try {
                    String txId = JSON.parseObject(e.toString()).getString("id");
//                    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)) {
@@ -107,7 +126,7 @@
                        } else if ("TransferContract".equals(type)) {
                            //trx 转账
                            transferContract(parseObject);
                            //transferContract(parseObject);
                        }
                    }
                } catch (Exception exception) {
@@ -146,6 +165,20 @@
     */
    public static String getTransactionInfoByBlockNum(BigInteger num) {
        String url = http + "/wallet/gettransactioninfobyblocknum";
        Map<String, Object> map = new HashMap<>();
        map.put("num", num);
        String param = JSON.toJSONString(map);
        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);
@@ -302,4 +335,23 @@
            return true;
        }
    }
    // 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<>();
        String forObject = restTemplate.getForObject(url, String.class);
        String number = JSON.parseObject(forObject).getJSONObject("block_header").getJSONObject("raw_data").getString("number");
        return Long.valueOf(number);
    }
}