Below is my attempt to implement the homework assignment.
Is the way I call the endpoint twice in payTrace
with minor differences appropriate?
Since payTest2
would not execute properly without modifying the original payContract
, is the way I handle the contract error appropriate?
payContract :: Contract () PaySchema Text ()
payContract = do
pp <- endpoint @"pay"
let tx = mustPayToPubKey (ppRecipient pp) $ lovelaceValueOf $ ppLovelace pp
Contract.handleError (\err -> Contract.logError $ "Caught error: " ++ unpack err) $ (void $ submitTx tx)
payContract
-- A trace that invokes the pay endpoint of payContract on Wallet 1 twice, each time with Wallet 2 as
-- recipient, but with amounts given by the two arguments. There should be a delay of one slot
-- after each endpoint call.
payTrace :: Integer -> Integer -> EmulatorTrace ()
payTrace x y = do
h1 <- activateContractWallet (Wallet 1) payContract
callEndpoint @"pay" h1 $ PayParams
{ ppRecipient = pubKeyHash $ walletPubKey $ Wallet 2
, ppLovelace = x
}
void $ Emulator.waitNSlots 1
xs <- observableState h1
Extras.logInfo $ show xs
callEndpoint @"pay" h1 $ PayParams
{ ppRecipient = pubKeyHash $ walletPubKey $ Wallet 2
, ppLovelace = y
}
void $ Emulator.waitNSlots 1
ys <- observableState h1
Extras.logInfo $ show ys
I retracted the surrounding code that has not been modified. The start code can be found here.