Skip to content

Commit

Permalink
server: Refactor TransactionService to remove the transaction transfo…
Browse files Browse the repository at this point in the history
…rmer

The logic was moved to the transformers package.
  • Loading branch information
AlexITC committed Feb 18, 2019
1 parent b8b44ea commit fc0696a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
25 changes: 24 additions & 1 deletion server/app/com/xsn/explorer/models/transformers/package.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.xsn.explorer.models

import com.xsn.explorer.models.persisted.Block
import com.xsn.explorer.models.persisted.{Block, Transaction}
import io.scalaland.chimney.dsl._

/**
Expand All @@ -15,4 +15,27 @@ package object transformers {
.withFieldConst(_.extractionMethod, Block.ExtractionMethod.ProofOfWork) // TODO: Get proper method
.transform
}

def toLightWalletTransactionInput(input: Transaction.Input): LightWalletTransaction.Input = {
input
.into[LightWalletTransaction.Input]
.withFieldRenamed(_.fromOutputIndex, _.index)
.withFieldRenamed(_.fromTxid, _.txid)
.transform
}

def toLightWalletTransactionOutput(output: Transaction.Output): LightWalletTransaction.Output = {
output.into[LightWalletTransaction.Output].transform
}

def toLightWalletTransaction(tx: Transaction.HasIO): LightWalletTransaction = {
val inputs = tx.inputs.map(toLightWalletTransactionInput)
val outputs = tx.outputs.map(toLightWalletTransactionOutput)

tx.transaction
.into[LightWalletTransaction]
.withFieldConst(_.inputs, inputs)
.withFieldConst(_.outputs, outputs)
.transform
}
}
22 changes: 2 additions & 20 deletions server/app/com/xsn/explorer/services/TransactionService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.alexitc.playsonify.validators.PaginatedQueryValidator
import com.xsn.explorer.data.async.TransactionFutureDataHandler
import com.xsn.explorer.errors._
import com.xsn.explorer.models._
import com.xsn.explorer.models.transformers._
import com.xsn.explorer.models.values._
import com.xsn.explorer.parsers.TransactionOrderingParser
import io.scalaland.chimney.dsl._
import javax.inject.Inject
import org.scalactic._
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -71,25 +71,7 @@ class TransactionService @Inject() (

transactions <- transactionFutureDataHandler.getBy(address, limit, lastSeenTxid, orderingCondition).toFutureOr
} yield {
val lightTxs = transactions.map { tx =>
val inputs = tx.inputs.map { input =>
input
.into[LightWalletTransaction.Input]
.withFieldRenamed(_.fromOutputIndex, _.index)
.withFieldRenamed(_.fromTxid, _.txid)
.transform
}

val outputs = tx.outputs.map { output =>
output.into[LightWalletTransaction.Output].transform
}

tx.transaction
.into[LightWalletTransaction]
.withFieldConst(_.inputs, inputs)
.withFieldConst(_.outputs, outputs)
.transform
}
val lightTxs = transactions.map(toLightWalletTransaction)

WrappedResult(lightTxs)
}
Expand Down

0 comments on commit fc0696a

Please sign in to comment.