Skip to content

Commit

Permalink
Change string order to OrderEnum in apis. UtxoSelectionStrategy imple…
Browse files Browse the repository at this point in the history
…mented
  • Loading branch information
satran004 committed May 26, 2021
1 parent 9e660a9 commit a821457
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.bloxbean.cardano.client.backend.api.AddressService;
import com.bloxbean.cardano.client.backend.api.BackendService;
import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.factory.BackendFactory;
import com.bloxbean.cardano.client.backend.impl.blockfrost.common.Constants;
Expand Down Expand Up @@ -48,7 +49,7 @@ public void testGetTransactions() throws ApiException {
@Test
public void testGetTransactionsWithOrder() throws ApiException {
String address = "addr_test1qzx9hu8j4ah3auytk0mwcupd69hpc52t0cw39a65ndrah86djs784u92a3m5w475w3w35tyd6v3qumkze80j8a6h5tuqq5xe8y";
List<String> txns = addressService.getTransactions(address, 50, 1, "desc").getValue();
List<String> txns = addressService.getTransactions(address, 50, 1, OrderEnum.desc).getValue();

System.out.println(txns);
assertTrue(txns.size() > 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bloxbean.cardano.client.backend.api;

import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.model.AddressContent;
import com.bloxbean.cardano.client.backend.model.Result;
Expand Down Expand Up @@ -37,5 +38,5 @@ public interface AddressService {
* @return
* @throws ApiException
*/
public Result<List<String>> getTransactions(String address, int count, int page, String order) throws ApiException;
public Result<List<String>> getTransactions(String address, int count, int page, OrderEnum order) throws ApiException;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bloxbean.cardano.client.backend.api;

import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.model.Result;
import com.bloxbean.cardano.client.backend.model.Utxo;
Expand All @@ -26,5 +27,5 @@ public interface UtxoService {
* @return
* @throws ApiException
*/
public Result<List<Utxo>> getUtxos(String address, int count, int page, String order) throws ApiException;
public Result<List<Utxo>> getUtxos(String address, int count, int page, OrderEnum order) throws ApiException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.bloxbean.cardano.client.backend.api.helper;

import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.model.Utxo;

import java.util.List;

/**
* Implement this interface to provide custom UtxoSelection Strategy
*/
public interface UtxoSelectionStrategy {

/**
* Filter and return selected utxos from the list
* @param utxos
* @return
*/
public List<Utxo> filter(List<Utxo> utxos);

/**
* Utxo record fetch size from the server per page
* @return
*/
default public int getUtxoFetchSize() {
return 40;
}

/**
* Utxo fetch order
* @return
*/
default public OrderEnum getUtxoFetchOrder() {
return OrderEnum.asc;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import com.bloxbean.cardano.client.backend.exception.InsufficientBalanceException;
import com.bloxbean.cardano.client.backend.model.Amount;
import com.bloxbean.cardano.client.backend.model.Result;
import com.bloxbean.cardano.client.backend.model.Utxo;
import com.bloxbean.cardano.client.common.CardanoConstants;
import com.bloxbean.cardano.client.common.MinAdaCalculator;
import com.bloxbean.cardano.client.exception.AddressExcepion;
import com.bloxbean.cardano.client.metadata.Metadata;
import com.bloxbean.cardano.client.transaction.model.TransactionDetailsParams;
import com.bloxbean.cardano.client.backend.model.Utxo;
import com.bloxbean.cardano.client.transaction.model.MintTransaction;
import com.bloxbean.cardano.client.transaction.model.PaymentTransaction;
import com.bloxbean.cardano.client.common.CardanoConstants;
import com.bloxbean.cardano.client.exception.AddressExcepion;
import com.bloxbean.cardano.client.transaction.model.TransactionDetailsParams;
import com.bloxbean.cardano.client.transaction.spec.*;
import com.bloxbean.cardano.client.util.AssetUtil;
import com.bloxbean.cardano.client.util.JsonUtil;
Expand All @@ -28,17 +28,32 @@
import java.util.*;

import static com.bloxbean.cardano.client.common.CardanoConstants.LOVELACE;
import static com.bloxbean.cardano.client.common.CardanoConstants.ONE_ADA;

public class UtxoTransactionBuilder {
private Logger LOG = LoggerFactory.getLogger(UtxoTransactionBuilder.class);

private final UtxoService utxoService;
private final TransactionService transactionService;
private UtxoSelectionStrategy utxoSelectionStrategy;

public UtxoTransactionBuilder(UtxoService utxoService, TransactionService transactionService) {
this.utxoService = utxoService;
this.transactionService = transactionService;

this.utxoSelectionStrategy = new UtxoSelectionStrategy() {
@Override
public List<Utxo> filter(List<Utxo> utxos) {
return utxos;
}
};
}

/**
* Set a custom UtxoSelectionStrategy
* @param utxoSelectionStrategy
*/
public void setUtxoSelectionStrategy(UtxoSelectionStrategy utxoSelectionStrategy) {
this.utxoSelectionStrategy = utxoSelectionStrategy;
}

/**
Expand Down Expand Up @@ -224,9 +239,12 @@ private List<Utxo> getUtxos(String address, String unit, BigInteger amount, Set<

boolean isLovlace = CardanoConstants.LOVELACE.equals(unit)? true : false;
while(canContinue) {
Result<List<Utxo>> result = utxoService.getUtxos(address, 40, i++);
Result<List<Utxo>> result = utxoService.getUtxos(address, utxoSelectionStrategy.getUtxoFetchSize(),
i++, utxoSelectionStrategy.getUtxoFetchOrder());
if(result.code() == 200) {
List<Utxo> data = result.getValue();
List<Utxo> fetchData = result.getValue();

List<Utxo> data = utxoSelectionStrategy.filter(fetchData);
if(data == null || data.isEmpty())
canContinue = false;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bloxbean.cardano.client.backend.impl.blockfrost.service;

import com.bloxbean.cardano.client.backend.api.AddressService;
import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.impl.blockfrost.service.http.AddressesApi;
import com.bloxbean.cardano.client.backend.model.AddressContent;
Expand Down Expand Up @@ -35,12 +36,12 @@ public Result<AddressContent> getAddressInfo(String address) throws ApiException

@Override
public Result<List<String>> getTransactions(String address, int count, int page) throws ApiException {
return getTransactions(address, count, page, "asc");
return getTransactions(address, count, page, OrderEnum.asc);
}

@Override
public Result<List<String>> getTransactions(String address, int count, int page, String order) throws ApiException {
Call<List<String>> call = addressApi.getTransactions(getProjectId(), address, count, page, order);
public Result<List<String>> getTransactions(String address, int count, int page, OrderEnum order) throws ApiException {
Call<List<String>> call = addressApi.getTransactions(getProjectId(), address, count, page, order.toString());

try {
Response<List<String>> response = call.execute();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bloxbean.cardano.client.backend.impl.blockfrost.service;

import com.bloxbean.cardano.client.backend.api.UtxoService;
import com.bloxbean.cardano.client.backend.common.OrderEnum;
import com.bloxbean.cardano.client.backend.exception.ApiException;
import com.bloxbean.cardano.client.backend.impl.blockfrost.service.http.AddressesApi;
import com.bloxbean.cardano.client.backend.model.Result;
Expand All @@ -22,11 +23,11 @@ public BFUtxoService(String baseUrl, String projectId) {

@Override
public Result<List<Utxo>> getUtxos(String address, int count, int page) throws ApiException {
return getUtxos(address, count, page, "asc");
return getUtxos(address, count, page, OrderEnum.asc);
}

public Result<List<Utxo>> getUtxos(String address, int count, int page, String order) throws ApiException {
Call<List<Utxo>> utxosCall = addressApi.getUtxos(getProjectId(), address, count, page, order);
public Result<List<Utxo>> getUtxos(String address, int count, int page, OrderEnum order) throws ApiException {
Call<List<Utxo>> utxosCall = addressApi.getUtxos(getProjectId(), address, count, page, order.toString());

try {
Response<List<Utxo>> response = utxosCall.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void testCalculateFeeSimplePaymentTransaction() throws ApiException, IOException
String receiver = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";

List<Utxo> utxos = loadUtxos(LIST_1);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand All @@ -109,7 +109,7 @@ void testCalculateFeePaymentTransactionWithMetadata() throws ApiException, IOExc
String receiver = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";

List<Utxo> utxos = loadUtxos(LIST_1);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

CBORMetadataMap mm = new CBORMetadataMap()
.put(new BigInteger("1978"), "1978value")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public void testGetUtxosWithLovelaceWillReturnCorrectUtxos() throws ApiException
String address = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";

List<Utxo> utxos = loadUtxos(LIST_2);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

List<Utxo> utxoList = utxoTransactionBuilder.getUtxos(address, "lovelace", BigInteger.valueOf(500000000));

verify(utxoService, times(1)).getUtxos(any(), anyInt(), anyInt());
verify(utxoService, times(1)).getUtxos(any(), anyInt(), anyInt(), any());

assertThat(utxoList, hasSize(2));
assertThat(utxoList.get(0).getAmount().get(0).getQuantity(), is(BigInteger.valueOf(1407406)));
Expand All @@ -94,7 +94,7 @@ public void testGetUtxosWithAssetWillReturnCorrectUtxos() throws ApiException, I
String unit = "329728f73683fe04364631c27a7912538c116d802416ca1eaf2d7a96736174636f696e";

List<Utxo> utxos = loadUtxos(LIST_2);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

List<Utxo> utxoList = utxoTransactionBuilder.getUtxos(address, unit, BigInteger.valueOf(400000000));

Expand All @@ -109,7 +109,7 @@ public void testBuildTransactionWithLovelaceWillReturnTrasaction() throws ApiExc
String receiver = "addr_test1qqwpl7h3g84mhr36wpetk904p7fchx2vst0z696lxk8ujsjyruqwmlsm344gfux3nsj6njyzj3ppvrqtt36cp9xyydzqzumz82";

List<Utxo> utxos = loadUtxos(LIST_2);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand Down Expand Up @@ -143,7 +143,7 @@ public void testBuildTransactionWithMultiAssetWillReturnTrasaction() throws ApiE
String unit = "329728f73683fe04364631c27a7912538c116d802416ca1eaf2d7a96736174636f696e";

List<Utxo> utxos = loadUtxos(LIST_3);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand Down Expand Up @@ -182,7 +182,7 @@ public void testBuildTransactionWithMultiAssetAndMultipleUtxosWillReturnTrasacti
String unit = "6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7";

List<Utxo> utxos = loadUtxos(LIST_3);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand Down Expand Up @@ -228,7 +228,7 @@ public void testBuildTransactionWithMultiAssetAndNotSufficientLovelaceWillReturn
String unit = "6b8d07d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7";

List<Utxo> utxos = loadUtxos(LIST_4);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand Down Expand Up @@ -274,7 +274,7 @@ public void testBuildTransactionWithMultiplePaymentsAndMultiAssetAndMultipleUtxo
String unit2 = "329728f73683fe04364631c27a7912538c116d802416ca1eaf2d7a96736174636f696e";

List<Utxo> utxos = loadUtxos(LIST_3);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand Down Expand Up @@ -315,7 +315,7 @@ public void testBuildTransactionWithMultiAssetAndNotSufficientLovelaceWillReturn
String unit = "777777d69639e9413dd637a1a815a7323c69c86abbafb66dbfdb1aa7";

List<Utxo> utxos = loadUtxos(LIST_5);
given(utxoService.getUtxos(any(), anyInt(), anyInt())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));
given(utxoService.getUtxos(any(), anyInt(), anyInt(), any())).willReturn(Result.success(utxos.toString()).withValue(utxos).code(200));

Account sender = new Account(Networks.testnet());
PaymentTransaction paymentTransaction = PaymentTransaction.builder()
Expand Down

0 comments on commit a821457

Please sign in to comment.