-
Notifications
You must be signed in to change notification settings - Fork 213
/
EscrowImpl.hs
378 lines (343 loc) · 14.5 KB
/
EscrowImpl.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:debug-context #-}
{- START OPTIONS_GHC -}
{-# OPTIONS_GHC -g -fplugin-opt PlutusTx.Plugin:coverage-all #-}
{- END OPTIONS_GHC -}
-- | A general-purpose escrow contract in Plutus
module EscrowImpl(
-- $escrow
Escrow
, EscrowError(..)
, AsEscrowError(..)
, EscrowParams(..)
, EscrowTarget(..)
, payToScriptTarget
, payToPaymentPubKeyTarget
, targetTotal
, escrowContract
, payRedeemRefund
, typedValidator
-- * Actions
, pay
, payEp
, redeem
, redeemEp
, refund
, refundEp
, RedeemFailReason(..)
, RedeemSuccess(..)
, RefundSuccess(..)
, EscrowSchema
-- * Exposed for test endpoints
, Action(..)
-- * Coverage
, covIdx
) where
import Control.Lens (makeClassyPrisms, review, view)
import Control.Monad (Monad ((>>)), void)
import Control.Monad.Error.Lens (throwing)
import Data.Aeson (FromJSON, ToJSON)
import GHC.Generics (Generic)
import Ledger (POSIXTime, PaymentPubKeyHash (unPaymentPubKeyHash), ScriptContext (ScriptContext, scriptContextTxInfo),
TxId, getCardanoTxId, interval, scriptOutputsAt, txSignedBy, valuePaidTo)
import Ledger qualified
import Ledger.Constraints (TxConstraints)
import Ledger.Constraints qualified as Constraints
import Ledger.Interval (after, before, from)
import Ledger.Interval qualified as Interval
import Ledger.Tx qualified as Tx
import Ledger.Typed.Scripts (TypedValidator)
import Ledger.Typed.Scripts qualified as Scripts
import Ledger.Value (Value, geq, lt)
import Plutus.Script.Utils.V1.Scripts qualified as Scripts
import Plutus.V1.Ledger.Api (Datum (Datum), DatumHash, ValidatorHash)
import Plutus.V1.Ledger.Contexts (ScriptContext (ScriptContext, scriptContextTxInfo), TxInfo (txInfoValidRange))
import Plutus.Contract (AsContractError (_ContractError), Contract, ContractError, Endpoint, HasEndpoint, Promise,
awaitTime, currentTime, endpoint, mapError, mkTxConstraints, ownPaymentPubKeyHash, promiseMap,
selectList, submitUnbalancedTx, type (.\/), utxosAt, waitNSlots)
import Plutus.Contract.Typed.Tx qualified as Typed
import PlutusTx qualified
{- START imports -}
import PlutusTx.Code qualified as PlutusTx
import PlutusTx.Coverage qualified as PlutusTx
{- END imports -}
import PlutusTx.Prelude (Bool (False), Either (Left, Right), all, either, foldl, id, mempty, traceIfFalse, ($), (&&),
(+), (-), (.), (<$>), (==), (>=))
import Prelude (Semigroup ((<>)), foldMap)
import Prelude qualified as Haskell
type EscrowSchema =
Endpoint "pay-escrow" Value
.\/ Endpoint "redeem-escrow" ()
.\/ Endpoint "refund-escrow" ()
data RedeemFailReason = DeadlinePassed | NotEnoughFundsAtAddress
deriving stock (Haskell.Eq, Haskell.Show, Generic)
deriving anyclass (ToJSON, FromJSON)
data EscrowError =
RedeemFailed RedeemFailReason
| RefundFailed
| EContractError ContractError
deriving stock (Haskell.Show, Generic)
deriving anyclass (ToJSON, FromJSON)
makeClassyPrisms ''EscrowError
instance AsContractError EscrowError where
_ContractError = _EContractError
-- $escrow
-- The escrow contract implements the exchange of value between multiple
-- parties. It is defined by a list of targets (public keys and script
-- addresses, each associated with a value). It works similar to the
-- crowdfunding contract in that the contributions can be made independently,
-- and the funds can be unlocked only by a transaction that pays the correct
-- amount to each target. A refund is possible if the outputs locked by the
-- contract have not been spent by the deadline. (Compared to the crowdfunding
-- contract, the refund policy is simpler because here because there is no
-- "collection period" during which the outputs may be spent after the deadline
-- has passed. This is because we're assuming that the participants in the
-- escrow contract will make their deposits as quickly as possible after
-- agreeing on a deal)
--
-- The contract supports two modes of operation, manual and automatic. In
-- manual mode, all actions are driven by endpoints that exposed via 'payEp'
-- 'redeemEp' and 'refundEp'. In automatic mode, the 'pay', 'redeem' and
-- 'refund'actions start immediately. This mode is useful when the escrow is
-- called from within another contract, for example during setup (collection of
-- the initial deposits).
-- | Defines where the money should go. Usually we have `d = Datum` (when
-- defining `EscrowTarget` values in off-chain code). Sometimes we have
-- `d = DatumHash` (when checking the hashes in on-chain code)
data EscrowTarget d =
PaymentPubKeyTarget PaymentPubKeyHash Value
| ScriptTarget ValidatorHash d Value
deriving (Haskell.Functor)
PlutusTx.makeLift ''EscrowTarget
-- | An 'EscrowTarget' that pays the value to a public key address.
payToPaymentPubKeyTarget :: PaymentPubKeyHash -> Value -> EscrowTarget d
payToPaymentPubKeyTarget = PaymentPubKeyTarget
-- | An 'EscrowTarget' that pays the value to a script address, with the
-- given data script.
payToScriptTarget :: ValidatorHash -> Datum -> Value -> EscrowTarget Datum
payToScriptTarget = ScriptTarget
-- | Definition of an escrow contract, consisting of a deadline and a list of targets
data EscrowParams d =
EscrowParams
{ escrowDeadline :: POSIXTime
-- ^ Latest point at which the outputs may be spent.
, escrowTargets :: [EscrowTarget d]
-- ^ Where the money should go. For each target, the contract checks that
-- the output 'mkTxOutput' of the target is present in the spending
-- transaction.
} deriving (Haskell.Functor)
PlutusTx.makeLift ''EscrowParams
-- | The total 'Value' that must be paid into the escrow contract
-- before it can be unlocked
targetTotal :: EscrowParams d -> Value
targetTotal = foldl (\vl tgt -> vl + targetValue tgt) mempty . escrowTargets
-- | The 'Value' specified by an 'EscrowTarget'
targetValue :: EscrowTarget d -> Value
targetValue = \case
PaymentPubKeyTarget _ vl -> vl
ScriptTarget _ _ vl -> vl
-- | Create a 'Ledger.TxOut' value for the target
mkTx :: EscrowTarget Datum -> TxConstraints Action PaymentPubKeyHash
mkTx = \case
PaymentPubKeyTarget pkh vl ->
Constraints.mustPayToPubKey pkh vl
ScriptTarget vs ds vl ->
Constraints.mustPayToOtherScript vs ds vl
data Action = Redeem | Refund
data Escrow
instance Scripts.ValidatorTypes Escrow where
type instance RedeemerType Escrow = Action
type instance DatumType Escrow = PaymentPubKeyHash
PlutusTx.unstableMakeIsData ''Action
PlutusTx.makeLift ''Action
{-# INLINABLE meetsTarget #-}
-- | @ptx `meetsTarget` tgt@ if @ptx@ pays at least @targetValue tgt@ to the
-- target address.
--
-- The reason why this does not require the target amount to be equal
-- to the actual amount is to enable any excess funds consumed by the
-- spending transaction to be paid to target addresses. This may happen if
-- the target address is also used as a change address for the spending
-- transaction, and allowing the target to be exceed prevents outsiders from
-- poisoning the contract by adding arbitrary outputs to the script address.
meetsTarget :: TxInfo -> EscrowTarget DatumHash -> Bool
meetsTarget ptx = \case
PaymentPubKeyTarget pkh vl ->
valuePaidTo ptx (unPaymentPubKeyHash pkh) `geq` vl
ScriptTarget validatorHash dataValue vl ->
case scriptOutputsAt validatorHash ptx of
[(dataValue', vl')] ->
traceIfFalse "dataValue" (dataValue' == dataValue)
&& traceIfFalse "value" (vl' `geq` vl)
_ -> False
{-# INLINABLE validate #-}
validate :: EscrowParams DatumHash -> PaymentPubKeyHash -> Action -> ScriptContext -> Bool
validate EscrowParams{escrowDeadline, escrowTargets} contributor action ScriptContext{scriptContextTxInfo} =
case action of
Redeem ->
traceIfFalse "escrowDeadline-after" (escrowDeadline `after` txInfoValidRange scriptContextTxInfo)
&& traceIfFalse "meetsTarget" (all (meetsTarget scriptContextTxInfo) escrowTargets)
Refund ->
traceIfFalse "escrowDeadline-before" ((escrowDeadline - 1) `before` txInfoValidRange scriptContextTxInfo)
&& traceIfFalse "txSignedBy" (scriptContextTxInfo `txSignedBy` unPaymentPubKeyHash contributor)
{- START typedValidator -}
typedValidator :: EscrowParams Datum -> Scripts.TypedValidator Escrow
typedValidator escrow = go (Haskell.fmap Scripts.datumHash escrow) where
go = Scripts.mkTypedValidatorParam @Escrow
$$(PlutusTx.compile [|| validate ||])
$$(PlutusTx.compile [|| wrap ||])
wrap = Scripts.mkUntypedValidator
{- END typedValidator -}
escrowContract
:: EscrowParams Datum
-> Contract () EscrowSchema EscrowError ()
escrowContract escrow =
let inst = typedValidator escrow
payAndRefund = endpoint @"pay-escrow" $ \vl -> do
_ <- pay inst escrow vl
_ <- awaitTime $ escrowDeadline escrow
refund inst escrow
in selectList
[ void payAndRefund
, void $ redeemEp escrow
]
-- | 'pay' with an endpoint that gets the owner's public key and the
-- contribution.
payEp ::
forall w s e.
( HasEndpoint "pay-escrow" Value s
, AsEscrowError e
)
=> EscrowParams Datum
-> Promise w s e TxId
payEp escrow = promiseMap
(mapError (review _EContractError))
(endpoint @"pay-escrow" $ pay (typedValidator escrow) escrow)
-- | Pay some money into the escrow contract.
pay ::
forall w s e.
( AsContractError e
)
=> TypedValidator Escrow
-- ^ The instance
-> EscrowParams Datum
-- ^ The escrow contract
-> Value
-- ^ How much money to pay in
-> Contract w s e TxId
pay inst escrow vl = do
pk <- ownPaymentPubKeyHash
let tx = Constraints.mustPayToTheScript pk vl
<> Constraints.mustValidateIn (Ledger.interval 1 (escrowDeadline escrow))
utx <- mkTxConstraints (Constraints.typedValidatorLookups inst) tx
getCardanoTxId <$> submitUnbalancedTx (Constraints.adjustUnbalancedTx utx)
newtype RedeemSuccess = RedeemSuccess TxId
deriving (Haskell.Eq, Haskell.Show)
-- | 'redeem' with an endpoint.
redeemEp ::
forall w s e.
( HasEndpoint "redeem-escrow" () s
, AsEscrowError e
)
=> EscrowParams Datum
-> Promise w s e RedeemSuccess
redeemEp escrow = promiseMap
(mapError (review _EscrowError))
(endpoint @"redeem-escrow" $ \() -> redeem (typedValidator escrow) escrow)
-- | Redeem all outputs at the contract address using a transaction that
-- has all the outputs defined in the contract's list of targets.
redeem ::
forall w s e.
( AsEscrowError e
)
=> TypedValidator Escrow
-> EscrowParams Datum
-> Contract w s e RedeemSuccess
redeem inst escrow = mapError (review _EscrowError) $ do
let addr = Scripts.validatorAddress inst
current <- currentTime
unspentOutputs <- utxosAt addr
let
valRange = Interval.to (Haskell.pred $ escrowDeadline escrow)
tx = Typed.collectFromScript unspentOutputs Redeem
<> foldMap mkTx (escrowTargets escrow)
<> Constraints.mustValidateIn valRange
if current >= escrowDeadline escrow
then throwing _RedeemFailed DeadlinePassed
else if foldMap (view Tx.ciTxOutValue) unspentOutputs `lt` targetTotal escrow
then throwing _RedeemFailed NotEnoughFundsAtAddress
else do
utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst
<> Constraints.unspentOutputs unspentOutputs
) tx
RedeemSuccess . getCardanoTxId <$> submitUnbalancedTx (Constraints.adjustUnbalancedTx utx)
newtype RefundSuccess = RefundSuccess TxId
deriving newtype (Haskell.Eq, Haskell.Show, Generic)
deriving anyclass (ToJSON, FromJSON)
-- | 'refund' with an endpoint.
refundEp ::
forall w s.
( HasEndpoint "refund-escrow" () s
)
=> EscrowParams Datum
-> Promise w s EscrowError RefundSuccess
refundEp escrow = endpoint @"refund-escrow" $ \() -> refund (typedValidator escrow) escrow
-- | Claim a refund of the contribution.
refund ::
forall w s.
TypedValidator Escrow
-> EscrowParams Datum
-> Contract w s EscrowError RefundSuccess
refund inst escrow = do
pk <- ownPaymentPubKeyHash
unspentOutputs <- utxosAt (Scripts.validatorAddress inst)
let flt _ ciTxOut = either id Scripts.datumHash (Tx._ciTxOutDatum ciTxOut) == Scripts.datumHash (Datum (PlutusTx.toBuiltinData pk))
tx' = Typed.collectFromScriptFilter flt unspentOutputs Refund
<> Constraints.mustValidateIn (from (Haskell.succ $ escrowDeadline escrow))
if Constraints.modifiesUtxoSet tx'
then do
utx <- mkTxConstraints ( Constraints.typedValidatorLookups inst
<> Constraints.unspentOutputs unspentOutputs
) tx'
RefundSuccess . getCardanoTxId <$> submitUnbalancedTx (Constraints.adjustUnbalancedTx utx)
else throwing _RefundFailed ()
-- | Pay some money into the escrow contract. Then release all funds to their
-- specified targets if enough funds were deposited before the deadline,
-- or reclaim the contribution if the goal has not been met.
payRedeemRefund ::
forall w s.
EscrowParams Datum
-> Value
-> Contract w s EscrowError (Either RefundSuccess RedeemSuccess)
payRedeemRefund params vl = do
let inst = typedValidator params
go = do
cur <- utxosAt (Scripts.validatorAddress inst)
let presentVal = foldMap (view Tx.ciTxOutValue) cur
if presentVal `geq` targetTotal params
then Right <$> redeem inst params
else do
time <- currentTime
if time >= escrowDeadline params
then Left <$> refund inst params
else waitNSlots 1 >> go
-- Pay the value 'vl' into the contract
_ <- pay inst params vl
go
{- START covIdx -}
covIdx :: PlutusTx.CoverageIndex
covIdx = PlutusTx.getCovIdx $$(PlutusTx.compile [|| validate ||])
{- END covIdx -}