-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e98ee2d
commit 5e4fad2
Showing
6 changed files
with
234 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters