Skip to content

Commit

Permalink
daml contract
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaAfonso committed Jan 3, 2023
1 parent c2d8191 commit c4a5be5
Show file tree
Hide file tree
Showing 35 changed files with 602 additions and 0 deletions.
8 changes: 8 additions & 0 deletions daml/Basic_Provenance/BasicProvenance/.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/Basic_Provenance/BasicProvenance/.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/Basic_Provenance/BasicProvenance/.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/Basic_Provenance/BasicProvenance/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: BasicProvenance
source: daml
init-script: Main:setup
version: 0.0.1
dependencies:
- daml-prim
- daml-stdlib
- daml-script
60 changes: 60 additions & 0 deletions daml/Basic_Provenance/BasicProvenance/daml/BasicProvinance.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
module BasicProvinance where

type ProvinanceId = ContractId Provinance

data ProvinanceStateType
= CreatedState
| InTransitState
| CompletedState
deriving (Eq, Show)

template Provinance
with
initiater : Party
counter : Party
previouscounter : Party
chainOwner : Party
chainObserver : Party
state : ProvinanceStateType
where
signatory initiater
observer counter
choice TransferResponsibility : ProvinanceId
with
newCounter : Party
controller counter
do
assertMsg "Invalid state of contract." (this.state /= CompletedState)
create this with
previouscounter = this.counter
counter = newCounter
state = InTransitState
choice Complete : ProvinanceId
controller chainOwner
do
assertMsg "Invalid state of contract." (this.state /= CompletedState)
create this with
previouscounter = this.counter
counter = this.chainOwner
state = CompletedState


template ProvinanceApp
with
initiater : Party
chainOwner : Party
chainObserver : Party
where
signatory initiater
choice Build : ProvinanceId
controller initiater
do
create Provinance
with
initiater = this.initiater
counter = this.initiater
previouscounter = this.initiater
chainOwner = this.chainOwner
chainObserver = this.chainObserver
state = CreatedState

32 changes: 32 additions & 0 deletions daml/Basic_Provenance/BasicProvenance/daml/Main.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module Main where

import BasicProvinance
import Daml.Script

setup : Script ProvinanceId
setup = script do
-- user_setup_begin
alice <- allocateParty "Alice"
bob <- allocateParty"Bob"
-- user_setup_end

test <- submit alice do
createCmd ProvinanceApp with
initiater = alice
chainOwner = alice
chainObserver = bob

test <- submit alice do
exerciseCmd test Build

test <- submit alice do
exerciseCmd test TransferResponsibility with newCounter = bob

test <- submit bob do
exerciseCmd test TransferResponsibility with newCounter = alice

test <- submit alice do
exerciseCmd test TransferResponsibility with newCounter = bob

submit alice do
exerciseCmd test Complete
8 changes: 8 additions & 0 deletions daml/Bazaar/Bazaar/.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/Bazaar/Bazaar/.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/Bazaar/Bazaar/.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/Bazaar/Bazaar/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: Bazaar
source: daml
init-script: Main:setup
version: 0.0.1
dependencies:
- daml-prim
- daml-stdlib
- daml-script
95 changes: 95 additions & 0 deletions daml/Bazaar/Bazaar/daml/Bazaar.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
module Bazaar where

type BazaarId = ContractId Bazaar
type ItemListingId = ContractId ItemListing

data AccountInfo
= AccountInfo {
account : Party,
balance : Int
} deriving (Eq, Show)

data ItemInfo
= ItemInfo {
name : Text,
price : Int
} deriving (Eq, Show)

data BazaarStateType
= PartyProvisioned
| ItemListed
| CurrentSaleFinalized
deriving (Eq, Show)

data ItemStateType
= ItemAvailable
| ItemSold
deriving (Eq, Show)

template ItemListing
with
owner : AccountInfo
item : ItemInfo
state : ItemStateType
bazar : BazaarId
buyer : AccountInfo
where
signatory owner.account
observer buyer.account
choice BuyItem : ItemListingId
controller buyer.account
do
assertMsg "Not enough funds." (this.item.price > this.buyer.balance)
exercise bazar UpdateBalance
create this with
state = ItemSold

