Skip to content

Commit

Permalink
Add field persistence for Slot primitive type.
Browse files Browse the repository at this point in the history
  • Loading branch information
paolino committed Mar 16, 2023
1 parent 4882f5b commit 00e41be
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/wallet/src/Cardano/Wallet/DB/Sqlite/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ import Cardano.Wallet.Primitive.Types
( EpochNo (..)
, FeePolicy
, PoolMetadataSource (..)
, Slot
, WalletId (..)
, WithOrigin (..)
, isValidEpochNo
, unsafeEpochNo
, unsafeToPMS
Expand Down Expand Up @@ -139,6 +141,8 @@ import qualified Cardano.Wallet.Primitive.Types.Coin as Coin
import qualified Data.Aeson as Aeson
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BL
import Data.Int
( Int64 )
import qualified Data.Text as T
import qualified Data.Text.Encoding as T

Expand Down Expand Up @@ -748,3 +752,16 @@ instance PersistField BlockHeight where

instance PersistFieldSql BlockHeight where
sqlType _ = sqlType (Proxy @Word32)

instance PersistField Slot where
toPersistValue Origin = toPersistValue ((-1) :: Int64)
toPersistValue (At s) = toPersistValue (fromIntegral $ unSlotNo s :: Int64)
fromPersistValue v = case fromPersistValue v of
Right (PersistInt64 x)
| x < (-1) -> Left "Slot must be positive or -1"
| x == (-1) -> Right Origin
| otherwise -> Right . At . SlotNo . fromIntegral $ x
_ -> Left "Slot must be an Int64"

instance PersistFieldSql Slot where
sqlType _ = sqlType (Proxy @Int64)

0 comments on commit 00e41be

Please sign in to comment.