Skip to content

Commit ddf0651

Browse files
committed
server: Updated coinsStaking value calculation
1 parent ed995e0 commit ddf0651

7 files changed

Lines changed: 36 additions & 19 deletions

File tree

server/app/com/xsn/explorer/data/StatisticsDataHandler.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ trait StatisticsDataHandler[F[_]] {
1212
def getRewardsSummary(numberOfBlocks: Int): F[BlockRewardsSummary]
1313

1414
def getCoinsInAddress(address: String): F[BigDecimal]
15+
16+
def getTposAddresses(merchantAddress: String): F[List[String]]
1517
}
1618

1719
trait StatisticsBlockingDataHandler extends StatisticsDataHandler[ApplicationResult]

server/app/com/xsn/explorer/data/anorm/StatisticsPostgresDataHandler.scala

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.xsn.explorer.data.anorm
33
import javax.inject.Inject
44
import com.alexitc.playsonify.core.ApplicationResult
55
import com.xsn.explorer.data.StatisticsBlockingDataHandler
6-
import com.xsn.explorer.data.anorm.dao.{StatisticsPostgresDAO, BalancePostgresDAO}
6+
import com.xsn.explorer.data.anorm.dao.{StatisticsPostgresDAO, BalancePostgresDAO, TPoSContractDAO}
77
import com.xsn.explorer.models.{BlockRewardsSummary, Statistics}
88
import org.scalactic.Good
99
import play.api.db.Database
@@ -12,6 +12,7 @@ import com.xsn.explorer.models.values.Address
1212
class StatisticsPostgresDataHandler @Inject()(
1313
override val database: Database,
1414
statisticsDAO: StatisticsPostgresDAO,
15+
tposContractDAO: TPoSContractDAO,
1516
balanceDAO: BalancePostgresDAO
1617
)
1718
extends StatisticsBlockingDataHandler
@@ -28,20 +29,12 @@ class StatisticsPostgresDataHandler @Inject()(
2829
}
2930

3031
override def getCoinsInAddress(address: String): ApplicationResult[BigDecimal] = withConnection {
31-
implicit conn => {
32-
val addressObj = Address.from(address)
33-
addressObj match {
34-
case Some(addr) => {
35-
val balanceObj = balanceDAO.getBy(addr)
36-
balanceObj match {
37-
case Some(balance) => Good(balance.available)
38-
case None => Good(0)
39-
}
40-
}
32+
implicit conn =>
33+
Good(balanceDAO.getBy(Address.from(address).get).get.available)
34+
}
4135

42-
case None => Good(0)
43-
}
44-
45-
}
36+
override def getTposAddresses(merchantAddress: String): ApplicationResult[List[String]] = withConnection {
37+
implicit conn =>
38+
Good(tposContractDAO.getTposAddresses(merchantAddress))
4639
}
4740
}

server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,17 @@ class TPoSContractDAO {
121121
)
122122
.as(parseTPoSContract.*)
123123
}
124+
125+
def getTposAddresses(merchantAddress: String)(implicit conn: Connection): List[String] = {
126+
SQL(
127+
"""
128+
|SELECT owner
129+
|FROM tpos_contracts
130+
|WHERE merchant={merchantAddress}
131+
|AND state='ACTIVE'
132+
""".stripMargin
133+
).on(
134+
'merchantAddress -> merchantAddress
135+
).as(SqlParser.scalar[String].*)
136+
}
124137
}

server/app/com/xsn/explorer/data/async/StatisticsFutureDataHandler.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ class StatisticsFutureDataHandler @Inject()(
3535
}
3636
}
3737

38+
override def getTposAddresses(merchantAddress: String): FutureApplicationResult[List[String]] =
39+
retryableFutureDataHandler.retrying {
40+
Future {
41+
blockingDataHandler.getTposAddresses(merchantAddress)
42+
}
43+
}
44+
3845
}

server/app/com/xsn/explorer/services/StatisticsService.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ class StatisticsService @Inject()(
6464
val result = for {
6565
tposNodesList <- tposNodes.map(x => Good(x)).toFutureOr
6666

67-
coinsStaking <- tposNodesList.map(t => t.payee.string).map(statisticsFutureDataHandler.getCoinsInAddress).toFutureOr
67+
tposAddressList <- tposNodesList.map(t => t.payee.string).map(statisticsFutureDataHandler.getTposAddresses).toFutureOr
68+
coinsStaking <- tposAddressList.flatten.map(statisticsFutureDataHandler.getCoinsInAddress).toFutureOr
6869
coinsStakingSum = coinsStaking.sum
6970
} yield coinsStakingSum
7071

server/test/com/xsn/explorer/data/StatisticsPostgresDataHandlerSpec.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.xsn.explorer.data
22

33
import com.alexitc.playsonify.sql.FieldOrderingSQLInterpreter
4-
import com.xsn.explorer.data.anorm.dao.{BalancePostgresDAO, StatisticsPostgresDAO}
4+
import com.xsn.explorer.data.anorm.dao.{BalancePostgresDAO, StatisticsPostgresDAO, TPoSContractDAO}
55
import com.xsn.explorer.data.anorm.{BalancePostgresDataHandler, StatisticsPostgresDataHandler}
66
import com.xsn.explorer.data.common.PostgresDataHandlerSpec
77
import com.xsn.explorer.gcs.{GolombCodedSet, UnsignedByte}
@@ -13,13 +13,12 @@ import com.xsn.explorer.models.persisted.Balance
1313
import com.xsn.explorer.models.values.{Address, Height}
1414
import org.scalactic.Good
1515
import org.scalatest.BeforeAndAfter
16-
import com.xsn.explorer.helpers.DataHelper
1716

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

2120
val secondsInOneDay = 24 * 60 * 60
22-
lazy val dataHandler = new StatisticsPostgresDataHandler(database, new StatisticsPostgresDAO, new BalancePostgresDAO(new FieldOrderingSQLInterpreter))
21+
lazy val dataHandler = new StatisticsPostgresDataHandler(database, new StatisticsPostgresDAO, new TPoSContractDAO(), new BalancePostgresDAO(new FieldOrderingSQLInterpreter))
2322
lazy val ledgerDataHandler = createLedgerDataHandler(database)
2423
lazy val balanceDataHandler =
2524
new BalancePostgresDataHandler(database, new BalancePostgresDAO(new FieldOrderingSQLInterpreter))

server/test/controllers/StatisticsControllerSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ class StatisticsControllerSpec extends MyAPISpec with BeforeAndAfterAll {
5858

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

61+
override def getTposAddresses(address: String): ApplicationResult[List[String]] = Good(List("123"))
62+
6163
}
6264

6365
val xsnService = mock[XSNService]

0 commit comments

Comments
 (0)