From 00cdefe59650ba13baf1a7007344644d92dd14f5 Mon Sep 17 00:00:00 2001 From: jotaAfonso Date: Fri, 10 Feb 2023 03:39:43 +0000 Subject: [PATCH] cleaned MarketPLaced --- .../SimpleMarketplace/daml.yaml | 1 + .../SimpleMarketplace/daml/Main.daml | 56 +++++------ .../daml/SimpleMarketplace.daml | 96 +++++++++++++++---- .../SimpleMarketplace/daml/Test.daml | 1 + .../simpleMarketPlace/.dlint.yaml | 8 ++ .../simpleMarketPlace/.gitattributes | 3 + .../simpleMarketPlace/.gitignore | 3 + .../simpleMarketPlace/NO_AUTO_COPYRIGHT | 0 .../simpleMarketPlace/daml.yaml | 12 +++ .../simpleMarketPlace/daml/Main.daml | 33 +++++++ .../daml/SimpleMarketplace.daml | 61 ++++++++++++ 11 files changed, 223 insertions(+), 51 deletions(-) create mode 100644 daml/Simple_Marketplace/SimpleMarketplace/daml/Test.daml create mode 100644 daml/draft_simple_market/simpleMarketPlace/.dlint.yaml create mode 100644 daml/draft_simple_market/simpleMarketPlace/.gitattributes create mode 100644 daml/draft_simple_market/simpleMarketPlace/.gitignore create mode 100644 daml/draft_simple_market/simpleMarketPlace/NO_AUTO_COPYRIGHT create mode 100644 daml/draft_simple_market/simpleMarketPlace/daml.yaml create mode 100644 daml/draft_simple_market/simpleMarketPlace/daml/Main.daml create mode 100644 daml/draft_simple_market/simpleMarketPlace/daml/SimpleMarketplace.daml diff --git a/daml/Simple_Marketplace/SimpleMarketplace/daml.yaml b/daml/Simple_Marketplace/SimpleMarketplace/daml.yaml index 84cf58f..969065e 100644 --- a/daml/Simple_Marketplace/SimpleMarketplace/daml.yaml +++ b/daml/Simple_Marketplace/SimpleMarketplace/daml.yaml @@ -6,6 +6,7 @@ name: SimpleMarketplace source: daml init-script: Main:setup version: 0.0.1 +init-script: Main:setup parties: - Alice - Bob diff --git a/daml/Simple_Marketplace/SimpleMarketplace/daml/Main.daml b/daml/Simple_Marketplace/SimpleMarketplace/daml/Main.daml index d3feed5..de20a8f 100644 --- a/daml/Simple_Marketplace/SimpleMarketplace/daml/Main.daml +++ b/daml/Simple_Marketplace/SimpleMarketplace/daml/Main.daml @@ -3,39 +3,35 @@ module Main where import SimpleMarketplace import Daml.Script -setup : Script MarketId +setup : Script () setup = script do -- user_setup_begin alice <- allocateParty "Alice" bob <- allocateParty "Bob" test <- submit alice do - createCmd Market with - owner = alice - buyer = bob - description = "test" - askingPrice = 5 - offerPrice = 0 - state = ItemAvailable - - {- - submitMustFail bob do - exerciseCmd test MakeOffer with offerPriceParam = 4 - -} - - {- - test <- submit alice do - exerciseCmd test RejectOffer - -} - - test <- submit bob do - exerciseCmd test MakeOffer with offerPriceParam = 6 - - test <- submit alice do - exerciseCmd test RejectOffer - - test <- submit bob do - exerciseCmd test MakeOffer with offerPriceParam = 6 - - submit alice do - exerciseCmd test AcceptOffer + createCmd Market with + owner = alice + buyer = bob + state = ItemAvailable + + item <- submit alice do + exerciseCmd test ListItem with + descriptionParam = "Test" + askingPriceParam = 10 + ownerParam = alice + + item2 <- submit alice do + exerciseCmd test ListItem with + descriptionParam = "Test" + askingPriceParam = 10 + ownerParam = alice + + (test, offer) <- submit bob do + exerciseCmd test MakeOffer with + item = item + observerSellerParam = alice + offerPriceParam = 10 + buyerParam = bob + + pure() \ No newline at end of file diff --git a/daml/Simple_Marketplace/SimpleMarketplace/daml/SimpleMarketplace.daml b/daml/Simple_Marketplace/SimpleMarketplace/daml/SimpleMarketplace.daml index 97d8ab0..a77dc23 100644 --- a/daml/Simple_Marketplace/SimpleMarketplace/daml/SimpleMarketplace.daml +++ b/daml/Simple_Marketplace/SimpleMarketplace/daml/SimpleMarketplace.daml @@ -1,6 +1,8 @@ module SimpleMarketplace where type MarketId = ContractId Market +type OfferId = ContractId Offer +type ItemId = ContractId Item data StateType = ItemAvailable @@ -12,47 +14,99 @@ template Market with owner : Party buyer : Party - description : Text - askingPrice : Int - offerPrice : Int state : StateType where signatory owner - ensure owner /= buyer - && description /= "" - && askingPrice > 0 + ensure owner /= buyer observer buyer - choice MakeOffer : MarketId - with - offerPriceParam : Int + nonconsuming choice ListItem : ItemId + with + descriptionParam : Text + askingPriceParam : Int + ownerParam : Party + controller owner + do + create Item with + description = descriptionParam + askingPrice = askingPriceParam + owner = ownerParam + observers = buyer + + choice MakeOffer : (MarketId, OfferId) + with + item : ItemId + observerSellerParam : Party + offerPriceParam : Int + buyerParam : Party controller buyer - do - assertMsg "Item is not available for an offer." (this.state == ItemAvailable) - assertMsg "Offer too low." (offerPriceParam > this.askingPrice) - assertMsg "Offer invalid." (offerPriceParam > 0) + do + Item{..} <- fetch item + assertMsg "Item is not available for an offer." (this.state == ItemAvailable) + assertMsg "Offer invalid." (offerPriceParam > 0) + assertMsg "Offer too low." (offerPriceParam >= askingPrice) - create Market with - offerPrice = offerPriceParam + market <- create this with state = OfferPlaced - .. + + offer <- create Offer with + observerSeller = observerSellerParam + offerPrice = offerPriceParam + buyer = buyerParam + + pure(market, offer) choice AcceptOffer : MarketId + with + offer : OfferId controller owner do assertMsg "Offer was not placed." (this.state == OfferPlaced) + + Offer{..} <- fetch offer + exercise offer ArchiveOffer + create this with state = Accepted - - choice RejectOffer : MarketId + + choice RejectOffer : () + with + offer : OfferId controller owner do assertMsg "Invalid state of contract." (this.state == OfferPlaced) - create Market with - offerPrice = 0 + + exercise offer ArchiveOffer + + create this with state = ItemAvailable - .. + + pure() + +template Item + with + description : Text + askingPrice : Int + owner : Party + observers : Party + where + signatory owner + observer observers + +template Offer + with + observerSeller : Party + offerPrice : Int + buyer : Party + where + signatory buyer + observer observerSeller + + choice ArchiveOffer : () + controller observerSeller + do + archive self {- template MarketApp diff --git a/daml/Simple_Marketplace/SimpleMarketplace/daml/Test.daml b/daml/Simple_Marketplace/SimpleMarketplace/daml/Test.daml new file mode 100644 index 0000000..7c0821e --- /dev/null +++ b/daml/Simple_Marketplace/SimpleMarketplace/daml/Test.daml @@ -0,0 +1 @@ +module Test where \ No newline at end of file diff --git a/daml/draft_simple_market/simpleMarketPlace/.dlint.yaml b/daml/draft_simple_market/simpleMarketPlace/.dlint.yaml new file mode 100644 index 0000000..add1b60 --- /dev/null +++ b/daml/draft_simple_market/simpleMarketPlace/.dlint.yaml @@ -0,0 +1,8 @@ +# This file controls the behaviour of the dlint linting tool. Below are two +# examples of how to enable or disable a rule. + +# This rule is enabled by default. Uncomment to disable. +#- ignore: {name: Use fewer imports} + +# This rule is disabled by default. Uncomment to enable. +#- suggest: {name: Use module export list} diff --git a/daml/draft_simple_market/simpleMarketPlace/.gitattributes b/daml/draft_simple_market/simpleMarketPlace/.gitattributes new file mode 100644 index 0000000..ec8726b --- /dev/null +++ b/daml/draft_simple_market/simpleMarketPlace/.gitattributes @@ -0,0 +1,3 @@ +# This tells GitHub to highlight Daml files as if they were Haskell. It's not +# perfect, but it's better than no syntax highlighting at all. +*.daml linguist-language=Haskell diff --git a/daml/draft_simple_market/simpleMarketPlace/.gitignore b/daml/draft_simple_market/simpleMarketPlace/.gitignore new file mode 100644 index 0000000..3b14695 --- /dev/null +++ b/daml/draft_simple_market/simpleMarketPlace/.gitignore @@ -0,0 +1,3 @@ +/.daml +/log +navigator.log diff --git a/daml/draft_simple_market/simpleMarketPlace/NO_AUTO_COPYRIGHT b/daml/draft_simple_market/simpleMarketPlace/NO_AUTO_COPYRIGHT new file mode 100644 index 0000000..e69de29 diff --git a/daml/draft_simple_market/simpleMarketPlace/daml.yaml b/daml/draft_simple_market/simpleMarketPlace/daml.yaml new file mode 100644 index 0000000..2416800 --- /dev/null +++ b/daml/draft_simple_market/simpleMarketPlace/daml.yaml @@ -0,0 +1,12 @@ +# for config file options, refer to +# https://docs.daml.com/tools/assistant.html#project-config-file-daml-yaml + +sdk-version: 2.4.2 +name: simpleMarketPlace +source: daml +init-script: Main:setup +version: 0.0.1 +dependencies: + - daml-prim + - daml-stdlib + - daml-script diff --git a/daml/draft_simple_market/simpleMarketPlace/daml/Main.daml b/daml/draft_simple_market/simpleMarketPlace/daml/Main.daml new file mode 100644 index 0000000..c87db85 --- /dev/null +++ b/daml/draft_simple_market/simpleMarketPlace/daml/Main.daml @@ -0,0 +1,33 @@ + +module Main where + +import SimpleMarketplace +import Daml.Script + +setup : Script SimpleMarketPlaceId +setup = script do +-- user_setup_begin + alice <- allocateParty "Alice" + bob <- allocateParty "Bob" + + test <- submit alice do + createCmd SimpleMarketPlace with + owner = alice + buyer = [] + offerPrice = 0 + state = ItemAvailable + + test <- submit alice do + exerciseCmd test RegisterP with buyerParticipant = bob + + test <- submit bob do + exerciseCmd test MakeOffer with offerPriceParam = 6 + + test <- submit alice do + exerciseCmd test RejectOffer + + test <- submit bob do + exerciseCmd test MakeOffer with offerPriceParam = 6 + + submit alice do + exerciseCmd test AcceptOffer \ No newline at end of file diff --git a/daml/draft_simple_market/simpleMarketPlace/daml/SimpleMarketplace.daml b/daml/draft_simple_market/simpleMarketPlace/daml/SimpleMarketplace.daml new file mode 100644 index 0000000..f34b903 --- /dev/null +++ b/daml/draft_simple_market/simpleMarketPlace/daml/SimpleMarketplace.daml @@ -0,0 +1,61 @@ +module SimpleMarketplace where + +import DA.List.Total +import DA.Optional + +type SimpleMarketPlaceId = ContractId SimpleMarketPlace + +data StateType + = ItemAvailable + | OfferPlaced + | Accepted + deriving (Eq, Show) + +template SimpleMarketPlace + with + owner : Party + buyer : [Party] + offerPrice : Int + state : StateType + + where + signatory owner + observer buyer + + choice RegisterP : SimpleMarketPlaceId + with + buyerParticipant : Party + controller owner + do + assertMsg "Already has a buyer registered." (isNone (init buyer)) + create SimpleMarketPlace with + buyer = [buyerParticipant] + .. + + choice MakeOffer : SimpleMarketPlaceId + with + offerPriceParam : Int + controller buyer + do + assertMsg "Item is not available for an offer." (this.state == ItemAvailable) + + create SimpleMarketPlace with + offerPrice = offerPriceParam + state = OfferPlaced + .. + + choice AcceptOffer : SimpleMarketPlaceId + controller owner + do + assertMsg "Offer was not placed." (this.state == OfferPlaced) + create this with + state = Accepted + + choice RejectOffer : SimpleMarketPlaceId + controller owner + do + assertMsg "Invalid state of contract." (this.state == OfferPlaced) + create SimpleMarketPlace with + offerPrice = 0 + state = ItemAvailable + .. \ No newline at end of file