Skip to content

Commit e2b52b4

Browse files
committed
Use Delegatee from ledger in StakceCertificateObject
1 parent 0e9d62c commit e2b52b4

File tree

1 file changed

+25
-33
lines changed

1 file changed

+25
-33
lines changed

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

Lines changed: 25 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,26 @@ data StakeCertificateObject
7070
= forall era. StakeCertificateObject
7171
{ era :: !(Exp.Era era)
7272
, stakeCredential :: !(Hash StakeKey) -- ToDo: Generalize to support scripts as well
73-
, deposit :: !(Maybe Coin)
73+
, mDeposit :: !(Maybe Coin)
7474
, action :: !StakeCertificateAction
75-
, delegateStake :: Maybe PoolId
75+
, mDelegatee :: Maybe Delegatee
7676
-- ToDo: Add DRep delegation
7777
}
7878

7979
deriving instance Show StakeCertificateObject
8080

8181
instance ToJSON StakeCertificateObject where
82-
toJSON (StakeCertificateObject{era, stakeCredential, deposit, action, delegateStake}) =
82+
toJSON (StakeCertificateObject{era, stakeCredential, mDeposit, action, mDelegatee}) =
8383
obtainCommonConstraints era $
8484
Aeson.object
8585
[ "era" .= Exp.Some era
8686
, "stakeCredential" .= Text.decodeUtf8 (Api.serialiseToRawBytesHex stakeCredential)
87-
, "deposit" .= deposit
87+
, "deposit" .= mDeposit
8888
, "action" .= case action of
8989
RegisterStake -> Aeson.String "RegisterStake"
9090
UnregisterStake -> Aeson.String "UnregisterStake"
9191
DelegateOnly -> Aeson.String "DelegateOnly"
92-
, "delegateStake" .= fmap (Text.decodeUtf8 . Api.serialiseToRawBytesHex) delegateStake
92+
, "delegateStake" .= mDelegatee
9393
]
9494

9595
instance FromJSON StakeCertificateObject where
@@ -100,31 +100,23 @@ instance FromJSON StakeCertificateObject where
100100
toMonadFail $
101101
rightOrError $
102102
Api.deserialiseFromRawBytesHex (Text.encodeUtf8 skHashText)
103-
deposit :: Maybe Coin <- o .:? "deposit"
103+
mDeposit :: Maybe Coin <- o .:? "deposit"
104104
actionStr :: Text <- o .: "action"
105105
action <-
106106
case actionStr of
107107
"RegisterStake" -> return RegisterStake
108108
"UnregisterStake" -> return UnregisterStake
109109
"DelegateOnly" -> return DelegateOnly
110110
_ -> toMonadFail $ throwError ("Invalid action for StakeCertificateObject: " ++ show actionStr)
111-
delegateStakeText :: Maybe Text <- o .:? "delegateStake"
112-
delegateStake :: Maybe PoolId <-
113-
traverse
114-
( toMonadFail
115-
. rightOrError
116-
. Api.deserialiseFromRawBytesHex
117-
. Text.encodeUtf8
118-
)
119-
delegateStakeText
111+
mDelegatee :: Maybe Delegatee <- o .:? "delegateStake"
120112
obtainCommonConstraints era $
121113
return $
122114
StakeCertificateObject
123115
{ era
124116
, stakeCredential
125-
, deposit
117+
, mDeposit
126118
, action
127-
, delegateStake
119+
, mDelegatee
128120
}
129121

130122
-- | Creates an empty stake certificate object for the given stake key base16 hash.
@@ -137,9 +129,9 @@ createStakeKeyCertificateImpl skHashStr = do
137129
StakeCertificateObject
138130
{ era = ConwayEra
139131
, stakeCredential = skHash
140-
, deposit = Nothing
132+
, mDeposit = Nothing
141133
, action = DelegateOnly
142-
, delegateStake = Nothing
134+
, mDelegatee = Nothing
143135
}
144136
where
145137
readHash :: MonadThrow m => String -> m (Hash StakeKey)
@@ -166,35 +158,35 @@ asDelegateOnlyImpl certObj =
166158
-- depositted for unregistration certificates.
167159
withDepositImpl :: Coin -> StakeCertificateObject -> StakeCertificateObject
168160
withDepositImpl dep certObj =
169-
certObj{deposit = Just dep}
161+
certObj{mDeposit = Just dep}
170162

