Skip to content
CCPEDIAby Unity Nodes
App Development

Daml json api interaction

4 posts103 views2 likesLast activity Sep 2024

  1. Dpk376OP

    http://localhost:7575/v1/create

    payload
    {
    “templateId”: “fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset”,
    “payload”: {
    “issuer”: “Alice”,
    “owner”: “Alice”,
    “name”: “TV”
    }
    }

    response

    {
    “errors”: [
    “INVALID_PARTY_IDENTIFIER(8,f7da743b): The given party is not a valid Canton party identifier: Unable to parse party: Invalid unique identifier Alice with missing namespace.”
    ],
    “status”: 400
    }

    Im not using canton Im just using sandbox

  2. Dpk376

    This is my authorization token from jwt.io

    payload ref
    {
    “https://daml.com/ledger-api”: {
    “ledgerId”: “sandbox”,
    “applicationId”: “my-app”,
    “actAs”: [“Alice”, “Bob”],
    “admin”: true
    }
    }

  3. rohitt

    Is “Alice” a valid related party?
    Instead of working with Alice you need to use the full partyID in your create request something like - Alice::randomlookingstring.

    You can use get parties to obtain that information.

    More information about party ID here.

  4. Dpk376 2

    Thank you so much !!

    I have successfully interacted with the daml smart contract through json api

    ```DAML
    module Main where
    
    import Daml.Script
    
    type AssetId = ContractId Asset
    
    template Asset
      with
        issuer : Party
        owner  : Party
        name   : Text
      where
        ensure name /= ""
        signatory issuer
        observer owner
        choice Give : AssetId
          with
            newOwner : Party
          controller owner
          do create this with
               owner = newOwner
    
    setup : Script AssetId
    setup = script do
    -- user_setup_begin
      alice <- allocatePartyWithHint "Alice" (PartyIdHint "Alice")
      bob <- allocatePartyWithHint "Bob" (PartyIdHint "Bob")
      aliceId <- validateUserId "alice"
      bobId <- validateUserId "bob"
      createUser (User aliceId (Some alice)) [CanActAs alice]
      createUser (User bobId (Some bob)) [CanActAs bob]
    -- user_setup_end
    
      aliceTV <- submit alice do
        createCmd Asset with
          issuer = alice
          owner = alice
          name = "TV"
    
      bobTV <- submit alice do
        exerciseCmd aliceTV Give with newOwner = bob
    
      submit bob do
        exerciseCmd bobTV Give with newOwner = alice
    

    " daml start "

    daml damlc inspect-dar .\.daml\dist\ownership-0.0.1.dar --json
    
    
     "main_package_id": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4"
    

    Payload for jwt.io

    {
      "https://daml.com/ledger-api": {
        "ledgerId": "sandbox",
        "applicationId": "your-app-id",
        "actAs": [
          "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
        ],
        "readAs": [
          "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
        ],
        "admin": true
      }
    }
    
    
    Authorization : Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwczovL2RhbWwuY29tL2xlZGdlci1hcGkiOnsibGVkZ2VySWQiOiJzYW5kYm94IiwiYXBwbGljYXRpb25JZCI6InlvdXItYXBwLWlkIiwiYWN0QXMiOlsiQWxpY2U6OjEyMjA0MTg5NGM1OTQ2YzlhN2ZiYWRiNTk4MGU0MjEyNjkyYjA5MTVkNzVmZDRkZTQxNjNmZjdkOWM2ZTc5ZTFlMWRlIl0sInJlYWRBcyI6WyJCb2I6OjEyMjA0MTg5NGM1OTQ2YzlhN2ZiYWRiNTk4MGU0MjEyNjkyYjA5MTVkNzVmZDRkZTQxNjNmZjdkOWM2ZTc5ZTFlMWRlIl0sImFkbWluIjp0cnVlfX0.Sqd997oqXjWM6wTTQUG7HInFqD0rx3VrlykG0cKrKMc
    

    Requests :

    1. Creating a contract

    POST

    http://localhost:7575/v1/create

    payload :

    {
        "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset",
        "payload": {
            "issuer": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
            "owner": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
            "name": "TV"
        }
    }
    
    

    Response :

    {
        "result": {
            "agreementText": "",
            "completionOffset": "00000000000000000a",
            "contractId": "00797ddc13220046702b74e1eaf55023a71f01a42561ab64a8d5d2360895cf1a61ca0312200d1a0e344535911d18b09043d8a2027b967855c3346479411e7fa04df0b17542",
            "observers": [],
            "payload": {
                "issuer": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                "owner": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                "name": "TV"
            },
            "signatories": [
                "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
            ],
            "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"
        },
        "status": 200
    }
    
    1. To retrieve users

    GET

    http://localhost:7575/v1/users

    {
        "result": [
            {
                "primaryParty": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                "userId": "alice"
            },
            {
                "primaryParty": "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                "userId": "bob"
            },
            {
                "userId": "participant_admin"
            }
        ],
        "status": 200
    }
    

    3.Excersing a choice Give

    POST

    http://localhost:7575/v1/exercise

    Payload :

    {
        "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset",
        "contractId": "00797ddc13220046702b74e1eaf55023a71f01a42561ab64a8d5d2360895cf1a61ca0312200d1a0e344535911d18b09043d8a2027b967855c3346479411e7fa04df0b17542",
        "choice": "Give",
        "argument": {
            "newOwner": "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
        }
    }
    

    Response :

    {
        "result": {
            "completionOffset": "00000000000000000b",
            "events": [
                {
                    "archived": {
                        "contractId": "00797ddc13220046702b74e1eaf55023a71f01a42561ab64a8d5d2360895cf1a61ca0312200d1a0e344535911d18b09043d8a2027b967855c3346479411e7fa04df0b17542",
                        "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"
                    }
                },
                {
                    "created": {
                        "agreementText": "",
                        "contractId": "008e4cea3a383465d37719654916ad4494fd08b60bdeb5ddaa26475c28f3d12b82ca031220901cc80bc39af569a3211f495da024ae4d5c4d5e7e6640fc992198cd51b33209",
                        "observers": [
                            "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
                        ],
                        "payload": {
                            "issuer": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                            "owner": "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                            "name": "TV"
                        },
                        "signatories": [
                            "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
                        ],
                        "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"
                    }
                }
            ],
            "exerciseResult": "008e4cea3a383465d37719654916ad4494fd08b60bdeb5ddaa26475c28f3d12b82ca031220901cc80bc39af569a3211f495da024ae4d5c4d5e7e6640fc992198cd51b33209"
        },
        "status": 200
    }
    
    
    1. Fetch Contract by ID

    POST

    http://localhost:7575/v1/fetch

    payload :

    {
        "contractId": "008e4cea3a383465d37719654916ad4494fd08b60bdeb5ddaa26475c28f3d12b82ca031220901cc80bc39af569a3211f495da024ae4d5c4d5e7e6640fc992198cd51b33209",
        "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"
    }
    

    Response :

    {
        "result": {
            "agreementText": "",
            "contractId": "008e4cea3a383465d37719654916ad4494fd08b60bdeb5ddaa26475c28f3d12b82ca031220901cc80bc39af569a3211f495da024ae4d5c4d5e7e6640fc992198cd51b33209",
            "observers": [
                "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
            ],
            "payload": {
                "issuer": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                "owner": "Bob::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                "name": "TV"
            },
            "signatories": [
                "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
            ],
            "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"
        },
        "status": 200
    }
    
    1. Query contract by owner

    POST

    http://localhost:7575/v1/query

    payload :

    {
      "templateIds": ["fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"],
      "query": {
        "owner": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
      }
    }
    
    

    Response :

    {
        "result": [
            {
                "agreementText": "",
                "contractId": "0027a89d25894149d7eb92720d30523687d46cdcbca55fa7bd02724e1575bebcc6ca0312202d4089b0f2fa5a871f5a86f0fc53c9ab94500a1d1ca56ec9dc73e5ac5f61fe99",
                "observers": [],
                "payload": {
                    "issuer": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                    "owner": "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de",
                    "name": "TV"
                },
                "signatories": [
                    "Alice::122041894c5946c9a7fbadb5980e4212692b0915d75fd4de4163ff7d9c6e79e1e1de"
                ],
                "templateId": "fc3df5fd2d44c946c40646f04913f576d2707dade4f6fa1dcbec89f6895e68d4:Main:Asset"
            }
        ],
        "status": 200
    }