Skip to content

Commit

Permalink
Bump Koios Java Client to 1.14.1 - Update
Browse files Browse the repository at this point in the history
  • Loading branch information
edridudi committed Oct 1, 2022
1 parent 85517b6 commit a7a805e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
Expand Down Expand Up @@ -67,28 +69,23 @@ public Result<List<AccountRewardsHistory>> getAccountRewardsHistory(String stake
@Override
public Result<List<AccountRewardsHistory>> getAccountRewardsHistory(String stakeAddress, int count, int page, OrderEnum order) throws ApiException {
try {
Option ordering = Order.by("earned_epoch", SortType.ASC);
if (order == OrderEnum.desc) {
ordering = Order.by("earned_epoch", SortType.DESC);
if (page != 1) {
return Result.success("OK").withValue(Collections.emptyList()).code(200);
}
Options options = Options.builder()
.option(Limit.of(count))
.option(Offset.of((long) (page - 1) * count))
.option(ordering).build();
rest.koios.client.backend.api.base.Result<List<AccountRewards>> accountRewardsResult = accountService.getAccountRewards(List.of(stakeAddress), null, options);
rest.koios.client.backend.api.base.Result<List<AccountRewards>> accountRewardsResult = accountService.getAccountRewards(List.of(stakeAddress), null, Options.EMPTY);
if (!accountRewardsResult.isSuccessful()) {
return Result.error(accountRewardsResult.getResponse()).code(accountRewardsResult.getCode());
}
if (accountRewardsResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
return convertToAccountRewards(accountRewardsResult.getValue().get(0).getRewards());
return convertToAccountRewards(accountRewardsResult.getValue().get(0).getRewards(), order);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

private Result<List<AccountRewardsHistory>> convertToAccountRewards(List<AccountReward> accountRewardsList) {
private Result<List<AccountRewardsHistory>> convertToAccountRewards(List<AccountReward> accountRewardsList, OrderEnum order) {
List<AccountRewardsHistory> accountRewardsHistories = new ArrayList<>();
if (accountRewardsList != null) {
accountRewardsList.forEach(accountRewards -> {
Expand All @@ -100,6 +97,11 @@ private Result<List<AccountRewardsHistory>> convertToAccountRewards(List<Account
accountRewardsHistories.add(accountRewardsHistory);
});
}
if (order==OrderEnum.asc) {
accountRewardsHistories.sort(Comparator.comparing(AccountRewardsHistory::getEpoch));
} else {
accountRewardsHistories.sort(Comparator.comparing(AccountRewardsHistory::getEpoch).reversed());
}
return Result.success("OK").withValue(accountRewardsHistories).code(200);
}

Expand All @@ -111,28 +113,23 @@ public Result<List<AccountHistory>> getAccountHistory(String stakeAddress, int c
@Override
public Result<List<AccountHistory>> getAccountHistory(String stakeAddress, int count, int page, OrderEnum order) throws ApiException {
try {
Option ordering = Order.by("epoch_no", SortType.ASC);
if (order == OrderEnum.desc) {
ordering = Order.by("epoch_no", SortType.DESC);
if (page != 1) {
return Result.success("OK").withValue(Collections.emptyList()).code(200);
}
Options options = Options.builder()
.option(Limit.of(count))
.option(Offset.of((long) (page - 1) * count))
.option(ordering).build();
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.account.model.AccountHistory>> accountHistoriesResult = accountService.getAccountHistory(List.of(stakeAddress), null, options);
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.account.model.AccountHistory>> accountHistoriesResult = accountService.getAccountHistory(List.of(stakeAddress), null, Options.EMPTY);
if (!accountHistoriesResult.isSuccessful()) {
return Result.error(accountHistoriesResult.getResponse()).code(accountHistoriesResult.getCode());
}
if (accountHistoriesResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
return convertToAccountHistories(accountHistoriesResult.getValue().get(0).getHistory());
return convertToAccountHistories(accountHistoriesResult.getValue().get(0).getHistory(), order);
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

private Result<List<AccountHistory>> convertToAccountHistories(List<AccountHistoryInner> accountHistories) {
private Result<List<AccountHistory>> convertToAccountHistories(List<AccountHistoryInner> accountHistories, OrderEnum order) {
List<AccountHistory> accountHistoryList = new ArrayList<>();
if (accountHistories != null) {
accountHistories.forEach(accountHistory -> {
Expand All @@ -143,6 +140,11 @@ private Result<List<AccountHistory>> convertToAccountHistories(List<AccountHisto
accountHistoryList.add(accountHist);
});
}
if (order==OrderEnum.asc) {
accountHistoryList.sort(Comparator.comparing(AccountHistory::getActiveEpoch));
} else {
accountHistoryList.sort(Comparator.comparing(AccountHistory::getActiveEpoch).reversed());
}
return Result.success("OK").withValue(accountHistoryList).code(200);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,12 @@ private Result<List<Utxo>> convertToUTxOs(AddressInfo addressInfo) {
utxo.setTxHash(addressUtxo.getTxHash());
utxo.setOutputIndex(addressUtxo.getTxIndex());
utxo.setDataHash(addressUtxo.getDatumHash());
utxo.setInlineDatum(addressUtxo.getInlineDatum().getBytes());
utxo.setReferenceScriptHash(addressUtxo.getReferenceScript().getHash());
if (addressUtxo.getInlineDatum() != null) {
utxo.setInlineDatum(addressUtxo.getInlineDatum().getBytes());
}
if (addressUtxo.getReferenceScript() != null) {
utxo.setReferenceScriptHash(addressUtxo.getReferenceScript().getHash());
}
List<Amount> amountList = new ArrayList<>();
amountList.add(new Amount(LOVELACE, new BigInteger(addressUtxo.getValue())));
for (Asset asset : addressUtxo.getAssetList()) {
Expand Down

0 comments on commit a7a805e

Please sign in to comment.