forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EditOfferTest.java
644 lines (594 loc) · 30.8 KB
/
EditOfferTest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.apitest.method.offer;
import bisq.core.payment.PaymentAccount;
import bisq.proto.grpc.OfferInfo;
import io.grpc.StatusRuntimeException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import static bisq.apitest.config.ApiTestConfig.BSQ;
import static bisq.cli.TableFormat.formatOfferTable;
import static bisq.core.btc.wallet.Restrictions.getDefaultBuyerSecurityDepositAsPercent;
import static bisq.proto.grpc.EditOfferRequest.EditType.*;
import static java.lang.String.format;
import static java.util.Collections.singletonList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static protobuf.OfferDirection.BUY;
import static protobuf.OfferDirection.SELL;
@SuppressWarnings("ALL")
@Disabled
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class EditOfferTest extends AbstractOfferTest {
// Some test fixtures to reduce duplication.
private static final Map<String, PaymentAccount> paymentAcctCache = new HashMap<>();
private static final String DOLLAR = "USD";
private static final String RUBLE = "RUB";
private static final long AMOUNT = 10000000L;
@Test
@Order(1)
public void testOfferDisableAndEnable() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("DE");
OfferInfo originalOffer = createMktPricedOfferForEdit(BUY.name(),
"EUR",
paymentAcct.getId(),
0.0,
NO_TRIGGER_PRICE);
log.info("ORIGINAL EUR OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "EUR"));
assertFalse(originalOffer.getIsActivated()); // Not activated until prep is done.
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
originalOffer = aliceClient.getMyOffer(originalOffer.getId());
assertTrue(originalOffer.getIsActivated());
// Disable offer
aliceClient.editOfferActivationState(originalOffer.getId(), DEACTIVATE_OFFER);
genBtcBlocksThenWait(1, 1500); // Wait for offer book removal.
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED EUR OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "EUR"));
assertFalse(editedOffer.getIsActivated());
// Re-enable offer
aliceClient.editOfferActivationState(editedOffer.getId(), ACTIVATE_OFFER);
genBtcBlocksThenWait(1, 1500); // Wait for offer book re-entry.
editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED EUR OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "EUR"));
assertTrue(editedOffer.getIsActivated());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(2)
public void testEditTriggerPrice() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("FI");
OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
"EUR",
paymentAcct.getId(),
0.0,
NO_TRIGGER_PRICE);
log.info("ORIGINAL EUR OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "EUR"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
originalOffer = aliceClient.getMyOffer(originalOffer.getId());
assertEquals(0 /*no trigger price*/, originalOffer.getTriggerPrice());
// Edit the offer's trigger price, nothing else.
var mktPrice = aliceClient.getBtcPrice("EUR");
var delta = 5_000.00;
var newTriggerPriceAsLong = calcPriceAsLong.apply(mktPrice, delta);
aliceClient.editOfferTriggerPrice(originalOffer.getId(), newTriggerPriceAsLong);
sleep(2500); // Wait for offer book re-entry.
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED EUR OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "EUR"));
assertEquals(newTriggerPriceAsLong, editedOffer.getTriggerPrice());
assertTrue(editedOffer.getUseMarketBasedPrice());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(3)
public void testSetTriggerPriceToNegativeValueShouldThrowException() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("FI");
final OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
"EUR",
paymentAcct.getId(),
0.0,
NO_TRIGGER_PRICE);
log.info("ORIGINAL EUR OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "EUR"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
// Edit the offer's trigger price, set to -1, check error.
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
aliceClient.editOfferTriggerPrice(originalOffer.getId(), -1L));
String expectedExceptionMessage =
format("UNKNOWN: programmer error: cannot set trigger price to a negative value in offer with id '%s'",
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
}
@Test
@Order(4)
public void testEditMktPriceMargin() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("US");
var originalMktPriceMargin = new BigDecimal("0.1").doubleValue();
OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
DOLLAR,
paymentAcct.getId(),
originalMktPriceMargin,
NO_TRIGGER_PRICE);
log.info("ORIGINAL USD OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "USD"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
assertEquals(scaledDownMktPriceMargin.apply(originalMktPriceMargin), originalOffer.getMarketPriceMargin());
// Edit the offer's price margin, nothing else.
var newMktPriceMargin = new BigDecimal("0.5").doubleValue();
aliceClient.editOfferPriceMargin(originalOffer.getId(), newMktPriceMargin);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED USD OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "USD"));
assertEquals(scaledDownMktPriceMargin.apply(newMktPriceMargin), editedOffer.getMarketPriceMargin());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(5)
public void testEditFixedPrice() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("RU");
double mktPriceAsDouble = aliceClient.getBtcPrice(RUBLE);
String fixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 200_000.0000);
OfferInfo originalOffer = createFixedPricedOfferForEdit(BUY.name(),
RUBLE,
paymentAcct.getId(),
fixedPriceAsString);
log.info("ORIGINAL RUB OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "RUB"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
// Edit the offer's fixed price, nothing else.
String editedFixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 100_000.0000);
aliceClient.editOfferFixedPrice(originalOffer.getId(), editedFixedPriceAsString);
// Wait for edited offer to be removed from offer-book, edited, and re-published.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED RUB OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "RUB"));
var expectedNewFixedPrice = scaledUpFiatOfferPrice.apply(new BigDecimal(editedFixedPriceAsString));
assertEquals(expectedNewFixedPrice, editedOffer.getPrice());
assertFalse(editedOffer.getUseMarketBasedPrice());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(6)
public void testEditFixedPriceAndDeactivation() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("RU");
double mktPriceAsDouble = aliceClient.getBtcPrice(RUBLE);
String fixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 200_000.0000);
OfferInfo originalOffer = createFixedPricedOfferForEdit(BUY.name(),
RUBLE,
paymentAcct.getId(),
fixedPriceAsString);
log.info("ORIGINAL RUB OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "RUB"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
// Edit the offer's fixed price and deactivate it.
String editedFixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 100_000.0000);
aliceClient.editOffer(originalOffer.getId(),
editedFixedPriceAsString,
originalOffer.getUseMarketBasedPrice(),
0.0,
NO_TRIGGER_PRICE,
DEACTIVATE_OFFER,
FIXED_PRICE_AND_ACTIVATION_STATE);
// Wait for edited offer to be removed from offer-book, edited, and re-published.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED RUB OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "RUB"));
var expectedNewFixedPrice = scaledUpFiatOfferPrice.apply(new BigDecimal(editedFixedPriceAsString));
assertEquals(expectedNewFixedPrice, editedOffer.getPrice());
assertFalse(editedOffer.getIsActivated());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(7)
public void testEditMktPriceMarginAndDeactivation() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("US");
var originalMktPriceMargin = new BigDecimal("0.0").doubleValue();
OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
DOLLAR,
paymentAcct.getId(),
originalMktPriceMargin,
0);
log.info("ORIGINAL USD OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "USD"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
originalOffer = aliceClient.getMyOffer(originalOffer.getId());
assertEquals(scaledDownMktPriceMargin.apply(originalMktPriceMargin), originalOffer.getMarketPriceMargin());
// Edit the offer's price margin and trigger price, and deactivate it.
var newMktPriceMargin = new BigDecimal("1.50").doubleValue();
aliceClient.editOffer(originalOffer.getId(),
"0.00",
originalOffer.getUseMarketBasedPrice(),
newMktPriceMargin,
0,
DEACTIVATE_OFFER,
MKT_PRICE_MARGIN_AND_ACTIVATION_STATE);
// Wait for edited offer to be removed from offer-book, edited, and re-published.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED USD OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "USD"));
assertEquals(scaledDownMktPriceMargin.apply(newMktPriceMargin), editedOffer.getMarketPriceMargin());
assertEquals(0, editedOffer.getTriggerPrice());
assertFalse(editedOffer.getIsActivated());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(8)
public void testEditMktPriceMarginAndTriggerPriceAndDeactivation() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("US");
var originalMktPriceMargin = new BigDecimal("0.0").doubleValue();
var mktPriceAsDouble = aliceClient.getBtcPrice(DOLLAR);
var originalTriggerPriceAsLong = calcPriceAsLong.apply(mktPriceAsDouble, -5_000.0000);
OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
DOLLAR,
paymentAcct.getId(),
originalMktPriceMargin,
originalTriggerPriceAsLong);
log.info("ORIGINAL USD OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "USD"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
originalOffer = aliceClient.getMyOffer(originalOffer.getId());
assertEquals(scaledDownMktPriceMargin.apply(originalMktPriceMargin), originalOffer.getMarketPriceMargin());
assertEquals(originalTriggerPriceAsLong, originalOffer.getTriggerPrice());
// Edit the offer's price margin and trigger price, and deactivate it.
var newMktPriceMargin = new BigDecimal("0.1").doubleValue();
var newTriggerPriceAsLong = calcPriceAsLong.apply(mktPriceAsDouble, -2_000.0000);
aliceClient.editOffer(originalOffer.getId(),
"0.00",
originalOffer.getUseMarketBasedPrice(),
newMktPriceMargin,
newTriggerPriceAsLong,
DEACTIVATE_OFFER,
MKT_PRICE_MARGIN_AND_TRIGGER_PRICE_AND_ACTIVATION_STATE);
// Wait for edited offer to be removed from offer-book, edited, and re-published.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED USD OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "USD"));
assertEquals(scaledDownMktPriceMargin.apply(newMktPriceMargin), editedOffer.getMarketPriceMargin());
assertEquals(newTriggerPriceAsLong, editedOffer.getTriggerPrice());
assertFalse(editedOffer.getIsActivated());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(9)
public void testEditingFixedPriceInMktPriceMarginBasedOfferShouldThrowException() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("US");
var originalMktPriceMargin = new BigDecimal("0.0").doubleValue();
final OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
DOLLAR,
paymentAcct.getId(),
originalMktPriceMargin,
NO_TRIGGER_PRICE);
log.info("ORIGINAL USD OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "USD"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
// Try to edit both the fixed price and mkt price margin.
var newMktPriceMargin = new BigDecimal("0.25").doubleValue();
var newFixedPrice = "50000.0000";
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
aliceClient.editOffer(originalOffer.getId(),
newFixedPrice,
originalOffer.getUseMarketBasedPrice(),
newMktPriceMargin,
NO_TRIGGER_PRICE,
ACTIVATE_OFFER,
MKT_PRICE_MARGIN_ONLY));
String expectedExceptionMessage =
format("UNKNOWN: programmer error: cannot set fixed price (%s) in"
+ " mkt price margin based offer with id '%s'",
newFixedPrice,
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
}
@Test
@Order(10)
public void testEditingTriggerPriceInFixedPriceOfferShouldThrowException() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("RU");
double mktPriceAsDouble = aliceClient.getBtcPrice(RUBLE);
String fixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 200_000.0000);
OfferInfo originalOffer = createFixedPricedOfferForEdit(BUY.name(),
RUBLE,
paymentAcct.getId(),
fixedPriceAsString);
log.info("ORIGINAL RUB OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "RUB"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
long newTriggerPrice = 1000000L;
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
aliceClient.editOfferTriggerPrice(originalOffer.getId(), newTriggerPrice));
String expectedExceptionMessage =
format("UNKNOWN: programmer error: cannot set a trigger price (%s) in"
+ " fixed price offer with id '%s'",
newTriggerPrice,
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
}
@Test
@Order(11)
public void testChangeFixedPriceOfferToPriceMarginBasedOfferWithTriggerPrice() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("MX");
double mktPriceAsDouble = aliceClient.getBtcPrice("MXN");
String fixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 0.00);
OfferInfo originalOffer = createFixedPricedOfferForEdit(BUY.name(),
"MXN",
paymentAcct.getId(),
fixedPriceAsString);
log.info("ORIGINAL MXN OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "MXN"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
// Change the offer to mkt price based and set a trigger price.
var newMktPriceMargin = new BigDecimal("0.05").doubleValue();
var delta = 200_000.0000; // trigger price on buy offer is 200K above mkt price
var newTriggerPriceAsLong = calcPriceAsLong.apply(mktPriceAsDouble, delta);
aliceClient.editOffer(originalOffer.getId(),
"0.00",
true,
newMktPriceMargin,
newTriggerPriceAsLong,
ACTIVATE_OFFER,
MKT_PRICE_MARGIN_AND_TRIGGER_PRICE);
// Wait for edited offer to be removed from offer-book, edited, and re-published.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED MXN OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "MXN"));
assertTrue(editedOffer.getUseMarketBasedPrice());
assertEquals(scaledDownMktPriceMargin.apply(newMktPriceMargin), editedOffer.getMarketPriceMargin());
assertEquals(newTriggerPriceAsLong, editedOffer.getTriggerPrice());
assertTrue(editedOffer.getIsActivated());
doSanityCheck(originalOffer, editedOffer);
}
@Test
@Order(12)
public void testChangePriceMarginBasedOfferToFixedPriceOfferAndDeactivateIt() {
PaymentAccount paymentAcct = getOrCreatePaymentAccount("GB");
double mktPriceAsDouble = aliceClient.getBtcPrice("GBP");
var originalMktPriceMargin = new BigDecimal("0.25").doubleValue();
var delta = 1_000.0000; // trigger price on sell offer is 1K below mkt price
var originalTriggerPriceAsLong = calcPriceAsLong.apply(mktPriceAsDouble, delta);
final OfferInfo originalOffer = createMktPricedOfferForEdit(SELL.name(),
"GBP",
paymentAcct.getId(),
originalMktPriceMargin,
originalTriggerPriceAsLong);
log.info("ORIGINAL GBP OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "GBP"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
String fixedPriceAsString = calcPriceAsString.apply(mktPriceAsDouble, 0.00);
aliceClient.editOffer(originalOffer.getId(),
fixedPriceAsString,
false,
0.00,
0,
DEACTIVATE_OFFER,
FIXED_PRICE_AND_ACTIVATION_STATE);
// Wait for edited offer to be removed from offer-book, edited, and re-published.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED GBP OFFER:\n{}", formatOfferTable(singletonList(editedOffer), "GBP"));
assertEquals(scaledUpFiatOfferPrice.apply(new BigDecimal(fixedPriceAsString)), editedOffer.getPrice());
assertFalse(editedOffer.getUseMarketBasedPrice());
assertEquals(0.00, editedOffer.getMarketPriceMargin());
assertEquals(0, editedOffer.getTriggerPrice());
assertFalse(editedOffer.getIsActivated());
}
@Test
@Order(13)
public void testChangeFixedPricedBsqOfferToPriceMarginBasedOfferShouldThrowException() {
createBsqPaymentAccounts();
OfferInfo originalOffer = aliceClient.createFixedPricedOffer(BUY.name(),
BSQ,
100_000_000L,
100_000_000L,
"0.00005", // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
BSQ);
log.info("ORIGINAL BSQ OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "BSQ"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
aliceClient.editOffer(originalOffer.getId(),
"0.00",
true,
0.1,
0,
ACTIVATE_OFFER,
MKT_PRICE_MARGIN_ONLY));
String expectedExceptionMessage = format("UNKNOWN: cannot set mkt price margin or"
+ " trigger price on fixed price altcoin offer with id '%s'",
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
}
@Test
@Order(14)
public void testEditTriggerPriceOnFixedPriceBsqOfferShouldThrowException() {
createBsqPaymentAccounts();
OfferInfo originalOffer = aliceClient.createFixedPricedOffer(BUY.name(),
BSQ,
100_000_000L,
100_000_000L,
"0.00005", // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
BSQ);
log.info("ORIGINAL BSQ OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "BSQ"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
var newTriggerPriceAsLong = calcPriceAsLong.apply(0.00005, 0.00);
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
aliceClient.editOffer(originalOffer.getId(),
"0.00",
false,
0.1,
newTriggerPriceAsLong,
ACTIVATE_OFFER,
TRIGGER_PRICE_ONLY));
String expectedExceptionMessage = format("UNKNOWN: cannot set mkt price margin or"
+ " trigger price on fixed price altcoin offer with id '%s'",
originalOffer.getId());
assertEquals(expectedExceptionMessage, exception.getMessage());
}
@Test
@Order(15)
public void testEditFixedPriceOnBsqOffer() {
createBsqPaymentAccounts();
String fixedPriceAsString = "0.00005"; // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
final OfferInfo originalOffer = aliceClient.createFixedPricedOffer(BUY.name(),
BSQ,
100_000_000L,
100_000_000L,
fixedPriceAsString,
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
BSQ);
log.info("ORIGINAL BSQ OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "BSQ"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
String newFixedPriceAsString = "0.00003111";
aliceClient.editOffer(originalOffer.getId(),
newFixedPriceAsString,
false,
0.0,
0,
ACTIVATE_OFFER,
FIXED_PRICE_ONLY);
// Wait for edited offer to be edited and removed from offer-book.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED BSQ OFFER:\n{}", formatOfferTable(singletonList(editedOffer), BSQ));
assertEquals(scaledUpAltcoinOfferPrice.apply(newFixedPriceAsString), editedOffer.getPrice());
assertTrue(editedOffer.getIsActivated());
assertFalse(editedOffer.getUseMarketBasedPrice());
assertEquals(0.00, editedOffer.getMarketPriceMargin());
assertEquals(0, editedOffer.getTriggerPrice());
}
@Test
@Order(16)
public void testDisableBsqOffer() {
createBsqPaymentAccounts();
String fixedPriceAsString = "0.00005"; // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
final OfferInfo originalOffer = aliceClient.createFixedPricedOffer(BUY.name(),
BSQ,
100_000_000L,
100_000_000L,
fixedPriceAsString,
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
BSQ);
log.info("ORIGINAL BSQ OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "BSQ"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
aliceClient.editOffer(originalOffer.getId(),
fixedPriceAsString,
false,
0.0,
0,
DEACTIVATE_OFFER,
ACTIVATION_STATE_ONLY);
// Wait for edited offer to be removed from offer-book.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED BSQ OFFER:\n{}", formatOfferTable(singletonList(editedOffer), BSQ));
assertFalse(editedOffer.getIsActivated());
assertEquals(scaledUpAltcoinOfferPrice.apply(fixedPriceAsString), editedOffer.getPrice());
assertFalse(editedOffer.getUseMarketBasedPrice());
assertEquals(0.00, editedOffer.getMarketPriceMargin());
assertEquals(0, editedOffer.getTriggerPrice());
}
@Test
@Order(17)
public void testEditFixedPriceAndDisableBsqOffer() {
createBsqPaymentAccounts();
String fixedPriceAsString = "0.00005"; // FIXED PRICE IN BTC (satoshis) FOR 1 BSQ
final OfferInfo originalOffer = aliceClient.createFixedPricedOffer(BUY.name(),
BSQ,
100_000_000L,
100_000_000L,
fixedPriceAsString,
getDefaultBuyerSecurityDepositAsPercent(),
alicesBsqAcct.getId(),
BSQ);
log.info("ORIGINAL BSQ OFFER:\n{}", formatOfferTable(singletonList(originalOffer), "BSQ"));
genBtcBlocksThenWait(1, 2500); // Wait for offer book entry.
String newFixedPriceAsString = "0.000045";
aliceClient.editOffer(originalOffer.getId(),
newFixedPriceAsString,
false,
0.0,
0,
DEACTIVATE_OFFER,
FIXED_PRICE_AND_ACTIVATION_STATE);
// Wait for edited offer to be edited and removed from offer-book.
genBtcBlocksThenWait(1, 2500);
OfferInfo editedOffer = aliceClient.getMyOffer(originalOffer.getId());
log.info("EDITED BSQ OFFER:\n{}", formatOfferTable(singletonList(editedOffer), BSQ));
assertFalse(editedOffer.getIsActivated());
assertEquals(scaledUpAltcoinOfferPrice.apply(newFixedPriceAsString), editedOffer.getPrice());
assertFalse(editedOffer.getUseMarketBasedPrice());
assertEquals(0.00, editedOffer.getMarketPriceMargin());
assertEquals(0, editedOffer.getTriggerPrice());
}
private OfferInfo createMktPricedOfferForEdit(String direction,
String currencyCode,
String paymentAccountId,
double marketPriceMargin,
long triggerPrice) {
return aliceClient.createMarketBasedPricedOffer(direction,
currencyCode,
AMOUNT,
AMOUNT,
marketPriceMargin,
getDefaultBuyerSecurityDepositAsPercent(),
paymentAccountId,
BSQ,
triggerPrice);
}
private OfferInfo createFixedPricedOfferForEdit(String direction,
String currencyCode,
String paymentAccountId,
String priceAsString) {
return aliceClient.createFixedPricedOffer(direction,
currencyCode,
AMOUNT,
AMOUNT,
priceAsString,
getDefaultBuyerSecurityDepositAsPercent(),
paymentAccountId,
BSQ);
}
private void doSanityCheck(OfferInfo originalOffer, OfferInfo editedOffer) {
// Assert some of the immutable offer fields are unchanged.
assertEquals(originalOffer.getDirection(), editedOffer.getDirection());
assertEquals(originalOffer.getAmount(), editedOffer.getAmount());
assertEquals(originalOffer.getMinAmount(), editedOffer.getMinAmount());
assertEquals(originalOffer.getTxFee(), editedOffer.getTxFee());
assertEquals(originalOffer.getMakerFee(), editedOffer.getMakerFee());
assertEquals(originalOffer.getPaymentAccountId(), editedOffer.getPaymentAccountId());
assertEquals(originalOffer.getDate(), editedOffer.getDate());
if (originalOffer.getDirection().equals(BUY.name()))
assertEquals(originalOffer.getBuyerSecurityDeposit(), editedOffer.getBuyerSecurityDeposit());
else
assertEquals(originalOffer.getSellerSecurityDeposit(), editedOffer.getSellerSecurityDeposit());
}
private PaymentAccount getOrCreatePaymentAccount(String countryCode) {
if (paymentAcctCache.containsKey(countryCode)) {
return paymentAcctCache.get(countryCode);
} else {
PaymentAccount paymentAcct = createDummyF2FAccount(aliceClient, countryCode);
paymentAcctCache.put(countryCode, paymentAcct);
return paymentAcct;
}
}
@AfterAll
public static void clearPaymentAcctCache() {
paymentAcctCache.clear();
}
}