Skip to content

Commit

Permalink
cleaned MarketPLaced
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaAfonso committed Feb 10, 2023
1 parent 5e4fad2 commit 00cdefe
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 51 deletions.
1 change: 1 addition & 0 deletions daml/Simple_Marketplace/SimpleMarketplace/daml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ name: SimpleMarketplace
source: daml
init-script: Main:setup
version: 0.0.1
init-script: Main:setup
parties:
- Alice
- Bob
Expand Down
56 changes: 26 additions & 30 deletions daml/Simple_Marketplace/SimpleMarketplace/daml/Main.daml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module SimpleMarketplace where

type MarketId = ContractId Market
type OfferId = ContractId Offer
type ItemId = ContractId Item

data StateType
= ItemAvailable
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions daml/Simple_Marketplace/SimpleMarketplace/daml/Test.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module Test where
8 changes: 8 additions & 0 deletions daml/draft_simple_market/simpleMarketPlace/.dlint.yaml
Original file line number Diff line number Diff line change
@@ -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}
3 changes: 3 additions & 0 deletions daml/draft_simple_market/simpleMarketPlace/.gitattributes
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions daml/draft_simple_market/simpleMarketPlace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.daml
/log
navigator.log
Empty file.
12 changes: 12 additions & 0 deletions daml/draft_simple_market/simpleMarketPlace/daml.yaml
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions daml/draft_simple_market/simpleMarketPlace/daml/Main.daml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
..

0 comments on commit 00cdefe

Please sign in to comment.