Skip to content

Commit 0e1c6cc

Browse files
committed
Fix lovelaceToAda function
The old version of this function had type `Micro -> Ada`, but that type allowed its use in `lovelaceToAda . MkFixed . fromIntegral` AND as `lovelaceToAda . fromIntegral` with the former giving INCORRECT results. By switching the type to `Integer -> Ada` it is no longer possible to use this function incorrectly. Related: #2031
1 parent 260f422 commit 0e1c6cc

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

cardano-db/src/Cardano/Db/Statement/Function/Query.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
module Cardano.Db.Statement.Function.Query where
1313

1414
import Cardano.Prelude (HasCallStack, Proxy (..), Word64, listToMaybe)
15-
import Data.Fixed (Fixed (..))
1615
import Data.Functor.Contravariant (Contravariant (..))
1716
import qualified Data.List.NonEmpty as NE
1817
import qualified Data.Text as Text
@@ -265,5 +264,5 @@ adaSumDecoder :: HsqlD.Row Ada
265264
adaSumDecoder = do
266265
amount <- HsqlD.column (HsqlD.nullable HsqlD.int8)
267266
case amount of
268-
Just value -> pure $ lovelaceToAda (MkFixed $ fromIntegral value)
267+
Just value -> pure $ lovelaceToAda (fromIntegral value)
269268
Nothing -> pure $ Ada 0

cardano-db/src/Cardano/Db/Types.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,16 +532,16 @@ word128Encoder = fromInteger . toInteger >$< HsqlE.numeric
532532
word128Decoder :: HsqlD.Value Word128
533533
word128Decoder = fromInteger . fromIntegral . coefficient <$> HsqlD.numeric
534534

535-
lovelaceToAda :: Micro -> Ada
535+
lovelaceToAda :: Integer -> Ada
536536
lovelaceToAda ll =
537-
Ada (ll / 1000000)
537+
Ada (fromIntegral ll / 1000000)
538538

539539
renderAda :: Ada -> Text
540540
renderAda (Ada a) = Text.pack (show a)
541541

542542
scientificToAda :: Scientific -> Ada
543543
scientificToAda s =
544-
word64ToAda $ floor (s * 1000000)
544+
lovelaceToAda $ floor (s * 1000000)
545545

546546
word64ToAda :: Word64 -> Ada
547547
word64ToAda w =

0 commit comments

Comments
 (0)