Test using scenario on Daml

Hello everybody! I’m currently working on an Auction Contract for my Master dissertation. I’m having some troubles with testing the contract by using the command scenario. In order to do that, I’ve been following this Testing using scenarios — Daml SDK 1.12.0 documentation page on the Daml website. At some point it displays a parse error and I don’t understand what’s wrong. Maybe the format for the time and date is incorrect? Can someone please take a look at it and let me know what can i do to make the error go away. Thank you so much in advance for the help.

import DA.Date

template Auction 
   with
      auctioneer: Party
      bidder: Party
      object: Text
      bid: Decimal
      limit: Decimal
      currency: Text
      start: Time
      end: Time
      dateAuction: Date
    where 
      signatory auctioneer , bidder 
      ensure limit > 0.0 && start < end

template AuctionBid
   with 
       auctioneer: Party
       bidder: Party
       object: Text
       bid: Decimal
       currency: Text
       limit: Decimal
       start: Time
       end: Time
    where 
       signatory auctioneer 
       
       controller bidder can
          MakeABid: ContractId Auction
             with
                limitArg: Decimal
                startArg: Time
                endArg: Time
                bidArg: Decimal
                currencyArg: Text
            do
              currentTime <- getTime
              create Auction
                 with
                    limit = limitArg
                    start = startArg
                    end = endArg
                    bid= bidArg
                    currency = currencyArg
                    auctioneer = auctioneer
                    bidder = bidder
                    object = object
                    dateAuction = toDateUTC currentTime

                    
                  
          Decline: ContractId DeclineBid 
             with 
                reason: Text 
             do 
              create DeclineBid with ..
            
                       
                        
                
template DeclineBid
   with
       auctioneer: Party
       bidder: Party
       object: Text
       bid: Decimal
       currency: Text
       limit: Decimal
       start: Time
       end: Time
       reason: Text
    where 
       signatory auctioneer , bidder 

       controller bidder can
          MakeANewBid: ContractId AuctionBid 
            do 
              create AuctionBid with ..
                 

test = scenario do 
  alice <- getParty "Alice"
  bob <- getParty "Bob"
  submit alice do
    create Auction
      with 
         auctioneer = alice
         bidder = bob 
         object = "Painting"
         bid = 2000.000
         limit = 5000.00
         currency = "Pound"
         start = T12:00:00
         end = T13:00:00
         dateAuction = 2021-11-11![Schermata 2021-11-13 alle 00.54.52|544x384]

I think more accurately the parsing error started in line 97.

I am not familiar of the standalone THH:MM:SS time format. I believe the ISO 8601 specifies the time format of something like “1994-11-05T08:15:30” where T is just a separator for date and time.

Try something like start = time (date 2007 Apr 5) 14 30 05

Reference:
https://docs.daml.com/daml/stdlib/Prelude.html#type-da-internal-lf-time-19011

2 Likes

Hi, thank you so much for responding me! When I try the format that you suggested it seems to work when I write it for start, but as soon as I write the end time it displays an error for both start and end.

import DA.Date

template Auction 
   with
      auctioneer: Party
      bidder: Party
      object: Text
      bid: Decimal
      limit: Decimal
      currency: Text
      start: Time
      end: Time
      dateAuction: Date
    where 
      signatory auctioneer , bidder 
      ensure limit > 0.0 && start < end

template AuctionBid
   with 
       auctioneer: Party
       bidder: Party
       object: Text
       bid: Decimal
       currency: Text
       limit: Decimal
       start: Time
       end: Time
    where 
       signatory auctioneer 
       
       controller bidder can
          MakeABid: ContractId Auction
             with
                limitArg: Decimal
                startArg: Time
                endArg: Time
                bidArg: Decimal
                currencyArg: Text
            do
              currentTime <- getTime
              create Auction
                 with
                    limit = limitArg
                    start = startArg
                    end = endArg
                    bid= bidArg
                    currency = currencyArg
                    auctioneer = auctioneer
                    bidder = bidder
                    object = object
                    dateAuction = toDateUTC currentTime

                    
                  
          Decline: ContractId DeclineBid 
             with 
                reason: Text 
             do 
              create DeclineBid with ..
            
                       
                        
                
template DeclineBid
   with
       auctioneer: Party
       bidder: Party
       object: Text
       bid: Decimal
       currency: Text
       limit: Decimal
       start: Time
       end: Time
       reason: Text
    where 
       signatory auctioneer , bidder 

       controller bidder can
          MakeANewBid: ContractId AuctionBid 
            do 
              create AuctionBid with ..
                 

test = scenario do 
  alice <- getParty "Alice"
  bob <- getParty "Bob"
  submit alice do
    create Auction
      with 
         auctioneer = alice
         bidder = bob 
         object = "Painting"
         bid = 2000.00
         limit = 5000.00
         currency = "Pound"
         start =  time : date 2022 Apr 5 14 30 00
         end =  time : date 2022 Apr 5 15 30 00
         dateAuction = date 2022 Apr 5

