Skip to content

Commit

Permalink
Fix: reduce validations assuming the YACI store will store validated …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
BerezinD committed May 7, 2024
1 parent e0980ce commit 6612624
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,41 +150,38 @@ public static Amount mapAmount(String value, String symbol, Integer decimals,

/**
* Maps a list of AddressBalanceDTOs to a Rosetta compatible AccountBalanceResponse.
*
* @param block The block from where the balances are calculated into the past
* @param balances The list of balances that should contain quantity on the latest block number
* @param balances The list of filtered balances up to {@code block} number.
* Each unit should occur only one time with the latest balance.
* Native assets should be present only as a lovelace unit.
* @return The Rosetta compatible AccountBalanceResponse
*/
public static AccountBalanceResponse mapToAccountBalanceResponse(Block block, List<AddressBalance> balances) {
// in case if there are multiple balances for the same unit, we take the one from the latest block
Map<String, AddressBalance> unitToBalance = balances.stream().collect(
Collectors.toMap(AddressBalance::unit, Function.identity(),
(balance1, balance2) -> balance1.number() > balance2.number() ? balance1 : balance2));
AddressBalance zeroBalance = AddressBalance.builder().quantity(BigInteger.ZERO).build();
// here should be only one balance, LOVELACE
BigInteger sum = unitToBalance.getOrDefault(Constants.LOVELACE, zeroBalance).quantity().add(
BigInteger.valueOf(1_000_000L).multiply( // make it LOVELACE out of ADA
unitToBalance.getOrDefault(Constants.ADA, zeroBalance).quantity()));
BigInteger lovelaceAmount = balances.stream()
.filter(b -> Constants.LOVELACE.equals(b.unit()))
.map(AddressBalance::quantity)
.findFirst()
.orElse(BigInteger.ZERO);
List<Amount> amounts = new ArrayList<>();
if (sum.compareTo(BigInteger.ZERO) > 0) {
amounts.add(mapAmount(String.valueOf(sum)));
if (lovelaceAmount.compareTo(BigInteger.ZERO) > 0) {
amounts.add(mapAmount(String.valueOf(lovelaceAmount)));
}
unitToBalance.entrySet().stream()
.filter(entry -> !entry.getKey().equals(Constants.LOVELACE)
&& !entry.getKey().equals(Constants.ADA))
.forEach(entry -> amounts.add(
mapAmount(entry.getValue().quantity().toString(),
entry.getKey().substring(Constants.POLICY_ID_LENGTH),
balances.stream()
.filter(b -> !Constants.LOVELACE.equals(b.unit()))
.forEach(b -> amounts.add(
mapAmount(b.quantity().toString(),
b.unit().substring(Constants.POLICY_ID_LENGTH),
Constants.MULTI_ASSET_DECIMALS,
new CurrencyMetadata(
entry.getKey().substring(0, Constants.POLICY_ID_LENGTH)))
new CurrencyMetadata(b.unit().substring(0, Constants.POLICY_ID_LENGTH)))
));
return AccountBalanceResponse.builder()
.blockIdentifier(BlockIdentifier.builder()
.hash(block.getHash())
.index(block.getNumber())
.build())
.balances(amounts)
.build();
.blockIdentifier(BlockIdentifier.builder()
.hash(block.getHash())
.index(block.getNumber())
.build())
.balances(amounts)
.build();
}

public static AccountBalanceResponse mapToStakeAddressBalanceResponse(Block block, StakeAddressBalance balance) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void getAccountBalanceNoStakeAddressPositiveTest() {
AccountIdentifier accountIdentifier = getMockedAccountIdentifierAndMockAccountBalanceRequest(
accountBalanceRequest, blockIdentifier, accountAddress);
Block block = getMockBlock();
AddressBalance addressBalance = new AddressBalance(accountAddress, ADA, 1L,
AddressBalance addressBalance = new AddressBalance(accountAddress, LOVELACE, 1L,
BigInteger.valueOf(1000L), 1L);
when(ledgerBlockService.findBlock(1L, HASH)).thenReturn(block);
when(ledgerDataProviderService.findBalanceByAddressAndBlock(accountAddress, 1L))
Expand All @@ -72,7 +72,7 @@ void getAccountBalanceNoStakeAddressPositiveTest() {
AccountBalanceResponse actual = accountService.getAccountBalance(accountBalanceRequest);

assertNotNull(actual);
assertEquals("1000000000", actual.getBalances().getFirst().getValue());
assertEquals("1000", actual.getBalances().getFirst().getValue());
assertNotNull(actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA, actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA_DECIMALS, actual.getBalances().getFirst().getCurrency().getDecimals());
Expand Down Expand Up @@ -126,7 +126,7 @@ void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
when(accountBalanceRequest.getAccountIdentifier()).thenReturn(accountIdentifier);
when(accountIdentifier.getAddress()).thenReturn(accountAddress);
Block block = getMockBlock();
AddressBalance addressBalance = new AddressBalance(accountAddress, ADA, 1L,
AddressBalance addressBalance = new AddressBalance(accountAddress, LOVELACE, 1L,
BigInteger.valueOf(1000L), 1L);
when(ledgerBlockService.findLatestBlock()).thenReturn(block);
when(ledgerDataProviderService.findBalanceByAddressAndBlock(accountAddress, 1L))
Expand All @@ -135,7 +135,7 @@ void getAccountBalanceNoStakeAddressNullBlockIdentifierPositiveTest() {
AccountBalanceResponse actual = accountService.getAccountBalance(accountBalanceRequest);

assertNotNull(actual);
assertEquals("1000000000", actual.getBalances().getFirst().getValue());
assertEquals("1000", actual.getBalances().getFirst().getValue());
assertNotNull(actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA, actual.getBalances().getFirst().getCurrency().getSymbol());
assertEquals(Constants.ADA_DECIMALS, actual.getBalances().getFirst().getCurrency().getDecimals());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ void AddressBalanceMintedTokenWithEmptyName_Test() {
accountBalanceResponse.getBalances().get(1).getValue());
assertNotEquals(accountBalanceResponse.getBalances().getFirst().getCurrency().getSymbol(),
accountBalanceResponse.getBalances().get(1).getCurrency().getSymbol());
assertEquals("", accountBalanceResponse.getBalances().get(1).getCurrency().getSymbol());
assertEquals("", accountBalanceResponse.getBalances().get(2).getCurrency().getSymbol());
}

@Test
Expand Down

0 comments on commit 6612624

Please sign in to comment.