Skip to content

Commit c2f3061

Browse files
committed
wip
1 parent 55059dc commit c2f3061

File tree

3 files changed

+102
-30
lines changed

3 files changed

+102
-30
lines changed

cardano-dex-contracts-onchain/ErgoDex/PContracts/PVestingWithPeriod.hs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ newtype VestingWithPeriodConfig (s :: S)
6060
)
6161
deriving stock (GHC.Generic)
6262
deriving
63-
(PIsData, PDataFields, PlutusType)
63+
(PIsData, PDataFields, PlutusType, PEq)
6464

6565
instance DerivePlutusType VestingWithPeriodConfig where type DPTStrat _ = PlutusTypeData
6666

6767
instance PUnsafeLiftDecl VestingWithPeriodConfig where type PLifted VestingWithPeriodConfig = VWP.VestingWithPeriodConfig
6868
deriving via (DerivePConstantViaData VWP.VestingWithPeriodConfig VestingWithPeriodConfig) instance (PConstantDecl VWP.VestingWithPeriodConfig)
6969

70+
instance PTryFrom PData (PAsData VestingWithPeriodConfig)
71+
7072
vestingWithPeriodValidatorT :: ClosedTerm (VestingWithPeriodConfig :--> VestingWithPeriodRedeemer :--> PScriptContext :--> PBool)
7173
vestingWithPeriodValidatorT = plam $ \conf' redeemer' ctx' -> unTermCont $ do
7274
let ctx = pfield @"txInfo" # ctx'
@@ -100,18 +102,30 @@ vestingWithPeriodValidatorT = plam $ \conf' redeemer' ctx' -> unTermCont $ do
100102

101103
correctReward <-
102104
tlet $
103-
isLastPeriod #|| (checkRewardCorrectness # ctx' # totalVested # periodVested # vestingPeriodIdx # vestingAC)
105+
isLastPeriod #|| (checkRewardAndDatumCorrectness # ctx' # conf' # totalVested # periodVested # vestingPeriodIdx # vestingAC)
104106
pure $ validTime
105107
#&& validSignature
106108
#&& correctReward
107109

