|
| 1 | +{-# LANGUAGE AllowAmbiguousTypes #-} |
| 2 | +{-# LANGUAGE NamedFieldPuns #-} |
| 3 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 4 | + |
1 | 5 | -- | Off-chain code for the aiken example |
2 | 6 | module Wst.Aiken.Offchain |
3 | 7 | ( register, |
| 8 | + Cip143Blueprint (..), |
| 9 | + lookupScripts, |
4 | 10 | ) |
5 | 11 | where |
6 | 12 |
|
| 13 | +import Cardano.Api (ScriptInAnyLang) |
| 14 | +import Cardano.Api qualified as C |
| 15 | +import Cardano.Api.Shelley qualified as C |
| 16 | +import Control.Monad.Reader (MonadReader, runReaderT) |
| 17 | +import Convex.BuildTx qualified as BuildTx |
| 18 | +import Convex.Class (MonadBlockchain) |
| 19 | +import Data.Functor.Constant (Constant (..)) |
| 20 | +import Data.Functor.Identity (Identity (..)) |
| 21 | +import Data.Map qualified as Map |
| 22 | +import PlutusLedgerApi.V3 (CurrencySymbol) |
| 23 | +import Wst.Aiken.Blueprint (Blueprint (..), BlueprintKey) |
7 | 24 | import Wst.Offchain.BuildTx.DirectorySet (InsertNodeArgs (..)) |
8 | 25 | import Wst.Offchain.BuildTx.DirectorySet qualified as Directory |
| 26 | +import Wst.Offchain.Env (HasDirectoryEnv) |
| 27 | +import Wst.Offchain.Query qualified as Query |
| 28 | + |
| 29 | +data Cip143Blueprint v |
| 30 | + = Cip143Blueprint |
| 31 | + { cbSymbol :: CurrencySymbol, |
| 32 | + cbTransfer :: v ScriptInAnyLang, |
| 33 | + cbIssuance :: v ScriptInAnyLang, |
| 34 | + cbGlobalStateCS :: Maybe CurrencySymbol |
| 35 | + } |
| 36 | + |
| 37 | +-- | Lookup the scripts that are referenced in the CIP 143 blueprint |
| 38 | +lookupScripts :: Blueprint -> Cip143Blueprint (Constant BlueprintKey) -> Either String (Cip143Blueprint Identity) |
| 39 | +lookupScripts Blueprint {validators} b@Cip143Blueprint {cbTransfer, cbIssuance} = do |
| 40 | + tr <- maybe (Left $ "Failed to find key " <> show (getConstant cbTransfer)) Right (Map.lookup (getConstant cbTransfer) validators) |
| 41 | + i <- maybe (Left $ "Failed to find key " <> show (getConstant cbIssuance)) Right (Map.lookup (getConstant cbIssuance) validators) |
| 42 | + pure $ b {cbIssuance = Identity i, cbTransfer = Identity tr} |
9 | 43 |
|
10 | | --- | Register the policy |
11 | | -register :: (Applicative m) => m () |
12 | | -register = do |
13 | | - let args = |
14 | | - InsertNodeArgs |
15 | | - { inaNewKey = undefined |
16 | | - } |
| 44 | +scriptHash :: ScriptInAnyLang -> C.ScriptHash |
| 45 | +scriptHash (C.ScriptInAnyLang _ s) = C.hashScript s |
| 46 | + |
| 47 | +mkArgs :: Cip143Blueprint Identity -> InsertNodeArgs |
| 48 | +mkArgs Cip143Blueprint {cbSymbol, cbTransfer, cbIssuance, cbGlobalStateCS} = |
| 49 | + InsertNodeArgs |
| 50 | + { inaNewKey = cbSymbol, |
| 51 | + inaTransferLogic = C.StakeCredentialByScript $ scriptHash $ runIdentity cbTransfer, |
| 52 | + inaIssuerLogic = C.StakeCredentialByScript $ scriptHash $ runIdentity cbIssuance, |
| 53 | + inaGlobalStateCS = cbGlobalStateCS |
| 54 | + } |
| 55 | + |
| 56 | +-- | Register the policies |
| 57 | +register :: forall era env m. (Monad m, C.IsBabbageBasedEra era, MonadReader env m, HasDirectoryEnv env, C.HasScriptLanguageInEra C.PlutusScriptV3 era, MonadBlockchain era m) => Cip143Blueprint Identity -> m () |
| 58 | +register blueprint = do |
| 59 | + let args = mkArgs blueprint |
| 60 | + paramsNode <- Query.globalParamsNode @era |
| 61 | + tx <- BuildTx.runBuildTxT @era (Directory.insertDirectoryNode @era _ _ args) |
17 | 62 | pure () |
| 63 | + |
| 64 | +-- other endpoints |
| 65 | +-- mint |
| 66 | +-- burn |
| 67 | +-- transfer |
| 68 | +-- force-transfer |
0 commit comments