Skip to content

Commit fafbced

Browse files
committed
Add type aliases to improve clarity
1 parent 184dd25 commit fafbced

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

cardano-wasm/src-lib/Cardano/Wasm/Api/Certificate/StakeCertificate.hs

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,41 @@ import Data.ByteString.Base16 qualified as Base16
3838
import Data.Text qualified as Text
3939
import Data.Text.Encoding qualified as Text
4040

41+
-- * Type aliases for clarity
42+
43+
-- | A stake key hash represented as a base16-encoded string.
44+
type StakeKeyHashBase16 = String
45+
46+
-- | A pool ID represented as a base16-encoded string.
47+
type PoolIdBase16 = String
48+
49+
-- | Deposit amount in lovelace.
50+
type DepositLovelace = Integer
51+
52+
-- | Certificate serialized to CBOR as a base16-encoded string.
53+
type CertificateCBORBase16 = String
54+
55+
-- * Stake Certificate function implementation
56+
4157
-- | Make a certificate that delegates a stake address to a stake pool in the current era.
42-
makeStakeAddressStakeDelegationCertificateImpl :: MonadThrow m => String -> String -> m String
58+
makeStakeAddressStakeDelegationCertificateImpl
59+
:: MonadThrow m => StakeKeyHashBase16 -> PoolIdBase16 -> m CertificateCBORBase16
4360
makeStakeAddressStakeDelegationCertificateImpl skHashStr poolIdStr = do
4461
stakeCertHash <- readHash skHashStr
4562
poolId <- readPoolId poolIdStr
4663
makeStakeAddressStakeDelegationCertificate currentEra stakeCertHash poolId
4764

4865
-- | Make a certificate that delegates a stake address to a stake pool in the current experimental era.
4966
makeStakeAddressStakeDelegationCertificateExperimentalEraImpl
50-
:: MonadThrow m => String -> String -> m String
67+
:: MonadThrow m => StakeKeyHashBase16 -> PoolIdBase16 -> m CertificateCBORBase16
5168
makeStakeAddressStakeDelegationCertificateExperimentalEraImpl skHashStr poolIdStr = do
5269
stakeCertHash <- readHash skHashStr
5370
poolId <- readPoolId poolIdStr
5471
era <- justOrError "No experimental era available" experimentalEra
5572
makeStakeAddressStakeDelegationCertificate era stakeCertHash poolId
5673

5774
makeStakeAddressStakeDelegationCertificate
58-
:: forall era m. MonadThrow m => Exp.Era era -> Hash StakeKey -> PoolId -> m String
75+
:: forall era m. MonadThrow m => Exp.Era era -> Hash StakeKey -> PoolId -> m CertificateCBORBase16
5976
makeStakeAddressStakeDelegationCertificate era stakeCertHash poolId =
6077
obtainCommonConstraints era $ do
6178
let cert :: Certificate (Exp.LedgerEra era) =
@@ -68,21 +85,22 @@ makeStakeAddressStakeDelegationCertificate era stakeCertHash poolId =
6885
return $ serialiseCertificateToCBOR era cert
6986

7087
-- | Make a stake address registration certificate in the current era.
71-
makeStakeAddressRegistrationCertificateImpl :: MonadThrow m => String -> Integer -> m String
88+
makeStakeAddressRegistrationCertificateImpl
89+
:: MonadThrow m => StakeKeyHashBase16 -> DepositLovelace -> m CertificateCBORBase16
7290
makeStakeAddressRegistrationCertificateImpl skHashStr deposit = do
7391
skHash <- readHash skHashStr
7492
makeStakeAddressRegistrationCertificateWrapper currentEra skHash deposit
7593

7694
--  | Make a stake address registration certificate in the current experimental era.
7795
makeStakeAddressRegistrationCertificateExperimentalEraImpl
78-
:: MonadThrow m => String -> Integer -> m String
96+
:: MonadThrow m => StakeKeyHashBase16 -> DepositLovelace -> m CertificateCBORBase16
7997
makeStakeAddressRegistrationCertificateExperimentalEraImpl skHashStr deposit = do
8098
skHash <- readHash skHashStr
8199
era <- justOrError "No experimental era available" experimentalEra
82100
makeStakeAddressRegistrationCertificateWrapper era skHash deposit
83101

