Skip to content

Commit

Permalink
Merge pull request #159 from edridudi/master
Browse files Browse the repository at this point in the history
Bump Koios Java Client to 1.14.1 - Fixes #148
  • Loading branch information
satran004 authored Oct 2, 2022
2 parents f42736f + a7a805e commit 427f3cc
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion backend-modules/koios/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies {
compile project(':')
compile project(':cardano-client-backend')

compile ('io.github.cardano-community:koios-java-client:1.13')
compile ('io.github.cardano-community:koios-java-client:1.14.1')

testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import com.bloxbean.cardano.client.backend.model.*;
import com.bloxbean.cardano.client.util.HexUtil;
import rest.koios.client.backend.api.account.AccountService;
import rest.koios.client.backend.api.account.model.AccountHistoryInner;
import rest.koios.client.backend.api.account.model.AccountInfo;
import rest.koios.client.backend.api.account.model.AccountReward;
import rest.koios.client.backend.api.account.model.AccountRewards;
import rest.koios.client.backend.factory.options.*;

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

/**
Expand All @@ -31,11 +35,14 @@ public KoiosAccountService(AccountService accountService) {
@Override
public Result<AccountInformation> getAccountInformation(String stakeAddress) throws ApiException {
try {
rest.koios.client.backend.api.base.Result<AccountInfo> accountInformationResult = accountService.getAccountInformation(stakeAddress);
rest.koios.client.backend.api.base.Result<List<AccountInfo>> accountInformationResult = accountService.getAccountInformation(List.of(stakeAddress), Options.EMPTY);
if (!accountInformationResult.isSuccessful()) {
return Result.error(accountInformationResult.getResponse()).code(accountInformationResult.getCode());
}
return convertToAccountInformation(accountInformationResult.getValue());
if (accountInformationResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
return convertToAccountInformation(accountInformationResult.getValue().get(0));
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
Expand All @@ -62,25 +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(stakeAddress, 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());
}
return convertToAccountRewards(accountRewardsResult.getValue());
if (accountRewardsResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
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<AccountRewards> accountRewardsList) {
private Result<List<AccountRewardsHistory>> convertToAccountRewards(List<AccountReward> accountRewardsList, OrderEnum order) {
List<AccountRewardsHistory> accountRewardsHistories = new ArrayList<>();
if (accountRewardsList != null) {
accountRewardsList.forEach(accountRewards -> {
Expand All @@ -92,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 @@ -103,25 +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(stakeAddress, 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());
}
return convertToAccountHistories(accountHistoriesResult.getValue());
if (accountHistoriesResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
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<rest.koios.client.backend.api.account.model.AccountHistory> accountHistories) {
private Result<List<AccountHistory>> convertToAccountHistories(List<AccountHistoryInner> accountHistories, OrderEnum order) {
List<AccountHistory> accountHistoryList = new ArrayList<>();
if (accountHistories != null) {
accountHistories.forEach(accountHistory -> {
Expand All @@ -132,6 +140,11 @@ private Result<List<AccountHistory>> convertToAccountHistories(List<rest.koios.c
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 All @@ -147,20 +160,23 @@ public Result<List<AccountAddress>> getAccountAddresses(String stakeAddress, int
.option(Limit.of(count))
.option(Offset.of((long) (page - 1) * count))
.build();
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.account.model.AccountAddress>> accountAddressesResult = accountService.getAccountAddresses(stakeAddress, options);
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.account.model.AccountAddress>> accountAddressesResult = accountService.getAccountAddresses(List.of(stakeAddress), null, options);
if (!accountAddressesResult.isSuccessful()) {
return Result.error(accountAddressesResult.getResponse()).code(accountAddressesResult.getCode());
}
return convertToAccountAddresses(accountAddressesResult.getValue());
if (accountAddressesResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
return convertToAccountAddresses(accountAddressesResult.getValue().get(0).getAddresses());
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
}

private Result<List<AccountAddress>> convertToAccountAddresses(List<rest.koios.client.backend.api.account.model.AccountAddress> accountAddressList) {
private Result<List<AccountAddress>> convertToAccountAddresses(List<String> accountAddressList) {
List<AccountAddress> accountAddresses = new ArrayList<>();
if (accountAddressList != null) {
accountAddressList.forEach(accountAddress -> accountAddresses.add(new AccountAddress(accountAddress.getAddress())));
accountAddressList.forEach(accountAddress -> accountAddresses.add(new AccountAddress(accountAddress)));
}
return Result.success("OK").withValue(accountAddresses).code(200);
}
Expand All @@ -177,11 +193,14 @@ public Result<List<AccountAsset>> getAccountAssets(String stakeAddress, int coun
.option(Limit.of(count))
.option(Offset.of((long) (page - 1) * count))
.build();
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.account.model.AccountAsset>> accountAssetsResult = accountService.getAccountAssets(stakeAddress, options);
rest.koios.client.backend.api.base.Result<List<rest.koios.client.backend.api.account.model.AccountAssets>> accountAssetsResult = accountService.getAccountAssets(List.of(stakeAddress), null, options);
if (!accountAssetsResult.isSuccessful()) {
return Result.error(accountAssetsResult.getResponse()).code(accountAssetsResult.getCode());
}
return convertToAccountAssets(accountAssetsResult.getValue());
if (accountAssetsResult.getValue().isEmpty()) {
return Result.error("Not Found").code(404);
}
return convertToAccountAssets(accountAssetsResult.getValue().get(0).getAssets());
} catch (rest.koios.client.backend.api.base.exception.ApiException e) {
throw new ApiException(e.getMessage(), e);
}
Expand All @@ -191,10 +210,13 @@ private Result<List<AccountAsset>> convertToAccountAssets(List<rest.koios.client
List<AccountAsset> accountAssets = new ArrayList<>();
if (accountAssetList!=null) {
accountAssetList.forEach(accountAsset -> {
AccountAsset accountAsset1 = new AccountAsset();
accountAsset1.setUnit(accountAsset.getAssetPolicy() + HexUtil.encodeHexString(accountAsset.getAssetName().getBytes(StandardCharsets.UTF_8)));
accountAsset1.setQuantity(accountAsset.getQuantity());
accountAssets.add(accountAsset1);
accountAsset.getAssets().forEach(assetInner -> {
AccountAsset accountAsset1 = new AccountAsset();
accountAsset1.setUnit(accountAsset.getPolicyId() + HexUtil.encodeHexString(assetInner.getAssetName().getBytes(StandardCharsets.UTF_8)));
accountAsset1.setQuantity(assetInner.getBalance());
accountAssets.add(accountAsset1);

});
});
}
return Result.success("OK").withValue(accountAssets).code(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private Result<AddressContent> convertToAddressContent(AddressInfo addressInfo)
txContentOutputAmountList.add(0, new TxContentOutputAmount("lovelace", addressInfo.getBalance()));
addressContent.setAmount(txContentOutputAmountList);
addressContent.setScript(addressInfo.getScriptAddress());

return Result.success("OK").withValue(addressContent).code(200);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ private Result<Asset> convertToAsset(AssetInformation assetInformation) {
asset.setQuantity(assetInformation.getTotalSupply());
asset.setInitialMintTxHash(assetInformation.getMintingTxHash());
asset.setMintOrBurnCount(assetInformation.getMintCnt() + assetInformation.getBurnCnt());
if (assetInformation.getMintingTxMetadata() != null && !assetInformation.getMintingTxMetadata().isEmpty()) {
JsonNode jsonNode = assetInformation.getMintingTxMetadata().get(0).getJson();
if (assetInformation.getMintingTxMetadata() != null) {
JsonNode jsonNode = assetInformation.getMintingTxMetadata().getJson();
if (jsonNode.get(assetInformation.getPolicyId()) != null && jsonNode.get(assetInformation.getPolicyId()).get(assetInformation.getAssetNameAscii()) != null) {
asset.setOnchainMetadata(jsonNode.get(assetInformation.getPolicyId()).get(assetInformation.getAssetNameAscii()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,12 @@ private Result<List<MetadataLabel>> convertToMetadataLabels(List<TxMetadataLabel
}

@Override
public Result<List<MetadataJSONContent>> getJSONMetadataByLabel(BigInteger label, int count, int page, OrderEnum order) throws ApiException {
public Result<List<MetadataJSONContent>> getJSONMetadataByLabel(BigInteger label, int count, int page, OrderEnum order) {
throw new UnsupportedOperationException();
}

@Override
public Result<List<MetadataCBORContent>> getCBORMetadataByLabel(BigInteger label, int count, int page, OrderEnum order) throws ApiException {
public Result<List<MetadataCBORContent>> getCBORMetadataByLabel(BigInteger label, int count, int page, OrderEnum order) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import rest.koios.client.backend.api.address.model.AddressInfo;
import rest.koios.client.backend.api.address.model.AddressUtxo;
import rest.koios.client.backend.api.address.model.Asset;
import rest.koios.client.backend.factory.options.Options;
import rest.koios.client.backend.factory.options.SortType;

import java.math.BigInteger;
Expand Down Expand Up @@ -45,6 +46,12 @@ private Result<List<Utxo>> convertToUTxOs(AddressInfo addressInfo) {
utxo.setTxHash(addressUtxo.getTxHash());
utxo.setOutputIndex(addressUtxo.getTxIndex());
utxo.setDataHash(addressUtxo.getDatumHash());
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 All @@ -65,7 +72,7 @@ public Result<List<Utxo>> getUtxos(String address, int count, int page, OrderEnu
}
rest.koios.client.backend.api.base.Result<AddressInfo> addressInformationResult;
if (order == OrderEnum.asc) {
addressInformationResult = addressService.getAddressInformation(address, SortType.ASC);
addressInformationResult = addressService.getAddressInformation(List.of(address), SortType.ASC, Options.EMPTY);
} else {
addressInformationResult = addressService.getAddressInformation(address);
}
Expand Down

0 comments on commit 427f3cc

Please sign in to comment.