How to do type conversion for parameters during daml upgrading

I need to upgrade an old contract to a new template contract. Some parameters of the old contract belong to the custom type of the old template, which is consistent with the field content of the new template, but they do not belong to the same type. How should I convert?


As mentioned here, please don’t post screenshots of code/errors/logs; use ``` above and below as explained at the link to enclose a block of code.

It depends on what the types are.

If it’s a contract ID, then you probably need to upgrade that contract, too; if the contract ID is referenced from multiple Bet contracts, you probably need to upgrade it separately and pass the new ID in as a choice argument so that you do not turn one old pool contract into a bunch of new pool contracts.

Otherwise, well, suppose I said “write a function that converts from [Int] to [Text]”. The only thing that is clear is that you have to do something, you can’t just return the argument; beyond that it depends on what your application needs.

1 Like

In the specific case here where you have a custom BetStatus type, you need to write a conversion function.
What that looks like depends on the definition of BetStatus. In all cases, I’ll assume that you have not changed the definition.

If it’s an Enum:

data BetStatus =
  Status1
  | Status2
  | Status3

You can write

betStatus = toEnum (fromEnum oldBet.betStatus)

toEnum and fromEnum convert between enum types and Ints so this is just saying take the enum value in the same place in the enum.

If it’s a Record:

data BetStatus = BetStatus with
  field1 : T1
  field2 : T2
  field3 : T3

You can define a function

upgradeBetStatus : Old.BetStatus -> New.BetStatus
upgradeBetStatus Old.BetStatus{..} = New.BetStatus with ..

and then

betStatus = upgradeBetStatus oldBet.betStatus

If it’s a variant:

data BetStatus = 
  StatusType1 Payload1
  StatusType2 Payload1 Payload2
  StatusType3

You can define a function

upgradeBetStatus : Old.BetStatus -> New.BetStatus
upgradeBetStatus x = case x of
  Old.StatusType1 y -> New.StatusType1 y
  Old.StatusType2 y z -> New.StatusType2 y z
  Old.StatusType3 -> New.StatusType3

and then

betStatus = upgradeBetStatus oldBet.betStatus
1 Like

thank you very much