|
1 | 1 | module HydraAuctionOnchain.Types.StandingBidState
|
2 | 2 | ( PStandingBidState (PStandingBidState)
|
| 3 | + , pvalidateNewBid |
3 | 4 | ) where
|
4 | 5 |
|
5 |
| -import HydraAuctionOnchain.Types.BidTerms (PBidTerms) |
6 |
| -import Plutarch.Api.V2 (PMaybeData) |
| 6 | +import HydraAuctionOnchain.Types.AuctionTerms (PAuctionTerms) |
| 7 | +import HydraAuctionOnchain.Types.BidTerms (PBidTerms, pvalidateBidTerms) |
| 8 | +import Plutarch.Api.V2 (PCurrencySymbol, PMaybeData) |
| 9 | +import Plutarch.Extra.Maybe (pmaybeData) |
| 10 | +import Plutarch.Monadic qualified as P |
7 | 11 |
|
8 | 12 | newtype PStandingBidState (s :: S) = PStandingBidState (Term s (PMaybeData PBidTerms))
|
9 | 13 | deriving stock (Generic)
|
10 | 14 | deriving anyclass (PlutusType, PIsData, PShow, PEq)
|
11 | 15 |
|
12 | 16 | instance DerivePlutusType PStandingBidState where
|
13 | 17 | type DPTStrat _ = PlutusTypeNewtype
|
| 18 | + |
| 19 | +instance PTryFrom PData PStandingBidState |
| 20 | + |
| 21 | +pvalidateNewBid |
| 22 | + :: Term |
| 23 | + s |
| 24 | + ( PCurrencySymbol |
| 25 | + :--> PAuctionTerms |
| 26 | + :--> PStandingBidState |
| 27 | + :--> PStandingBidState |
| 28 | + :--> PBool |
| 29 | + ) |
| 30 | +pvalidateNewBid = phoistAcyclic $ |
| 31 | + plam $ \auctionCs auctionTerms oldBidState newBidState -> |
| 32 | + pmaybeData |
| 33 | + # pcon PFalse -- The new bid state should not be empty. |
| 34 | + # plam |
| 35 | + ( \newTerms -> |
| 36 | + (pvalidateBidTerms # auctionCs # auctionTerms # newTerms) |
| 37 | + #&& (pvalidateCompareBids # auctionTerms # oldBidState # newTerms) |
| 38 | + ) |
| 39 | + # pto newBidState |
| 40 | + |
| 41 | +pvalidateCompareBids :: Term s (PAuctionTerms :--> PStandingBidState :--> PBidTerms :--> PBool) |
| 42 | +pvalidateCompareBids = phoistAcyclic $ |
| 43 | + plam $ \auctionTerms oldBidState newTerms -> |
| 44 | + pmaybeData |
| 45 | + # (pvalidateStartingBid # auctionTerms # newTerms) |
| 46 | + # plam (\oldTerms -> pvalidateBidIncrement # auctionTerms # oldTerms # newTerms) |
| 47 | + # pto oldBidState |
| 48 | + |
| 49 | +-- The first bid's price is no smaller than the auction's starting price. |
| 50 | +pvalidateStartingBid :: Term s (PAuctionTerms :--> PBidTerms :--> PBool) |
| 51 | +pvalidateStartingBid = phoistAcyclic $ |
| 52 | + plam $ \auctionTerms newTerms -> P.do |
| 53 | + startingBid <- plet $ pfromData $ pfield @"startingBid" # auctionTerms |
| 54 | + bidPrice <- plet $ pfromData $ pfield @"btPrice" # newTerms |
| 55 | + startingBid #<= bidPrice |
| 56 | + |
| 57 | +-- The difference between the old and new bid price is no smaller than |
| 58 | +-- the auction's minimum bid increment. |
| 59 | +pvalidateBidIncrement :: Term s (PAuctionTerms :--> PBidTerms :--> PBidTerms :--> PBool) |
| 60 | +pvalidateBidIncrement = phoistAcyclic $ |
| 61 | + plam $ \auctionTerms oldTerms newTerms -> P.do |
| 62 | + oldBidPrice <- plet $ pfromData $ pfield @"btPrice" # oldTerms |
| 63 | + newBidPrice <- plet $ pfromData $ pfield @"btPrice" # newTerms |
| 64 | + minBidIncrement <- plet $ pfromData $ pfield @"minBidIncrement" # auctionTerms |
| 65 | + oldBidPrice + minBidIncrement #<= newBidPrice |
0 commit comments