-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
40 additions
and
1,856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,18 @@ | ||
# Print Hello World in Clarity | ||
|
||
The primary intent of this example is to demonstrate printing "Hello World" in Clarity. Additionally, there are Clarity samples for arithmetic operations, defining and applying constants, and using "maps" and "fold" as utilities, among others. | ||
The primary intent of this example is to demonstrate printing "Hello World" within a Clarity function. The example also demonstrates both read-only and public functions, simple handling of function parameters, and throwing an error within a function. | ||
|
||
## Know your Contract | ||
|
||
Review the [hello-world.clar](/examples/hello-world/contracts/hello-world.clar) contract. | ||
|
||
We want developers to familiarize themselves with these basic concepts. You can interact with this sample by commenting/uncommenting respective functions, altering the behavior, and running them through Clarinet or unit tests. | ||
You can interact with this sample by commenting/uncommenting respective functions, altering the behavior, and running them through Clarinet or unit tests. | ||
|
||
To add new contracts, follow detailed instructions at [Add new Contract](https://docs.hiro.so/clarinet/how-to-guides/how-to-add-contract). | ||
|
||
> **NOTE**: To use this example with Clarinet inside [Hiro Platform](https://platform.hiro.so), you can open the terminal session inside VS code by navigating to File -> View -> Terminal. | ||
## Test your Contract | ||
|
||
+ You can manually test your your contracts in the [Clarinet console](https://docs.hiro.so/clarinet/how-to-guides/how-to-test-contract#load-contracts-in-a-console). | ||
+ You can programmatically test your contracts with [unit tests](https://docs.hiro.so/clarinet/how-to-guides/how-to-test-contract). | ||
|
||
- You can manually test your your contracts in the [Clarinet console](https://docs.hiro.so/clarinet/how-to-guides/how-to-test-contract#load-contracts-in-a-console). | ||
- You can programmatically test your contracts with [unit tests](https://docs.hiro.so/clarinet/how-to-guides/how-to-test-contract). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,14 @@ | ||
;; A print expression | ||
(print "Hello World") | ||
|
||
;; A function that returns a message | ||
(define-public (say-hi) | ||
;; A read-only function that returns a message | ||
(define-read-only (say-hi) | ||
(ok "Hello World") | ||
) | ||
|
||
;; A function that returns an input number | ||
(define-public (echo-number (val int)) | ||
;; A read-only function that returns an input number | ||
(define-read-only (echo-number (val int)) | ||
(ok val) | ||
) | ||
|
||
;; A function that conditionally returns an ok or an error | ||
;; A public function that conditionally returns an ok or an error | ||
(define-public (check-it (flag bool)) | ||
(if flag (ok 1) (err u100)) | ||
) | ||
|
||
;; Constants | ||
(define-constant MY_CONSTANT "This is a constant value") | ||
(define-constant CONTRACT_OWNER tx-sender) | ||
|
||
;; A private function (can only be called by this contract) | ||
(define-private (is-valid-caller) | ||
(is-eq CONTRACT_OWNER tx-sender) | ||
) | ||
|
||
;; Get the STX balance of a wallet's address or a contract | ||
(stx-get-balance 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE) | ||
(stx-get-balance 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE.my-contract) | ||
|
||
;; Addition of 2 + 3 | ||
(+ 2 3) | ||
|
||
;; Lists | ||
(list 4 8 15 16 23 42) | ||
(list "Hello" "World" "!") | ||
|
||
;; Map a list: inverts the boolean values | ||
(map not (list true true false false)) | ||
|
||
;; Fold a list: sums all the numbers | ||
(fold + (list u1 u2 u3) u0) | ||
|
||
;; Mutable variable | ||
(define-data-var myNumber uint u0) | ||
(var-set myNumber u5000) | ||
|
||
;; Tuple data structure | ||
{ | ||
id: u5, ;; a uint | ||
username: "ClarityIsAwesome", ;; an ASCI string | ||
address: 'ST1HTBVD3JG9C05J7HBJTHGR0GGW7KXW28M5JS8QE ;; and a principal | ||
} | ||
|
||
;; Map data structure | ||
(define-map Scores principal uint) | ||
;; Insert a value to a map | ||
(map-insert Scores tx-sender u100) | ||
;; This second insert will do nothing because the key already exists | ||
(map-insert Scores tx-sender u200) | ||
;; The score for tx-sender will be u100. | ||
(print (map-get? Scores tx-sender)) | ||
;; Delete the entry for tx-sender. | ||
(map-delete Scores tx-sender) | ||
;; Will return none because the entry got deleted. | ||
(print (map-get? Scores tx-sender)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"clarinetVersion": "2.4.0", | ||
"clarinetVersion": "2.6.0", | ||
"examples": [ | ||
{ | ||
"title": "blank-project", | ||
|
Oops, something went wrong.