Skip to content

Commit

Permalink
server: Updated coinsStaking value calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
vhurryharry committed Sep 21, 2020
1 parent ed995e0 commit ddf0651
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 19 deletions.
2 changes: 2 additions & 0 deletions server/app/com/xsn/explorer/data/StatisticsDataHandler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ trait StatisticsDataHandler[F[_]] {
def getRewardsSummary(numberOfBlocks: Int): F[BlockRewardsSummary]

def getCoinsInAddress(address: String): F[BigDecimal]

def getTposAddresses(merchantAddress: String): F[List[String]]
}

trait StatisticsBlockingDataHandler extends StatisticsDataHandler[ApplicationResult]
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.xsn.explorer.data.anorm
import javax.inject.Inject
import com.alexitc.playsonify.core.ApplicationResult
import com.xsn.explorer.data.StatisticsBlockingDataHandler
import com.xsn.explorer.data.anorm.dao.{StatisticsPostgresDAO, BalancePostgresDAO}
import com.xsn.explorer.data.anorm.dao.{StatisticsPostgresDAO, BalancePostgresDAO, TPoSContractDAO}
import com.xsn.explorer.models.{BlockRewardsSummary, Statistics}
import org.scalactic.Good
import play.api.db.Database
Expand All @@ -12,6 +12,7 @@ import com.xsn.explorer.models.values.Address
class StatisticsPostgresDataHandler @Inject()(
override val database: Database,
statisticsDAO: StatisticsPostgresDAO,
tposContractDAO: TPoSContractDAO,
balanceDAO: BalancePostgresDAO
)
extends StatisticsBlockingDataHandler
Expand All @@ -28,20 +29,12 @@ class StatisticsPostgresDataHandler @Inject()(
}

override def getCoinsInAddress(address: String): ApplicationResult[BigDecimal] = withConnection {
implicit conn => {
val addressObj = Address.from(address)
addressObj match {
case Some(addr) => {
val balanceObj = balanceDAO.getBy(addr)
balanceObj match {
case Some(balance) => Good(balance.available)
case None => Good(0)
}
}
implicit conn =>
Good(balanceDAO.getBy(Address.from(address).get).get.available)
}

case None => Good(0)
}

}
override def getTposAddresses(merchantAddress: String): ApplicationResult[List[String]] = withConnection {
implicit conn =>
Good(tposContractDAO.getTposAddresses(merchantAddress))
}
}
13 changes: 13 additions & 0 deletions server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,17 @@ class TPoSContractDAO {
)
.as(parseTPoSContract.*)
}

def getTposAddresses(merchantAddress: String)(implicit conn: Connection): List[String] = {
SQL(
"""
|SELECT owner
|FROM tpos_contracts
|WHERE merchant={merchantAddress}
|AND state='ACTIVE'
""".stripMargin
).on(
'merchantAddress -> merchantAddress
).as(SqlParser.scalar[String].*)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ class StatisticsFutureDataHandler @Inject()(
}
}

override def getTposAddresses(merchantAddress: String): FutureApplicationResult[List[String]] =
retryableFutureDataHandler.retrying {
Future {
blockingDataHandler.getTposAddresses(merchantAddress)
}
}

}
3 changes: 2 additions & 1 deletion server/app/com/xsn/explorer/services/StatisticsService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class StatisticsService @Inject()(
val result = for {
tposNodesList <- tposNodes.map(x => Good(x)).toFutureOr

coinsStaking <- tposNodesList.map(t => t.payee.string).map(statisticsFutureDataHandler.getCoinsInAddress).toFutureOr
tposAddressList <- tposNodesList.map(t => t.payee.string).map(statisticsFutureDataHandler.getTposAddresses).toFutureOr
coinsStaking <- tposAddressList.flatten.map(statisticsFutureDataHandler.getCoinsInAddress).toFutureOr
coinsStakingSum = coinsStaking.sum
} yield coinsStakingSum

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.xsn.explorer.data

import com.alexitc.playsonify.sql.FieldOrderingSQLInterpreter
import com.xsn.explorer.data.anorm.dao.{BalancePostgresDAO, StatisticsPostgresDAO}
import com.xsn.explorer.data.anorm.dao.{BalancePostgresDAO, StatisticsPostgresDAO, TPoSContractDAO}
import com.xsn.explorer.data.anorm.{BalancePostgresDataHandler, StatisticsPostgresDataHandler}
import com.xsn.explorer.data.common.PostgresDataHandlerSpec
import com.xsn.explorer.gcs.{GolombCodedSet, UnsignedByte}
Expand All @@ -13,13 +13,12 @@ import com.xsn.explorer.models.persisted.Balance
import com.xsn.explorer.models.values.{Address, Height}
import org.scalactic.Good
import org.scalatest.BeforeAndAfter
import com.xsn.explorer.helpers.DataHelper

@com.github.ghik.silencer.silent
class StatisticsPostgresDataHandlerSpec extends PostgresDataHandlerSpec with BeforeAndAfter {

val secondsInOneDay = 24 * 60 * 60
lazy val dataHandler = new StatisticsPostgresDataHandler(database, new StatisticsPostgresDAO, new BalancePostgresDAO(new FieldOrderingSQLInterpreter))
lazy val dataHandler = new StatisticsPostgresDataHandler(database, new StatisticsPostgresDAO, new TPoSContractDAO(), new BalancePostgresDAO(new FieldOrderingSQLInterpreter))
lazy val ledgerDataHandler = createLedgerDataHandler(database)
lazy val balanceDataHandler =
new BalancePostgresDataHandler(database, new BalancePostgresDAO(new FieldOrderingSQLInterpreter))
Expand Down
2 changes: 2 additions & 0 deletions server/test/controllers/StatisticsControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ class StatisticsControllerSpec extends MyAPISpec with BeforeAndAfterAll {

override def getCoinsInAddress(address: String): ApplicationResult[BigDecimal] = Good(100)

override def getTposAddresses(address: String): ApplicationResult[List[String]] = Good(List("123"))

}

val xsnService = mock[XSNService]
Expand Down

0 comments on commit ddf0651

Please sign in to comment.