tweag / cooked-validators

MIT License
39 stars 11 forks source link

Add a function to test for specific tokens and wallets on the final ledger state #333

Open facundominguez opened 1 year ago

facundominguez commented 1 year ago

In an audit I ended up using a function like the following to test the final ledger state of a trace.

-- | Assert success of a validator where wallets are holding the given
-- amounts of a certain token
assertSuccessWithTokens ::
  (C.IsProp prop, Show a) =>
  C.InitialDistribution ->
  [(C.Wallet, Pl.AssetClass, Integer)] ->
  C.StagedMockChain a ->
  prop
assertSuccessWithTokens dst xs =
  C.testSucceedsFrom'
    ( \_ s -> (`C.testAll` xs) $ \(w, ac, i) ->
        let actual = Value.assetClassValueOf (holdingInState s w) ac
         in testBoolMsg (message w ac i actual) $ actual == i
    )
    dst
  where
    message w ac i actual =
      "Expected wallet "
        <> walletString w
        <> " to hold "
        <> show i
        <> " of token "
        <> show ac
        <> " but found "
        <> show actual
    walletString w = fromMaybe "unknown" (CW.mwPrintAs w)

This function is helpful when using tokens other than ada. With ada is harder to guess the exact amounts since they depend on the fees that have been charged.

To deal with Ada, maybe the above function could be generalized to receive predicates for each token

assertSuccessWithTokens ::
  (C.IsProp prop, Show a) =>
  C.InitialDistribution ->
  [(C.Wallet, Pl.AssetClass, Integer -> Bool)] ->
  C.StagedMockChain a ->
  prop