Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
387 changes: 108 additions & 279 deletions docs-main/appdev/modules/m3-design-patterns.mdx

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
```haskell
choice Lock : ContractId LockedCoin
with maturity: Time; locker: Party
controller owner
do create LockedCoin with coin=this; maturity; locker
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
```haskell
template LockedCoin
with
coin: Coin
maturity: Time
locker: Party
where
signatory coin.issuer, coin.owner
observer locker

choice Unlock
: ContractId Coin
controller locker
do create coin
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```haskell
-- a coin can only be archived by the issuer under the condition that the issuer is the owner of the coin. This ensures the issuer cannot archive coins at will.
choice Archives
: ()
controller issuer
do assert (issuer == owner)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
```haskell
template CoinCommitment
with
owner: Party
issuer: Party
amount: Decimal
where
signatory issuer
observer owner

nonconsuming choice LockCoin
: ContractId LockedCoin
with
coinCid: ContractId Coin
maturity: Time
locker: Party
controller owner
do
inputCoin <- fetch coinCid
assert (inputCoin.owner == owner && inputCoin.issuer == issuer && inputCoin.amount == amount)
-- the original coin is transferred to the issuer, then archived
prop <- exercise coinCid Transfer with newOwner = issuer
id <- exercise prop AcceptTransfer
exercise id Archives
create LockedCoin with
coin = inputCoin with owner; issuer; amount
maturity; locker
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
```haskell
template LockRequest
with
locker: Party
maturity: Time
coin: Coin
where
signatory locker
observer coin.owner

choice Accept : LockResult
with coinCid : ContractId Coin
controller coin.owner
do
inputCoin <- fetch coinCid
assert (inputCoin == coin)
tpCid <- exercise coinCid Transfer with newOwner = locker
coinCid <- exercise tpCid AcceptTransfer
lockCid <- create LockedCoinV2 with locker; maturity; coin
return LockResult {coinCid; lockCid}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
```haskell
template LockedCoinV2
with
coin: Coin
maturity: Time
locker: Party
where
signatory locker, coin.owner

choice UnlockV2
: ContractId Coin
with coinCid : ContractId Coin
controller locker
do
inputCoin <- fetch coinCid
assert (inputCoin.owner == locker)
tpCid <- exercise coinCid Transfer with newOwner = coin.owner
exercise tpCid AcceptTransfer

choice ClawbackV2
: ContractId Coin
with coinCid : ContractId Coin
controller coin.owner
do
currTime <- getTime
assert (currTime >= maturity)
inputCoin <- fetch coinCid
assert (inputCoin == coin with owner=locker)
tpCid <- exercise coinCid Transfer with newOwner = coin.owner
exercise tpCid AcceptTransfer
```