Skip to content

Commit 3a34eda

Browse files
committed
Make DepositLovelace a Natural
1 parent 187fe99 commit 3a34eda

5 files changed

Lines changed: 28 additions & 11 deletions

File tree

cardano-wasm/cardano-wasm.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ library cardano-wasi-lib
3838
Cardano.Wasm.Api.TypeScriptDefs
3939
Cardano.Wasm.Api.Wallet
4040
Cardano.Wasm.ExceptionHandling
41+
Cardano.Wasm.NumberConversion
4142

4243
other-modules:
4344
Cardano.Wasm.Internal.Api.Era

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import Control.Monad.Catch (MonadThrow)
3737
import Data.ByteString.Base16 qualified as Base16
3838
import Data.Text qualified as Text
3939
import Data.Text.Encoding qualified as Text
40+
import Numeric.Natural (Natural)
4041

4142
-- * Type aliases for clarity
4243

@@ -47,7 +48,7 @@ type StakeKeyHashBase16 = String
4748
type PoolIdBase16 = String
4849

4950
-- | Deposit amount in lovelace.
50-
type DepositLovelace = Integer
51+
type DepositLovelace = Natural
5152

5253
-- | Certificate serialized to CBOR as a base16-encoded string.
5354
type CertificateCBORBase16 = String
@@ -106,7 +107,7 @@ makeStakeAddressRegistrationCertificateWrapper era skHash deposit =
106107
let cert :: Certificate (Exp.LedgerEra era) =
107108
Exp.makeStakeAddressRegistrationCertificate
108109
(StakeCredentialByKey skHash)
109-
(Coin deposit)
110+
(Coin $ toInteger deposit)
110111
return $ serialiseCertificateToCBOR era cert
111112

112113
-- | Make a stake address unregistration certificate in the current era.
@@ -131,7 +132,7 @@ makeStakeAddressUnregistrationCertificateWrapper era skHash deposit =
131132
let cert :: Certificate (Exp.LedgerEra era) =
132133
Exp.makeStakeAddressUnregistrationCertificate
133134
(StakeCredentialByKey skHash)
134-
(Coin deposit)
135+
(Coin $ toInteger deposit)
135136
return $ serialiseCertificateToCBOR era cert
136137

137138
serialiseCertificateToCBOR
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module Cardano.Wasm.NumberConversion
2+
( integerToNatural
3+
)
4+
where
5+
6+
import Cardano.Wasm.ExceptionHandling (throwError)
7+
8+
import Numeric.Natural (Natural)
9+
10+
integerToNatural :: Integer -> IO Natural
11+
integerToNatural i
12+
| i < 0 = throwError $ "Expected natural number, but got a negative value: " ++ show i
13+
| otherwise = return $ fromIntegral i

cardano-wasm/src-wasi/Cardano/Wasi/Internal/Api/Certificate/StakeCertificate.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ where
1212

1313
import Cardano.Wasi.Internal.Conversion (cstrToInt)
1414
import Cardano.Wasm.Api.Certificate.StakeCertificate qualified as Wasm
15+
import Cardano.Wasm.NumberConversion (integerToNatural)
1516

1617
import Control.Monad (join)
1718

@@ -67,7 +68,7 @@ makeStakeAddressRegistrationCertificate stakeKeyHashCStr depositCStr =
6768
=<< join
6869
( Wasm.makeStakeAddressRegistrationCertificateImpl
6970
<$> peekCString stakeKeyHashCStr
70-
<*> (toInteger <$> cstrToInt "deposit" depositCStr)
71+
<*> (integerToNatural =<< cstrToInt "deposit" depositCStr)
7172
)
7273

7374
-- | Make a stake address registration certificate in the upcoming era.
@@ -77,7 +78,7 @@ makeStakeAddressRegistrationCertificateUpcomingEra stakeKeyHashCStr depositCStr
7778
=<< join
7879
( Wasm.makeStakeAddressRegistrationCertificateUpcomingEraImpl
7980
<$> peekCString stakeKeyHashCStr
80-
<*> (toInteger <$> cstrToInt "deposit" depositCStr)
81+
<*> (integerToNatural =<< cstrToInt "deposit" depositCStr)
8182
)
8283

