-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharbitrage bellman ford att 2 java
54 lines (43 loc) · 1.91 KB
/
arbitrage bellman ford att 2 java
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
import com.xrpl.xrpl.XRPL;
import com.xrpl.xrpl.model.AccountInfo;
import com.xrpl.xrpl.model.Currency;
import com.xrpl.xrpl.model.Price;
import com.xrpl.xrpl.model.Transaction;
import java.util.List;
public class Main {
public static void main(String[] args) {
// Create an XRPL instance with the default test net URL.
XRPL xrpl = XRPL.newBuilder().build();
// Get the list of supported currencies.
List<Currency> currencies = xrpl.getCurrencies();
// Initialize a Bellman-Ford graph with the number of vertices equal to the number of currencies.
int numCurrencies = currencies.size();
int[][] graph = new int[numCurrencies][numCurrencies];
// Initialize the graph with the prices of each currency relative to XRP.
for (int i = 0; i < numCurrencies; i++) {
Currency currency = currencies.get(i);
String currencyCode = currency.getCode();
// Get the price of the currency relative to XRP.
Price price = xrpl.getPrice(currencyCode, "XRP");
double priceInXRP = price.getPrice();
// Add the price to the graph as a weighted edge.
for (int j = 0; j < numCurrencies; j++) {
graph[i][j] = (int) (priceInXRP * 100);
}
}
// Use the Bellman-Ford algorithm to detect negative weight cycles in the graph.
boolean hasNegativeCycle = bellmanFord(graph, numCurrencies);
if (hasNegativeCycle) {
System.out.println("The graph contains a negative weight cycle.");
} else {
System.out.println("The graph does not contain a negative weight cycle.");
}
// Get the secret key and address of the account you want to sign a transaction for.
String secretKey = "your secret key here";
String address = "your address here";
// Create a new transaction.
Transaction transaction = new Transaction.Builder()
.setAccount(address)
.build();
// Sign the transaction with the secret key.
transaction = xrpl.sign(transaction, secretKey);