Skip to content
CCPEDIAby Unity Nodes
App Development

An introduction to Daml -> 4. Transforming data using choices

2 posts229 views6 likesLast activity Nov 2021

  1. yuvalapelOP 2

    Hey, in the code below, Alice transfers money to Bob. It’s possible because Alice is an owner , and the owner is the controller of Transfer.
    During Transfer choice, after the “do” block, its create a new SimpleIou with the new owner(in our case is Bob).
    Can you explain how it’s possible?
    I thought that only the signatory of SimpleIou(in our case “Dora”) can create a new SimpleIou, and in this case the creation happen during the Transfer choice, which Alice implemented.

    module SimpleIou where
    
    import Daml.Script
    
    data Cash = Cash with
      currency : Text
      amount : Decimal
        deriving (Eq, Show)
    
    template SimpleIou
      with
        issuer : Party
        owner : Party
        cash : Cash
      where
        signatory issuer
    
        controller owner can
          Transfer
            : ContractId SimpleIou
            with
              newOwner : Party
            do
              create this with owner = newOwner
    
    test_iou = script do
      alice <- allocateParty "Alice"
      bob <- allocateParty "Bob"
      charlie <- allocateParty "Charlie"
      dora <- allocateParty "Dora"
    
      -- Dora issues an Iou for $100 to Alice.
      iou <- submit dora do
        createCmd SimpleIou with
          issuer = dora
          owner = alice
          cash = Cash with
            amount = 100.0
            currency = "USD"
    
      **-- Alice transfers it to Bob.**
    **  iou2 <- submit alice do**
    **    exerciseCmd iou Transfer with**
    **      newOwner = bob**
    
      -- Bob transfers it to Charlie.
      submit bob do
        exerciseCmd iou2 Transfer with
          newOwner = charlie
    
  2. Gary_Verhaegen 4

    In order to create a SimpleIou contract, you need to have the authority of the issuer, not necessarily be the issuer.

    In this case, here’s how to read this. The issuer (dora) very deliberately delegates to the owner (alice) the ability to create a new SimpleIou provided that the currency, amount and issuer are the same and the existing SimpleIou is destroyed in the process.

    Another way to think about it is that, within the code of the Transfer choice, you have the full authority of both the controller (alice) and all of the signatory's of the contract.