forked from woodser/monero-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSampleCode.java
173 lines (142 loc) · 7.71 KB
/
TestSampleCode.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.File;
import java.math.BigInteger;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import monero.common.MoneroConnectionManager;
import monero.common.MoneroConnectionManagerListener;
import monero.common.MoneroRpcConnection;
import monero.daemon.MoneroDaemon;
import monero.daemon.MoneroDaemonRpc;
import monero.daemon.model.MoneroNetworkType;
import monero.daemon.model.MoneroTx;
import monero.wallet.MoneroWalletFull;
import monero.wallet.MoneroWalletRpc;
import monero.wallet.model.MoneroOutputWallet;
import monero.wallet.model.MoneroTxConfig;
import monero.wallet.model.MoneroTxWallet;
import monero.wallet.model.MoneroWalletConfig;
import monero.wallet.model.MoneroWalletListener;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import utils.TestUtils;
/**
* Test the sample code for README.md.
*/
public class TestSampleCode {
private static boolean FUNDS_RECEIVED = false;
@BeforeAll
public static void setUpBeforeClass() throws Exception {
// all wallets need to wait for txs to confirm to reliably sync
TestUtils.WALLET_TX_TRACKER.reset();
// pre-create test wallet
MoneroWalletRpc wallet = TestUtils.getWalletRpc();
wallet.close();
// create directory for test wallets if it doesn't exist
File testWalletsDir = new File(TestUtils.TEST_WALLETS_DIR);
if (!testWalletsDir.exists()) testWalletsDir.mkdirs();
}
// Sample code demonstration
@SuppressWarnings("unused")
@Test
public void testSampleCode() throws InterruptedException {
// connect to daemon
MoneroDaemon daemon = new MoneroDaemonRpc("http://localhost:28081", "", "");
long height = daemon.getHeight(); // 1523651
List<MoneroTx> txsInPool = daemon.getTxPool(); // get transactions in the pool
// create wallet from seed using JNI bindings to monero-project
MoneroWalletFull walletFull = MoneroWalletFull.createWallet(new MoneroWalletConfig()
.setPath("./test_wallets/" + UUID.randomUUID().toString()) // *** CHANGE README TO "sample_wallet_full" ***
.setPassword("supersecretpassword123")
.setNetworkType(MoneroNetworkType.TESTNET)
.setServerUri("http://localhost:28081")
.setServerUsername("superuser")
.setServerPassword("abctesting123")
.setSeed(TestUtils.SEED) // *** REPLACE WITH SEED IN README ***
.setRestoreHeight(TestUtils.FIRST_RECEIVE_HEIGHT)); // *** REPLACE WITH FIRST RECEIVE HEIGHT IN README ***
// synchronize the wallet and receive progress notifications
walletFull.sync(new MoneroWalletListener() {
@Override
public void onSyncProgress(long height, long startHeight, long endHeight, double percentDone, String message) {
// feed a progress bar?
}
});
// synchronize in the background every 5 seconds
walletFull.startSyncing(5000l);
// receive notifications when funds are received, confirmed, and unlocked
walletFull.addListener(new MoneroWalletListener() {
@Override
public void onOutputReceived(MoneroOutputWallet output) {
BigInteger amount = output.getAmount();
String txHash = output.getTx().getHash();
Boolean isConfirmed = output.getTx().isConfirmed();
Boolean isLocked = output.getTx().isLocked();
FUNDS_RECEIVED = true;
}
});
// connect to wallet RPC and open wallet
MoneroWalletRpc walletRpc = new MoneroWalletRpc(TestUtils.WALLET_RPC_URI, TestUtils.WALLET_RPC_USERNAME, TestUtils.WALLET_RPC_PASSWORD); // *** REPLACE WITH CONSTANTS IN README ***
walletRpc.openWallet("test_wallet_1", "supersecretpassword123"); // *** CHANGE README TO "sample_wallet_rpc" ***
String primaryAddress = walletRpc.getPrimaryAddress(); // 555zgduFhmKd2o8rPUz...
BigInteger balance = walletRpc.getBalance(); // 533648366742
List<MoneroTxWallet> txs = walletRpc.getTxs(); // get transactions containing transfers to/from the wallet
// send funds from RPC wallet to full wallet
TestUtils.WALLET_TX_TRACKER.waitForWalletTxsToClearPool(walletRpc); // *** REMOVE FROM README SAMPLE ***
TestUtils.WALLET_TX_TRACKER.waitForUnlockedBalance(walletRpc, 0, null, new BigInteger("250000000000")); // *** REMOVE FROM README SAMPLE ***
MoneroTxWallet createdTx = walletRpc.createTx(new MoneroTxConfig()
.setAccountIndex(0)
.setAddress(walletFull.getAddress(1, 0))
.setAmount("250000000000") // send 0.25 XMR (denominated in atomic units)
.setRelay(false)); // create transaction and relay to the network if true
BigInteger fee = createdTx.getFee(); // "Are you sure you want to send... ?"
walletRpc.relayTx(createdTx); // relay the transaction
// recipient receives unconfirmed funds within 5 seconds
TimeUnit.SECONDS.sleep(5);
assertTrue(FUNDS_RECEIVED);
// save and close full wallet
walletFull.close(true);
}
// Connection manager demonstration
@SuppressWarnings("unused")
@Test
public void testConnectionManagerDemo() {
// create connection manager
MoneroConnectionManager connectionManager = new MoneroConnectionManager();
// add managed connections with priorities
connectionManager.addConnection(new MoneroRpcConnection("http://localhost:28081").setPriority(1)); // use localhost as first priority
connectionManager.addConnection(new MoneroRpcConnection("http://example.com")); // default priority is prioritized last
// set current connection
connectionManager.setConnection(new MoneroRpcConnection("http://foo.bar", "admin", "password")); // connection is added if new
// create or open wallet governed by connection manager
MoneroWalletFull walletFull = MoneroWalletFull.createWallet(new MoneroWalletConfig()
.setPath("./test_wallets/" + UUID.randomUUID().toString()) // *** CHANGE README TO "sample_wallet_full" ***
.setPassword("supersecretpassword123")
.setNetworkType(MoneroNetworkType.TESTNET)
.setConnectionManager(connectionManager)
.setSeed(TestUtils.SEED) // *** REPLACE WITH SEED IN README ***
.setRestoreHeight(TestUtils.FIRST_RECEIVE_HEIGHT)); // *** REPLACE WITH FIRST RECEIVE HEIGHT IN README ***
// check connection status
connectionManager.checkConnection();
System.out.println("Connection manager is connected: " + connectionManager.isConnected());
System.out.println("Connection is online: " + connectionManager.getConnection().isOnline());
System.out.println("Connection is authenticated: " + connectionManager.getConnection().isAuthenticated());
// receive notifications of any changes to current connection
connectionManager.addListener(new MoneroConnectionManagerListener() {
@Override
public void onConnectionChanged(MoneroRpcConnection connection) {
System.out.println("Connection changed to: " + connection);
}
});
// check connections every 10 seconds (in order of priority) and switch to the best
connectionManager.startPolling(10000l);
// get best available connection in order of priority then response time
MoneroRpcConnection bestConnection = connectionManager.getBestAvailableConnection();
// check status of all connections
connectionManager.checkConnections();
// get connections in order of current connection, online status from last check, priority, and name
List<MoneroRpcConnection> connections = connectionManager.getConnections();
// clear connection manager
connectionManager.clear();
}
}