-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathContractTestUtils.purs
205 lines (184 loc) · 6.46 KB
/
ContractTestUtils.purs
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
-- | This module demonstrates how various assertions from `Contract.Test.Utils`
-- | can be used to test `Contract`s. It creates a transaction with metadata
-- | that performs three actions: (1) sends some amount of Ada to the receiver's
-- | address, (2) mints the specified non-Ada value (3) then sends it to the
-- | owner's address with a datum attached.
module Ctl.Examples.ContractTestUtils
( ContractParams
, mkContract
, mkChecks
) where
import Contract.Prelude
import Cardano.Transaction.Builder
( CredentialWitness(PlutusScriptCredential)
, ScriptWitness(ScriptValue)
, TransactionBuilderStep(Pay, MintAsset)
)
import Cardano.Types
( BigNum
, Coin
, ExUnits(ExUnits)
, PaymentCredential(PaymentCredential)
, StakeCredential(StakeCredential)
, TransactionOutput(TransactionOutput)
, _body
, _datum
, _fee
, _output
)
import Cardano.Types.BigNum as BigNum
import Cardano.Types.Credential (Credential(PubKeyHashCredential))
import Cardano.Types.DataHash (hashPlutusData)
import Cardano.Types.Int as Int
import Cardano.Types.PlutusScript (PlutusScript)
import Cardano.Types.RedeemerDatum as RedeemerDatum
import Cardano.Types.ScriptRef (ScriptRef(PlutusScriptRef))
import Contract.Address (Address, PaymentPubKeyHash, StakePubKeyHash, mkAddress)
import Contract.Hashing (datumHash)
import Contract.Log (logInfo')
import Contract.Monad (Contract, liftContractM, liftedM)
import Contract.PlutusData (Datum, OutputDatum(OutputDatumHash))
import Contract.Test.Assert
( ContractCheck
, assertOutputHasDatum
, assertOutputHasRefScript
, assertionToCheck
, checkExUnitsNotExceed
, checkGainAtAddress'
, checkLossAtAddress
, checkTokenGainAtAddress'
, label
)
import Contract.Transaction
( TransactionHash
, TransactionUnspentOutput
, awaitTxConfirmed
, balanceTx
, buildTx
, lookupTxHash
, signTransaction
, submit
)
import Contract.Utxos (utxosAt)
import Contract.Value (CurrencySymbol, TokenName, Value)
import Contract.Value (lovelaceValueOf, singleton) as Value
import Contract.Wallet
( getWalletAddresses
, ownPaymentPubKeyHashes
, ownStakePubKeyHashes
)
import Data.Array (head)
import Data.Lens (view)
import Data.Map as Map
import Effect.Exception (throw)
type ContractParams =
{ receiverPkh :: PaymentPubKeyHash
, receiverSkh :: Maybe StakePubKeyHash
, adaToSend :: Coin
, mintingPolicy :: PlutusScript
, tokensToMint :: Tuple3 CurrencySymbol TokenName BigNum
, datumToAttach :: Datum
}
type ContractResult =
{ txHash :: TransactionHash
, txFinalFee :: Coin
, txOutputUnderTest :: TransactionOutput
}
mkChecks
:: ContractParams
-> Contract (Array (ContractCheck ContractResult))
mkChecks p = do
senderAddress <-
liftedM "Failed to get sender address" $ head <$> getWalletAddresses
receiverAddress <- getReceiverAddress p
let dhash = datumHash p.datumToAttach
pure
[ checkGainAtAddress' (label receiverAddress "Receiver")
(BigNum.toBigInt $ unwrap p.adaToSend)
, checkLossAtAddress (label senderAddress "Sender")
case _ of
Just { txFinalFee } -> pure
( BigNum.toBigInt (unwrap p.adaToSend) + BigNum.toBigInt
(unwrap txFinalFee)
)
Nothing -> liftEffect $
throw "Unable to estimate expected loss in wallet"
, checkTokenGainAtAddress' (label senderAddress "Sender")
( uncurry3 (\cs tn amount -> cs /\ tn /\ BigNum.toBigInt amount)
p.tokensToMint
)
, checkExUnitsNotExceed
(ExUnits { mem: BigNum.fromInt 800, steps: BigNum.fromInt 161100 })
, assertionToCheck "Sender's output has a datum"
\{ txOutputUnderTest } ->
assertOutputHasDatum (Just $ OutputDatumHash dhash)
(label txOutputUnderTest "Sender's output with datum hash")
, assertionToCheck "Output has a reference script"
\{ txOutputUnderTest } ->
assertOutputHasRefScript
(PlutusScriptRef p.mintingPolicy)
(label txOutputUnderTest "Sender's output with reference script")
]
mkContract :: ContractParams -> Contract ContractResult
mkContract p = do
logInfo' "Running Examples.ContractTestUtils"
ownPkh <- liftedM "Failed to get own PKH" $ head <$> ownPaymentPubKeyHashes
ownSkh <- join <<< head <$> ownStakePubKeyHashes
receiverAddress <- mkAddress
(PaymentCredential $ PubKeyHashCredential $ unwrap p.receiverPkh)
(StakeCredential <<< PubKeyHashCredential <<< unwrap <$> p.receiverSkh)
ownAddress <- mkAddress
(PaymentCredential $ PubKeyHashCredential $ unwrap ownPkh)
(StakeCredential <<< PubKeyHashCredential <<< unwrap <$> ownSkh)
let
adaValue :: Value
adaValue = Value.lovelaceValueOf (unwrap p.adaToSend)
nonAdaValue :: Value
nonAdaValue = uncurry3 Value.singleton p.tokensToMint
scriptHash /\ assetName /\ mintAmount /\ _ = p.tokensToMint
plan =
[ Pay $ TransactionOutput
{ address: receiverAddress
, amount: adaValue
, datum: Nothing
, scriptRef: Nothing
}
, MintAsset scriptHash assetName (Int.newPositive mintAmount)
$ PlutusScriptCredential (ScriptValue p.mintingPolicy)
RedeemerDatum.unit
, Pay $ TransactionOutput
{ address: ownAddress
, amount: nonAdaValue
, datum: Just $ OutputDatumHash $ hashPlutusData p.datumToAttach
, scriptRef: Just $ PlutusScriptRef p.mintingPolicy
}
]
unbalancedTx <- buildTx plan
balancedTx <- balanceTx unbalancedTx Map.empty mempty
balancedSignedTx <- signTransaction balancedTx
txId <- submit balancedSignedTx
logInfo' $ "Tx ID: " <> show txId
awaitTxConfirmed txId
logInfo' "Tx submitted successfully!"
senderAddress <- liftedM "Failed to get sender address" $ head <$>
getWalletAddresses
utxos <- utxosAt senderAddress
txOutputUnderTest <-
view _output <$>
liftContractM "Could not find required unspent output with datum hash"
(find hasDatumHash $ lookupTxHash txId utxos)
pure
{ txHash: txId
, txFinalFee: view (_body <<< _fee) balancedSignedTx
, txOutputUnderTest
}
where
hasDatumHash :: TransactionUnspentOutput -> Boolean
hasDatumHash = view (_output <<< _datum) >>>
case _ of
Just (OutputDatumHash _) -> true
_ -> false
getReceiverAddress :: ContractParams -> Contract Address
getReceiverAddress { receiverPkh, receiverSkh } =
mkAddress (wrap $ PubKeyHashCredential $ unwrap receiverPkh)
(wrap <<< PubKeyHashCredential <<< unwrap <$> receiverSkh)