Skip to content

Commit 86385a7

Browse files
committed
feat:make example using .env
Signed-off-by: grapebaba <281165273@qq.com>
1 parent ccef15f commit 86385a7

9 files changed

Lines changed: 82 additions & 24 deletions

File tree

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ bin/
4040
.vscode/
4141

4242
### Mac OS ###
43-
.DS_Store
43+
.DS_Store
44+
45+
*.env

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ Based on [Specs](https://github.com/flashbots/mev-share)
1111

1212
## Using
1313

14-
Download the latest jar from maven central.
15-
1614
### Maven
1715

1816
```xml

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ dependencies {
3030
testImplementation platform('org.junit:junit-bom:5.9.1')
3131
testImplementation 'org.junit.jupiter:junit-jupiter'
3232
testImplementation 'org.apache.logging.log4j:log4j-slf4j2-impl:2.20.0'
33+
testImplementation 'io.github.cdimascio:dotenv-java:3.0.0'
3334
}
3435

3536
sourceSets.test.java {

example/.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# private key used to sign requests to Flashbots APIs; used for reputation, should not have funds
2+
AUTH_PRIVATE_KEY=
3+
# private key used to sign transactions sent to mev-share
4+
SENDER_PRIVATE_KEY=
5+
# GOERLI Ethereum RPC endpoint
6+
PROVIDER_URL=

example/bundle/RpcMevSendBundle.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
import java.io.IOException;
44
import java.math.BigInteger;
5+
import java.nio.file.Paths;
56
import java.util.List;
67
import java.util.concurrent.CompletableFuture;
78
import java.util.concurrent.ExecutionException;
89

10+
import io.github.cdimascio.dotenv.Dotenv;
911
import io.reactivex.disposables.Disposable;
1012
import net.flashbots.MevShareClient;
1113
import net.flashbots.models.bundle.BundleItemType;
@@ -33,9 +35,13 @@
3335
public class RpcMevSendBundle {
3436

3537
public static void main(String[] args) throws ExecutionException, InterruptedException, IOException {
36-
Credentials sender = Credentials.create("<hex string of privateKey>");
37-
var web3j = Web3j.build(new HttpService("<L1 network url>"));
38-
var mevShareClient = new MevShareClient(Network.GOERLI, sender, web3j);
38+
Dotenv dotenv = Dotenv.configure()
39+
.directory(Paths.get("", "example").toAbsolutePath().toString())
40+
.filename(".env")
41+
.load();
42+
Credentials authSigner = Credentials.create(dotenv.get("AUTH_PRIVATE_KEY"));
43+
Web3j web3j = Web3j.build(new HttpService(dotenv.get("PROVIDER_URL")));
44+
var mevShareClient = new MevShareClient(Network.GOERLI, authSigner, web3j);
3945

4046
CompletableFuture<MevShareEvent> future = new CompletableFuture<>();
4147
Disposable eventSource = mevShareClient.subscribe(mevShareEvent -> {
@@ -56,14 +62,14 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
5662

5763
BundleItemType.HashItem bundleItem = new BundleItemType.HashItem().setHash(mevShareEvent.getHash());
5864

59-
Credentials signer = Credentials.create("<private key>");
65+
Credentials signer = Credentials.create(dotenv.get("SENDER_PRIVATE_KEY"));
6066
BigInteger nonce = web3j.ethGetTransactionCount(signer.getAddress(), DefaultBlockParameterName.PENDING)
6167
.send()
6268
.getTransactionCount();
6369
BigInteger gasPrice = web3j.ethGasPrice().send().getGasPrice();
6470
BigInteger gasLimit = DefaultGasProvider.GAS_LIMIT;
65-
final String to = "<to address>";
66-
final String amount = "<ether amount>";
71+
final String to = "0x56EdF679B0C80D528E17c5Ffe514dc9a1b254b9c";
72+
final String amount = "0.001";
6773
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
6874
nonce,
6975
gasPrice,

example/bundle/RpcMevSimBundle.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import java.io.IOException;
44
import java.math.BigInteger;
5+
import java.nio.file.Paths;
56
import java.util.List;
67
import java.util.concurrent.ExecutionException;
78

9+
import io.github.cdimascio.dotenv.Dotenv;
810
import net.flashbots.MevShareClient;
911
import net.flashbots.models.bundle.BundleItemType;
1012
import net.flashbots.models.bundle.BundleParams;
@@ -31,10 +33,13 @@
3133
public class RpcMevSimBundle {
3234

3335
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
34-
35-
Credentials sender = Credentials.create("<hex string of privateKey>");
36-
var web3j = Web3j.build(new HttpService("<L1 network url>"));
37-
var mevShareClient = new MevShareClient(Network.GOERLI, sender, web3j);
36+
Dotenv dotenv = Dotenv.configure()
37+
.directory(Paths.get("", "example").toAbsolutePath().toString())
38+
.filename(".env")
39+
.load();
40+
Credentials authSigner = Credentials.create(dotenv.get("AUTH_PRIVATE_KEY"));
41+
Web3j web3j = Web3j.build(new HttpService(dotenv.get("PROVIDER_URL")));
42+
var mevShareClient = new MevShareClient(Network.GOERLI, authSigner, web3j);
3843

3944
var latestBlock = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false)
4045
.send()
@@ -48,11 +53,11 @@ public static void main(String[] args) throws IOException, ExecutionException, I
4853
.setBlock(latestBlock.getNumber().subtract(BigInteger.ONE))
4954
.setMaxBlock(latestBlock.getNumber().add(BigInteger.valueOf(10)));
5055

51-
Credentials signer = Credentials.create("<private key>");
56+
Credentials signer = Credentials.create(dotenv.get("SENDER_PRIVATE_KEY"));
5257
BigInteger nonce = web3j.ethGetTransactionCount(signer.getAddress(), DefaultBlockParameterName.PENDING)
5358
.send()
5459
.getTransactionCount();
55-
final String to = "<to address>";
60+
final String to = "0x56EdF679B0C80D528E17c5Ffe514dc9a1b254b9c";
5661
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(
5762
nonce,
5863
web3j.ethGasPrice().send().getGasPrice(),

example/bundle/RpcSendPrivateTx.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import java.io.IOException;
44
import java.math.BigInteger;
55
import java.nio.charset.StandardCharsets;
6+
import java.nio.file.Paths;
67
import java.util.concurrent.CompletableFuture;
78
import java.util.concurrent.ExecutionException;
89

10+
import io.github.cdimascio.dotenv.Dotenv;
911
import net.flashbots.MevShareClient;
1012
import net.flashbots.models.bundle.HintPreferences;
1113
import net.flashbots.models.bundle.PrivateTxOptions;
@@ -29,21 +31,25 @@
2931
public class RpcSendPrivateTx {
3032

3133
public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
32-
Credentials sender = Credentials.create("<hex string of privateKey>");
33-
var web3j = Web3j.build(new HttpService("<L1 network url>"));
34-
var mevShareClient = new MevShareClient(Network.GOERLI, sender, web3j);
34+
Dotenv dotenv = Dotenv.configure()
35+
.directory(Paths.get("", "example").toAbsolutePath().toString())
36+
.filename(".env")
37+
.load();
38+
Credentials authSigner = Credentials.create(dotenv.get("AUTH_PRIVATE_KEY"));
39+
Web3j web3j = Web3j.build(new HttpService(dotenv.get("PROVIDER_URL")));
40+
var mevShareClient = new MevShareClient(Network.GOERLI, authSigner, web3j);
3541

3642
EthBlock.Block latest = web3j.ethGetBlockByNumber(DefaultBlockParameterName.LATEST, false)
3743
.send()
3844
.getBlock();
3945

4046
BigInteger maxPriorityFeePerGas = BigInteger.valueOf(1_000_000_000L);
4147

42-
Credentials signer = Credentials.create("<private key>");
48+
Credentials signer = Credentials.create(dotenv.get("SENDER_PRIVATE_KEY"));
4349
BigInteger nonce = web3j.ethGetTransactionCount(signer.getAddress(), DefaultBlockParameterName.PENDING)
4450
.send()
4551
.getTransactionCount();
46-
final String to = "<to address>";
52+
final String to = "0x56EdF679B0C80D528E17c5Ffe514dc9a1b254b9c";
4753

4854
RawTransaction rawTransaction = RawTransaction.createTransaction(
4955
5L,

example/event/SseHistorical.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package event;
22

33
import java.math.BigInteger;
4+
import java.nio.file.Paths;
45
import java.util.List;
56
import java.util.concurrent.CompletableFuture;
67
import java.util.concurrent.ExecutionException;
78

9+
import io.github.cdimascio.dotenv.Dotenv;
810
import net.flashbots.MevShareClient;
911
import net.flashbots.models.common.Network;
1012
import net.flashbots.models.event.EventHistoryEntry;
1113
import net.flashbots.models.event.EventHistoryInfo;
1214
import net.flashbots.models.event.EventHistoryParams;
15+
import org.web3j.crypto.Credentials;
16+
import org.web3j.protocol.Web3j;
17+
import org.web3j.protocol.http.HttpService;
1318

1419
/**
1520
* SSE historical example
@@ -20,7 +25,13 @@
2025
public class SseHistorical {
2126

2227
public static void main(String[] args) throws ExecutionException, InterruptedException {
23-
var mevShareClient = new MevShareClient(Network.GOERLI, null, null);
28+
Dotenv dotenv = Dotenv.configure()
29+
.directory(Paths.get("", "example").toAbsolutePath().toString())
30+
.filename(".env")
31+
.load();
32+
Credentials authSigner = Credentials.create(dotenv.get("AUTH_PRIVATE_KEY"));
33+
Web3j web3j = Web3j.build(new HttpService(dotenv.get("PROVIDER_URL")));
34+
var mevShareClient = new MevShareClient(Network.GOERLI, authSigner, web3j);
2435

2536
// get event history info
2637
CompletableFuture<EventHistoryInfo> historyInfoFuture = mevShareClient.getEventHistoryInfo();

example/event/SseSubscribe.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
package event;
22

3+
import java.nio.file.Paths;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
import java.util.concurrent.CountDownLatch;
37
import java.util.function.Consumer;
48

9+
import io.github.cdimascio.dotenv.Dotenv;
510
import io.reactivex.disposables.Disposable;
611
import net.flashbots.MevShareClient;
712
import net.flashbots.models.common.Network;
813
import net.flashbots.models.event.MevShareEvent;
14+
import org.web3j.crypto.Credentials;
15+
import org.web3j.protocol.Web3j;
16+
import org.web3j.protocol.http.HttpService;
917

1018
/**
1119
* SSE subscribe Example
@@ -15,15 +23,30 @@
1523
*/
1624
public class SseSubscribe {
1725

18-
public static void main(String[] args) {
19-
var mevShareClient = new MevShareClient(Network.GOERLI, null, null);
26+
public static void main(String[] args) throws InterruptedException {
27+
Dotenv dotenv = Dotenv.configure()
28+
.directory(Paths.get("", "example").toAbsolutePath().toString())
29+
.filename(".env")
30+
.load();
31+
Credentials authSigner = Credentials.create(dotenv.get("AUTH_PRIVATE_KEY"));
32+
Web3j web3j = Web3j.build(new HttpService(dotenv.get("PROVIDER_URL")));
33+
34+
var mevShareClient = new MevShareClient(Network.GOERLI, authSigner, web3j);
35+
36+
CountDownLatch latch = new CountDownLatch(5);
37+
List<MevShareEvent> events = new ArrayList<>();
2038
Consumer<MevShareEvent> eventListener = mevShareEvent -> {
21-
// do something and do not block here...
39+
events.add(mevShareEvent);
40+
latch.countDown();
2241
};
2342

2443
Disposable disposable = mevShareClient.subscribe(eventListener);
2544

45+
latch.await();
46+
2647
// remember to release when no longer to subscribe events
2748
disposable.dispose();
49+
50+
System.out.println(events);
2851
}
2952
}

0 commit comments

Comments
 (0)