Skip to content

Commit

Permalink
autobalancing test for deregistering certificate
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Jan 8, 2025
1 parent 02eb4b4 commit 69c8afa
Showing 1 changed file with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ module Test.Cardano.Api.Transaction.Autobalance
where

import Cardano.Api
import Cardano.Api.Address (toShelleyStakeCredential)
import Cardano.Api.Fees
import qualified Cardano.Api.Ledger as L
import qualified Cardano.Api.Ledger.Lens as L
Expand Down Expand Up @@ -90,12 +91,12 @@ prop_make_transaction_body_autobalance_return_correct_fee_for_multi_asset = H.pr
defaultTxBodyContent sbe
& setTxIns txInputs
& setTxInsCollateral txInputsCollateral
& setTxOuts (mkTxOutput beo address Nothing) -- include minted asset in txout manually
& setTxOuts (mkTxOutput beo address (L.Coin 2_000_000) Nothing) -- include minted asset in txout manually
& setTxMintValue txMint
& setTxProtocolParams (pure $ pure pparams)

-- tx body content with manually added asset to TxOut
let contentWithTxoutAsset = content & setTxOuts (mkTxOutput beo address (Just scriptHash))
let contentWithTxoutAsset = content & setTxOuts (mkTxOutput beo address (L.Coin 2_000_000) (Just scriptHash))

-- change txout only with ADA
(BalancedTxBody balancedContentWithTxoutAsset _ _ feeWithTxoutAsset) <-
Expand Down Expand Up @@ -137,6 +138,80 @@ prop_make_transaction_body_autobalance_return_correct_fee_for_multi_asset = H.pr
H.diff balancedContentWithTxoutAsset (\_ _ -> feeWithTxoutAsset == fee) balancedContent
feeWithTxoutAsset === fee

prop_make_transaction_body_autobalance_when_deregistering_certs :: Property
prop_make_transaction_body_autobalance_when_deregistering_certs = H.propertyOnce $ do
let ceo = ConwayEraOnwardsConway
beo = convert ceo
sbe = convert beo
meo = convert beo
era = toCardanoEra sbe
aeo <- H.nothingFail $ forEraMaybeEon @AlonzoEraOnwards era

systemStart <-
fmap SystemStart . H.evalIO $
DT.parseTimeM True DT.defaultTimeLocale "%Y-%m-%dT%H:%M:%S%QZ" "2021-09-01T00:00:00Z"

let epochInfo = LedgerEpochInfo $ CS.fixedEpochInfo (CS.EpochSize 100) (CS.mkSlotLength 1000)

pparams <-
LedgerProtocolParameters
<$> H.readJsonFileOk "test/cardano-api-test/files/input/protocol-parameters/conway.json"

(sh@(ScriptHash scriptHash), plutusWitness) <- loadPlutusWitness ceo
let policyId' = PolicyId sh
-- one UTXO with an asset - the same we're minting in the transaction
let utxos = mkUtxos beo scriptHash
txInputs = map (,BuildTxWith (KeyWitness KeyWitnessForSpending)) . toList . M.keys . unUTxO $ utxos
txInputsCollateral = TxInsCollateral aeo $ toList . M.keys . unUTxO $ utxos
let address = mkAddress sbe scriptHash
let txMint =
TxMintValue
meo
[(policyId', [("eeee", 1, BuildTxWith plutusWitness)])]
stakeCred <- forAll genStakeCredential
let deregDeposit = L.Coin 200_000
let certs =
[ ConwayCertificate ceo $
L.ConwayTxCertDeleg (L.ConwayUnRegCert (toShelleyStakeCredential stakeCred) (L.SJust deregDeposit))
]

let content =
defaultTxBodyContent sbe
& setTxIns txInputs
& setTxInsCollateral txInputsCollateral
& setTxOuts (mkTxOutput beo address (L.Coin 2_000_000) Nothing)
& setTxMintValue txMint
& setTxProtocolParams (pure $ pure pparams)
& setTxCertificates (TxCertificates sbe certs (BuildTxWith []))

-- autobalanced body has assets and ADA in the change txout
(BalancedTxBody balancedContent _ txOut fee) <-
H.leftFail $
makeTransactionBodyAutoBalance
sbe
systemStart
epochInfo
pparams
mempty
[(stakeCred, deregDeposit)]
mempty
utxos
content
address
Nothing

-- 335_475 === fee
H.noteShow fee
H.noteShowPretty txOut

TxReturnCollateral _ (TxOut _ txOutValue _ _) <-
H.noteShowPretty $ txReturnCollateral balancedContent
let assets = [a | a@(AssetId _ _, _) <- toList $ txOutValueToValue txOutValue]
H.note_ "Check that all assets from UTXO, from the collateral txin, are in the return collateral."
[(AssetId policyId' "eeee", 1)] === assets

H.failure

prop_make_transaction_body_autobalance_multi_asset_collateral :: Property
prop_make_transaction_body_autobalance_multi_asset_collateral = H.propertyOnce $ do
let ceo = ConwayEraOnwardsConway
Expand Down Expand Up @@ -172,7 +247,7 @@ prop_make_transaction_body_autobalance_multi_asset_collateral = H.propertyOnce $
defaultTxBodyContent sbe
& setTxIns txInputs
& setTxInsCollateral txInputsCollateral
& setTxOuts (mkTxOutput beo address Nothing)
& setTxOuts (mkTxOutput beo address (L.Coin 2_000_000) Nothing)
& setTxMintValue txMint
& setTxProtocolParams (pure $ pure pparams)

Expand Down Expand Up @@ -352,17 +427,19 @@ mkAddress sbe scriptHash =
mkTxOutput
:: BabbageEraOnwards era
-> AddressInEra era
-> L.Coin
-- ^ output ADA
-> Maybe (L.ScriptHash L.StandardCrypto)
-- ^ there will be an asset in the txout if provided
-> [TxOut CtxTx era]
mkTxOutput beo address mScriptHash = babbageEraOnwardsConstraints beo $ do
mkTxOutput beo address coin mScriptHash = babbageEraOnwardsConstraints beo $ do
let sbe = convert beo
[ TxOut
address
( TxOutValueShelleyBased
sbe
( L.MaryValue
(L.Coin 2_000_000)
coin
( L.MultiAsset $
fromList
[(L.PolicyID scriptHash, [(L.AssetName "eeee", 2)]) | scriptHash <- maybeToList mScriptHash]
Expand All @@ -383,5 +460,8 @@ tests =
, testProperty
"makeTransactionBodyAutoBalance autobalances multi-asset collateral"
prop_make_transaction_body_autobalance_multi_asset_collateral
, testProperty
"makeTransactionBodyAutoBalance autobalances when deregistering certificates"
prop_make_transaction_body_autobalance_when_deregistering_certs
, testProperty "calcReturnAndTotalCollateral constraints hold" prop_calcReturnAndTotalCollateral
]

0 comments on commit 69c8afa

Please sign in to comment.