Created
March 15, 2025 15:29
-
-
Save HiroNakamura/e1cdebc9381ced3cc64c6c1d25079099 to your computer and use it in GitHub Desktop.
Cardano y Plutus en ejemplos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE DeriveAnyClass #-} | |
{-# LANGUAGE DeriveGeneric #-} | |
{-# LANGUAGE NoImplicitPrelude #-} | |
{-# LANGUAGE TemplateHaskell #-} | |
module MiPrimerToken where | |
import PlutusTx | |
import PlutusTx.Prelude | |
import Ledger | |
import Ledger.Value | |
import Ledger.Ada | |
import Ledger.Typed.Scripts | |
import Playground.Contract | |
-- Definimos el tipo de dato para nuestro token | |
data MiToken = MiToken | |
{ tokenName :: TokenName | |
, tokenSymbol :: CurrencySymbol | |
} deriving (Show, Generic, ToJSON, FromJSON) | |
-- Parámetros del contrato | |
data TokenParams = TokenParams | |
{ tpToken :: MiToken | |
, tpAmount :: Integer | |
} deriving (Show, Generic, ToJSON, FromJSON) | |
-- Validación del contrato | |
validateToken :: TokenParams -> () -> ScriptContext -> Bool | |
validateToken params _ ctx = | |
-- Verificamos que la cantidad de tokens sea correcta | |
let txOuts = txInfoOutputs $ scriptContextTxInfo ctx | |
totalTokens = foldMap (valueOf (tpToken params)) txOuts | |
in totalTokens == tpAmount params | |
-- Instancia de ScriptType para el contrato | |
instance ScriptType MiToken where | |
type instance RedeemerType MiToken = () | |
type instance DatumType MiToken = () | |
-- Compilación del validador | |
tokenValidator :: TokenParams -> Validator | |
tokenValidator params = mkValidatorScript $ | |
$$(PlutusTx.compile [|| validateToken ||]) | |
`PlutusTx.applyCode` | |
PlutusTx.liftCode params | |
-- Código para interactuar con el contrato en el Playground | |
endpoints :: Contract () TokenParams Text () | |
endpoints = do | |
params <- endpoint @"crear-token" | |
let val = tokenValidator params | |
logInfo @Text $ "Token creado: " <> show (tpToken params) | |
logInfo @Text $ "Cantidad total: " <> show (tpAmount params) | |
void $ submitTxConstraints (typedValidator val) mempty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment