An introduction to Daml -> 4. Transforming data using choices
- 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 - Gary_Verhaegen 4
In order to create a
SimpleIoucontract, you need to have the authority of theissuer, not necessarily be theissuer.In this case, here’s how to read this. The
issuer(dora) very deliberately delegates to theowner(alice) the ability to create a newSimpleIouprovided that the currency, amount and issuer are the same and the existingSimpleIouis 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 thesignatory's of the contract.