closing the auction

This commit is contained in:
Lars Brünjes 2021-03-31 13:03:06 +02:00
parent 7d256947d7
commit d0e24919eb
No known key found for this signature in database
GPG key ID: B488B9045DC1A087

View file

@ -17,9 +17,9 @@
module EnglishAuction module EnglishAuction
( Auction (..) ( Auction (..)
, StartParams (..) , StartParams (..), BidParams (..), CloseParams (..)
, AuctionSchema , AuctionSchema
, start , start, bid, close
, endpoints , endpoints
, schemas , schemas
, ensureKnownCurrencies , ensureKnownCurrencies
@ -126,10 +126,16 @@ data BidParams = BidParams
, bpBid :: !Integer , bpBid :: !Integer
} deriving (Generic, ToJSON, FromJSON, ToSchema) } deriving (Generic, ToJSON, FromJSON, ToSchema)
data CloseParams = CloseParams
{ cpCurrency :: !CurrencySymbol
, cpToken :: !TokenName
} deriving (Generic, ToJSON, FromJSON, ToSchema)
type AuctionSchema = type AuctionSchema =
BlockchainActions BlockchainActions
.\/ Endpoint "start" StartParams .\/ Endpoint "start" StartParams
.\/ Endpoint "bid" BidParams .\/ Endpoint "bid" BidParams
.\/ Endpoint "close" CloseParams
start :: (HasBlockchainActions s, AsContractError e) => StartParams -> Contract w s e () start :: (HasBlockchainActions s, AsContractError e) => StartParams -> Contract w s e ()
start StartParams{..} = do start StartParams{..} = do
@ -151,8 +157,9 @@ start StartParams{..} = do
bid :: forall w s. HasBlockchainActions s => BidParams -> Contract w s Text () bid :: forall w s. HasBlockchainActions s => BidParams -> Contract w s Text ()
bid BidParams{..} = do bid BidParams{..} = do
(oref, o, d@AuctionDatum{..}) <- findAuction (oref, o, d@AuctionDatum{..}) <- findAuction bpCurrency bpToken
logInfo @String $ printf "found auction utxo with datum %s" (show d) logInfo @String $ printf "found auction utxo with datum %s" (show d)
when (bpBid < minBid d) $ when (bpBid < minBid d) $
throwError $ pack $ printf "bid lower than minimal bid %d" (minBid d) throwError $ pack $ printf "bid lower than minimal bid %d" (minBid d)
pkh <- pubKeyHash <$> ownPubKey pkh <- pubKeyHash <$> ownPubKey
@ -174,30 +181,65 @@ bid BidParams{..} = do
mustSpendScriptOutput oref r mustSpendScriptOutput oref r
ledgerTx <- submitTxConstraintsWith lookups tx ledgerTx <- submitTxConstraintsWith lookups tx
void $ awaitTxConfirmed $ txId ledgerTx void $ awaitTxConfirmed $ txId ledgerTx
logInfo @String "" logInfo @String $ printf "made bid of %d lovelace in auction %s for token (%s, %s)"
where bpBid
findAuction :: Contract w s Text (TxOutRef, TxOutTx, AuctionDatum) (show adAuction)
findAuction = do (show bpCurrency)
utxos <- utxoAt $ ScriptAddress auctionHash (show bpToken)
let xs = [ (oref, o)
| (oref, o) <- Map.toList utxos close :: forall w s. HasBlockchainActions s => CloseParams -> Contract w s Text ()
, Value.valueOf (txOutValue $ txOutTxOut o) bpCurrency bpToken == 1 close CloseParams{..} = do
] (oref, o, d@AuctionDatum{..}) <- findAuction cpCurrency cpToken
case xs of logInfo @String $ printf "found auction utxo with datum %s" (show d)
[(oref, o)] -> case txOutType $ txOutTxOut o of
PayToPubKey -> throwError "unexpected out type" let t = Value.singleton cpCurrency cpToken 1
PayToScript h -> case Map.lookup h $ txData $ txOutTxTx o of r = Redeemer $ PlutusTx.toData Close
Nothing -> throwError "datum not found" seller = aSeller adAuction
Just (Datum e) -> case PlutusTx.fromData e of
Nothing -> throwError "datum has wrong type" lookups = Constraints.scriptInstanceLookups auctionInstance <>
Just d -> return (oref, o, d) Constraints.otherScript auctionValidator <>
_ -> throwError "auction utxo not found" Constraints.unspentOutputs (Map.singleton oref o)
tx = case adHighestBid of
Nothing -> mustPayToPubKey seller t <>
mustValidateIn (from $ aDeadline adAuction) <>
mustSpendScriptOutput oref r
Just Bid{..} -> mustPayToPubKey bBidder t <>
mustPayToPubKey seller (Ada.lovelaceValueOf bBid) <>
mustValidateIn (from $ aDeadline adAuction) <>
mustSpendScriptOutput oref r
ledgerTx <- submitTxConstraintsWith lookups tx
void $ awaitTxConfirmed $ txId ledgerTx
logInfo @String $ printf "closed auction %s for token (%s, %s)"
(show adAuction)
(show cpCurrency)
(show cpToken)
findAuction :: HasBlockchainActions s
=> CurrencySymbol
-> TokenName
-> Contract w s Text (TxOutRef, TxOutTx, AuctionDatum)
findAuction cs tn = do
utxos <- utxoAt $ ScriptAddress auctionHash
let xs = [ (oref, o)
| (oref, o) <- Map.toList utxos
, Value.valueOf (txOutValue $ txOutTxOut o) cs tn == 1
]
case xs of
[(oref, o)] -> case txOutType $ txOutTxOut o of
PayToPubKey -> throwError "unexpected out type"
PayToScript h -> case Map.lookup h $ txData $ txOutTxTx o of
Nothing -> throwError "datum not found"
Just (Datum e) -> case PlutusTx.fromData e of
Nothing -> throwError "datum has wrong type"
Just d -> return (oref, o, d)
_ -> throwError "auction utxo not found"
endpoints :: Contract () AuctionSchema Text () endpoints :: Contract () AuctionSchema Text ()
endpoints = (start' `select` bid') >> endpoints endpoints = (start' `select` bid' `select` close') >> endpoints
where where
start' = endpoint @"start" >>= start start' = endpoint @"start" >>= start
bid' = endpoint @"bid" >>= bid bid' = endpoint @"bid" >>= bid
close' = endpoint @"close" >>= close
mkSchemaDefinitions ''AuctionSchema mkSchemaDefinitions ''AuctionSchema