use v2 types

This commit is contained in:
Hongrui Fang 2022-08-15 21:27:57 +08:00
parent b453f2b216
commit fec449ceb5
36 changed files with 504 additions and 360 deletions

View file

@ -23,39 +23,31 @@ import Agora.Effect.TreasuryWithdrawal (
TreasuryWithdrawalDatum (TreasuryWithdrawalDatum),
treasuryWithdrawalValidator,
)
import Data.Default (def)
import Plutarch.Api.V1 (mkValidator, validatorHash)
import PlutusLedgerApi.V1 (
import Plutarch.Api.V2 (mkValidator, validatorHash)
import PlutusLedgerApi.V1.Interval qualified as Interval (always)
import PlutusLedgerApi.V1.Value qualified as Value (singleton)
import PlutusLedgerApi.V2 (
Address (Address),
Credential (..),
CurrencySymbol,
DatumHash (DatumHash),
OutputDatum (OutputDatumHash),
PubKeyHash,
Redeemer (Redeemer),
ScriptContext (..),
ScriptPurpose (Spending),
TokenName (TokenName),
TxInInfo (TxInInfo),
TxInfo (
TxInfo,
txInfoDCert,
txInfoData,
txInfoFee,
txInfoId,
txInfoInputs,
txInfoMint,
txInfoOutputs,
txInfoSignatories,
txInfoValidRange,
txInfoWdrl
),
TxInfo (..),
TxOut (..),
TxOutRef (TxOutRef),
Validator,
ValidatorHash (ValidatorHash),
Value,
toBuiltinData,
)
import PlutusLedgerApi.V1.Interval qualified as Interval (always)
import PlutusLedgerApi.V1.Value qualified as Value (singleton)
import PlutusTx.AssocMap qualified as AssocMap
import Sample.Shared (deterministicTracingConfing)
import Test.Util (scriptCredentials, userCredentials)
-- | A sample Currency Symbol.
@ -81,7 +73,8 @@ inputGAT =
TxOut
{ txOutAddress = Address (ScriptCredential $ validatorHash validator) Nothing
, txOutValue = Value.singleton currSymbol validatorHashTN 1 -- Stake ST
, txOutDatumHash = Just (DatumHash "")
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Create an input given the index of the treasury and the 'Value' at this input.
@ -92,7 +85,8 @@ inputTreasury indx val =
TxOut
{ txOutAddress = Address (treasuries !! indx) Nothing
, txOutValue = val
, txOutDatumHash = Just (DatumHash "")
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Create a input given the index of the user and the 'Value' at this input.
@ -103,7 +97,8 @@ inputUser indx val =
TxOut
{ txOutAddress = Address (users !! indx) Nothing
, txOutValue = val
, txOutDatumHash = Just (DatumHash "")
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Create a input representing the collateral given by a user.
@ -114,7 +109,8 @@ inputCollateral indx =
TxOut
{ txOutAddress = Address (users !! indx) Nothing
, txOutValue = Value.singleton "" "" 2000000
, txOutDatumHash = Just (DatumHash "")
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Create an output at the nth treasury with the given 'Value'.
@ -123,7 +119,8 @@ outputTreasury indx val =
TxOut
{ txOutAddress = Address (treasuries !! indx) Nothing
, txOutValue = val
, txOutDatumHash = Nothing
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Create an output at the nth user with the given 'Value'.
@ -132,7 +129,8 @@ outputUser indx val =
TxOut
{ txOutAddress = Address (users !! indx) Nothing
, txOutValue = val
, txOutDatumHash = Nothing
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Create a list of the outputs that are required as encoded in 'TreasuryWithdrawalDatum'.
@ -143,12 +141,13 @@ buildReceiversOutputFromDatum (TreasuryWithdrawalDatum xs _) = f <$> xs
TxOut
{ txOutAddress = Address (fst x) Nothing
, txOutValue = snd x
, txOutDatumHash = Nothing
, txOutDatum = OutputDatumHash (DatumHash "")
, txOutReferenceScript = Nothing
}
-- | Effect validator instance.
validator :: Validator
validator = mkValidator def $ treasuryWithdrawalValidator currSymbol
validator = mkValidator deterministicTracingConfing $ treasuryWithdrawalValidator currSymbol
-- | 'TokenName' that represents the hash of the 'Agora.Stake.Stake' validator.
validatorHashTN :: TokenName
@ -156,20 +155,25 @@ validatorHashTN = let ValidatorHash vh = validatorHash validator in TokenName vh
buildScriptContext :: [TxInInfo] -> [TxOut] -> ScriptContext
buildScriptContext inputs outputs =
ScriptContext
{ scriptContextTxInfo =
TxInfo
{ txInfoInputs = inputs
, txInfoOutputs = outputs
, txInfoFee = Value.singleton "" "" 2
, txInfoMint = Value.singleton currSymbol validatorHashTN (-1)
, txInfoDCert = []
, txInfoWdrl = []
, txInfoValidRange = Interval.always
, txInfoSignatories = [signer]
, txInfoData = []
, txInfoId = "0b2086cbf8b6900f8cb65e012de4516cb66b5cb08a9aaba12a8b88be"
}
, scriptContextPurpose =
Spending (TxOutRef "0b2086cbf8b6900f8cb65e012de4516cb66b5cb08a9aaba12a8b88be" 1)
}
let spending = Spending (TxOutRef "0b2086cbf8b6900f8cb65e012de4516cb66b5cb08a9aaba12a8b88be" 1)
in ScriptContext
{ scriptContextTxInfo =
TxInfo
{ txInfoInputs = inputs
, txInfoReferenceInputs = []
, txInfoOutputs = outputs
, txInfoFee = Value.singleton "" "" 2
, txInfoMint = Value.singleton currSymbol validatorHashTN (-1)
, txInfoDCert = []
, txInfoWdrl = AssocMap.empty
, txInfoValidRange = Interval.always
, txInfoSignatories = [signer]
, txInfoData = AssocMap.empty
, txInfoRedeemers =
AssocMap.fromList
[ (spending, Redeemer $ toBuiltinData ())
]
, txInfoId = "0b2086cbf8b6900f8cb65e012de4516cb66b5cb08a9aaba12a8b88be"
}
, scriptContextPurpose = spending
}