Skip to content

Commit

Permalink
Delete toShelleyLovelace, fromShelleyLovelace, lovelaceToCoin and coi…
Browse files Browse the repository at this point in the history
…nToLovelace
  • Loading branch information
newhoggy committed Nov 14, 2023
1 parent 4486d12 commit 91b5a29
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 128 deletions.
25 changes: 9 additions & 16 deletions cardano-api/internal/Cardano/Api/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ makeStakeAddressRegistrationCertificate = \case
StakeAddrRegistrationConway cOnwards deposit scred ->
conwayEraOnwardsConstraints cOnwards
$ ConwayCertificate cOnwards
$ Ledger.mkRegDepositTxCert (toShelleyStakeCredential scred) (toShelleyLovelace deposit)
$ Ledger.mkRegDepositTxCert (toShelleyStakeCredential scred) deposit

makeStakeAddressUnregistrationCertificate :: StakeAddressRequirements era -> Certificate era
makeStakeAddressUnregistrationCertificate req =
case req of
StakeAddrRegistrationConway cOnwards deposit scred ->
conwayEraOnwardsConstraints cOnwards
$ ConwayCertificate cOnwards
$ Ledger.mkUnRegDepositTxCert (toShelleyStakeCredential scred) (toShelleyLovelace deposit)
$ Ledger.mkUnRegDepositTxCert (toShelleyStakeCredential scred) deposit

StakeAddrRegistrationPreConway atMostEra scred ->
shelleyToBabbageEraConstraints atMostEra
Expand Down Expand Up @@ -396,10 +396,7 @@ makeDrepRegistrationCertificate :: ()
makeDrepRegistrationCertificate (DRepRegistrationRequirements conwayOnwards vcred deposit) anchor =
ConwayCertificate conwayOnwards
. Ledger.ConwayTxCertGov
$ Ledger.ConwayRegDRep
vcred
(toShelleyLovelace deposit)
(noInlineMaybeToStrictMaybe anchor)
$ Ledger.ConwayRegDRep vcred deposit (noInlineMaybeToStrictMaybe anchor)

data CommitteeHotKeyAuthorizationRequirements era where
CommitteeHotKeyAuthorizationRequirements
Expand Down Expand Up @@ -448,8 +445,7 @@ makeDrepUnregistrationCertificate :: ()
makeDrepUnregistrationCertificate (DRepUnregistrationRequirements conwayOnwards vcred deposit) =
ConwayCertificate conwayOnwards
. Ledger.ConwayTxCertGov
. Ledger.ConwayUnRegDRep vcred
$ toShelleyLovelace deposit
$ Ledger.ConwayUnRegDRep vcred deposit

makeStakeAddressAndDRepDelegationCertificate :: ()
=> ConwayEraOnwards era
Expand All @@ -460,10 +456,7 @@ makeStakeAddressAndDRepDelegationCertificate :: ()
makeStakeAddressAndDRepDelegationCertificate w cred delegatee deposit =
conwayEraOnwardsConstraints w
$ ConwayCertificate w
$ Ledger.mkRegDepositDelegTxCert
(toShelleyStakeCredential cred)
delegatee
(toShelleyLovelace deposit)
$ Ledger.mkRegDepositDelegTxCert (toShelleyStakeCredential cred) delegatee deposit

-- ----------------------------------------------------------------------------
-- Helper functions
Expand Down Expand Up @@ -588,8 +581,8 @@ toShelleyPoolParams StakePoolParameters {
Ledger.PoolParams {
Ledger.ppId = poolkh
, Ledger.ppVrf = vrfkh
, Ledger.ppPledge = toShelleyLovelace stakePoolPledge
, Ledger.ppCost = toShelleyLovelace stakePoolCost
, Ledger.ppPledge = stakePoolPledge
, Ledger.ppCost = stakePoolCost
, Ledger.ppMargin = fromMaybe
(error "toShelleyPoolParams: invalid PoolMargin")
(Ledger.boundRational stakePoolMargin)
Expand Down Expand Up @@ -655,10 +648,10 @@ fromShelleyPoolParams
StakePoolParameters {
stakePoolId = StakePoolKeyHash ppId
, stakePoolVRF = VrfKeyHash ppVrf
, stakePoolCost = fromShelleyLovelace ppCost
, stakePoolCost = ppCost
, stakePoolMargin = Ledger.unboundRational ppMargin
, stakePoolRewardAccount = fromShelleyStakeAddr ppRewardAcnt
, stakePoolPledge = fromShelleyLovelace ppPledge
, stakePoolPledge = ppPledge
, stakePoolOwners = map StakeKeyHash (Set.toList ppOwners)
, stakePoolRelays = map fromShelleyStakePoolRelay
(Foldable.toList ppRelays)
Expand Down
3 changes: 1 addition & 2 deletions cardano-api/internal/Cardano/Api/Convenience/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import Cardano.Api.Query
import Cardano.Api.Query.Expr
import Cardano.Api.TxBody
import Cardano.Api.Utils
import Cardano.Api.Value

import qualified Cardano.Ledger.Api as L
import Cardano.Ledger.CertState (DRepState (..))
Expand Down Expand Up @@ -120,7 +119,7 @@ queryStateForBalancedTx era allTxIns certs = runExceptT $ do

drepDelegDeposits <-
forEraInEon @ConwayEraOnwards era (pure mempty) $ \_ ->
Map.map (fromShelleyLovelace . drepDeposit) <$>
Map.map drepDeposit <$>
(lift (queryDRepState sbe drepCreds)
& onLeft (left . QceUnsupportedNtcVersion)
& onLeft (left . QueryEraMismatch))
Expand Down
13 changes: 5 additions & 8 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ evaluateTransactionFee sbe pp txbody keywitcount _byronwitcount =
shelleyBasedEraConstraints sbe $
case makeSignedTransaction' (shelleyBasedToCardanoEra sbe) [] txbody of
ByronTx w _ -> disjointByronEraOnlyAndShelleyBasedEra w sbe
ShelleyTx _ tx -> fromShelleyLovelace $ Ledger.evaluateTransactionFee pp tx keywitcount
ShelleyTx _ tx -> Ledger.evaluateTransactionFee pp tx keywitcount

-- | Give an approximate count of the number of key witnesses (i.e. signatures)
-- a transaction will need.
Expand Down Expand Up @@ -585,13 +585,11 @@ evaluateTransactionBalance sbe pp poolids stakeDelegDeposits drepDelegDeposits u
lookupDelegDeposit ::
Ledger.Credential 'Ledger.Staking L.StandardCrypto -> Maybe L.Coin
lookupDelegDeposit stakeCred =
toShelleyLovelace <$>
Map.lookup (fromShelleyStakeCredential stakeCred) stakeDelegDeposits

lookupDRepDeposit ::
Ledger.Credential 'Ledger.DRepRole L.StandardCrypto -> Maybe L.Coin
lookupDRepDeposit drepCred =
toShelleyLovelace <$>
Map.lookup drepCred drepDelegDeposits

-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -816,7 +814,7 @@ makeTransactionBodyAutoBalance sbe systemstart history lpp@(LedgerProtocolParame
TxMintNone -> mempty
TxMintValue w v _ -> toLedgerValue w v
let change = mconcat [incoming, minted, negateLedgerValue sbe outgoing]
let changeWithMaxLovelace = change & A.adaAssetL sbe .~ lovelaceToCoin maxLovelaceChange
let changeWithMaxLovelace = change & A.adaAssetL sbe .~ maxLovelaceChange
let changeTxOut = forShelleyBasedEraInEon sbe
(lovelaceToTxOutValue era maxLovelaceChange)
(\w -> maryEraOnwardsConstraints w $ TxOutValueShelleyBased sbe changeWithMaxLovelace)
Expand Down Expand Up @@ -934,16 +932,15 @@ makeTransactionBodyAutoBalance sbe systemstart history lpp@(LedgerProtocolParame
let txOuts = catMaybes [ Map.lookup txin utxo' | txin <- collIns]
totalCollateralLovelace = mconcat $ map (\(TxOut _ txOutVal _ _) -> txOutValueToLovelace txOutVal) txOuts
requiredCollateral@(L.Coin reqAmt) = fromIntegral colPerc * fee
totalCollateral = TxTotalCollateral retColSup . fromShelleyLovelace
. L.rationalToCoinViaCeiling
totalCollateral = TxTotalCollateral retColSup . L.rationalToCoinViaCeiling
$ reqAmt % 100
-- Why * 100? requiredCollateral is the product of the collateral percentage and the tx fee
-- We choose to multiply 100 rather than divide by 100 to make the calculation
-- easier to manage. At the end of the calculation we then use % 100 to perform our division
-- and round up.
enoughCollateral = totalCollateralLovelace * 100 >= requiredCollateral
L.Coin amt = totalCollateralLovelace * 100 - requiredCollateral
returnCollateral = fromShelleyLovelace . L.rationalToCoinViaFloor $ amt % 100
returnCollateral = L.rationalToCoinViaFloor $ amt % 100

case (txReturnCollateral, txTotalCollateral) of
#if MIN_VERSION_base(4,16,0)
Expand Down Expand Up @@ -1164,4 +1161,4 @@ calculateMinimumUTxO
calculateMinimumUTxO sbe txout pp =
shelleyBasedEraConstraints sbe
$ let txOutWithMinCoin = L.setMinCoinTxOut pp (toShelleyTxOutAny sbe txout)
in fromShelleyLovelace (txOutWithMinCoin ^. L.coinTxOutL)
in txOutWithMinCoin ^. L.coinTxOutL
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import qualified Cardano.Api.ReexposeLedger as Ledger
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseTextEnvelope
import Cardano.Api.TxIn
import Cardano.Api.Value

import qualified Cardano.Binary as CBOR
import qualified Cardano.Ledger.Address as L
Expand Down Expand Up @@ -92,7 +91,7 @@ toGovernanceAction sbe =
InfoAct ->
Gov.InfoAction
TreasuryWithdrawal withdrawals ->
let m = Map.fromList [(L.mkRwdAcnt nw (toShelleyStakeCredential sc), toShelleyLovelace l) | (nw,sc,l) <- withdrawals]
let m = Map.fromList [(L.mkRwdAcnt nw (toShelleyStakeCredential sc), l) | (nw,sc,l) <- withdrawals]
in Gov.TreasuryWithdrawals m
InitiateHardfork prevGovId pVer ->
Gov.HardForkInitiation prevGovId pVer
Expand All @@ -113,7 +112,7 @@ fromGovernanceAction = \case
Gov.HardForkInitiation prevGovId pVer ->
InitiateHardfork prevGovId pVer
Gov.TreasuryWithdrawals withdrawlMap ->
let res = [ (L.getRwdNetwork rwdAcnt, fromShelleyStakeCredential (L.getRwdCred rwdAcnt), fromShelleyLovelace coin)
let res = [ (L.getRwdNetwork rwdAcnt, fromShelleyStakeCredential (L.getRwdCred rwdAcnt), coin)
| (rwdAcnt, coin) <- Map.toList withdrawlMap
]
in TreasuryWithdrawal res
Expand Down Expand Up @@ -165,7 +164,7 @@ createProposalProcedure
createProposalProcedure sbe nw dep (StakeKeyHash retAddrh) govAct anchor =
shelleyBasedEraConstraints sbe $
Proposal Gov.ProposalProcedure
{ Gov.pProcDeposit = toShelleyLovelace dep
{ Gov.pProcDeposit = dep
, Gov.pProcReturnAddr = L.mkRwdAcnt nw (L.KeyHashObj retAddrh)
, Gov.pProcGovAction = toGovernanceAction sbe govAct
, Gov.pProcAnchor = anchor
Expand All @@ -177,7 +176,7 @@ fromProposalProcedure
-> (L.Coin, Hash StakeKey, GovernanceAction era)
fromProposalProcedure sbe (Proposal pp) =
shelleyBasedEraConstraints sbe
( fromShelleyLovelace $ Gov.pProcDeposit pp
( Gov.pProcDeposit pp
, case fromShelleyStakeCredential (L.getRwdCred (Gov.pProcReturnAddr pp)) of
StakeCredentialByKey keyhash -> keyhash
StakeCredentialByScript _scripthash ->
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/internal/Cardano/Api/LedgerEvent.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
import Cardano.Api.Address (StakeCredential, fromShelleyStakeCredential)
import Cardano.Api.Block (EpochNo)
import Cardano.Api.Keys.Shelley (Hash (StakePoolKeyHash), StakePoolKey)
import Cardano.Api.Value (fromShelleyDeltaLovelace, fromShelleyLovelace)
import Cardano.Api.Value (fromShelleyDeltaLovelace)

import Cardano.Ledger.Alonzo.Rules (AlonzoBbodyEvent (..), AlonzoUtxoEvent (..),
AlonzoUtxosEvent (FailedPlutusScriptsEvent, SuccessfulPlutusScriptsEvent),
Expand Down Expand Up @@ -210,8 +210,8 @@ pattern LEMirTransfer rp tp rtt ttr <-
( MirEvent
( MirTransfer
( InstantaneousRewards
(Map.mapKeys fromShelleyStakeCredential . fmap fromShelleyLovelace -> rp)
(Map.mapKeys fromShelleyStakeCredential . fmap fromShelleyLovelace -> tp)
(Map.mapKeys fromShelleyStakeCredential -> rp)
(Map.mapKeys fromShelleyStakeCredential -> tp)
(fromShelleyDeltaLovelace -> rtt)
(fromShelleyDeltaLovelace -> ttr)
)
Expand Down Expand Up @@ -309,4 +309,4 @@ convertRetiredPoolsMap ::
-> Map StakeCredential (Map (Hash StakePoolKey) L.Coin)
convertRetiredPoolsMap =
Map.mapKeys fromShelleyStakeCredential
. fmap (Map.mapKeys StakePoolKeyHash . fmap fromShelleyLovelace)
. fmap (Map.mapKeys StakePoolKeyHash)
Loading

0 comments on commit 91b5a29

Please sign in to comment.