Skip to content

Commit

Permalink
Simplify hello-world template (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
leahjlou authored Apr 23, 2024
1 parent 1cd9000 commit 9916df0
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 1,856 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Furthermore, [Hiro Platform](https://platform.hiro.so/) allows developers to dis
## Available Examples

| Clarity Example | Description |
| --------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
| [hello-world](/examples/hello-world/) | A beginner smart contract with examples of commonly used Clarity expressions and data structures |
| [blank-project](/examples/blank-project) | Create a project from scratch: Start from a blank scaffold for your next Clarity contract |
| [stx-defi](/examples/stx-defi) | Demonstrates a DeFi protocol with deposits of STX, borrowing and repaying the STX-based loan, and interest and yield calculations |
| [counter](/examples/counter/) | Demonstrates how to use and interact with "variables" in Clarity by incrementing a 32-bit unsigned integer |
| [clarity-bitcoin](/examples/clarity-bitcoin/) | Demonstrates how to parse Bitcoin transactions and block headers, and to verify Bitcoin transactions |
| [fungible-token](/examples/fungible-token/) | Demonstrates a basic fungible token that conforms to the SIP-010 FT standard |
| [non-fungible-token](/examples/non-fungible-token/) | Demonstrates a basic NFT collection that conforms to the SIP-009 NFT standard |
| [nft-marketplace](/examples/nft-marketplace/) | Demonstrates a minimalistic NFT marketplace that allows users to list NFT for sale |
| [lightning-swaps](/examples/lightning-swaps/) | Demonstrates LNSwap's submarine swaps between Stacks and Bitcoin |
| [ordyswap](/examples/ordyswap/) | Demonstrates trustless atomic swaps between Ordinals and Stacks |
| Clarity Example | Description |
| --------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| [hello-world](/examples/hello-world/) | A beginner smart contract with examples of read-only and public functions |
| [blank-project](/examples/blank-project) | Create a project from scratch: Start from a blank scaffold for your next Clarity contract |
| [stx-defi](/examples/stx-defi) | Demonstrates a DeFi protocol with deposits of STX, borrowing and repaying the STX-based loan, and interest and yield calculations |
| [counter](/examples/counter/) | Demonstrates how to use and interact with "variables" in Clarity by incrementing a 32-bit unsigned integer |
| [clarity-bitcoin](/examples/clarity-bitcoin/) | Demonstrates how to parse Bitcoin transactions and block headers, and to verify Bitcoin transactions |
| [fungible-token](/examples/fungible-token/) | Demonstrates a basic fungible token that conforms to the SIP-010 FT standard |
| [non-fungible-token](/examples/non-fungible-token/) | Demonstrates a basic NFT collection that conforms to the SIP-009 NFT standard |
| [nft-marketplace](/examples/nft-marketplace/) | Demonstrates a minimalistic NFT marketplace that allows users to list NFT for sale |
| [lightning-swaps](/examples/lightning-swaps/) | Demonstrates LNSwap's submarine swaps between Stacks and Bitcoin |
| [ordyswap](/examples/ordyswap/) | Demonstrates trustless atomic swaps between Ordinals and Stacks |

## Logistics

Expand Down
9 changes: 4 additions & 5 deletions examples/hello-world/README.md
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).
63 changes: 5 additions & 58 deletions examples/hello-world/contracts/hello-world.clar
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))
4 changes: 2 additions & 2 deletions examples/hello-world/tests/hello-world.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const address1 = accounts.get("wallet_1")!;

describe("test contract functions", () => {
it("returns 'Hello World' from say-hi", () => {
const sayHiResponse = simnet.callPublicFn(
const sayHiResponse = simnet.callReadOnlyFn(
"hello-world",
"say-hi",
[],
Expand All @@ -16,7 +16,7 @@ describe("test contract functions", () => {
});

it("returns the same number from echo-number", () => {
const echoNumberResponse = simnet.callPublicFn(
const echoNumberResponse = simnet.callReadOnlyFn(
"hello-world",
"echo-number",
[Cl.int(42)],
Expand Down
2 changes: 1 addition & 1 deletion metadata.json
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",
Expand Down
Loading

0 comments on commit 9916df0

Please sign in to comment.