|  |  |  | 
|---|
|  |  |  | * 0x55d398326f99059fF775485246999027B3197955 | 
|---|
|  |  |  | * 测试链 0x337610d27c682E347C9cD60BD4b3b107C9d34dDd | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | BSC_USDT("BSC", "0x7FC948E091C4b71fC063cE59B8Dad1062B1c5065", | 
|---|
|  |  |  | "0xbf6f11f5689961d5351375bebbae751de0d0d5c2e2095c1017368485dc909ff8", | 
|---|
|  |  |  | BSC_USDT("BSC", "0xA60AaC0da34C76F3f60207ee09e9F75043319ab4", | 
|---|
|  |  |  | "221be67f85375a5af6dd0f4b77fbe877ad2e57e05db201de1b1383fb76494d18", | 
|---|
|  |  |  | "https://bsc-dataseed1.ninicoin.io", | 
|---|
|  |  |  | "0x55d398326f99059fF775485246999027B3197955", | 
|---|
|  |  |  | ""); | 
|---|
|  |  |  | 
|---|
|  |  |  | //        ChainService.wssBaseCoinEventListener(block, baseCoinService); | 
|---|
|  |  |  | //        ChainService.wssBaseCoinEventListener(block, bscUsdtContractEvent); | 
|---|
|  |  |  | //        ChainService.wssContractEventListener(block, bscUsdtContractEvent, ChainEnum.BSC_USDT.name()); | 
|---|
|  |  |  | ChainService.wssContractEventListener2(block, bscUsdtContractEvent, ChainEnum.BSC_USDT.name()); | 
|---|
|  |  |  | long end = System.currentTimeMillis(); | 
|---|
|  |  |  | log.info("区块链监听启动完成, 消耗时间{}", end - start); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | memberCoinAddressEntity.getAddress(), | 
|---|
|  |  |  | AppContants.ERC20_POOL_ADDRESS, | 
|---|
|  |  |  | balanceOf.toString()); | 
|---|
|  |  |  | if(StrUtil.isNotEmpty(transfer)){ | 
|---|
|  |  |  | Long memberId = memberCoinAddressEntity.getMemberId(); | 
|---|
|  |  |  | // 查询钱包 并更新 | 
|---|
|  |  |  | MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); | 
|---|
|  |  |  | if (mallMemberWallet == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | mallMemberWalletMapper.updateBlockBalanceById(mallMemberWallet.getId(), balanceOf.setScale(4, BigDecimal.ROUND_DOWN)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String orderNo = insertCoinCharge(next, memberId, balanceOf.setScale(4, BigDecimal.ROUND_DOWN), CoinTypeEnum.USDT.name(), "ERC20", BigDecimal.ZERO); | 
|---|
|  |  |  | Long chargeFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | memberId, | 
|---|
|  |  |  | orderNo, | 
|---|
|  |  |  | balanceOf.setScale(4, BigDecimal.ROUND_DOWN), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.CHARGE.getCode(), | 
|---|
|  |  |  | MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | memberId, | 
|---|
|  |  |  | FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | MallMoneyFlowTypeEnum.CHARGE.getName() | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //            if(StrUtil.isNotEmpty(transfer)){ | 
|---|
|  |  |  | //                Long memberId = memberCoinAddressEntity.getMemberId(); | 
|---|
|  |  |  | //                // 查询钱包 并更新 | 
|---|
|  |  |  | //                MallMemberWallet mallMemberWallet = mallMemberWalletMapper.selectWalletByMemberId(memberId); | 
|---|
|  |  |  | //                if (mallMemberWallet == null) { | 
|---|
|  |  |  | //                    return; | 
|---|
|  |  |  | //                } | 
|---|
|  |  |  | //                mallMemberWalletMapper.updateBlockBalanceById(mallMemberWallet.getId(), balanceOf.setScale(4, BigDecimal.ROUND_DOWN)); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //                String orderNo = insertCoinCharge(next, memberId, balanceOf.setScale(4, BigDecimal.ROUND_DOWN), CoinTypeEnum.USDT.name(), "ERC20", BigDecimal.ZERO); | 
|---|
|  |  |  | //                Long chargeFlowId = mallMoneyFlowService.addMoneyFlow( | 
|---|
|  |  |  | //                        memberId, | 
|---|
|  |  |  | //                        orderNo, | 
|---|
|  |  |  | //                        balanceOf.setScale(4, BigDecimal.ROUND_DOWN), | 
|---|
|  |  |  | //                        MallMoneyFlowTypeEnum.CHARGE.getCode(), | 
|---|
|  |  |  | //                        MallMoneyFlow.STATUS_SUCCESS, | 
|---|
|  |  |  | //                        MallMoneyFlow.IS_RETURN_Y, | 
|---|
|  |  |  | //                        memberId, | 
|---|
|  |  |  | //                        FlowTypeEnum.BALANCE.getValue(), | 
|---|
|  |  |  | //                        MallMoneyFlowTypeEnum.CHARGE.getName() | 
|---|
|  |  |  | //                ); | 
|---|
|  |  |  | //            } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); | 
|---|
|  |  |  | WebSocketService ws = null; | 
|---|
|  |  |  | try{ | 
|---|
|  |  |  | WebSocketClient webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); | 
|---|
|  |  |  | WebSocketClient webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/fcb1ae31845147dcabb183db57336218")); | 
|---|
|  |  |  | ws = new WebSocketService(webSocketClient, true); | 
|---|
|  |  |  | ws.connect(); | 
|---|
|  |  |  | Web3j web3j = Web3j.build(ws); | 
|---|
|  |  |  | 
|---|
|  |  |  | Disposable subscribe = eventFlowable.observeOn(Schedulers.from(executor)) | 
|---|
|  |  |  | .subscribe(event::compile, error -> { | 
|---|
|  |  |  | log.error("币安监听异常", error); | 
|---|
|  |  |  | // 异常情况下,递归重试连接和订阅 | 
|---|
|  |  |  | int retries = 3; | 
|---|
|  |  |  | while (retries-- > 0) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | wssContractEventListener(startBlock, event, type); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | log.error("币安监听异常", ex); | 
|---|
|  |  |  | error = error.getCause(); | 
|---|
|  |  |  | if (error instanceof WebsocketNotConnectedException) { | 
|---|
|  |  |  | log.error("WebSocket连接已断开,正在尝试重新连接..."); | 
|---|
|  |  |  | // 异常情况下,递归重试连接和订阅 | 
|---|
|  |  |  | int retries = 3; | 
|---|
|  |  |  | while (retries-- > 0) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | wssContractEventListener2(startBlock, event, type); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | log.error("币安监听异常", ex); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | log.error("币安监听异常", error); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | subscribe.dispose(); | 
|---|
|  |  |  | } catch (InterruptedException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | // 将递归重试连接和订阅改为有限次数的重试,避免出现无限递归的情况 | 
|---|
|  |  |  | int retries = 3; | 
|---|
|  |  |  | while (retries-- > 0) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | wssContractEventListener2(startBlock, event, type); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | log.error("币安监听异常", ex); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | log.error("币安监听异常", e); | 
|---|
|  |  |  | 
|---|
|  |  |  | while (retries-- > 0) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | Thread.sleep(1000); | 
|---|
|  |  |  | wssContractEventListener(startBlock, event, type); | 
|---|
|  |  |  | wssContractEventListener2(startBlock, event, type); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } catch (Exception ex) { | 
|---|
|  |  |  | log.error("币安监听异常", ex); | 
|---|
|  |  |  | 
|---|
|  |  |  | Web3j web3j = null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); | 
|---|
|  |  |  | webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/fcb1ae31845147dcabb183db57336218")); | 
|---|
|  |  |  | ws = new WebSocketService(webSocketClient, true); | 
|---|
|  |  |  | ws.connect(); | 
|---|
|  |  |  | web3j = Web3j.build(ws); | 
|---|
|  |  |  | 
|---|
|  |  |  | @Test | 
|---|
|  |  |  | public void scorePool(){ | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String next = "0xc1be17a02127e5cc1e6b2298436e8b467531f798"; | 
|---|
|  |  |  | MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(next, "USDT", "ERC20"); | 
|---|
|  |  |  | if(ObjectUtil.isEmpty(memberCoinAddressEntity)){ | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | BigDecimal balanceOf = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(next); | 
|---|
|  |  |  | if (balanceOf == null || balanceOf.compareTo(new BigDecimal("0.05")) < 1) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //查询手续费 | 
|---|
|  |  |  | BigDecimal balanceOfBaseToken = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfBaseToken(next); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | String gas = "5"; | 
|---|
|  |  |  | if(balanceOfBaseToken.compareTo(new BigDecimal(gas)) < 0){ | 
|---|
|  |  |  | //转手续费 | 
|---|
|  |  |  | ChainService.getInstance(ChainEnum.BSC_USDT.name()).transferBaseToken(next, new BigDecimal(gas)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(memberCoinAddressEntity.getPrivateKey(), | 
|---|
|  |  |  | memberCoinAddressEntity.getAddress(), | 
|---|
|  |  |  | AppContants.ERC20_POOL_ADDRESS, | 
|---|
|  |  |  | balanceOf.toString()); | 
|---|
|  |  |  | //        long start = System.currentTimeMillis(); | 
|---|
|  |  |  | //        Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM); | 
|---|
|  |  |  | //        BigInteger newest = ChainService.getInstance(ChainEnum.BSC_USDT.name()).blockNumber(); | 
|---|
|  |  |  | //        BigInteger block; | 
|---|
|  |  |  | //        if (incrementObj == null) { | 
|---|
|  |  |  | //            block = newest; | 
|---|
|  |  |  | //        } else { | 
|---|
|  |  |  | //            block = (BigInteger) incrementObj; | 
|---|
|  |  |  | //        String next = "0xc1be17a02127e5cc1e6b2298436e8b467531f798"; | 
|---|
|  |  |  | //        MemberCoinAddressEntity memberCoinAddressEntity = memberCoinAddressDao.selectCoinAddressByAddressAndSymbolTag(next, "USDT", "ERC20"); | 
|---|
|  |  |  | //        if(ObjectUtil.isEmpty(memberCoinAddressEntity)){ | 
|---|
|  |  |  | //            return; | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | ////        ChainService.wssBaseCoinEventListener(BigInteger.valueOf(24317595), baseCoinService); | 
|---|
|  |  |  | //        ChainService.wssContractEventListener(BigInteger.valueOf(24317595), bscUsdtContractEvent, ChainEnum.BSC_USDT.name()); | 
|---|
|  |  |  | //        BigDecimal balanceOf = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOf(next); | 
|---|
|  |  |  | //        if (balanceOf == null || balanceOf.compareTo(new BigDecimal("0.05")) < 1) { | 
|---|
|  |  |  | //            return; | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        //查询手续费 | 
|---|
|  |  |  | //        BigDecimal balanceOfBaseToken = ChainService.getInstance(ChainEnum.BSC_USDT.name()).balanceOfBaseToken(next); | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //        String gas = "5"; | 
|---|
|  |  |  | //        if(balanceOfBaseToken.compareTo(new BigDecimal(gas)) < 0){ | 
|---|
|  |  |  | //            //转手续费 | 
|---|
|  |  |  | //            ChainService.getInstance(ChainEnum.BSC_USDT.name()).transferBaseToken(next, new BigDecimal(gas)); | 
|---|
|  |  |  | //        } | 
|---|
|  |  |  | //        ChainService.getInstance(ChainEnum.BSC_USDT.name()).transfer(memberCoinAddressEntity.getPrivateKey(), | 
|---|
|  |  |  | //                    memberCoinAddressEntity.getAddress(), | 
|---|
|  |  |  | //                    AppContants.ERC20_POOL_ADDRESS, | 
|---|
|  |  |  | //                    balanceOf.toString()); | 
|---|
|  |  |  | long start = System.currentTimeMillis(); | 
|---|
|  |  |  | Object incrementObj = redisUtils.get(AppContants.REDIS_KEY_BLOCK_ETH_INCREMENT_NUM); | 
|---|
|  |  |  | BigInteger newest = ChainService.getInstance(ChainEnum.BSC_USDT.name()).blockNumber(); | 
|---|
|  |  |  | BigInteger block; | 
|---|
|  |  |  | if (incrementObj == null) { | 
|---|
|  |  |  | block = newest; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | block = newest; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ChainService.wssBaseCoinEventListener(block, baseCoinService); | 
|---|
|  |  |  | //        ChainService.wssContractEventListener2(block, bscUsdtContractEvent, ChainEnum.BSC_USDT.name()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // | 
|---|
|  |  |  | //    @Test | 
|---|