Skip to content

Commit

Permalink
Merge pull request #4554 from ghubstan/create-offer
Browse files Browse the repository at this point in the history
Change 'createoffer' api method return type
  • Loading branch information
sqrrm authored Oct 1, 2020
2 parents 0a88ee6 + d190d09 commit 0b1894a
Show file tree
Hide file tree
Showing 13 changed files with 427 additions and 81 deletions.
6 changes: 4 additions & 2 deletions apitest/scripts/mainnet-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,19 @@
}

@test "test getversion call with quoted password" {
load 'version-parser'
run ./bisq-cli --password="xyz" getversion
[ "$status" -eq 0 ]
echo "actual output: $output" >&2
[ "$output" = "1.3.9" ]
[ "$output" = "$CURRENT_VERSION" ]
}

@test "test getversion" {
load 'version-parser'
run ./bisq-cli --password=xyz getversion
[ "$status" -eq 0 ]
echo "actual output: $output" >&2
[ "$output" = "1.3.9" ]
[ "$output" = "$CURRENT_VERSION" ]
}

@test "test setwalletpassword \"a b c\"" {
Expand Down
5 changes: 5 additions & 0 deletions apitest/scripts/version-parser.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

# Bats helper script for parsing current version from Version.java.

export CURRENT_VERSION=$(grep "String VERSION =" common/src/main/java/bisq/common/app/Version.java | sed 's/[^0-9.]*//g')
112 changes: 112 additions & 0 deletions apitest/src/test/java/bisq/apitest/method/CreateOfferTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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;

import bisq.core.btc.wallet.Restrictions;

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

import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static bisq.apitest.Scaffold.BitcoinCoreApp.bitcoind;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.MethodOrderer.OrderAnnotation;


@Slf4j
@TestMethodOrder(OrderAnnotation.class)
public class CreateOfferTest extends MethodTest {

@BeforeAll
public static void setUp() {
try {
// setUpScaffold(new String[]{"--supportingApps", "bitcoind,seednode,arbdaemon,alicedaemon", "--enableBisqDebugging", "true"});
setUpScaffold(bitcoind, seednode, arbdaemon, alicedaemon);

// Generate 1 regtest block for alice's wallet to show 10 BTC balance,
// and give alicedaemon time to parse the new block.
bitcoinCli.generateBlocks(1);
MILLISECONDS.sleep(1500);
} catch (Exception ex) {
fail(ex);
}
}

@Test
@Order(1)
public void testCreateBuyOffer() {
var paymentAccount = getDefaultPerfectDummyPaymentAccount(alicedaemon);
var req = CreateOfferRequest.newBuilder()
.setCurrencyCode("USD")
.setDirection("BUY")
.setPrice(0)
.setUseMarketBasedPrice(true)
.setMarketPriceMargin(0.00)
.setAmount(10000000)
.setMinAmount(10000000)
.setBuyerSecurityDeposit(Restrictions.getDefaultBuyerSecurityDepositAsPercent())
.setPaymentAccountId(paymentAccount.getId())
.build();
var newOffer = grpcStubs(alicedaemon).offersService.createOffer(req).getOffer();
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());
}

@Test
@Order(2)
public void testGetNewBuyOffer() {
var req = GetOffersRequest.newBuilder().setDirection("BUY").setCurrencyCode("USD").build();
var reply = grpcStubs(alicedaemon).offersService.getOffers(req);

assertEquals(1, reply.getOffersCount());
OfferInfo offer = reply.getOffersList().get(0);
assertEquals("BUY", offer.getDirection());
assertTrue(offer.getUseMarketBasedPrice());
assertEquals(10000000, offer.getAmount());
assertEquals(10000000, offer.getMinAmount());
assertEquals(1500000, offer.getBuyerSecurityDeposit());
assertEquals("", offer.getPaymentAccountId());
assertEquals("BTC", offer.getBaseCurrencyCode());
assertEquals("USD", offer.getCounterCurrencyCode());
}

@AfterAll
public static void tearDown() {
tearDownScaffold();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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;

import bisq.proto.grpc.GetPaymentAccountsRequest;

import protobuf.PaymentAccount;
import protobuf.PerfectMoneyAccountPayload;

import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static bisq.apitest.Scaffold.BitcoinCoreApp.bitcoind;
import static bisq.apitest.config.BisqAppConfig.alicedaemon;
import static java.util.Comparator.comparing;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.MethodOrderer.OrderAnnotation;


@Slf4j
@TestMethodOrder(OrderAnnotation.class)
public class CreatePaymentAccountTest extends MethodTest {

static final String PERFECT_MONEY_ACCT_NAME = "Perfect Money USD";
static final String PERFECT_MONEY_ACCT_NUMBER = "0123456789";

@BeforeAll
public static void setUp() {
try {
setUpScaffold(bitcoind, alicedaemon);
} catch (Exception ex) {
fail(ex);
}
}

@Test
@Order(1)
public void testCreatePerfectMoneyUSDPaymentAccount() {
var perfectMoneyPaymentAccountRequest = createCreatePerfectMoneyPaymentAccountRequest(
PERFECT_MONEY_ACCT_NAME,
PERFECT_MONEY_ACCT_NUMBER,
"USD");
//noinspection ResultOfMethodCallIgnored
grpcStubs(alicedaemon).paymentAccountsService.createPaymentAccount(perfectMoneyPaymentAccountRequest);

var getPaymentAccountsRequest = GetPaymentAccountsRequest.newBuilder().build();
var reply = grpcStubs(alicedaemon).paymentAccountsService.getPaymentAccounts(getPaymentAccountsRequest);

// The daemon is running against the regtest/dao setup files, and was set up with
// two dummy accounts ("PerfectMoney dummy", "ETH dummy") before any tests ran.
// We just added 1 test account, making 3 total.
assertEquals(3, reply.getPaymentAccountsCount());

// Sort the returned list by creation date; the last item in the sorted
// list will be the payment acct we just created.
List<PaymentAccount> paymentAccountList = reply.getPaymentAccountsList().stream()
.sorted(comparing(PaymentAccount::getCreationDate))
.collect(Collectors.toList());
PaymentAccount paymentAccount = paymentAccountList.get(2);
PerfectMoneyAccountPayload perfectMoneyAccount = paymentAccount
.getPaymentAccountPayload()
.getPerfectMoneyAccountPayload();

assertEquals(PERFECT_MONEY_ACCT_NAME, paymentAccount.getAccountName());
assertEquals("USD",
paymentAccount.getSelectedTradeCurrency().getFiatCurrency().getCurrency().getCurrencyCode());
assertEquals(PERFECT_MONEY_ACCT_NUMBER, perfectMoneyAccount.getAccountNr());
}

@AfterAll
public static void tearDown() {
tearDownScaffold();
}
}
56 changes: 50 additions & 6 deletions apitest/src/test/java/bisq/apitest/method/MethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,26 @@

package bisq.apitest.method;

import bisq.proto.grpc.CreatePaymentAccountRequest;
import bisq.proto.grpc.GetBalanceRequest;
import bisq.proto.grpc.GetFundingAddressesRequest;
import bisq.proto.grpc.GetPaymentAccountsRequest;
import bisq.proto.grpc.LockWalletRequest;
import bisq.proto.grpc.RegisterDisputeAgentRequest;
import bisq.proto.grpc.RemoveWalletPasswordRequest;
import bisq.proto.grpc.SetWalletPasswordRequest;
import bisq.proto.grpc.UnlockWalletRequest;

import protobuf.PaymentAccount;

import java.util.stream.Collectors;

import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import static bisq.core.payment.payload.PaymentMethod.PERFECT_MONEY;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.MEDIATOR;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.REFUNDAGENT;
import static java.util.Comparator.comparing;
import static org.junit.jupiter.api.Assertions.assertEquals;



Expand Down Expand Up @@ -64,12 +75,6 @@ protected final GetFundingAddressesRequest createGetFundingAddressesRequest() {
return GetFundingAddressesRequest.newBuilder().build();
}

protected final RegisterDisputeAgentRequest createRegisterDisputeAgentRequest(String disputeAgentType) {
return RegisterDisputeAgentRequest.newBuilder()
.setDisputeAgentType(disputeAgentType)
.setRegistrationKey(DEV_PRIVILEGE_PRIV_KEY).build();
}

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

protected final long getBalance(BisqAppConfig bisqAppConfig) {
Expand All @@ -96,4 +101,43 @@ protected final String getUnusedBtcAddress(BisqAppConfig bisqAppConfig) {
.get()
.getAddress();
}

protected final CreatePaymentAccountRequest createCreatePerfectMoneyPaymentAccountRequest(
String accountName,
String accountNumber,
String currencyCode) {
return CreatePaymentAccountRequest.newBuilder()
.setPaymentMethodId(PERFECT_MONEY.getId())
.setAccountName(accountName)
.setAccountNumber(accountNumber)
.setCurrencyCode(currencyCode)
.build();
}

protected final PaymentAccount getDefaultPerfectDummyPaymentAccount(BisqAppConfig bisqAppConfig) {
var getPaymentAccountsRequest = GetPaymentAccountsRequest.newBuilder().build();
var paymentAccountsService = grpcStubs(bisqAppConfig).paymentAccountsService;
PaymentAccount paymentAccount = paymentAccountsService.getPaymentAccounts(getPaymentAccountsRequest)
.getPaymentAccountsList()
.stream()
.sorted(comparing(PaymentAccount::getCreationDate))
.collect(Collectors.toList()).get(0);
assertEquals("PerfectMoney dummy", paymentAccount.getAccountName());
return paymentAccount;
}

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

protected static RegisterDisputeAgentRequest createRegisterDisputeAgentRequest(String disputeAgentType) {
return RegisterDisputeAgentRequest.newBuilder()
.setDisputeAgentType(disputeAgentType.toLowerCase())
.setRegistrationKey(DEV_PRIVILEGE_PRIV_KEY).build();
}

@SuppressWarnings("ResultOfMethodCallIgnored")
protected static void registerDisputeAgents(BisqAppConfig bisqAppConfig) {
var disputeAgentsService = grpcStubs(bisqAppConfig).disputeAgentsService;
disputeAgentsService.registerDisputeAgent(createRegisterDisputeAgentRequest(MEDIATOR.name()));
disputeAgentsService.registerDisputeAgent(createRegisterDisputeAgentRequest(REFUNDAGENT.name()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import static bisq.apitest.config.BisqAppConfig.arbdaemon;
import static bisq.apitest.config.BisqAppConfig.seednode;
import static bisq.common.app.DevEnv.DEV_PRIVILEGE_PRIV_KEY;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.ARBITRATOR;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.MEDIATOR;
import static bisq.core.support.dispute.agent.DisputeAgent.DisputeAgentType.REFUNDAGENT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
Expand All @@ -44,10 +47,6 @@
@TestMethodOrder(OrderAnnotation.class)
public class RegisterDisputeAgentsTest extends MethodTest {

private static final String ARBITRATOR = "arbitrator";
private static final String MEDIATOR = "mediator";
private static final String REFUNDAGENT = "refundagent";

@BeforeAll
public static void setUp() {
try {
Expand All @@ -61,7 +60,7 @@ public static void setUp() {
@Order(1)
public void testRegisterArbitratorShouldThrowException() {
var req =
createRegisterDisputeAgentRequest(ARBITRATOR);
createRegisterDisputeAgentRequest(ARBITRATOR.name());
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req));
assertEquals("INVALID_ARGUMENT: arbitrators must be registered in a Bisq UI",
Expand All @@ -83,7 +82,7 @@ public void testInvalidDisputeAgentTypeArgShouldThrowException() {
@Order(3)
public void testInvalidRegistrationKeyArgShouldThrowException() {
var req = RegisterDisputeAgentRequest.newBuilder()
.setDisputeAgentType(REFUNDAGENT)
.setDisputeAgentType(REFUNDAGENT.name().toLowerCase())
.setRegistrationKey("invalid" + DEV_PRIVILEGE_PRIV_KEY).build();
Throwable exception = assertThrows(StatusRuntimeException.class, () ->
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req));
Expand All @@ -95,15 +94,15 @@ public void testInvalidRegistrationKeyArgShouldThrowException() {
@Order(4)
public void testRegisterMediator() {
var req =
createRegisterDisputeAgentRequest(MEDIATOR);
createRegisterDisputeAgentRequest(MEDIATOR.name());
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req);
}

@Test
@Order(5)
public void testRegisterRefundAgent() {
var req =
createRegisterDisputeAgentRequest(REFUNDAGENT);
createRegisterDisputeAgentRequest(REFUNDAGENT.name());
grpcStubs(arbdaemon).disputeAgentsService.registerDisputeAgent(req);
}

Expand Down
Loading

0 comments on commit 0b1894a

Please sign in to comment.