forked from woodser/monero-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintBalances.java
More file actions
98 lines (87 loc) · 3.25 KB
/
Copy pathPrintBalances.java
File metadata and controls
98 lines (87 loc) · 3.25 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import monero.wallet.MoneroWallet;
import monero.wallet.model.MoneroAccount;
import monero.wallet.model.MoneroSubaddress;
/**
* Prints the balances of the wallet.
*/
public class PrintBalances {
public static void main(String[] args) {
printBalances();
}
public static void printBalances() {
printBalances(null);
}
public static void printBalances(MoneroWallet wallet) {
// collect info about subaddresses
List<Pair<String, List<Object>>> pairs = new ArrayList<Pair<String, List<Object>>>();
if (wallet == null) wallet = TestUtils.getWalletRpc();
//if (wallet == null) wallet = TestUtils.getWalletFull();
BigInteger balance = wallet.getBalance();
BigInteger unlockedBalance = wallet.getUnlockedBalance();
List<MoneroAccount> accounts = wallet.getAccounts(true);
System.out.println("Wallet balance: " + balance);
System.out.println("Wallet unlocked balance: " + unlockedBalance);
for (MoneroAccount account : accounts) {
add(pairs, "ACCOUNT", account.getIndex());
add(pairs, "SUBADDRESS", "");
add(pairs, "LABEL", "");
add(pairs, "ADDRESS", "");
add(pairs, "BALANCE", account.getBalance());
add(pairs, "UNLOCKED", account.getUnlockedBalance());
for (MoneroSubaddress subaddress : account.getSubaddresses()) {
add(pairs, "ACCOUNT", account.getIndex());
add(pairs, "SUBADDRESS", subaddress.getIndex());
add(pairs, "LABEL", subaddress.getLabel());
add(pairs, "ADDRESS", subaddress.getAddress());
add(pairs, "BALANCE", subaddress.getBalance());
add(pairs, "UNLOCKED", subaddress.getUnlockedBalance());
}
}
// convert info to csv
Integer length = null;
for (Pair<String, List<Object>> pair : pairs) {
if (length == null) length = pair.getSecond().size();
else assertEquals((int) length, (int) pair.getSecond().size());
}
System.out.println(pairsToCsv(pairs));
}
private static void add(List<Pair<String, List<Object>>> pairs, String header, Object value) {
if (value == null) value = "";
Pair<String, List<Object>> pair = null;
for (Pair<String, List<Object>> aPair : pairs) {
if (aPair.getFirst().equals(header)) {
pair = aPair;
break;
}
}
if (pair == null) {
List<Object> vals = new ArrayList<Object>();
pair = new Pair<String, List<Object>>(header, vals);
pairs.add(pair);
}
assertNotNull(value);
pair.getSecond().add(value);
}
private static String pairsToCsv(List<Pair<String, List<Object>>> pairs) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pairs.size(); i++) {
sb.append(pairs.get(i).getFirst());
if (i < pairs.size() - 1) sb.append(',');
else sb.append('\n');
}
for (int i = 0; i < pairs.get(0).getSecond().size(); i++) {
for (int j = 0; j < pairs.size(); j++) {
sb.append(pairs.get(j).getSecond().get(i));
if (j < pairs.size() - 1) sb.append(',');
else sb.append('\n');
}
}
return sb.toString();
}
}