Skip to content

Commit e61725a

Browse files
authored
fix: negating value if it's an input operation (#124)
* fix: negating value if it's an input operation * fix: adjusted tests
1 parent 63dc3c3 commit e61725a

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

api/src/main/java/org/cardanofoundation/rosetta/api/block/mapper/AbstractToOperation.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,23 @@ protected Amount updateDepositAmount(BigInteger deposit) {
7979

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

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

94101
}

api/src/main/java/org/cardanofoundation/rosetta/api/block/mapper/InputToOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Operation toDto(Utxo model, OperationStatus status, int index) {
2828
mp.map(f -> Constants.INPUT, Operation::setType);
2929
mp.<CoinAction>map(f -> CoinAction.SPENT, (d, v) -> d.getCoinChange().setCoinAction(v));
3030
mp.map(f-> mapToOperationMetaData(true, model.getAmounts()), Operation::setMetadata);
31-
mapOthers(model, status, index, mp);
31+
mapOthers(model, status, index, mp, true);
3232
})
3333
.map(model);
3434
}

api/src/main/java/org/cardanofoundation/rosetta/api/block/mapper/OutputToOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public Operation toDto(Utxo model, OperationStatus status, int index) {
2828
mp.map(f -> Constants.OUTPUT, Operation::setType);
2929
mp.<CoinAction>map(f -> CoinAction.CREATED, (d, v) -> d.getCoinChange().setCoinAction(v));
3030
mp.map(f-> mapToOperationMetaData(false, model.getAmounts()), Operation::setMetadata);
31-
mapOthers(model, status, index, mp);
31+
mapOthers(model, status, index, mp, false);
3232
})
3333
.map(model);
3434
}

api/src/test/java/org/cardanofoundation/rosetta/api/block/mapper/BlockTxToRosettaTransactionTest.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ void toDto_Test_getInputsAsOperations() {
316316
assertThat(opInto.getStatus()).isEqualTo("success");
317317
assertThat(opInto.getOperationIdentifier().getIndex()).isEqualTo(0); //index in array
318318
assertThat(opInto.getAccount().getAddress()).isEqualTo(firstFrom.getOwnerAddr());
319-
assertThat(opInto.getAmount()).isEqualTo(amountActual("10"));
319+
assertThat(opInto.getAmount()).isEqualTo(amountActual("-10"));
320320

321321
CoinChange coinChange = opInto.getCoinChange();
322322
assertThat(coinChange.getCoinAction()).isEqualTo(CoinAction.SPENT);
@@ -403,7 +403,7 @@ private Utxo newUtxoIn() {
403403
.slot(22L)
404404
.txHash("txHash1")
405405
.outputIndex(44)
406-
.amounts(List.of(newAmt()))
406+
.amounts(List.of(newTokenAmt(), newAdaAmt()))
407407
.dataHash("in_dataHash1")
408408
.inlineDatum("in_inlineDatum1")
409409
.isCollateralReturn(true)
@@ -424,7 +424,7 @@ private Utxo newUtxoOut() {
424424
.slot(22L)
425425
.txHash("txHash1")
426426
.outputIndex(44)
427-
.amounts(List.of(newAmt()))
427+
.amounts(List.of(newTokenAmt(), newAdaAmt()))
428428
.dataHash("out_dataHash1")
429429
.inlineDatum("out_inlineDatum1")
430430
.isCollateralReturn(true)
@@ -438,7 +438,15 @@ private Utxo newUtxoOut() {
438438
.build();
439439
}
440440

441-
private static Amt newAmt() {
441+
private static Amt newAdaAmt() {
442+
return Amt.builder()
443+
.assetName(Constants.LOVELACE)
444+
.quantity(BigInteger.TEN)
445+
.unit(Constants.LOVELACE)
446+
.build();
447+
}
448+
449+
private static Amt newTokenAmt() {
442450
return Amt.builder()
443451
.assetName("assetName1")
444452
.policyId("policyId1")

0 commit comments

Comments
 (0)