108-
checkRewardCorrectness :: Term s (PScriptContext :--> PInteger :--> PInteger :--> PInteger :--> PAssetClass :--> PBool)
109-
checkRewardCorrectness =
110-
plam $ \ctx totalVested periodVested periodId vestingAC -> unTermCont $ do
110+
checkRewardAndDatumCorrectness :: Term s (PScriptContext :--> VestingWithPeriodConfig :--> PInteger :--> PInteger :--> PInteger :--> PAssetClass :--> PBool)
111+
checkRewardAndDatumCorrectness =
112+
plam $ \ctx prevCfg totalVested periodVested periodId vestingAC -> unTermCont $ do
111113
let
112114
selfOutputsList = getContinuingOutputs # ctx
113115
selfOutput = phead # selfOutputsList
114-
selfValue = pfield @"value" # selfOutput
116+
selfValue = pfield @"value" # selfOutput
117+
118+
txOutDatum <- tletField @"datum" selfOutput
119+
120+
POutputDatum txOutOutputDatum <- pmatchC txOutDatum
121+
122+
rawDatum <- tletField @"outputDatum" txOutOutputDatum
123+
124+
PDatum vestingDatumRaw <- pmatchC rawDatum
125+
126+
newVestingConfig <- tletUnwrap $ ptryFromData @(VestingWithPeriodConfig) $ vestingDatumRaw
127+
let
115128
correctOutQty = totalVested - (periodId * periodVested)
116129
realOutQty = assetClassValueOf # selfValue # vestingAC
117-
pure $ realOutQty #== correctOutQty
130+
correctConfigs = prevCfg #== newVestingConfig
131+
pure $ (realOutQty #== correctOutQty) #&& correctConfigs

cardano-dex-contracts-onchain/test/Spec.hs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ main = do
1919
defaultMain tests
2020

2121
tests = testGroup "Contracts"
22-
[ checkStakeChangeMintingPolicy
23-
, checkPool
24-
, checkPoolRedeemer
25-
, checkRedeem
26-
, checkRedeemIdentity
27-
, checkRedeemIsFair
28-
, checkRedeemRedeemer
29-
, checkDeposit
30-
, checkDepositChange
31-
, checkDepositRedeemer
32-
, checkDepositIdentity
33-
, checkDepositLq
34-
, checkDepositTokenReward
35-
, checkSwap
36-
, checkSwapRedeemer
37-
, checkSwapIdentity
38-
, checkVesting
39-
, checkVestingWithPeriod
40-
, checkPkhLockStaking
22+
[
23+
-- checkStakeChangeMintingPolicy
24+
-- , checkPool
25+
-- , checkPoolRedeemer
26+
-- , checkRedeem
27+
-- , checkRedeemIdentity
28+
-- , checkRedeemIsFair
29+
-- , checkRedeemRedeemer
30+
-- , checkDeposit
31+
-- , checkDepositChange
32+
-- , checkDepositRedeemer
33+
-- , checkDepositIdentity
34+
-- , checkDepositLq
35+
-- , checkDepositTokenReward
36+
-- , checkSwap
37+
-- , checkSwapRedeemer
38+
-- , checkSwapIdentity
39+
-- , checkVesting
40+
checkVestingWithPeriod
41+
-- , checkPkhLockStaking
4142
]

cardano-dex-contracts-onchain/test/Tests/VestingWithPeriod.hs

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ import Gen.Models
2828
import Debug.Trace
2929

3030
checkVestingWithPeriod = testGroup "checkVestingWithPeriod"
31-
[ HH.testProperty "correct_vesting" correctVesting
32-
, HH.testProperty "incorrect_signature_qty_vesting" incorrectSignatureQtyVesting
33-
, HH.testProperty "incorrect_time_vesting" incorrectTimeVesting
34-
, HH.testProperty "incorrect_value_vesting" incorrectValueVesting
31+
[
32+
-- HH.testProperty "correct_vesting" correctVesting
33+
-- , HH.testProperty "incorrect_signature_qty_vesting" incorrectSignatureQtyVesting
34+
-- , HH.testProperty "incorrect_time_vesting" incorrectTimeVesting
35+
-- , HH.testProperty "incorrect_value_vesting" incorrectValueVesting
36+
, HH.testProperty "incorrect_datum_in_output" incorrectDatumVesting
3537
]
3638

3739
correctVesting :: Property
@@ -290,4 +292,59 @@ incorrectValueVesting = property $ do
290292
in (newVestingBoxInput, prevResult && isRight resultEither)
291293
) (initialVestingWPTxIn, True) periodsList
292294

295+
result === False
296+
297+
incorrectDatumVesting :: Property
298+
incorrectDatumVesting = property $ do
299+
let
300+
range = linear 24 4096
301+
firstPkh <- forAll genPkh
302+
secondPkh <- forAll genPkh
303+
vestingOutTxRef <- forAll genTxOutRef
304+
vestingAC <- forAll genAssetClass
305+
vestingStartInt <- (forAll $ int range)
306+
307+
vestingPeriodDurationInt <- forAll $ int range
308+
309+
totalVestedInt <- forAll $ int range
310+
let
311+
vestedRange = linear 24 totalVestedInt
312+
313+
maxPeriodIdInt <- forAll $ int vestedRange
314+
let
315+
316+
vestingStart = toInteger vestingStartInt
317+
318+
vestingPeriodDuration = toInteger vestingPeriodDurationInt
319+
320+
totalVested = toInteger totalVestedInt
321+
maxPeriodId = toInteger maxPeriodIdInt
322+
323+
periodVested = totalVested `div` maxPeriodId
324+
325+
vestingWPCfg = genVestingWithPeriodConfig vestingStart vestingPeriodDuration totalVested periodVested [firstPkh, secondPkh] vestingAC
326+
vestingWPCfgData = toData vestingWPCfg
327+
vestingWPDatum = OutputDatum $ mkDatum vestingWPCfg
328+
initialVestingWPTxIn = genVestingWPTxIn vestingOutTxRef vestingWPDatum vestingAC totalVested
329+
periodsList = [1..maxPeriodId]
330+
331+
deadline = vestingStart + vestingPeriodDuration * periodId
332+
333+
vestingRedeemToData = toData $ PPVestingWP.VestingWithPeriodRedeemer 0 periodId
334+
335+
newVestingBox = genVestingWPTxOut NoOutputDatum vestingAC (totalVested - periodId * periodVested)
336+
337+
newVestingBoxInput = mkTxIn vestingOutTxRef newVestingBox
338+
339+
userTxOut = genUserTxOut vestingAC periodVested firstPkh
340+
341+
txInfo = mkVestingTxInfo [vestingBox] [newVestingBox, userTxOut] (deadline + 5) (deadline + 10) [secondPkh]
342+
343+
purpose = mkPurpose vestingOutTxRef
344+
345+
cxtData = toData $ mkContext txInfo purpose
346+
347+
resultEither = eraseRight $ evalWithArgs (wrapValidator PVestingWP.vestingWithPeriodValidatorT) [vestingWPCfgData, vestingRedeemToData, cxtData]
348+
349+
293350
result === False

0 commit comments

Comments
 (0)