Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add API 'gettrades' method #5976

Merged
merged 35 commits into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
55a54f3
Add GetTrades proto message, + needed TradeInfo fields
ghubstan Jan 16, 2022
e6e944b
Add GrpcTradesService.getTrades service method
ghubstan Jan 16, 2022
b4fa52f
Add List<Trade> getTrades() to FailedTradesManager (w/out transitive …
ghubstan Jan 16, 2022
50bbbfe
Add List<Trade> getTrades() to TradeManager (w/out transitive jfx dep)
ghubstan Jan 16, 2022
a53fee3
Add List<OpenOffer> getCanceledOpenOffers() ClosedTradableManager
ghubstan Jan 16, 2022
5c21779
Add buyer/seller dep & status-desc fields to TradeInfoV1Builder
ghubstan Jan 16, 2022
a9e7484
Add buyer/seller dep & status-desc fields to TradeInfo
ghubstan Jan 16, 2022
fdd94fe
Remove commented code
ghubstan Jan 16, 2022
d98d795
Add server impl for gettrades (open|closed), refactor getrole methods
ghubstan Jan 16, 2022
4c7ae49
Format gettrades CLI outout
ghubstan Jan 16, 2022
ffa31b0
Support gRPC gettrades call in CLI
ghubstan Jan 16, 2022
3cd1e05
Try to display accurate status of closed trades, or "Pending" if stil…
ghubstan Jan 18, 2022
a5857e2
Re-set log level to INFO in apitest cases
ghubstan Jan 18, 2022
0ec0952
Remove trailing spaces
ghubstan Jan 20, 2022
2e7346a
Fix comments
ghubstan Jan 20, 2022
b10cb11
Display trade.txFee for BSQ swap trades
ghubstan Jan 20, 2022
69e6c9c
Remove duplicated buyer/seller proto fields (already exist no OfferInfo)
ghubstan Jan 20, 2022
2a3a298
Fix maker/taker fee column headers & value func defs
ghubstan Jan 20, 2022
6e02996
Fill out gettrades-help.txt
ghubstan Jan 22, 2022
fadaaf9
Display tx-fee from BsqSwapProtocolModel
ghubstan Jan 22, 2022
0e020e8
Add payout fields to trade proto msgs, show optional trade/tx fees in…
ghubstan Jan 23, 2022
3e14bd3
Change var name
ghubstan Jan 23, 2022
fdd91a8
Fix displayed bsq-swap's optional trade & tx fees
ghubstan Jan 23, 2022
2db20c0
Split BsqSwap tests into "buy" and "sell" tests
ghubstan Jan 23, 2022
fd0c4c2
Add BsqSwapSellBtcTradeTest
ghubstan Jan 23, 2022
89c4ab3
Use toTradeFeeBtc, not toTradeFeeBsq
ghubstan Jan 23, 2022
d902e77
Add Bisq license banners
ghubstan Jan 25, 2022
210d966
Fix method name
ghubstan Jan 25, 2022
4ab4c3d
Log exception and return "Not Available" when trade role cannot be found
ghubstan Jan 25, 2022
ef00a2c
Remove unused class
ghubstan Jan 25, 2022
4c8cec5
Push Trade->TradeModel mapping down from CoreApi into CoreTradesService
ghubstan Jan 25, 2022
f231aac
Sort API gettrades's response list by trade date
ghubstan Jan 26, 2022
ad638e3
Revert "Sort API gettrades's response list by trade date"
ghubstan Jan 27, 2022
8206425
Remove unnecessary casts
ghubstan Jan 27, 2022
111e39f
Sort the final, cumulative trades list in gRPC GetTrades service
ghubstan Jan 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -17,12 +17,15 @@

package bisq.apitest.method.offer;

import bisq.core.offer.OfferDirection;

import bisq.proto.grpc.OfferInfo;

import protobuf.PaymentAccount;

import java.math.BigDecimal;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
Expand All @@ -42,12 +45,16 @@
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.cli.table.builder.TableType.OFFER_TBL;
import static bisq.common.util.MathUtils.exactMultiply;
import static java.lang.String.format;
import static java.lang.System.out;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;



import bisq.apitest.method.MethodTest;
import bisq.cli.CliMain;
import bisq.cli.GrpcClient;
import bisq.cli.table.builder.TableBuilder;

@Slf4j
Expand Down Expand Up @@ -122,6 +129,34 @@ public static void setUp() {
protected final Function<List<OfferInfo>, String> toOffersTable = (offers) ->
new TableBuilder(OFFER_TBL, offers).build().toString();

protected OfferInfo getAvailableBsqSwapOffer(GrpcClient client,
OfferDirection direction,
boolean checkForLoggedExceptions) {
List<OfferInfo> bsqSwapOffers = new ArrayList<>();
int numFetchAttempts = 0;
while (bsqSwapOffers.size() == 0) {
bsqSwapOffers.addAll(client.getBsqSwapOffers(direction.name()));
numFetchAttempts++;
if (bsqSwapOffers.size() == 0) {
log.warn("No available bsq swap offers found after {} fetch attempts.", numFetchAttempts);
if (numFetchAttempts > 9) {
if (checkForLoggedExceptions) {
printNodeExceptionMessages(log);
}
fail(format("Bob gave up on fetching available bsq swap offers after %d attempts.", numFetchAttempts));
}
sleep(1_000);
} else {
assertEquals(1, bsqSwapOffers.size());
log.debug("Bob found new available bsq swap offer on attempt # {}.", numFetchAttempts);
break;
}
}
var bsqSwapOffer = bobClient.getBsqSwapOffer(bsqSwapOffers.get(0).getId());
assertEquals(bsqSwapOffers.get(0).getId(), bsqSwapOffer.getId());
return bsqSwapOffer;
}

@SuppressWarnings("ConstantConditions")
public static void initSwapPaymentAccounts() {
// A bot may not know what the default 'BSQ Swap' account name is,
Expand Down
Expand Up @@ -17,24 +17,24 @@

package bisq.apitest.method.trade;

import bisq.proto.grpc.OfferInfo;
import bisq.proto.grpc.TradeInfo;

import java.util.ArrayList;
import java.util.List;
import protobuf.OfferDirection;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
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.apitest.config.ApiTestConfig.BTC;
import static bisq.core.offer.OfferDirection.BUY;
import static bisq.proto.grpc.GetOfferCategoryReply.OfferCategory.BSQ_SWAP;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -43,17 +43,16 @@
import static org.junit.jupiter.api.Assertions.fail;
import static protobuf.BsqSwapTrade.State.COMPLETED;
import static protobuf.BsqSwapTrade.State.PREPARATION;
import static protobuf.OfferDirection.BUY;



import bisq.apitest.method.offer.AbstractOfferTest;
import bisq.cli.GrpcClient;

// @Disabled
@Disabled
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BsqSwapTradeTest extends AbstractTradeTest {
public class BsqSwapBuyBtcTradeTest extends AbstractTradeTest {

// Long-running swap trade tests might want to check node logs for exceptions.
@Setter
Expand All @@ -80,15 +79,16 @@ public void testGetBalancesBeforeTrade() {

@Test
@Order(2)
public void testAliceCreateBsqSwapBuyOffer() {
var mySwapOffer = aliceClient.createBsqSwapOffer(BUY.name(),
public void testAliceCreateBsqSwapBuyBtcOffer() {
// Alice buys BTC, pays trade fee. Bob (BTC seller) pays miner tx fee.
var mySwapOffer = aliceClient.createBsqSwapOffer(OfferDirection.BUY.name(),
1_000_000L, // 0.01 BTC
1_000_000L,
"0.00005");
log.debug("Pending BsqSwap Sell BSQ (Buy BTC) OFFER:\n{}", toOfferTable.apply(mySwapOffer));
var newOfferId = mySwapOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(BUY.name(), mySwapOffer.getDirection());
assertEquals(OfferDirection.BUY.name(), mySwapOffer.getDirection());
assertEquals(5_000, mySwapOffer.getPrice());
assertEquals(1_000_000L, mySwapOffer.getAmount());
assertEquals(1_000_000L, mySwapOffer.getMinAmount());
Expand All @@ -107,7 +107,7 @@ public void testAliceCreateBsqSwapBuyOffer() {
@Test
@Order(3)
public void testBobTakesBsqSwapOffer() {
var availableSwapOffer = getAvailableBsqSwapOffer(bobClient);
var availableSwapOffer = getAvailableBsqSwapOffer(bobClient, BUY, true);

// Before sending a TakeOfferRequest, the CLI needs to know what kind of Offer
// it is taking (v1 or BsqSwap). Only BSQ swap offers can be taken with a
Expand Down Expand Up @@ -163,32 +163,6 @@ public void testGetBalancesAfterTrade() {
log.debug("Bob's After Trade Balance:\n{}", formatBalancesTbls(bobsBalances));
}

private OfferInfo getAvailableBsqSwapOffer(GrpcClient client) {
List<OfferInfo> bsqSwapOffers = new ArrayList<>();
int numFetchAttempts = 0;
while (bsqSwapOffers.size() == 0) {
bsqSwapOffers.addAll(client.getBsqSwapOffers(BUY.name()));
numFetchAttempts++;
if (bsqSwapOffers.size() == 0) {
log.warn("No available bsq swap offers found after {} fetch attempts.", numFetchAttempts);
if (numFetchAttempts > 9) {
if (checkForLoggedExceptions) {
printNodeExceptionMessages(log);
}
fail(format("Bob gave up on fetching available bsq swap offers after %d attempts.", numFetchAttempts));
}
sleep(1_000);
} else {
assertEquals(1, bsqSwapOffers.size());
log.debug("Bob found new available bsq swap offer on attempt # {}.", numFetchAttempts);
break;
}
}
var bsqSwapOffer = bobClient.getBsqSwapOffer(bsqSwapOffers.get(0).getId());
assertEquals(bsqSwapOffers.get(0).getId(), bsqSwapOffer.getId());
return bsqSwapOffer;
}

private TradeInfo getBsqSwapTrade(GrpcClient client, String tradeId) {
int numFetchAttempts = 0;
while (true) {
Expand Down
@@ -0,0 +1,183 @@
/*
* 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.trade;

import bisq.proto.grpc.TradeInfo;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
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.apitest.config.ApiTestConfig.BTC;
import static bisq.core.offer.OfferDirection.SELL;
import static bisq.proto.grpc.GetOfferCategoryReply.OfferCategory.BSQ_SWAP;
import static java.lang.String.format;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static protobuf.BsqSwapTrade.State.COMPLETED;
import static protobuf.BsqSwapTrade.State.PREPARATION;



import bisq.apitest.method.offer.AbstractOfferTest;
import bisq.cli.GrpcClient;

@Disabled
@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class BsqSwapSellBtcTradeTest extends AbstractTradeTest {

// Long-running swap trade tests might want to check node logs for exceptions.
@Setter
private boolean checkForLoggedExceptions;

@BeforeAll
public static void setUp() {
AbstractOfferTest.setUp();
}

@BeforeEach
public void generateBtcBlock() {
genBtcBlocksThenWait(1, 2_000);
}

@Test
@Order(1)
public void testGetBalancesBeforeTrade() {
var alicesBalances = aliceClient.getBalances();
log.debug("Alice's Before Trade Balance:\n{}", formatBalancesTbls(alicesBalances));
var bobsBalances = bobClient.getBalances();
log.debug("Bob's Before Trade Balance:\n{}", formatBalancesTbls(bobsBalances));
}

@Test
@Order(2)
public void testAliceCreateBsqSwapSellBtcOffer() {
// Alice sells BTC, pays miner tx fee. Bob (BTC buyer) pays trade fee.
var mySwapOffer = aliceClient.createBsqSwapOffer(SELL.name(),
1_000_000L, // 0.01 BTC
1_000_000L,
"0.00005");
log.debug("Pending BsqSwap Buy BSQ (Sell BTC) OFFER:\n{}", toOfferTable.apply(mySwapOffer));
var newOfferId = mySwapOffer.getId();
assertNotEquals("", newOfferId);
assertEquals(SELL.name(), mySwapOffer.getDirection());
assertEquals(5_000, mySwapOffer.getPrice());
assertEquals(1_000_000L, mySwapOffer.getAmount());
assertEquals(1_000_000L, mySwapOffer.getMinAmount());
assertEquals(BSQ, mySwapOffer.getBaseCurrencyCode());
assertEquals(BTC, mySwapOffer.getCounterCurrencyCode());

genBtcBlocksThenWait(1, 2_500);

mySwapOffer = aliceClient.getOffer(newOfferId);
log.debug("My fetched BsqSwap Buy BSQ (Sell BTC) OFFER:\n{}", toOfferTable.apply(mySwapOffer));
assertNotEquals(0, mySwapOffer.getMakerFee());

runCliGetOffer(newOfferId);
}

@Test
@Order(3)
public void testBobTakesBsqSwapOffer() {
var availableSwapOffer = getAvailableBsqSwapOffer(bobClient, SELL, true);

// Before sending a TakeOfferRequest, the CLI needs to know what kind of Offer
// it is taking (v1 or BsqSwap). Only BSQ swap offers can be taken with a
// single offerId parameter. Taking v1 offers requires an additional
// paymentAccountId param. The test case knows what kind of offer is being taken,
// but we test the gRPC GetOfferCategory service here.
var availableOfferCategory = bobClient.getAvailableOfferCategory(availableSwapOffer.getId());
assertTrue(availableOfferCategory.equals(BSQ_SWAP));

sleep(10_000);

var swapTrade = bobClient.takeBsqSwapOffer(availableSwapOffer.getId());
tradeId = swapTrade.getTradeId(); // Cache the tradeId for following test case(s).
log.debug("BsqSwap Trade at PREPARATION:\n{}", toTradeDetailTable.apply(swapTrade));
assertEquals(PREPARATION.name(), swapTrade.getState());
genBtcBlocksThenWait(1, 3_000);

swapTrade = getBsqSwapTrade(bobClient, tradeId);
log.debug("BsqSwap Trade at COMPLETION:\n{}", toTradeDetailTable.apply(swapTrade));
assertEquals(COMPLETED.name(), swapTrade.getState());

runCliGetClosedTrades();
}

@Test
@Order(4)
public void testCompletedSwapTxConfirmations() {
sleep(2_000); // Wait for TX confirmation to happen on node.

var alicesTrade = getBsqSwapTrade(aliceClient, tradeId);
log.debug("Alice's BsqSwap Trade at COMPLETION:\n{}", toTradeDetailTable.apply(alicesTrade));
assertEquals(1, alicesTrade.getBsqSwapTradeInfo().getNumConfirmations());

var bobsTrade = getBsqSwapTrade(bobClient, tradeId);
log.debug("Bob's BsqSwap Trade at COMPLETION:\n{}", toTradeDetailTable.apply(bobsTrade));
assertEquals(1, bobsTrade.getBsqSwapTradeInfo().getNumConfirmations());

genBtcBlocksThenWait(1, 2_000);

bobsTrade = getBsqSwapTrade(bobClient, tradeId);
log.debug("Bob's BsqSwap Trade at COMPLETION:\n{}", toTradeDetailTable.apply(bobsTrade));
assertEquals(2, bobsTrade.getBsqSwapTradeInfo().getNumConfirmations());

runCliGetClosedTrades();
}

@Test
@Order(5)
public void testGetBalancesAfterTrade() {
var alicesBalances = aliceClient.getBalances();
log.debug("Alice's After Trade Balance:\n{}", formatBalancesTbls(alicesBalances));
var bobsBalances = bobClient.getBalances();
log.debug("Bob's After Trade Balance:\n{}", formatBalancesTbls(bobsBalances));
}

private TradeInfo getBsqSwapTrade(GrpcClient client, String tradeId) {
int numFetchAttempts = 0;
while (true) {
try {
numFetchAttempts++;
return client.getTrade(tradeId);
} catch (Exception ex) {
log.warn(ex.getMessage());
if (numFetchAttempts > 9) {
if (checkForLoggedExceptions) {
printNodeExceptionMessages(log);
}
fail(format("Could not find new bsq swap trade after %d attempts.", numFetchAttempts));
} else {
sleep(1_000);
}
}
}
}
}
Expand Up @@ -31,7 +31,7 @@


import bisq.apitest.method.offer.AbstractOfferTest;
import bisq.apitest.method.trade.BsqSwapTradeTest;
import bisq.apitest.method.trade.BsqSwapBuyBtcTradeTest;

@EnabledIf("envLongRunningTestEnabled")
@Slf4j
Expand All @@ -49,15 +49,15 @@ public static void setUp() {
@Order(1)
public void testBsqSwaps() {
// TODO Fix wallet inconsistency bugs after N(?) trades.
BsqSwapTradeTest test = new BsqSwapTradeTest();
BsqSwapBuyBtcTradeTest test = new BsqSwapBuyBtcTradeTest();
test.setCheckForLoggedExceptions(true);

for (int swapCount = 1; swapCount <= MAX_SWAPS; swapCount++) {
log.info("Beginning BSQ Swap # {}", swapCount);

test.testGetBalancesBeforeTrade();

test.testAliceCreateBsqSwapBuyOffer();
test.testAliceCreateBsqSwapBuyBtcOffer();
genBtcBlocksThenWait(1, 8_000);

test.testBobTakesBsqSwapOffer();
Expand Down