-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSseSubscribe.java
More file actions
84 lines (69 loc) · 2.84 KB
/
SseSubscribe.java
File metadata and controls
84 lines (69 loc) · 2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package event;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.function.Consumer;
import io.github.cdimascio.dotenv.Dotenv;
import io.reactivex.disposables.Disposable;
import net.flashbots.MevShareClient;
import net.flashbots.models.common.Network;
import net.flashbots.models.event.MevShareEvent;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
/**
* SSE subscribe Example
*
* @author kaichen
* @since 0.1.0
*/
public class SseSubscribe {
public static void main(String[] args) throws InterruptedException {
Dotenv dotenv = Dotenv.configure()
.directory(Paths.get("", "example").toAbsolutePath().toString())
.filename(".env")
.load();
Credentials authSigner = Credentials.create(dotenv.get("AUTH_PRIVATE_KEY"));
Web3j web3j = Web3j.build(new HttpService(dotenv.get("PROVIDER_URL")));
var mevShareClient = new MevShareClient(Network.GOERLI, authSigner, web3j);
CountDownLatch latch = new CountDownLatch(5);
List<MevShareEvent> events = new ArrayList<>();
Consumer<MevShareEvent> eventListener = mevShareEvent -> {
events.add(mevShareEvent);
latch.countDown();
};
Disposable disposable = mevShareClient.subscribe(eventListener);
latch.await();
// remember to release when no longer to subscribe events
disposable.dispose();
System.out.println(events);
CountDownLatch latch1 = new CountDownLatch(5);
List<MevShareEvent> txEvents = new ArrayList<>();
Consumer<MevShareEvent> txEventListener = mevShareEvent -> {
txEvents.add(mevShareEvent);
latch1.countDown();
};
Disposable disposable1 = mevShareClient.subscribeTx(txEventListener);
latch1.await();
// remember to release when no longer to subscribe events
disposable1.dispose();
System.out.println(txEvents);
mevShareClient.close();
// subscribe bundle events
// bundle events are very rare, so we comment it out
// CountDownLatch latch2 = new CountDownLatch(1);
// List<MevShareEvent> bundleEvents = new ArrayList<>();
// Consumer<MevShareEvent> bundleEventListener = mevShareEvent -> {
// bundleEvents.add(mevShareEvent);
// latch2.countDown();
// };
// Disposable disposable2 = mevShareClient.subscribeBundle(bundleEventListener);
// latch2.await();
//
// // remember to release when no longer to subscribe events
// disposable2.dispose();
//
// System.out.println(bundleEvents);
}
}