template Bazaar
with
seller : AccountInfo
buyer : AccountInfo
item : ItemInfo
state : BazaarStateType
where
signatory seller.account
observer buyer.account
choice Listing : BazaarId
controller buyer.account
do
newC <- create this with
state = ItemListed

item <- create ItemListing with
owner = this.seller
item = this.item
state = ItemAvailable
bazar = newC
buyer = this.buyer
-- imporve
create this with
state = ItemListed
choice UpdateBalance : BazaarId
controller buyer.account
do
create this with
seller = AccountInfo this.seller.account (this.seller.balance + this.item.price)
buyer = AccountInfo this.buyer.account (this.buyer.balance - this.item.price)
state = CurrentSaleFinalized


template BazaarApp
with
seller : Party
buyer : Party
where
signatory seller
observer buyer
choice Build : BazaarId
controller seller
do
create Bazaar
with
seller = AccountInfo seller 10
buyer = AccountInfo buyer 10
item = ItemInfo "ItemName" 5
state = PartyProvisioned
12 changes: 12 additions & 0 deletions daml/Bazaar/Bazaar/daml/Main.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Main where

import Daml.Script
import Bazaar

setup : Script (ContractId BazaarApp)
setup = script do
alice <- allocateParty "Alice"
bob <- allocateParty"Bob"

submit alice do
createCmd BazaarApp with seller = alice, buyer = bob
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}
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
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/Frequent_Flyer_Rewards_Calculator/FrequentFlyer/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: FrequentFlyer
source: daml
init-script: Main:setup
version: 0.0.1
dependencies:
- daml-prim
- daml-stdlib
- daml-script
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module FrequentFlyer where

type FrequentFlyerId = ContractId FrequentFlyer

data FlyerStateType
= SetFlyerReward
| MilesAdded
deriving (Eq, Show)

data TotalRewardsState
= Behind
| UpToDate
deriving (Eq, Show)

template FrequentFlyer
with
flyer : Party
airlineRep : Party
rewardsPMile: Int
miles : [Int]
state : FlyerStateType
rewardsState: TotalRewardsState
totalRewards: Int
where
signatory airlineRep
ensure flyer /= airlineRep
observer flyer
choice AddMiles : FrequentFlyerId
with
millesAdd : [Int]
controller flyer
do
create this with
miles = (++) millesAdd this.miles
state = MilesAdded
rewardsState = Behind
choice ComputeTotalRewards : FrequentFlyerId
controller flyer
do
assertMsg "Invalid state of contract." (this.state == MilesAdded)
create this with
totalRewards = sum (fmap (*this.rewardsPMile) miles)
rewardsState = UpToDate


template FrequentFlyerApp
with
flyer : Party
airlineRep : Party
rewardsPMile: Int
where
signatory airlineRep
observer flyer
choice Build : FrequentFlyerId
controller airlineRep
do
create FrequentFlyer
with
flyer = this.flyer
airlineRep = this.airlineRep
rewardsPMile= this.rewardsPMile
miles = []
state = SetFlyerReward
rewardsState= UpToDate
totalRewards= 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module Main where

import FrequentFlyer
import Daml.Script

setup : Script FrequentFlyerId
setup = script do
alice <- allocateParty "Alice"
bob <- allocateParty "Bob"

test <- submit bob do
createCmd FrequentFlyerApp with
flyer = alice
airlineRep = bob
rewardsPMile = 10

test <- submit bob do
exerciseCmd test Build

test <- submit alice do
exerciseCmd test AddMiles with millesAdd = [1]

test <- submit alice do
exerciseCmd test AddMiles with millesAdd = [2]

test <- submit alice do
exerciseCmd test ComputeTotalRewards

test <- submit alice do
exerciseCmd test AddMiles with millesAdd = [3]

submit alice do
exerciseCmd test ComputeTotalRewards

Loading

0 comments on commit c4a5be5

Please sign in to comment.