Skip to content

Commit

Permalink
fix: RA-84 Add related_ops
Browse files Browse the repository at this point in the history
  • Loading branch information
shleger committed May 3, 2024
1 parent 61e922e commit e0e7723
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.cardanofoundation.rosetta.api.block.mapper;

import java.util.List;
import java.util.stream.Collectors;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

Expand All @@ -9,6 +10,7 @@
import org.apache.commons.lang3.mutable.MutableInt;
import org.modelmapper.ModelMapper;
import org.openapitools.client.model.Operation;
import org.openapitools.client.model.OperationIdentifier;
import org.openapitools.client.model.OperationStatus;
import org.openapitools.client.model.Transaction;

Expand Down Expand Up @@ -55,16 +57,30 @@ public Transaction toDto(BlockTx model) {
.setPostConverter(ctx -> {
MutableInt ix = new MutableInt(0);
@NotNull @Valid List<Operation> destOp = ctx.getDestination().getOperations();
destOp.addAll(inputToOperation.convert(model.getInputs(), status, ix));
List<Operation> inpOps = inputToOperation.convert(model.getInputs(), status, ix);
destOp.addAll(inpOps);
destOp.addAll(withdrawalToOperation.convert(model.getWithdrawals(), status, ix));
destOp.addAll(stakeToOperation.convert(model.getStakeRegistrations(), status, ix));
destOp.addAll(delegationToOperation.convert(model.getDelegations(), status, ix));
destOp.addAll(poolRegistrationToOperation.convert(model.getPoolRegistrations(), status, ix));
destOp.addAll(poolRetirementToOperation.convert(model.getPoolRetirements(), status, ix));
destOp.addAll(outputToOperation.convert(model.getOutputs(), status, ix));

List<Operation> outOps = outputToOperation.convert(model.getOutputs(), status, ix);
outOps.forEach(op -> op.setRelatedOperations(getOperationIndexes(inpOps)));

destOp.addAll(outOps);
return ctx.getDestination();
})
.map(model);

}


public static List<OperationIdentifier> getOperationIndexes(List<Operation> operations) {
return operations.stream()
.map(operation -> OperationIdentifier
.builder()
.index(operation.getOperationIdentifier().getIndex()).build())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
import static org.cardanofoundation.rosetta.common.util.Constants.ADA;
import static org.cardanofoundation.rosetta.common.util.Constants.ADA_DECIMALS;

class BlockTxToRosettaTransactionTest extends BaseMapperTest {
class
BlockTxToRosettaTransactionTest extends BaseMapperTest {

@Autowired
private BlockTxToRosettaTransaction my;
Expand Down Expand Up @@ -276,6 +277,10 @@ void toDto_Test_getOutputsAsOperations() {
assertThat(opInto.getAccount().getAddress()).isEqualTo(firstFrom.getOwnerAddr());
assertThat(opInto.getAmount()).isEqualTo(amountActual("10"));

assertThat(opInto.getRelatedOperations()).isNotNull();
assertThat(opInto.getRelatedOperations().size()).isEqualTo(1);
assertThat(opInto.getRelatedOperations().getFirst().getIndex()).isEqualTo(0);

CoinChange coinChange = opInto.getCoinChange();
assertThat(coinChange.getCoinAction()).isEqualTo(CoinAction.CREATED);
assertThat(coinChange.getCoinIdentifier().getIdentifier())
Expand Down

0 comments on commit e0e7723

Please sign in to comment.