{
	
	"owner": "_generated_diagnostic_collection_name_#0",
	"severity": 8,
	   • Couldn't match type ‘(->)’ with ‘date 2022 'Apr’\n      Expected type: date 2022 'Apr 5 14 30 0\n        Actual type: Date -> Int -> Int -> Int -> Time\n    • In the ‘start’ field of a record\n      In the first argument of ‘create’, namely\n        ‘Auction\n           {auctioneer = alice, bidder = bob, object = \"Painting\",\n            bid = 2000.00, limit = 5000.00, currency = \"Pound\",\n            start = time : date 2022 Apr 5 14 30 0,\n            end = time : date 2022 Apr 5 15 30 0,\n            dateAuction = date 2022 Apr 5}’\n      In a stmt of a 'do' block:\n        create\n          Auction\n            {auctioneer = alice, bidder = bob, object = \"Painting\",\n             bid = 2000.00, limit = 5000.00, currency = \"Pound\",\n             start = time : date 2022 Apr 5 14 30 0,\n             end = time : date 2022 Apr 5 15 30 0,\n             dateAuction = date 2022 Apr 5}",
	"source": "typecheck",
	"startLineNumber": 98,
	"startColumn": 19,
	"endLineNumber": 98,
	"endColumn": 23
}

Do you have any idea on how to solve this? Thank you in advance

Hi @Alice, try replacing the start and end by

         start =  time (date 2022 Apr 5) 14 30 00
         end =  time (date 2022 Apr 5) 15 30 00

To expand on this: Daml has no builtin syntax for time stamps. Instead you get two functions date and time. date takes 3 arguments for year, month and day and gives you back a Date. time takes four arguments, the first being the date and then hours, minutes and seconds and gives you back a time.

Thanks! The date actually works now! Now I’m having problems because it says :
{

"owner": "_generated_diagnostic_collection_name_#0",
"severity": 8,
"message": "Scenario execution failed on commit at Auction:89:3:\n  0: create of Auction:Auction at DA.Internal.Prelude:373:26\n     failed due to a missing authorization from 'Bob'\n\nStack trace:\n- identity (DA.Internal.Prelude:117:1)\n- test (Auction:86:1)\n\nLedger time: 1970-01-01T00:00:00Z\n\nPartial transaction:\n  Sub-transactions:\n     0\n     └─> create Auction:Auction\n         with\n           auctioneer = 'Alice';\n           bidder = 'Bob';\n           object = \"Painting\";\n           bid = 2000.0000000000;\n           limit = 5000.0000000000;\n           currency = \"Pound\";\n           start = 2022-04-05T14:30:00Z;\n           end = 2022-04-05T15:30:00Z;\n           dateAuction = 2022-04-05T",
"source": "Script",
"startLineNumber": 86,
"startColumn": 1,
"endLineNumber": 86,
"endColumn": 5

}
how can I solve this? Am I supposed to use the command submitMustFail? If yes, how do I write that? I tried to write it like the previous command but substituting alice with bob but it didn’t work.
Thanks in advance!

The issue here is that your auction contract is signed by both the auctioneer (Alice) and the bidder (Bob). But you only have the authorization of the submitter here (Alice) so Bob’s authorization is missing.

Looking at your model, I’m not actually sure this is an issue. Looking at your code it seems like you don’t want people to create Auction contracts directly, instead they should be created by exercising MakeABid. In that context you have authorization from both the signatory of the AuctionBid contract as well as the controller so you can create it. If your adjust your scenario to do this, it passes:

test = scenario do 
  alice <- getParty "Alice"
  bob <- getParty "Bob"
  bid <- submit alice do
    create AuctionBid
      with 
         auctioneer = alice
         bidder = bob 
         object = "Painting"
         bid = 2000.00
         limit = 5000.00
         currency = "Pound"
         start =  time (date 2022 Apr 5) 14 30 00
         end =  time (date 2022 Apr 5) 15 30 00
  submit bob do
    exercise bid MakeABid with
      limitArg = 5000.00
      startArg = time (date 2022 Apr 5) 14 30 00
      endArg = time (date 2022 Apr 5) 15 30 00
      bidArg = 2000.00
      currencyArg = "Pound"
  pure ()

Note my comment from Parse error on input do - #9 by cocreature You are currently duplicating things like start and end and other fields both on creation of the Auction and then also as choice arguments. You might want to reconsider this and try to structure things such that they only appear in one or the other:

  1. start and end should probably be a property of the action not of the individual bid so set them in the template.
  2. Same for the limit.
  3. The bid amount however seems like it should not be set in the template and only passed in MakeABid.
  4. Currency depends a bit. Should all bids have the same currency? If so put it on the template, if not put it in the choice.

In summary, something like this might be closer to what you were trying to model:

template AuctionBid
   with 
       auctioneer: Party
       bidder: Party
       object: Text
       currency: Text
       limit: Decimal
       start: Time
       end: Time
    where 
       signatory auctioneer 
       
       controller bidder can
          MakeABid: ContractId Auction
             with
                bid: Decimal
            do
              currentTime <- getTime
              create Auction
                 with
                    limit = limit
                    start = start
                    end = end
                    bid= bid
                    currency = currency
                    auctioneer = auctioneer
                    bidder = bidder
                    object = object
                    dateAuction = toDateUTC currentTime

                    
                  
          Decline: ContractId DeclineBid 
             with 
                reason: Text 
             do 
              create DeclineBid with ..

Thank you so much for your help! Now the test is functioning perfectly! :slight_smile:

1 Like