Skip to main content

The Pact smart contract

The smart contract is called cka-message-store and can be found here . The folder contains two files message-store.pact which is the smart contract written in Pact but also message-store.repl which contains a supporting test suite. The contract is also deployed on testnet chain 0 as free.cka-message-store.

The two main functions of the contract are read-message and write-message which are shown below:

(defun read-message (account:string)
  "Read a message for a specific account"
 
  (with-default-read messages account
    { "message": "You haven't written any message yet" }
    { "message" := message }
    message
  )
)
(defun read-message (account:string)
  "Read a message for a specific account"
 
  (with-default-read messages account
    { "message": "You haven't written any message yet" }
    { "message" := message }
    message
  )
)

Reading a message is unrestricted, so everyone can access the smart contract and read the message a user has written, given the acount is provided.

(defun write-message (account:string message:string)
  "Write a message"
 
  (enforce (<= (length message) 150) "Message can be a maximum of 150 characters long")
 
  ;; Try to acquire the `ACCOUNT-OWNER` capability which checks
  ;; that the transaction owner is also the owner of the KDA account provided as parameter to our `write-messages` function.
  (with-capability (ACCOUNT-OWNER account)
    (write messages account { "message" : message })
  )
)
(defun write-message (account:string message:string)
  "Write a message"
 
  (enforce (<= (length message) 150) "Message can be a maximum of 150 characters long")
 
  ;; Try to acquire the `ACCOUNT-OWNER` capability which checks
  ;; that the transaction owner is also the owner of the KDA account provided as parameter to our `write-messages` function.
  (with-capability (ACCOUNT-OWNER account)
    (write messages account { "message" : message })
  )
)

Writing a message is guarded by a capability ACCOUNT-OWNER, so only the account owner kan write a message.

The contract contains a single table messages that stores the messages for all users.

This readme doesn't aim to be a tutorial for Pact therefore we aren't going into the complete details of the contract nor the Pact language. For more detailed info on Pact development visit the Build section on docs.kadena.io .