Skip to content

Commit

Permalink
fix: negating value if it's an input operation (#124)
Browse files Browse the repository at this point in the history
* fix: negating value if it's an input operation

* fix: adjusted tests
  • Loading branch information
Kammerlo authored Apr 16, 2024
1 parent 63dc3c3 commit e61725a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,23 @@ protected Amount updateDepositAmount(BigInteger deposit) {

//common mappings for InputToOperation and OutputToOperation
protected static void mapOthers(Utxo model, OperationStatus status, int index,
ConfigurableConditionExpression<Utxo, Operation> mp) {
ConfigurableConditionExpression<Utxo, Operation> mp, boolean input) {
mp.map(f -> status.getStatus(), Operation::setStatus);
mp.<Long>map(f -> index, (d, v) -> d.getOperationIdentifier().setIndex(v));
mp.<String>map(Utxo::getOwnerAddr, (d, v) -> d.getAccount().setAddress(v));
mp.map(Utxo::getLovelaceAmount, (d, v) -> d.getAmount().setValue(String.valueOf(v)));
mp.<String>map( f -> getAdaAmount(model, input), (d, v) -> d.getAmount().setValue(v));
mp.<String>map(f -> ADA, (d, v) -> d.getAmount().getCurrency().setSymbol(v));
mp.<Integer>map(f -> ADA_DECIMALS, (d, v) -> d.getAmount().getCurrency().setDecimals(v));
mp.<String>map(f -> model.getTxHash() + ":" + model.getOutputIndex(),
(d, v) -> d.getCoinChange().getCoinIdentifier().setIdentifier(v));
}

private static String getAdaAmount(Utxo f, boolean input) {
BigInteger adaAmount = Optional.ofNullable(f.getAmounts())
.map(amts -> amts.stream().filter(amt -> amt.getAssetName().equals(
Constants.LOVELACE)).findFirst().map(Amt::getQuantity).orElse(BigInteger.ZERO))
.orElse(BigInteger.ZERO);
return input ? adaAmount.negate().toString() : adaAmount.toString();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Operation toDto(Utxo model, OperationStatus status, int index) {
mp.map(f -> Constants.INPUT, Operation::setType);
mp.<CoinAction>map(f -> CoinAction.SPENT, (d, v) -> d.getCoinChange().setCoinAction(v));
mp.map(f-> mapToOperationMetaData(true, model.getAmounts()), Operation::setMetadata);
mapOthers(model, status, index, mp);
mapOthers(model, status, index, mp, true);
})
.map(model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Operation toDto(Utxo model, OperationStatus status, int index) {
mp.map(f -> Constants.OUTPUT, Operation::setType);
mp.<CoinAction>map(f -> CoinAction.CREATED, (d, v) -> d.getCoinChange().setCoinAction(v));
mp.map(f-> mapToOperationMetaData(false, model.getAmounts()), Operation::setMetadata);
mapOthers(model, status, index, mp);
mapOthers(model, status, index, mp, false);
})
.map(model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void toDto_Test_getInputsAsOperations() {
assertThat(opInto.getStatus()).isEqualTo("success");
assertThat(opInto.getOperationIdentifier().getIndex()).isEqualTo(0); //index in array
assertThat(opInto.getAccount().getAddress()).isEqualTo(firstFrom.getOwnerAddr());
assertThat(opInto.getAmount()).isEqualTo(amountActual("10"));
assertThat(opInto.getAmount()).isEqualTo(amountActual("-10"));

CoinChange coinChange = opInto.getCoinChange();
assertThat(coinChange.getCoinAction()).isEqualTo(CoinAction.SPENT);
Expand Down Expand Up @@ -403,7 +403,7 @@ private Utxo newUtxoIn() {
.slot(22L)
.txHash("txHash1")
.outputIndex(44)
.amounts(List.of(newAmt()))
.amounts(List.of(newTokenAmt(), newAdaAmt()))
.dataHash("in_dataHash1")
.inlineDatum("in_inlineDatum1")
.isCollateralReturn(true)
Expand All @@ -424,7 +424,7 @@ private Utxo newUtxoOut() {
.slot(22L)
.txHash("txHash1")
.outputIndex(44)
.amounts(List.of(newAmt()))
.amounts(List.of(newTokenAmt(), newAdaAmt()))
.dataHash("out_dataHash1")
.inlineDatum("out_inlineDatum1")
.isCollateralReturn(true)
Expand All @@ -438,7 +438,15 @@ private Utxo newUtxoOut() {
.build();
}

private static Amt newAmt() {
private static Amt newAdaAmt() {
return Amt.builder()
.assetName(Constants.LOVELACE)
.quantity(BigInteger.TEN)
.unit(Constants.LOVELACE)
.build();
}

private static Amt newTokenAmt() {
return Amt.builder()
.assetName("assetName1")
.policyId("policyId1")
Expand Down

0 comments on commit e61725a

Please sign in to comment.