8384
-- | Make a stake address unregistration certificate in the current era.
@@ -87,7 +88,7 @@ makeStakeAddressUnregistrationCertificate stakeKeyHashCStr depositCStr =
8788
=<< join
8889
( Wasm.makeStakeAddressUnregistrationCertificateImpl
8990
<$> peekCString stakeKeyHashCStr
90-
<*> (toInteger <$> cstrToInt "deposit" depositCStr)
91+
<*> (integerToNatural =<< cstrToInt "deposit" depositCStr)
9192
)
9293

9394
-- | Make a stake address unregistration certificate in the upcoming era.
@@ -97,5 +98,5 @@ makeStakeAddressUnregistrationCertificateUpcomingEra stakeKeyHashCStr depositCSt
9798
=<< join
9899
( Wasm.makeStakeAddressUnregistrationCertificateUpcomingEraImpl
99100
<$> peekCString stakeKeyHashCStr
100-
<*> (toInteger <$> cstrToInt "deposit" depositCStr)
101+
<*> (integerToNatural =<< cstrToInt "deposit" depositCStr)
101102
)

cardano-wasm/src-wasm/Cardano/Wasm/Internal/JavaScript/Bridge.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import Cardano.Wasm.Internal.JavaScript.GRPC
3131
, js_submitTx
3232
)
3333
import Cardano.Wasm.Internal.JavaScript.GRPCTypes (JSGRPCClient, JSUtxos)
34+
import Cardano.Wasm.NumberConversion (integerToNatural)
3435

3536
import Control.Exception (evaluate)
3637
import Control.Monad
@@ -446,7 +447,7 @@ makeStakeAddressRegistrationCertificate jsStakeKeyHash jsDeposit =
446447
=<< join
447448
( Wasm.makeStakeAddressRegistrationCertificateImpl
448449
<$> fromJSVal jsStakeKeyHash
449-
<*> (fromInteger <$> fromJSBigInt jsDeposit)
450+
<*> (integerToNatural =<< fromJSBigInt jsDeposit)
450451
)
451452

452453
-- | Make a stake address registration certificate in the upcoming era.
@@ -456,7 +457,7 @@ makeStakeAddressRegistrationCertificateUpcomingEra jsStakeKeyHash jsDeposit =
456457
=<< join
457458
( Wasm.makeStakeAddressRegistrationCertificateUpcomingEraImpl
458459
<$> fromJSVal jsStakeKeyHash
459-
<*> (fromInteger <$> fromJSBigInt jsDeposit)
460+
<*> (integerToNatural =<< fromJSBigInt jsDeposit)
460461
)
461462

462463
-- | Make a stake address unregistration certificate in the current era.
@@ -466,7 +467,7 @@ makeStakeAddressUnregistrationCertificate jsStakeKeyHash jsDeposit =
466467
=<< join
467468
( Wasm.makeStakeAddressUnregistrationCertificateImpl
468469
<$> fromJSVal jsStakeKeyHash
469-
<*> (fromInteger <$> fromJSBigInt jsDeposit)
470+
<*> (integerToNatural =<< fromJSBigInt jsDeposit)
470471
)
471472

472473
-- | Make a stake address unregistration certificate in the upcoming era.
@@ -476,7 +477,7 @@ makeStakeAddressUnregistrationCertificateUpcomingEra jsStakeKeyHash jsDeposit =
476477
=<< join
477478
( Wasm.makeStakeAddressUnregistrationCertificateUpcomingEraImpl
478479
<$> fromJSVal jsStakeKeyHash
479-
<*> (fromInteger <$> fromJSBigInt jsDeposit)
480+
<*> (integerToNatural =<< fromJSBigInt jsDeposit)
480481
)
481482

482483
-- | Set the transaction fee for an unsigned transaction.

0 commit comments

Comments
 (0)