Skip to content

Commit

Permalink
Merge pull request #4608 from ghubstan/getoffer-for-id
Browse files Browse the repository at this point in the history
Add new 'getoffer offer-id' api method
  • Loading branch information
sqrrm authored Oct 15, 2020
2 parents 8883629 + 8394bd1 commit 44394ad
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 78 deletions.
11 changes: 11 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import bisq.proto.grpc.CreatePaymentAccountRequest;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.GetOfferRequest;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.MarketPriceRequest;
import bisq.proto.grpc.OfferInfo;
import bisq.proto.grpc.RegisterDisputeAgentRequest;
import bisq.proto.grpc.RemoveWalletPasswordRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
Expand Down Expand Up @@ -82,6 +84,10 @@ protected final MarketPriceRequest createMarketPriceRequest(String currencyCode)
return MarketPriceRequest.newBuilder().setCurrencyCode(currencyCode).build();
}

protected final GetOfferRequest createGetOfferRequest(String offerId) {
return GetOfferRequest.newBuilder().setId(offerId).build();
}

// Convenience methods for calling frequently used & thoroughly tested gRPC services.

protected final long getBalance(BisqAppConfig bisqAppConfig) {
Expand Down Expand Up @@ -138,6 +144,11 @@ protected final double getMarketPrice(BisqAppConfig bisqAppConfig, String curren
return grpcStubs(bisqAppConfig).priceService.getMarketPrice(req).getPrice();
}

protected final OfferInfo getOffer(BisqAppConfig bisqAppConfig, String offerId) {
var req = createGetOfferRequest(offerId);
return grpcStubs(bisqAppConfig).offersService.getOffer(req).getOffer();
}

// Static conveniences for test methods and test case fixture setups.

protected static RegisterDisputeAgentRequest createRegisterDisputeAgentRequest(String disputeAgentType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ static void startSupportingApps() {
}
}

protected final OfferInfo getOffer(String offerId) {
return aliceStubs.offersService.getOffer(createGetOfferRequest(offerId)).getOffer();
}

protected final OfferInfo getMostRecentOffer(String direction, String currencyCode) {
List<OfferInfo> offerInfoList = getOffersSortedByDate(direction, currencyCode);
if (offerInfoList.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import bisq.core.btc.wallet.Restrictions;

import bisq.proto.grpc.CreateOfferRequest;
import bisq.proto.grpc.OfferInfo;

import lombok.extern.slf4j.Slf4j;

Expand Down Expand Up @@ -66,17 +65,17 @@ public void testCreateAUDBTCBuyOfferUsingFixedPrice16000() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("AUD", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("buy", "aud");
assertEquals(newOfferId, offer.getId());
assertEquals("BUY", offer.getDirection());
assertFalse(offer.getUseMarketBasedPrice());
assertEquals(160000000, offer.getPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("AUD", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("BUY", newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(160000000, newOffer.getPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("AUD", newOffer.getCounterCurrencyCode());
}

@Test
Expand Down Expand Up @@ -107,17 +106,17 @@ public void testCreateUSDBTCBuyOfferUsingFixedPrice100001234() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("USD", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("buy", "usd");
assertEquals(newOfferId, offer.getId());
assertEquals("BUY", offer.getDirection());
assertFalse(offer.getUseMarketBasedPrice());
assertEquals(100001234, offer.getPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("USD", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("BUY", newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(100001234, newOffer.getPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("USD", newOffer.getCounterCurrencyCode());
}

@Test
Expand Down Expand Up @@ -148,16 +147,16 @@ public void testCreateEURBTCSellOfferUsingFixedPrice95001234() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("EUR", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("sell", "eur");
assertEquals(newOfferId, offer.getId());
assertEquals("SELL", offer.getDirection());
assertFalse(offer.getUseMarketBasedPrice());
assertEquals(95001234, offer.getPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("EUR", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("SELL", newOffer.getDirection());
assertFalse(newOffer.getUseMarketBasedPrice());
assertEquals(95001234, newOffer.getPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("EUR", newOffer.getCounterCurrencyCode());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,18 @@ public void testCreateUSDBTCBuyOffer5PctPriceMargin() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("USD", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("buy", "usd");
assertEquals(newOfferId, offer.getId());
assertEquals("BUY", offer.getDirection());
assertTrue(offer.getUseMarketBasedPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("USD", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("BUY", newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("USD", newOffer.getCounterCurrencyCode());

assertCalculatedPriceIsCorrect(offer, priceMarginPctInput);
assertCalculatedPriceIsCorrect(newOffer, priceMarginPctInput);
}

@Test
Expand Down Expand Up @@ -119,18 +119,18 @@ public void testCreateNZDBTCBuyOfferMinus2PctPriceMargin() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("NZD", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("buy", "nzd");
assertEquals(newOfferId, offer.getId());
assertEquals("BUY", offer.getDirection());
assertTrue(offer.getUseMarketBasedPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("NZD", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("BUY", newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("NZD", newOffer.getCounterCurrencyCode());

assertCalculatedPriceIsCorrect(offer, priceMarginPctInput);
assertCalculatedPriceIsCorrect(newOffer, priceMarginPctInput);
}

@Test
Expand Down Expand Up @@ -162,18 +162,18 @@ public void testCreateGBPBTCSellOfferMinus1Point5PctPriceMargin() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("GBP", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("sell", "gbp");
assertEquals(newOfferId, offer.getId());
assertEquals("SELL", offer.getDirection());
assertTrue(offer.getUseMarketBasedPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("GBP", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("SELL", newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("GBP", newOffer.getCounterCurrencyCode());

assertCalculatedPriceIsCorrect(offer, priceMarginPctInput);
assertCalculatedPriceIsCorrect(newOffer, priceMarginPctInput);
}

@Test
Expand Down Expand Up @@ -205,18 +205,18 @@ public void testCreateBRLBTCSellOffer6Point55PctPriceMargin() {
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("BRL", newOffer.getCounterCurrencyCode());

OfferInfo offer = getMostRecentOffer("sell", "brl");
assertEquals(newOfferId, offer.getId());
assertEquals("SELL", offer.getDirection());
assertTrue(offer.getUseMarketBasedPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("BRL", offer.getCounterCurrencyCode());
newOffer = getOffer(newOfferId);
assertEquals(newOfferId, newOffer.getId());
assertEquals("SELL", newOffer.getDirection());
assertTrue(newOffer.getUseMarketBasedPrice());
assertEquals(10000000, newOffer.getAmount());
assertEquals(10000000, newOffer.getMinAmount());
assertEquals(1500000, newOffer.getBuyerSecurityDeposit());
assertEquals(paymentAccount.getId(), newOffer.getPaymentAccountId());
assertEquals("BTC", newOffer.getBaseCurrencyCode());
assertEquals("BRL", newOffer.getCounterCurrencyCode());

assertCalculatedPriceIsCorrect(offer, priceMarginPctInput);
assertCalculatedPriceIsCorrect(newOffer, priceMarginPctInput);
}

private void assertCalculatedPriceIsCorrect(OfferInfo offer, double priceMarginPctInput) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public void testAmtTooLargeShouldThrowException() {
.setPrice("10000.0000")
.setBuyerSecurityDeposit(Restrictions.getDefaultBuyerSecurityDepositAsPercent())
.build();
@SuppressWarnings("ResultOfMethodCallIgnored")
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
aliceStubs.offersService.createOffer(req).getOffer());
assertEquals("UNKNOWN: An error occurred at task: ValidateOffer",
Expand Down
16 changes: 16 additions & 0 deletions cli/src/main/java/bisq/cli/CliMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import bisq.proto.grpc.GetAddressBalanceRequest;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.GetOfferRequest;
import bisq.proto.grpc.GetOffersRequest;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.GetVersionRequest;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class CliMain {

private enum Method {
createoffer,
getoffer,
getoffers,
createpaymentacct,
getpaymentaccts,
Expand Down Expand Up @@ -223,6 +225,19 @@ public static void run(String[] args) {
out.println(formatOfferTable(singletonList(reply.getOffer()), currencyCode));
return;
}
case getoffer: {
if (nonOptionArgs.size() < 2)
throw new IllegalArgumentException("incorrect parameter count, expecting offer id");

var offerId = nonOptionArgs.get(1);
var request = GetOfferRequest.newBuilder()
.setId(offerId)
.build();
var reply = offersService.getOffer(request);
out.println(formatOfferTable(singletonList(reply.getOffer()),
reply.getOffer().getCounterCurrencyCode()));
return;
}
case getoffers: {
if (nonOptionArgs.size() < 3)
throw new IllegalArgumentException("incorrect parameter count,"
Expand Down Expand Up @@ -364,6 +379,7 @@ private static void printHelp(OptionParser parser, PrintStream stream) {
stream.format(rowFormat, "", "amount (btc), min amount, use mkt based price, \\", "");
stream.format(rowFormat, "", "fixed price (btc) | mkt price margin (%), \\", "");
stream.format(rowFormat, "", "security deposit (%)", "");
stream.format(rowFormat, "getoffer", "offer id", "Get current offer with id");
stream.format(rowFormat, "getoffers", "buy | sell, currency code", "Get current offers");
stream.format(rowFormat, "createpaymentacct", "account name, account number, currency code", "Create PerfectMoney dummy account");
stream.format(rowFormat, "getpaymentaccts", "", "Get user payment accounts");
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/bisq/core/api/CoreApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ public void registerDisputeAgent(String disputeAgentType, String registrationKey
// Offers
///////////////////////////////////////////////////////////////////////////////////////////

public Offer getOffer(String id) {
return coreOffersService.getOffer(id);
}

public List<Offer> getOffers(String direction, String currencyCode) {
return coreOffersService.getOffers(direction, currencyCode);
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/bisq/core/api/CoreOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import static bisq.core.locale.CurrencyUtil.isCryptoCurrency;
import static bisq.core.offer.OfferPayload.Direction;
import static bisq.core.offer.OfferPayload.Direction.BUY;
import static java.lang.String.format;

@Slf4j
class CoreOffersService {
Expand All @@ -67,6 +68,13 @@ public CoreOffersService(CreateOfferService createOfferService,
this.user = user;
}

Offer getOffer(String id) {
return offerBookService.getOffers().stream()
.filter(o -> o.getId().equals(id))
.findAny().orElseThrow(() ->
new IllegalStateException(format("offer with id '%s' not found", id)));
}

List<Offer> getOffers(String direction, String currencyCode) {
List<Offer> offers = offerBookService.getOffers().stream()
.filter(o -> {
Expand Down
19 changes: 19 additions & 0 deletions daemon/src/main/java/bisq/daemon/grpc/GrpcOffersService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import bisq.proto.grpc.CreateOfferReply;
import bisq.proto.grpc.CreateOfferRequest;
import bisq.proto.grpc.GetOfferReply;
import bisq.proto.grpc.GetOfferRequest;
import bisq.proto.grpc.GetOffersReply;
import bisq.proto.grpc.GetOffersRequest;
import bisq.proto.grpc.OffersGrpc;
Expand All @@ -49,6 +51,23 @@ public GrpcOffersService(CoreApi coreApi) {
this.coreApi = coreApi;
}

@Override
public void getOffer(GetOfferRequest req,
StreamObserver<GetOfferReply> responseObserver) {
try {
Offer offer = coreApi.getOffer(req.getId());
var reply = GetOfferReply.newBuilder()
.setOffer(toOfferInfo(offer).toProtoMessage())
.build();
responseObserver.onNext(reply);
responseObserver.onCompleted();
} catch (IllegalStateException | IllegalArgumentException cause) {
var ex = new StatusRuntimeException(Status.UNKNOWN.withDescription(cause.getMessage()));
responseObserver.onError(ex);
throw ex;
}
}

@Override
public void getOffers(GetOffersRequest req,
StreamObserver<GetOffersReply> responseObserver) {
Expand Down
9 changes: 9 additions & 0 deletions proto/src/main/proto/grpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,21 @@ message RegisterDisputeAgentReply {
///////////////////////////////////////////////////////////////////////////////////////////

service Offers {
rpc GetOffer (GetOfferRequest) returns (GetOfferReply) {
}
rpc GetOffers (GetOffersRequest) returns (GetOffersReply) {
}
rpc CreateOffer (CreateOfferRequest) returns (CreateOfferReply) {
}
}

message GetOfferRequest {
string id = 1;
}

message GetOfferReply {
OfferInfo offer = 1;
}
message GetOffersRequest {
string direction = 1;
string currencyCode = 2;
Expand Down

0 comments on commit 44394ad

Please sign in to comment.