diff --git a/sdk/docs/BUILD.bazel b/sdk/docs/BUILD.bazel index 00bb3222c4f0..12724920ba95 100644 --- a/sdk/docs/BUILD.bazel +++ b/sdk/docs/BUILD.bazel @@ -427,6 +427,18 @@ daml_test( for version in [DEV_LF_VERSION] ] +daml_test( + name = "docs-canton-network-daml-test", + timeout = "long", + srcs = glob(["source/sdk/docs-canton-network/code-snippets/**/*.daml"]), + additional_compiler_flags = [ + "-Wupgrade-interfaces", + "-Wupgrade-exceptions", + ], + deps = ["//daml-script/daml:daml-script.dar"], +) + + daml_test( name = "quickstart-daml-test", srcs = glob( @@ -474,6 +486,8 @@ daml_test( for version in [DEV_LF_VERSION] ] + + daml_test( name = "daml-intro-9-daml-test", srcs = glob(["source/sdk/tutorials/smart-contracts/daml/daml-intro-9/**/*.daml"]), diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevDeepDivesCompositionMultiParty.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevDeepDivesCompositionMultiParty.daml new file mode 100644 index 000000000000..cabdadb720ee --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevDeepDivesCompositionMultiParty.daml @@ -0,0 +1,35 @@ +module DocsCN_AppdevDeepDivesCompositionMultiParty where + +import DA.Optional + +-- appdev_deep-dives_composition-multi-party_L78.mdx +template TradeRequest + with + buyer : Party + seller : Party + asset : Text + price : Decimal + where + signatory buyer + observer seller + + choice ConfirmTrade : ContractId TradeSettlement + controller seller + do create TradeSettlement with + buyer + seller + asset + price + +template TradeSettlement + with + buyer : Party + seller : Party + asset : Text + price : Decimal + where + signatory buyer, seller + + choice Settle : () + controller seller + do pure () diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevDeepDivesPrivacyModel.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevDeepDivesPrivacyModel.daml new file mode 100644 index 000000000000..9a506bc3a98e --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevDeepDivesPrivacyModel.daml @@ -0,0 +1,22 @@ +module DocsCN_AppdevDeepDivesPrivacyModel where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_deep-dives_privacy-model_L92.mdx + -- choice SettleTrade : () + -- controller buyer + -- do + -- -- Fetching sellerAsset here divulges it to the buyer + -- asset <- fetch sellerAssetId + -- archive sellerAssetId + -- create Asset with owner = buyer, issuer = asset.issuer, value = asset.value diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM1MentalModels.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM1MentalModels.daml new file mode 100644 index 000000000000..0a5a546cd38d --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM1MentalModels.daml @@ -0,0 +1,19 @@ +module DocsCN_AppdevModulesM1MentalModels where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_modules_m1-mental-models_L84.mdx + -- choice DoThing : Result + -- controller owner -- Declaration, not code + -- do + -- -- Only owner can reach here diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2MigrationChecklist.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2MigrationChecklist.daml new file mode 100644 index 000000000000..d50717f03d7a --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2MigrationChecklist.daml @@ -0,0 +1,21 @@ +module DocsCN_AppdevModulesM2MigrationChecklist where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_modules_m2-migration-checklist_L166.mdx + -- -- Daml: No modifier needed + -- choice AdminAction : () + -- controller admin -- Only admin can exercise + -- do + -- -- Protocol enforces this + -- pure () diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2MultiPartyWorkflows.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2MultiPartyWorkflows.daml new file mode 100644 index 000000000000..90063301569c --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2MultiPartyWorkflows.daml @@ -0,0 +1,73 @@ +module DocsCN_AppdevModulesM2MultiPartyWorkflows where + +import DA.Optional + +-- appdev_modules_m2-multi-party-workflows_L174.mdx +data WorkflowState + = Proposed + | Approved + | Settled + deriving (Eq, Show) + +template Workflow + with + initiator : Party + approver : Party + settler : Party + state : WorkflowState + payload : Text + where + signatory initiator + observer approver, settler + + choice Approve : ContractId Workflow + controller approver + do + assertMsg "Must be in Proposed state" (state == Proposed) + create this with state = Approved + + choice Settle : ContractId Workflow + controller settler + do + assertMsg "Must be in Approved state" (state == Approved) + create this with state = Settled + +-- appdev_modules_m2-multi-party-workflows_L208.mdx +-- choice ExecuteSwap : () +-- with +-- assetA : ContractId Asset +-- assetB : ContractId Asset +-- controller buyer, seller +-- do +-- -- Both happen atomically or neither does +-- exercise assetA Transfer with newOwner = buyer +-- exercise assetB Transfer with newOwner = seller + +-- appdev_modules_m2-multi-party-workflows_L260.mdx +-- template Vote +-- with +-- proposal : Text +-- voters : [Party] +-- votes : [(Party, Bool)] +-- threshold : Int +-- where +-- signatory (map fst votes) +-- observer voters + +-- choice CastVote : ContractId Vote +-- with +-- voter : Party +-- approve : Bool +-- controller voter +-- do +-- assertMsg "Not a voter" (voter `elem` voters) +-- assertMsg "Already voted" (voter `notElem` map fst votes) +-- create this with votes = (voter, approve) :: votes + +-- choice TallyAndExecute : () +-- controller voters +-- do +-- let approvals = length [v | (_, v) <- votes, v] +-- assertMsg "Threshold not met" (approvals >= threshold) +-- -- Execute the proposal... +-- pure () diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2SmartContractParadigm.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2SmartContractParadigm.daml new file mode 100644 index 000000000000..4adb58d7e14d --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM2SmartContractParadigm.daml @@ -0,0 +1,28 @@ +module DocsCN_AppdevModulesM2SmartContractParadigm where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_modules_m2-smart-contract-paradigm_L152.mdx + -- process : [Decimal] -> Update [Decimal] + -- process items = do + -- forA items \item -> do + -- assertMsg "Over threshold" (item <= threshold) + -- pure (item * 2.0) + + -- appdev_modules_m2-smart-contract-paradigm_L86.mdx + -- -- Canton: Holdings are individual contracts + -- -- Alice's total balance = sum of all Token contracts where owner = Alice + -- + -- -- Query: Find all my tokens + -- myTokens <- queryContractKey @Token myParty + -- totalBalance <- pure $ sum [amount | Token{amount} <- myTokens] diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM3DesignPatterns.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM3DesignPatterns.daml new file mode 100644 index 000000000000..34a62c1610b5 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM3DesignPatterns.daml @@ -0,0 +1,16 @@ +module DocsCN_AppdevModulesM3DesignPatterns where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_modules_m3-design-patterns_L80.mdx + -- import Intro.Asset diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM3LanguageFundamentals.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM3LanguageFundamentals.daml new file mode 100644 index 000000000000..731cc67a9ad2 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM3LanguageFundamentals.daml @@ -0,0 +1,31 @@ +module DocsCN_AppdevModulesM3LanguageFundamentals where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_modules_m3-language-fundamentals_L109.mdx + -- increment3 = (1 +) + -- double = (* 2) + + -- appdev_modules_m3-language-fundamentals_L280.mdx + -- boolToInt2 b = if b + -- then 1 + -- else 0 + + -- appdev_modules_m3-language-fundamentals_L52.mdx + -- add n m = n + m + + -- appdev_modules_m3-language-fundamentals_L84.mdx + -- increment2 = add 1 + + -- appdev_modules_m3-language-fundamentals_L96.mdx + -- three = 1 `add` 2 diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM4BackendDev.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM4BackendDev.daml new file mode 100644 index 000000000000..c8a5657278f0 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM4BackendDev.daml @@ -0,0 +1,16 @@ +module DocsCN_AppdevModulesM4BackendDev where + +import DA.Optional + +-- appdev_modules_m4-backend-dev_L261.mdx +template LicenseComment + with + provider : Party + user : Party + licenseNum : Int + commenter : Party + body : Text + createdAt : Time + where + signatory commenter + observer provider, user diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM7Performance.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM7Performance.daml new file mode 100644 index 000000000000..4a5f8f6de9fe --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevModulesM7Performance.daml @@ -0,0 +1,25 @@ +module DocsCN_AppdevModulesM7Performance where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_modules_m7-performance_L36.mdx + -- -- Avoid: creates N contracts in one transaction + -- choice ProcessBatch : [ContractId Item] + -- controller processor + -- do mapA (\item -> create Item with ...) items + + -- appdev_modules_m7-performance_L45.mdx + -- -- Better: one contract representing the batch + -- choice ProcessBatch : ContractId BatchResult + -- controller processor + -- do create BatchResult with items = processedItems diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevReferenceDamlLanguageReference.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevReferenceDamlLanguageReference.daml new file mode 100644 index 000000000000..da747a219bd5 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevReferenceDamlLanguageReference.daml @@ -0,0 +1,27 @@ +module DocsCN_AppdevReferenceDamlLanguageReference where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_reference_daml-language-reference_L185.mdx + -- -- Code from: code-snippets/Snippets.daml + -- -- [Include actual code example here] + + -- appdev_reference_daml-language-reference_L36.mdx + -- data Address = Address + -- { street : Text + -- , city : Text + -- } + -- + -- data Color = Red | Green | Blue + -- + -- newtype Token = Token Int diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevTroubleshootingGuideDevelopmentIssues.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevTroubleshootingGuideDevelopmentIssues.daml new file mode 100644 index 000000000000..5e94f770cd5f --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_AppdevTroubleshootingGuideDevelopmentIssues.daml @@ -0,0 +1,16 @@ +module DocsCN_AppdevTroubleshootingGuideDevelopmentIssues where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- appdev_troubleshooting-guide_development-issues_L21.mdx + -- asset <- fetch assetCid diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_OverviewLearnLedgerModel.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_OverviewLearnLedgerModel.daml new file mode 100644 index 000000000000..9e70469e1869 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_OverviewLearnLedgerModel.daml @@ -0,0 +1,28 @@ +module DocsCN_OverviewLearnLedgerModel where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- overview_learn_ledger-model_L269.mdx + -- choice ClaimAfterDeadline : () + -- controller beneficiary + -- do + -- assertDeadlineExceeded "claim-after-deadline" deadline + -- -- ... claim logic + + -- overview_learn_ledger-model_L283.mdx + -- -- Atomic swap: both transfers happen or neither does + -- choice ExecuteSwap : () + -- controller buyer + -- do + -- exercise assetId Transfer with newOwner = buyer + -- exercise paymentId Transfer with newOwner = seller diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_OverviewLearnPrivacyModel.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_OverviewLearnPrivacyModel.daml new file mode 100644 index 000000000000..10a5ae88aa56 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_OverviewLearnPrivacyModel.daml @@ -0,0 +1,22 @@ +module DocsCN_OverviewLearnPrivacyModel where + +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- overview_learn_privacy-model_L281.mdx + -- -- Transaction reveals more than intended + -- choice RevealingChoice : () + -- controller buyer + -- do + -- -- Fetching this contract divulges it to all transaction stakeholders + -- sensitiveAsset <- fetch sensitiveAssetId + -- -- buyer now knows about sensitiveAsset, even if not originally an observer diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_SdksToolsCliToolsDamlScript.daml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_SdksToolsCliToolsDamlScript.daml new file mode 100644 index 000000000000..94f59ecb4857 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/DocsCN_SdksToolsCliToolsDamlScript.daml @@ -0,0 +1,33 @@ +module DocsCN_SdksToolsCliToolsDamlScript where + +import Daml.Script +import DA.Time +import DA.Optional + +-- Wrapper for documentation code fragments +template DocSnippetWrapper + with + owner : Party + where + signatory owner + + -- Code fragments below are for documentation only + -- They are wrapped for compilation checking + + -- sdks-tools_cli-tools_daml-script_L102.mdx + -- -- Query all active contracts of a type visible to a party + -- contracts <- query @MyTemplate alice + -- + -- -- Query a specific contract by ID + -- optContract <- queryContractId alice cid + + -- sdks-tools_cli-tools_daml-script_L112.mdx + -- assert (length contracts == 3) + -- assertMsg "Owner should be Bob" (contract.owner == bob) + + -- sdks-tools_cli-tools_daml-script_L121.mdx + -- -- Advance ledger time + -- passTime (days 30) + -- + -- -- Set a specific time + -- setTime (time (date 2025 Jun 15) 0 0 0) diff --git a/sdk/docs/source/sdk/docs-canton-network/code-snippets/daml.yaml b/sdk/docs/source/sdk/docs-canton-network/code-snippets/daml.yaml new file mode 100644 index 000000000000..9cdd883b74d9 --- /dev/null +++ b/sdk/docs/source/sdk/docs-canton-network/code-snippets/daml.yaml @@ -0,0 +1,10 @@ +sdk-version: 3.4.10 +name: docs-canton-network-snippets +source: . +version: 0.0.1 +dependencies: + - daml-prim + - daml-stdlib + - daml-script +build-options: + - --target=2.dev