Skip to content

Commit

Permalink
Use ledger types in constructors of Redeemer. (#4241)
Browse files Browse the repository at this point in the history
## Issue

ADP-3184

## Description

This PR adjusts the `Redeemer` type so that its constructors use ledger
types, thus eliminating usages of types from:
- `cardano-api`;
- `cardano-wallet-primitive`.
  • Loading branch information
jonathanknowles authored Nov 16, 2023
2 parents 3a47c55 + aad32e5 commit bf42709
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 43 deletions.
4 changes: 4 additions & 0 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ module Internal.Cardano.Write.Tx
, datumHashFromBytes
, datumHashToBytes

-- ** Rewards
, RewardAccount

-- ** Script
, Script
, Alonzo.isPlutusScript
Expand Down Expand Up @@ -557,6 +560,7 @@ type TxOutInBabbage = Babbage.BabbageTxOut (Babbage.BabbageEra StandardCrypto)

type Address = Ledger.Addr StandardCrypto

type RewardAccount = Ledger.RewardAcnt StandardCrypto
type Script = AlonzoScript
type Value = MaryValue StandardCrypto

Expand Down
52 changes: 15 additions & 37 deletions lib/balance-tx/lib/internal/Internal/Cardano/Write/Tx/Redeemers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ module Internal.Cardano.Write.Tx.Redeemers

import Prelude

import Cardano.Crypto.Hash.Class
( Hash
)
import Cardano.Ledger.Alonzo.TxInfo
( TranslationError
)
Expand All @@ -35,12 +32,8 @@ import Cardano.Ledger.Api
, scriptIntegrityHashTxBodyL
, witsTxL
)
import Cardano.Ledger.Mary.Value
( PolicyID (..)
)
import Cardano.Ledger.Shelley.API
( ScriptHash (..)
, StrictMaybe (..)
( StrictMaybe (..)
)
import Cardano.Slotting.EpochInfo
( EpochInfo
Expand Down Expand Up @@ -86,9 +79,6 @@ import Data.Map.Strict
( Map
, (!)
)
import Data.Maybe
( fromMaybe
)
import Fmt
( Buildable (..)
)
Expand All @@ -97,10 +87,13 @@ import GHC.Generics
)
import Internal.Cardano.Write.Tx
( PParams
, PolicyId
, RecentEra
, RecentEraLedgerConstraints
, RewardAccount
, ShelleyLedgerEra
, StandardCrypto
, TxIn
, UTxO
, txBody
, withConstraints
Expand All @@ -111,19 +104,13 @@ import Internal.Cardano.Write.Tx.TimeTranslation
, systemStartTime
)

import qualified Cardano.Api as CardanoApi
import qualified Cardano.Api.Shelley as CardanoApi
import qualified Cardano.Crypto.Hash as Crypto
import qualified Cardano.Ledger.Alonzo.PlutusScriptApi as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts.Data as Alonzo
import qualified Cardano.Ledger.Alonzo.Tx as Alonzo
import qualified Cardano.Ledger.Alonzo.TxWits as Alonzo
import qualified Cardano.Ledger.Api as Ledger
import qualified Cardano.Wallet.Primitive.Types.Hash as W
import qualified Cardano.Wallet.Primitive.Types.TokenPolicy as W
import qualified Cardano.Wallet.Primitive.Types.Tx.TxIn as W
import qualified Cardano.Wallet.Shelley.Compatibility.Ledger as Convert
import qualified Data.ByteString.Lazy as BL
import qualified Data.Map as Map
import qualified Data.Map.Merge.Strict as Map
Expand Down Expand Up @@ -273,19 +260,19 @@ assignScriptRedeemers era pparams timeTranslation utxo redeemers tx =
--

data Redeemer
= RedeemerSpending ByteString W.TxIn
| RedeemerMinting ByteString W.TokenPolicyId
| RedeemerRewarding ByteString CardanoApi.StakeAddress
= RedeemerSpending ByteString TxIn
| RedeemerMinting ByteString PolicyId
| RedeemerRewarding ByteString RewardAccount
deriving (Eq, Generic, Show)

instance Buildable Redeemer where
build = \case
RedeemerSpending _ input ->
"spending(" <> build input <> ")"
"spending(" <> build (show input) <> ")"
RedeemerMinting _ pid ->
"minting(" <> build pid <> ")"
RedeemerRewarding _ addr ->
"rewarding(" <> build (CardanoApi.serialiseToBech32 addr) <> ")"
"minting(" <> build (show pid) <> ")"
RedeemerRewarding _ acc ->
"rewarding(" <> build (show acc) <> ")"

redeemerData :: Redeemer -> ByteString
redeemerData = \case
Expand All @@ -296,20 +283,11 @@ redeemerData = \case
toScriptPurpose :: Redeemer -> Alonzo.ScriptPurpose StandardCrypto
toScriptPurpose = \case
RedeemerSpending _ txin ->
Alonzo.Spending (Convert.toLedger txin)
Alonzo.Spending txin
RedeemerMinting _ pid ->
Alonzo.Minting (toPolicyID pid)
RedeemerRewarding _ (CardanoApi.StakeAddress ntwrk acct) ->
Alonzo.Rewarding (Ledger.RewardAcnt ntwrk acct)

toPolicyID :: W.TokenPolicyId -> PolicyID StandardCrypto
toPolicyID (W.UnsafeTokenPolicyId (W.Hash bytes)) =
PolicyID (ScriptHash (unsafeHashFromBytes bytes))
where
unsafeHashFromBytes :: Crypto.HashAlgorithm h => ByteString -> Hash h a
unsafeHashFromBytes =
fromMaybe (error "unsafeHashFromBytes: wrong length")
. Crypto.hashFromBytes
Alonzo.Minting pid
RedeemerRewarding _ acc ->
Alonzo.Rewarding acc

--------------------------------------------------------------------------------
-- Utils
Expand Down
14 changes: 10 additions & 4 deletions lib/wallet/api/http/Cardano/Wallet/Api/Http/Shelley/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,16 @@ import Cardano.Api
, toNetworkMagic
, unNetworkMagic
)
import Cardano.Api.Shelley
( StakeAddress (..)
)
import Cardano.BM.Tracing
( HasPrivacyAnnotation (..)
, HasSeverityAnnotation (..)
)
import Cardano.Ledger.Address
( RewardAcnt (..)
)
import Cardano.Mnemonic
( SomeMnemonic
)
Expand Down Expand Up @@ -4936,11 +4942,11 @@ fromExternalInput ApiExternalInput
fromApiRedeemer :: ApiRedeemer n -> Redeemer
fromApiRedeemer = \case
ApiRedeemerSpending (ApiBytesT bytes) (ApiT i) ->
RedeemerSpending bytes i
RedeemerSpending bytes (toLedger i)
ApiRedeemerMinting (ApiBytesT bytes) (ApiT p) ->
RedeemerMinting bytes p
ApiRedeemerRewarding (ApiBytesT bytes) r ->
RedeemerRewarding bytes r
RedeemerMinting bytes (toLedger p)
ApiRedeemerRewarding (ApiBytesT bytes) (StakeAddress x y) ->
RedeemerRewarding bytes (RewardAcnt x y)

{-------------------------------------------------------------------------------
Api Layer
Expand Down
8 changes: 6 additions & 2 deletions lib/wallet/test/unit/Internal/Cardano/Write/Tx/BalanceSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,9 @@ spec_balanceTransaction = describe "balanceTransaction" $ do
-- With ix 1 instead of 0, making it point to an input which
-- doesn't exist in the tx.
let faultyRedeemer =
RedeemerSpending (unsafeFromHex "D87A80") (W.TxIn tid 1)
RedeemerSpending
(unsafeFromHex "D87A80")
(Convert.toLedger (W.TxIn tid 1))

let withFaultyRedeemer =
over #redeemers $ mapFirst $ const faultyRedeemer
Expand Down Expand Up @@ -2487,7 +2489,9 @@ pingPong_2 = PartialTx
)
]
, redeemers =
[ RedeemerSpending (unsafeFromHex "D87A80") (W.TxIn (W.Hash tid) 0)
[ RedeemerSpending
(unsafeFromHex "D87A80")
(Convert.toLedger (W.TxIn (W.Hash tid) 0))
]
}
where
Expand Down

0 comments on commit bf42709

Please sign in to comment.