Skip to content

Commit

Permalink
chore: fixed assets and multi assets removal in case of a non modifia…
Browse files Browse the repository at this point in the history
…ble list
  • Loading branch information
nemo83 committed Oct 14, 2024
1 parent 288173c commit bc216e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

Expand Down Expand Up @@ -214,13 +216,24 @@ public Value subtract(Value that) {
BigInteger sumCoin = (getCoin() == null ? BigInteger.ZERO.subtract(that.getCoin()) : getCoin().subtract(that.getCoin()));
List<MultiAsset> difMultiAssets = MultiAsset.subtractMultiAssetLists(getMultiAssets(), that.getMultiAssets());

//Remove all asset with value == 0
difMultiAssets.forEach(multiAsset ->
multiAsset.getAssets().removeIf(asset -> BigInteger.ZERO.equals(asset.getValue())));
//Remove multiasset if there's no asset
difMultiAssets.removeIf(multiAsset -> multiAsset.getAssets() == null || multiAsset.getAssets().isEmpty());

return Value.builder().coin(sumCoin).multiAssets(difMultiAssets).build();
List<MultiAsset> filteredMultiAssets = difMultiAssets
.stream()
.flatMap(multiAsset -> {
List<Asset> assets = multiAsset
.getAssets()
.stream()
.filter(asset -> !asset.getValue().equals(BigInteger.ZERO))
.collect(Collectors.toList());
if (!assets.isEmpty()) {
multiAsset.setAssets(assets);
return Stream.of(multiAsset);
} else {
return Stream.empty();
}
})
.collect(Collectors.toList());

return Value.builder().coin(sumCoin).multiAssets(filteredMultiAssets).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ public void addSingleTokenUnit() {
String policyId = "ef76f6f0b3558ea0aaad6af5c9a5f3e5bf20b393314de747662e8ce9";
BigInteger hundredMil = BigInteger.valueOf(100_000_000L);
Value value = Value.builder().coin(BigInteger.valueOf(10_000_000L)).build();
Value actual = value.add("ef76f6f0b3558ea0aaad6af5c9a5f3e5bf20b393314de747662e8ce9.506f6c795065657237353436", hundredMil);
Value actual = value.add("ef76f6f0b3558ea0aaad6af5c9a5f3e5bf20b393314de747662e8ce9506f6c795065657237353436", hundredMil);

Value expected = value
.toBuilder()
Expand Down Expand Up @@ -679,7 +679,7 @@ public void amountOfMissingTokenFromUnitIsZero() {
value = value.add(policyId, "0x506f6c795065657237353436", hundredMil);
String assetName = new String(HexUtil.decodeHexString("506f6c795065657237353436"));
value = value.add(policyId, assetName, hundredMil);
BigInteger actual = value.amountOf("4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd.546f6b68756e");
BigInteger actual = value.amountOf("4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd546f6b68756e");
Assertions.assertEquals(BigInteger.ZERO, actual);
}

Expand Down Expand Up @@ -724,10 +724,10 @@ public void isPositiveAdaOnlyParametric(String amount, boolean outcome) {

@ParameterizedTest
@CsvSource({
"0,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd.546f6b68756e,1000000,true",
"0,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd546f6b68756e,1000000,true",
"0,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd546f6b68756e,-1000000,false",
"-1000000,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd546f6b68756e,1000000,false",
"-1000000,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd.546f6b68756e,-1000000,false",
"-1000000,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd546f6b68756e,-1000000,false",
"1000000,4247d5091db82330100904963ab8d0850976c80d3f1b927e052e07bd546f6b68756e,0,true",
})
public void isPositiveParametric(String lovelace, String unit, String tokenAmount, boolean outcome) {
Expand Down

0 comments on commit bc216e0

Please sign in to comment.