Couldn't match expected type ‘ContractId CashTransfer’ with actual type ‘Optional (ContractId Cash)’

HI!
Am trying to create a BetAgreement contract with another (cashTransfer) contract from a different template referenced into it.
First issue, un-matching types error.

Couldn’t match expected type ‘ContractId CashTransfer’ with actual type ‘Optional (ContractId Cash)’

module Bet where 

import Event 
import Cash 

-- BetSlip Data Record
data BetSlip = BetSlip with 
    wager : Numeric 2 
  deriving (Eq, Show)


template BetProposalAgreement 
  with
    owner : Party 
    betReceiver : Party
    event : ContractId Event 
    proposedWager : ContractId CashTransfer
    betType : BetSlip   
    participantToWin : Text  
  where 
    signatory owner, betReceiver

nonconsuming choice CreateBet : ContractId BetProposal 
      with
        wager : Numeric 2 
        event : ContractId Event 
        predictedWinner : Text
        cashIds : [ContractId Cash]
      controller owner 
      do 
        let betType = BetSlip with ..  
        -- Create a proposal contract

        cashTransferCid <- lookupByKey @Cash CashTransfer

        create BetProposal
          with
            --cashTransferCid = cashTransferCid
            betAgreement = BetProposalAgreement with
              owner 
              betReceiver = issuer
              event
              proposedWager =  cashTransferCid
              betType  
              participantToWin = predictedWinner

Second issue; trying to create this (cashTransfer) dynamically when creating BetAgreement and not sure if this possible.

Only 6 weeks into learning daml, any thoughts on this matter would help. Thanks!

1 Like

lookupByKey gives you back an Optional (ContractId BetProposalAgreement). That’s where the mismatch is coming from: betAgreement has type ContractId BetproposalAgreement.

The easiest way to fix this is to switch to fetchByKey. That will give you a (ContractId BetProposalAgreement, BetProposalAgreement). If there is no contract with the given key, your transaction will be aborted. Alternatively, you could stick to lookupByKey but handle the None case and do something else in that case.

(cashTransferCid, _) <- fetchByKey @Cash CashTransfer

Second issue; trying to create this (cashTransfer) dynamically when creating BetAgreement and not sure if this possible.

I’m not quite following you here. What do you mean by “creating CashTransfer dynamically”?

1 Like

Thank you. I made the change to fetchByKey and now get this:

Couldn’t match expected type ‘ContractId CashTransfer’
with actual type ‘(ContractId Cash, Cash)’

As for “creating CashTransfer dynamically” I mean to make that contract at the same time as the bet agreement contract.

It sounds like you used

cashTransferCid <- fetchByKey @Cash CashTransfer

as opposed to

(cashTransferCid, _) <- fetchByKey @Cash CashTransfer

which I suggested.

As for “creating CashTransfer dynamically” I mean to make that contract at the same time as the bet agreement contract.

Do you mean the BetProposalAgreement? There is no BetAgreement in your code. You can do whatever you want in your choice, so you could just add another create statement there. Something like this:

      do 
        let betType = BetSlip with ..  
        -- Create a proposal contract

        cashTransferCid <- create CashTransfer with .. -- insert your values hereCashTransfer

        create BetProposal
          with
            --cashTransferCid = cashTransferCid
            betAgreement = BetProposalAgreement with
              owner 
              betReceiver = issuer
              event
              proposedWager =  cashTransferCid
              betType  
              participantToWin = predictedWinner