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

**URL:** <https://forum.canton.network/t/couldnt-match-expected-type-contractid-cashtransfer-with-actual-type-optional-contractid-cash/6585>\
**Category:** App Development\
**Tags:** daml\
**Created:** [May 22, 2023, 8:36pm UTC](https://forum.canton.network/t/couldnt-match-expected-type-contractid-cashtransfer-with-actual-type-optional-contractid-cash/6585 "2023-05-22T20:36:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gabmora](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gabmora/32/6170_2.png) [@gabmora](https://forum.canton.network/u/gabmora)\
**Post date:** [May 22, 2023, 8:36pm UTC](https://forum.canton.network/t/couldnt-match-expected-type-contractid-cashtransfer-with-actual-type-optional-contractid-cash/6585/1 "2023-05-22T20:36:41Z")

</div>

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)’

```haskell
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

```

```haskell

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!

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 23, 2023, 4:49am UTC](https://forum.canton.network/t/couldnt-match-expected-type-contractid-cashtransfer-with-actual-type-optional-contractid-cash/6585/2 "2023-05-23T04:49:06Z")

</div>

`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.

```haskell
(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”?

---

<div class="post-metadata">

**Author:** ![gabmora](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/gabmora/32/6170_2.png) [@gabmora](https://forum.canton.network/u/gabmora)\
**Post date:** [May 23, 2023, 4:57pm UTC](https://forum.canton.network/t/couldnt-match-expected-type-contractid-cashtransfer-with-actual-type-optional-contractid-cash/6585/3 "2023-05-23T16:57:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cocreature](https://yyz2.discourse-cdn.com/flex034/user_avatar/forum.canton.network/cocreature/32/36_2.png) [@cocreature](https://forum.canton.network/u/cocreature)\
**Post date:** [May 24, 2023, 5:38pm UTC](https://forum.canton.network/t/couldnt-match-expected-type-contractid-cashtransfer-with-actual-type-optional-contractid-cash/6585/4 "2023-05-24T17:38:02Z")

</div>

It sounds like you used

```haskell
cashTransferCid <- fetchByKey @Cash CashTransfer

```

as opposed to

```haskell
(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:

```haskell
      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

```
