Skip to content

Commit 4befdda

Browse files
committed
feat(standing_bid): use safe function to get own input
1 parent 3172743 commit 4befdda

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

src/HydraAuctionOnchain/Errors/StandingBid.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import HydraAuctionOnchain.Types.Error (ErrorCodePrefix (errorCodePrefix))
77

88
data PStandingBidError (s :: S)
99
= -- Common errors
10-
StandingBid'Error'OwnInputMissingToken
10+
StandingBid'Error'MissingStandingBidInput
11+
| StandingBid'Error'OwnInputMissingToken
1112
| StandingBid'Error'UnexpectedTokensMintedBurned
1213
| -- NewBid errors
1314
StandingBid'NewBid'Error'MissingOwnOutput

src/HydraAuctionOnchain/Helpers.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module HydraAuctionOnchain.Helpers
55
, pfindUnique
66
, pfindUniqueInputWithToken
77
, pfindUniqueOutputWithAddress
8+
, pgetOwnInput
89
, pintervalFiniteClosedOpen
910
, pserialise
1011
, putxoAddress
@@ -18,6 +19,8 @@ import Plutarch.Api.V2
1819
, PInterval (PInterval)
1920
, PLowerBound (PLowerBound)
2021
, POutputDatum (POutputDatum)
22+
, PScriptContext
23+
, PScriptPurpose (PSpending)
2124
, PTokenName
2225
, PTxInInfo
2326
, PTxInfo
@@ -70,6 +73,18 @@ pfindUniqueOutputWithAddress = phoistAcyclic $
7073
#$ pfield @"outputs"
7174
# txInfo
7275

76+
pgetOwnInput :: Term s (PScriptContext :--> PMaybe PTxInInfo)
77+
pgetOwnInput = phoistAcyclic $
78+
plam $ \ctx ->
79+
pmatch (pfield @"purpose" # ctx) $ \case
80+
PSpending rec -> P.do
81+
ownOutRef <- plet $ pfield @"_0" # rec
82+
inputs <- plet $ pfromData $ pfield @"inputs" #$ pfield @"txInfo" # ctx
83+
pfind
84+
# plam (\utxo -> pfield @"outRef" # utxo #== ownOutRef)
85+
# inputs
86+
_ -> pnothing
87+
7388
pintervalFiniteClosedOpen :: PIsData a => Term s (a :--> a :--> PInterval a)
7489
pintervalFiniteClosedOpen = phoistAcyclic $
7590
plam $ \a b ->

src/HydraAuctionOnchain/Validators/StandingBid.hs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import HydraAuctionOnchain.Helpers
99
( pdecodeInlineDatum
1010
, pfindUniqueInputWithToken
1111
, pfindUniqueOutputWithAddress
12+
, pgetOwnInput
1213
, putxoAddress
1314
)
1415
import HydraAuctionOnchain.MintingPolicies.Auction
@@ -30,7 +31,7 @@ import Plutarch.Api.V2
3031
)
3132
import Plutarch.Extra.Interval (pcontains)
3233
import Plutarch.Extra.Maybe (pmaybe)
33-
import Plutarch.Extra.ScriptContext (ptryFromRedeemer, ptryOwnInput, ptxSignedBy)
34+
import Plutarch.Extra.ScriptContext (ptryFromRedeemer, ptxSignedBy)
3435
import Plutarch.Monadic qualified as P
3536

3637
--------------------------------------------------------------------------------
@@ -63,15 +64,21 @@ standingBidValidator
6364
)
6465
standingBidValidator = phoistAcyclic $
6566
plam $ \auctionCs auctionTerms oldBidState redeemer ctx -> P.do
66-
ownInput <- plet $ ptryOwnInput # ctx
67+
-- (STBD0) The validator's own input should exist.
68+
ownInput <-
69+
plet $
70+
passertMaybe
71+
$(errCode StandingBid'Error'MissingStandingBidInput)
72+
(pgetOwnInput # ctx)
73+
6774
txInfo <- plet $ pfield @"txInfo" # ctx
6875

69-
-- (STBD0) The standing bid input should contain the standing
76+
-- (STBD1) The standing bid input should contain the standing
7077
-- bid token.
7178
passert $(errCode StandingBid'Error'OwnInputMissingToken) $
7279
ptxOutContainsStandingBidToken # auctionCs #$ pfield @"resolved" # ownInput
7380

74-
-- (STBD1) There should be no tokens minted or burned.
81+
-- (STBD2) There should be no tokens minted or burned.
7582
mintValue <- plet $ pfield @"mint" # txInfo
7683
passert $(errCode StandingBid'Error'UnexpectedTokensMintedBurned) $
7784
pfromData mintValue #== mempty
@@ -100,32 +107,32 @@ pcheckNewBid
100107
)
101108
pcheckNewBid = phoistAcyclic $
102109
plam $ \txInfo auctionCs auctionTerms ownInput oldBidState -> P.do
103-
-- (STBD2) The standing bid output should exist.
110+
-- (STBD3) The standing bid output should exist.
104111
ownOutput <-
105112
plet $
106113
passertMaybe
107114
$(errCode StandingBid'NewBid'Error'MissingOwnOutput)
108115
(pfindUniqueOutputWithAddress # (putxoAddress # ownInput) # txInfo)
109116

110-
-- (STBD3) The standing bid output should contain the standing
117+
-- (STBD4) The standing bid output should contain the standing
111118
-- bid token.
112119
passert $(errCode StandingBid'NewBid'Error'OwnOutputMissingToken) $
113120
ptxOutContainsStandingBidToken # auctionCs # ownOutput
114121

115-
-- (STBD4) The standing bid output's datum should be decodable
122+
-- (STBD5) The standing bid output's datum should be decodable
116123
-- as a standing bid state.
117124
newBidState <-
118125
plet $
119126
passertMaybe
120127
$(errCode StandingBid'NewBid'Error'FailedToDecodeNewBid)
121128
(pdecodeInlineDatum # ownOutput)
122129

123-
-- (STBD5) The transition from the old bid state to the new
130+
-- (STBD6) The transition from the old bid state to the new
124131
-- bid state should be valid.
125132
passert $(errCode StandingBid'NewBid'Error'InvalidNewBidState) $
126133
pvalidateNewBid # auctionCs # auctionTerms # oldBidState # newBidState
127134

128-
-- (STBD6) The transaction validity should end before the
135+
-- (STBD7) The transaction validity should end before the
129136
-- bidding end time.
130137
txInfoValidRange <- plet $ pfield @"validRange" # txInfo
131138
passert $(errCode StandingBid'NewBid'Error'IncorrectValidityInterval) $
@@ -142,12 +149,12 @@ pcheckMoveToHydra = phoistAcyclic $
142149
plam $ \txInfo auctionTerms -> P.do
143150
txInfoFields <- pletFields @["signatories", "validRange"] txInfo
144151

145-
-- (STBD7) The transaction should be signed by all the delegates.
152+
-- (STBD8) The transaction should be signed by all the delegates.
146153
delegates <- plet $ pfield @"delegates" # auctionTerms
147154
passert $(errCode StandingBid'MoveToHydra'Error'MissingDelegateSignatures) $
148155
pall # plam (\sig -> ptxSignedBy # txInfoFields.signatories # sig) # delegates
149156

150-
-- (STBD8) The transaction validity should end before the
157+
-- (STBD9) The transaction validity should end before the
151158
-- bidding end time.
152159
passert $(errCode StandingBid'MoveToHydra'Error'IncorrectValidityInterval) $
153160
pcontains # (pbiddingPeriod # auctionTerms) # txInfoFields.validRange
@@ -161,15 +168,15 @@ pcheckMoveToHydra = phoistAcyclic $
161168
pcheckConcludeAuction :: Term s (PTxInfo :--> PCurrencySymbol :--> PUnit)
162169
pcheckConcludeAuction = phoistAcyclic $
163170
plam $ \txInfo auctionCs -> P.do
164-
-- (STBD9) There is an input that contains
171+
-- (STBD10) There is an input that contains
165172
-- the auction escrow token.
166173
auctionEscrowUtxo <-
167174
plet $
168175
passertMaybe
169176
$(errCode StandingBid'ConcludeAuction'Error'MissingAuctionEscrowInput)
170177
(pfindUniqueInputWithToken # auctionCs # auctionEscrowTokenName # txInfo)
171178

172-
-- (STBD10) The auction escrow input is being spent with the
179+
-- (STBD11) The auction escrow input is being spent with the
173180
-- `BidderBuys` or `SellerReclaims` redeemer. Implicitly, this
174181
-- means that the auction is concluding with either the winning
175182
-- bidder buying the auction lot or the seller reclaiming it.

0 commit comments

Comments
 (0)