171163
-- | Resets the deposit for the stake certificate.
172164
withoutDepositImpl :: StakeCertificateObject -> StakeCertificateObject
173165
withoutDepositImpl certObj =
174-
certObj{deposit = Nothing}
166+
certObj{mDeposit = Nothing}
175167

176168
-- | Sets the pool to which the stake key will be delegated.
177169
withDelegationImpl :: PoolId -> StakeCertificateObject -> StakeCertificateObject
178170
withDelegationImpl poolId certObj =
179-
certObj{delegateStake = Just poolId}
171+
certObj{mDelegatee = Just (DelegStake $ unStakePoolKeyHash poolId)}
180172

181173
-- | Resets the delegation for the stake certificate.
182174
withoutDelegationImpl :: StakeCertificateObject -> StakeCertificateObject
183175
withoutDelegationImpl certObj =
184-
certObj{delegateStake = Nothing}
176+
certObj{mDelegatee = Nothing}
185177

186178
-- | Convert a StakeCertificateObject to the base16 encoding of its CBOR representation.
187179
toCborImpl :: MonadThrow m => StakeCertificateObject -> m String
188180
toCborImpl
189181
( StakeCertificateObject
190182
{ era
191183
, stakeCredential
192-
, deposit
184+
, mDeposit
193185
, action
194-
, delegateStake
186+
, mDelegatee
195187
}
196188
) = do
197-
stakeCert <- toCardanoApiCertificate era stakeCredential deposit action delegateStake
189+
stakeCert <- toCardanoApiCertificate era stakeCredential mDeposit action mDelegatee
198190
return $
199191
obtainCommonConstraints era $
200192
Text.unpack $
@@ -209,27 +201,27 @@ toCardanoApiCertificate
209201
-> Hash StakeKey
210202
-> Maybe Coin
211203
-> StakeCertificateAction
212-
-> Maybe PoolId
204+
-> Maybe Delegatee
213205
-> m (Certificate (Exp.LedgerEra era))
214-
toCardanoApiCertificate era stakeCredential mDeposit action delegateStake =
206+
toCardanoApiCertificate era stakeCredential mDeposit action mDelegatee =
215207
Exp.obtainCommonConstraints era $
216208
conwayEraOnwardsConstraints (convert era) $
217209
Certificate
218-
<$> ( case (action, delegateStake) of
210+
<$> ( case (action, mDelegatee) of
219211
(DelegateOnly, Nothing) ->
220212
throwError
221213
"Certificate must at least either: register, unregister, or delegate"
222214
(RegisterStake, Nothing) -> makeRegUnregCertWithMaybeDeposit era mkRegDepositTxCert mkRegTxCert stakeCredential mDeposit
223215
(UnregisterStake, Nothing) -> makeRegUnregCertWithMaybeDeposit era mkUnRegDepositTxCert mkUnRegTxCert stakeCredential mDeposit
224-
(DelegateOnly, Just poolId) ->
216+
(DelegateOnly, Just delegatee) ->
225217
return $
226218
mkDelegTxCert
227219
(KeyHashObj $ unStakeKeyHash stakeCredential)
228-
(DelegStake $ unStakePoolKeyHash poolId)
229-
(RegisterStake, Just poolId) ->
220+
delegatee
221+
(RegisterStake, Just delegatee) ->
230222
mkRegDepositDelegTxCert
231223
(KeyHashObj $ unStakeKeyHash stakeCredential)
232-
(DelegStake $ unStakePoolKeyHash poolId)
224+
delegatee
233225
<$> case mDeposit of
234226
Just dep -> return dep
235227
Nothing -> throwError "Deposit must be specified for stake registration and delegation certificate"

0 commit comments

Comments
 (0)