84102
makeStakeAddressRegistrationCertificateWrapper
85-
:: forall era m. MonadThrow m => Era era -> Hash StakeKey -> Integer -> m String
103+
:: forall era m. MonadThrow m => Era era -> Hash StakeKey -> DepositLovelace -> m CertificateCBORBase16
86104
makeStakeAddressRegistrationCertificateWrapper era skHash deposit =
87105
obtainCommonConstraints era $ do
88106
let cert :: Certificate (Exp.LedgerEra era) =
@@ -92,21 +110,22 @@ makeStakeAddressRegistrationCertificateWrapper era skHash deposit =
92110
return $ serialiseCertificateToCBOR era cert
93111

94112
-- | Make a stake address unregistration certificate in the current era.
95-
makeStakeAddressUnregistrationCertificateImpl :: MonadThrow m => String -> Integer -> m String
113+
makeStakeAddressUnregistrationCertificateImpl
114+
:: MonadThrow m => StakeKeyHashBase16 -> DepositLovelace -> m CertificateCBORBase16
96115
makeStakeAddressUnregistrationCertificateImpl skHashStr deposit = do
97116
skHash <- readHash skHashStr
98117
makeStakeAddressUnregistrationCertificateWrapper currentEra skHash deposit
99118

100119
-- | Make a stake address unregistration certificate in the current experimental era.
101120
makeStakeAddressUnregistrationCertificateExperimentalEraImpl
102-
:: MonadThrow m => String -> Integer -> m String
121+
:: MonadThrow m => StakeKeyHashBase16 -> DepositLovelace -> m CertificateCBORBase16
103122
makeStakeAddressUnregistrationCertificateExperimentalEraImpl skHashStr deposit = do
104123
skHash <- readHash skHashStr
105124
era <- justOrError "No experimental era available" experimentalEra
106125
makeStakeAddressUnregistrationCertificateWrapper era skHash deposit
107126

108127
makeStakeAddressUnregistrationCertificateWrapper
109-
:: forall era m. MonadThrow m => Era era -> Hash StakeKey -> Integer -> m String
128+
:: forall era m. MonadThrow m => Era era -> Hash StakeKey -> DepositLovelace -> m CertificateCBORBase16
110129
makeStakeAddressUnregistrationCertificateWrapper era skHash deposit =
111130
obtainCommonConstraints era $ do
112131
let cert :: Certificate (Exp.LedgerEra era) =
@@ -115,7 +134,8 @@ makeStakeAddressUnregistrationCertificateWrapper era skHash deposit =
115134
(Coin deposit)
116135
return $ serialiseCertificateToCBOR era cert
117136

118-
serialiseCertificateToCBOR :: Exp.Era era -> Certificate (Exp.LedgerEra era) -> String
137+
serialiseCertificateToCBOR
138+
:: Exp.Era era -> Certificate (Exp.LedgerEra era) -> CertificateCBORBase16
119139
serialiseCertificateToCBOR era cert =
120140
obtainCommonConstraints era $ do
121141
Text.unpack $
@@ -124,8 +144,8 @@ serialiseCertificateToCBOR era cert =
124144
serialiseToCBOR
125145
cert
126146

127-
readHash :: MonadThrow m => String -> m (Hash StakeKey)
147+
readHash :: MonadThrow m => StakeKeyHashBase16 -> m (Hash StakeKey)
128148
readHash = rightOrError . Api.deserialiseFromRawBytesHex . Text.encodeUtf8 . Text.pack
129149

130-
readPoolId :: MonadThrow m => String -> m PoolId
150+
readPoolId :: MonadThrow m => PoolIdBase16 -> m PoolId
131151
readPoolId = rightOrError . Api.deserialiseFromRawBytesHex . Text.encodeUtf8 . Text.pack

0 commit comments

Comments
 (0)