Skip to content

Commit

Permalink
Sort era summaries for getCurrentEra
Browse files Browse the repository at this point in the history
  • Loading branch information
kozross committed Sep 28, 2023
1 parent fda55a1 commit 9b3e68e
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/Contract/Time.purs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import Ctl.Internal.Types.Interval
) as Interval
import Ctl.Internal.Types.SystemStart (SystemStart)
import Ctl.Internal.Types.SystemStart (SystemStart(SystemStart)) as ExportSystemStart
import Data.Array as Array
import Data.BigInt as BigInt
import Data.Foldable (find)
import Data.Maybe (Maybe(Just, Nothing))
Expand All @@ -119,15 +120,18 @@ getCurrentEra = do
currentSlot <- getCurrentSlot
logInfo' $ show eraSummaries
logInfo' $ show currentSlot
-- Assumes that eras are sorted: this may not be stable in the future.
liftContractM "Could not find era summary"
$ find (slotInRange currentSlot)
$ Array.sortBy (comparing getStartSlot)
$ unwrap eraSummaries
where
getStartSlot :: ExportEraSummaries.EraSummary -> Slot
getStartSlot = unwrap >>> _.start >>> unwrap >>> _.slot

slotInRange :: Slot -> ExportEraSummaries.EraSummary -> Boolean
slotInRange currentSlot era =
let
eraStartSlot = era # unwrap # _.start # unwrap # _.slot
eraStartSlot = getStartSlot era
startNotAfterUs = eraStartSlot <= currentSlot
in
case era # unwrap # _.end of
Expand Down

0 comments on commit 9b3e68e

Please sign in to comment.