Skip to content

Commit

Permalink
daml bazaar
Browse files Browse the repository at this point in the history
  • Loading branch information
jotaAfonso committed Jan 13, 2023
1 parent e98ee2d commit 5e4fad2
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 125 deletions.
111 changes: 111 additions & 0 deletions daml/Auction/Auction/daml/Auction.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
module Auction where

import Daml.Script
import DA.Map

type AuctionId = ContractId Auction

data BidderInfo
= BidderInfo {
bidder : Party,
currentBid : Int
} deriving (Eq, Show)

data AuctionStateType
= StartedState
| BiddingState
| WithdrawingState
| AuctionClosedState
deriving (Eq, Show)

template Auction
with
beneficiary : Party
currentHBidder : BidderInfo
pendingReturns : Map Party Int
state : AuctionStateType
newBidder : Party
where
signatory beneficiary
ensure beneficiary /= currentHBidder.bidder
observer currentHBidder.bidder
choice Bid : AuctionId
with
biddingAmount : Int
controller newBidder
do
assertMsg "Invalid state of contract." (this.state == StartedState || this.state == WithdrawingState)
-- assertMsg "Cant bid while being the highest bidder." (newBidder /= currentHBidder.bidder)
assertMsg "Invalid bid, due to low value." (biddingAmount > currentHBidder.currentBid)

create this with
currentHBidder = BidderInfo newBidder biddingAmount
pendingReturns = insert this.currentHBidder.bidder this.currentHBidder.currentBid this.pendingReturns
state = BiddingState

choice Withdraw : AuctionId
controller newBidder
do
assertMsg "Does not have pending returns." (member newBidder pendingReturns)
create this with
pendingReturns = insert this.currentHBidder.bidder 0 this.pendingReturns
state = WithdrawingState

choice EndAuction : AuctionId
controller beneficiary
do
assertMsg "Invalid state of contract." (this.state /= AuctionClosedState)
create this with
state = AuctionClosedState

template AuctionApp
with
beneficiary : Party
currentBidder : Party
where
signatory beneficiary
observer currentBidder
choice Build : AuctionId
controller beneficiary
do
create Auction
with
beneficiary = this.beneficiary
currentHBidder = BidderInfo this.currentBidder 0
pendingReturns = empty
state = StartedState
newBidder = this.currentBidder

setup : Script AuctionId
setup = script do
-- user_setup_begin
alice <- allocatePartyWithHint "Alice" (PartyIdHint "Alice")
bob <- allocatePartyWithHint "Bob" (PartyIdHint "Bob")
aliceId <- validateUserId "alice"
bobId <- validateUserId "bob"
createUser (User aliceId (Some alice)) [CanActAs alice]
createUser (User bobId (Some bob)) [CanActAs bob]
-- user_setup_end

test <- submit alice do
createCmd AuctionApp with
beneficiary = alice
currentBidder = bob

test <- submit alice do
exerciseCmd test Build

test <- submit bob do
exerciseCmd test Bid with biddingAmount = 10

test <- submit bob do
exerciseCmd test Withdraw

test <- submit bob do
exerciseCmd test Bid with biddingAmount = 20

test <- submit alice do
exerciseCmd test EndAuction

submit bob do
exerciseCmd test Withdraw
44 changes: 0 additions & 44 deletions daml/Auction/Auction/daml/Main.daml

This file was deleted.

11 changes: 11 additions & 0 deletions daml/Bazaar/Bazaar/daml/Account.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Account where

type AccountId = ContractId Account

template Account
with
account : Party
balance : Int
where
signatory account
observer account
105 changes: 28 additions & 77 deletions daml/Bazaar/Bazaar/daml/Bazaar.daml
Original file line number Diff line number Diff line change
@@ -1,95 +1,46 @@
module Bazaar where

type BazaarId = ContractId Bazaar
type ItemListingId = ContractId ItemListing

data AccountInfo
= AccountInfo {
account : Party,
balance : Int
} deriving (Eq, Show)
import Item
import DA.List
import Account

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

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
signatories : [AccountId]
signatoriesP : [Party]
state : BazaarStateType
where
signatory seller.account
observer buyer.account
choice Listing : BazaarId
controller buyer.account
do
newC <- create this with
state = ItemListed
signatory signatoriesP
ensure unique signatoriesP
observer signatoriesP

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
-- creates an item listing
choice ListingItem : ItemListingId with seller : AccountId, sellerP : Party, priceName : Text, priceItem : Int
controller sellerP
do
seller_l <- fetch seller
assert (seller_l.account == sellerP)
-- assert (elem (seller_l) (this.signatories))

-- changes state
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

state = ItemListed

-- creates an ItemListing, with the owner, the parties involved (including owner),
-- item's name and price
create ItemListing with
owner = seller_l
ownerAID = seller
partiesInvolvedP = signatoriesP
item = ItemInfo priceName priceItem
state = ItemAvailable

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

55 changes: 55 additions & 0 deletions daml/Bazaar/Bazaar/daml/Item.daml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module Item where

import DA.List
import Prelude
import Account

type ItemListingId = ContractId ItemListing

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

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

template ItemListing
with
owner : Account
ownerAID : AccountId
partiesInvolvedP : [Party]
item : ItemInfo
state : ItemStateType
where
signatory owner.account
ensure elem (this.owner.account) (this.partiesInvolvedP)
observer partiesInvolvedP

choice BuyItem : () with buyer : AccountId, buyerP : Party
controller buyerP
do
buyer_l <- fetch buyer
assert (buyer_l.account == buyerP)
assertMsg "Not enough funds." (this.item.price < buyer_l.balance)

create Account with
account = buyer_l.account
balance = buyer_l.balance - this.item.price
archive buyer

ownerUpdate <- create Account with
account = owner.account
balance = owner.balance + this.item.price

l_ownerUpdate <- fetch ownerUpdate
create this with
owner = l_ownerUpdate
ownerAID = ownerUpdate
state = ItemSold

archive ownerUpdate
pure()
33 changes: 29 additions & 4 deletions daml/Bazaar/Bazaar/daml/Main.daml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,36 @@ module Main where

import Daml.Script
import Bazaar
import Account
import Item

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

submit alice do
createCmd BazaarApp with seller = alice, buyer = bob
accountAlice <- submit alice do
createCmd Account with account = alice, balance = 10

accountBob <- submit bob do
createCmd Account with account = bob, balance = 10

bazaar <- submitMulti [alice, bob] [] do
createCmd Bazaar with
signatories = [accountAlice, accountBob]
signatoriesP = [alice, bob]
state = PartyProvisioned

item <- submit alice do
exerciseCmd bazaar ListingItem with
seller = accountAlice
sellerP = alice
priceName = "Name"
priceItem = 5

something <- submit bob do
exerciseCmd item BuyItem with
buyer = accountBob
buyerP = bob

pure()

0 comments on commit 5e4fad2

Please sign in to comment.