Skip to content

Commit

Permalink
server: fix negative fees
Browse files Browse the repository at this point in the history
  • Loading branch information
iBlackShadow authored Mar 26, 2021
1 parent c0b2f01 commit 77bc6b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/app/com/xsn/explorer/models/TransactionInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ case class TransactionInfo(
received: BigDecimal,
height: Height
) {
def fee: BigDecimal = sent - received
def fee: BigDecimal = (sent - received).max(BigDecimal(0))
}

object TransactionInfo {
Expand Down
38 changes: 38 additions & 0 deletions server/test/com/xsn/explorer/models/TransactionInfoSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.xsn.explorer.models

import com.xsn.explorer.helpers.DataGenerator
import com.xsn.explorer.models.values._
import org.scalatest.matchers.must.Matchers
import org.scalatest.wordspec.AnyWordSpec

class TransactionInfoSpec extends AnyWordSpec with Matchers with DataGenerator {
"fee" should {
"calculate the paid fee" in {
val transactionInfo = TransactionInfo(
randomTransactionId,
randomBlockhash,
0L,
Size(0),
BigDecimal(125),
BigDecimal(25),
Height(0)
)

transactionInfo.fee mustBe BigDecimal(100)
}

"return 0 when result would be negative" in {
val transactionInfo = TransactionInfo(
randomTransactionId,
randomBlockhash,
0L,
Size(0),
BigDecimal(25),
BigDecimal(125),
Height(0)
)

transactionInfo.fee mustBe BigDecimal(0)
}
}
}

0 comments on commit 77bc6b4

Please sign in to comment.