| | |
| | | import cc.mrbird.febs.mall.chain.enums.ChainEnum; |
| | | import cc.mrbird.febs.mall.chain.enums.EthService; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.sun.xml.internal.bind.v2.TODO; |
| | | import io.reactivex.Flowable; |
| | | import io.reactivex.disposables.Disposable; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.java_websocket.WebSocket; |
| | | import org.java_websocket.exceptions.WebsocketNotConnectedException; |
| | | import org.web3j.crypto.Credentials; |
| | | import org.web3j.protocol.Web3j; |
| | | import org.web3j.protocol.core.DefaultBlockParameter; |
| | |
| | | WebSocketService ws = null; |
| | | WebSocketClient webSocketClient = null; |
| | | Web3j web3j = null; |
| | | ChainEnum chain = ChainEnum.getValueByName(type); |
| | | |
| | | // try { |
| | | // webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | // ws = new WebSocketService(webSocketClient, true); |
| | | // ws.connect(); |
| | | // } catch (Exception e) { |
| | | // try { |
| | | // ws.close(); |
| | | // //TODO |
| | | // if(ObjectUtil.isEmpty(webSocketClient)){ |
| | | // webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | // ws = new WebSocketService(webSocketClient, true); |
| | | // ws.connect(); |
| | | // } |
| | | // WebSocket.READYSTATE readyState = webSocketClient.getReadyState(); |
| | | // if((WebSocket.READYSTATE.OPEN != readyState |
| | | // && WebSocket.READYSTATE.CONNECTING != readyState)){ |
| | | // webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | // ws = new WebSocketService(webSocketClient, true); |
| | | // ws.connect(); |
| | | // } |
| | | // } catch (Exception connectException) { |
| | | // connectException.printStackTrace(); |
| | | // } |
| | | // } |
| | | |
| | | |
| | | try { |
| | | webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | ws = new WebSocketService(webSocketClient, true); |
| | | ws.connect(); |
| | | } catch (Exception e) { |
| | | try { |
| | | ws.close(); |
| | | //TODO |
| | | if(ObjectUtil.isEmpty(webSocketClient)){ |
| | | webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | ws = new WebSocketService(webSocketClient, true); |
| | | ws.connect(); |
| | | } |
| | | WebSocket.READYSTATE readyState = webSocketClient.getReadyState(); |
| | | if((WebSocket.READYSTATE.OPEN != readyState |
| | | && WebSocket.READYSTATE.CONNECTING != readyState)){ |
| | | webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | ws = new WebSocketService(webSocketClient, true); |
| | | ws.connect(); |
| | | } |
| | | } catch (Exception connectException) { |
| | | connectException.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | try { |
| | | web3j = Web3j.build(ws); |
| | | ChainEnum chain = ChainEnum.getValueByName(type); |
| | | |
| | | assert chain != null; |
| | | try{ |
| | | EthUsdtContract ethUsdtContract = wssContract(chain.getPrivateKey(), chain.getContractAddress(), web3j); |
| | | EthFilter filter = getFilter(startBlock, startBlock, chain.getContractAddress()); |
| | | Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = ethUsdtContract.transferEventFlowable(filter); |
| | | Disposable subscribe = eventFlowable.subscribe(event::compile, error -> { |
| | | log.error("币安监听异常", error); |
| | | |
| | | EthUsdtContract ethUsdtContract = wssContract(chain.getPrivateKey(), chain.getContractAddress(), web3j); |
| | | EthFilter filter = getFilter(startBlock, startBlock, chain.getContractAddress()); |
| | | }); |
| | | }catch(WebsocketNotConnectedException e){ |
| | | ws.close(); |
| | | webSocketClient = new WebSocketClient(new URI("wss://bsc-mainnet.nodereal.io/ws/v1/78074065950e4915aef4f12b6f357d16")); |
| | | ws = new WebSocketService(webSocketClient, true); |
| | | ws.connect(); |
| | | web3j = Web3j.build(ws); |
| | | assert chain != null; |
| | | EthUsdtContract ethUsdtContract = wssContract(chain.getPrivateKey(), chain.getContractAddress(), web3j); |
| | | EthFilter filter = getFilter(startBlock, startBlock, chain.getContractAddress()); |
| | | Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = ethUsdtContract.transferEventFlowable(filter); |
| | | Disposable subscribe = eventFlowable.subscribe(event::compile, error -> { |
| | | log.error("币安监听异常", error); |
| | | |
| | | }); |
| | | } |
| | | |
| | | Flowable<EthUsdtContract.TransferEventResponse> eventFlowable = ethUsdtContract.transferEventFlowable(filter); |
| | | Disposable subscribe = eventFlowable.subscribe(event::compile, error -> { |
| | | log.error("币安监听异常", error); |
| | | }); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |