Skip to content

Commit

Permalink
some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwaits committed Aug 11, 2024
1 parent 6530ea1 commit 448f597
Show file tree
Hide file tree
Showing 28 changed files with 322 additions and 220 deletions.
20 changes: 11 additions & 9 deletions content/docs/stacks/clarity/functions/define-constant.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,28 @@ Use `define-constant` when you need to:
Let's implement a simple token contract using constants for configuration:

```clarity
(define-constant TOKEN_NAME "MyToken")
(define-constant TOKEN_SYMBOL "MTK")
(define-constant TOKEN_NAME "DOG-GO-TO-THE-MOON")
(define-constant TOKEN_SYMBOL "DOG")
(define-constant TOKEN_DECIMALS u6)
(define-constant TOKEN_SUPPLY u1000000000000) ;; 1 million tokens with 6 decimals
(define-fungible-token my-token TOKEN_SUPPLY)
(define-fungible-token DOG-GO-TO-THE-MOON TOKEN_SUPPLY)
(define-read-only (get-name)
TOKEN_NAME)
TOKEN_NAME
)
(define-read-only (get-symbol)
TOKEN_SYMBOL)
TOKEN_SYMBOL
)
(define-read-only (get-decimals)
TOKEN_DECIMALS)
TOKEN_DECIMALS
)
(define-read-only (get-balance (account principal))
(ok (ft-get-balance my-token account)))
;; Other token functions...
(ok (ft-get-balance DOG-GO-TO-THE-MOON account))
)
```

This example demonstrates:
Expand Down
13 changes: 8 additions & 5 deletions content/docs/stacks/clarity/functions/define-data-var.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,23 @@ Let's implement a basic counter using `define-data-var`:
(define-public (increment)
(begin
(var-set counter (+ (var-get counter) u1))
(ok (var-get counter))))
(ok (var-get counter))
)
)
(define-public (decrement)
(begin
(asserts! (> (var-get counter) u0) (err u"Counter cannot be negative"))
(var-set counter (- (var-get counter) u1))
(ok (var-get counter))))
(ok (var-get counter))
)
)
(define-read-only (get-counter)
(ok (var-get counter)))
(ok (var-get counter))
)
```



This example demonstrates:
1. Using `define-data-var` to create a mutable counter.
2. Implementing functions to increment and decrement the counter.
Expand Down
25 changes: 13 additions & 12 deletions content/docs/stacks/clarity/functions/define-fungible-token.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,36 +44,37 @@ Use `define-fungible-token` when you need to:
Let's implement a basic fungible token with a fixed supply:

```clarity
(define-fungible-token my-token u1000000)
(define-fungible-token DOG-GO-TO-THE-MOON u1000000)
(define-data-var tokenAdmin principal tx-sender)
(define-public (transfer (amount uint) (sender principal) (recipient principal))
(begin
(asserts! (is-eq tx-sender sender) (err u1))
(ft-transfer? my-token amount sender recipient)))
(ft-transfer? DOG-GO-TO-THE-MOON amount sender recipient)
)
)
(define-read-only (get-balance (account principal))
(ok (ft-get-balance my-token account)))
(ok (ft-get-balance DOG-GO-TO-THE-MOON account))
)
(define-public (mint (amount uint) (recipient principal))
(begin
(asserts! (is-eq tx-sender (var-get token-admin)) (err u3))
(ft-mint? my-token amount recipient)))
;; Initialize admin
(define-data-var token-admin principal tx-sender)
(asserts! (is-eq tx-sender (var-get tokenAdmin)) (err u3))
(ft-mint? DOG-GO-TO-THE-MOON amount recipient)
)
)
```




This example demonstrates:
1. Using `define-fungible-token` to create a new token with a fixed supply of 1,000,000.
2. Implementing basic token operations like transfer and balance checking.
3. Adding a mint function with admin-only access control.

## Common Pitfalls

1. Forgetting that omitting the total supply parameter allows unlimited minting.
1. Forgetting that omitting the total supply parameter allows unlimited minting, if not handled manually.
2. Not implementing proper access controls for sensitive operations like minting.
3. Overlooking the need for additional functionality like burning or pausing.

Expand Down
27 changes: 12 additions & 15 deletions content/docs/stacks/clarity/functions/define-map.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,29 @@ Use `define-map` when you need to:
Let's implement a basic user profile system using `define-map`:

```clarity
(define-map user-profiles
principal
{ name: (string-ascii 50), age: uint, is-active: bool })
(define-map UserProfiles principal { name: (string-ascii 50), age: uint, isActive: bool })
(define-public (set-profile (name (string-ascii 50)) (age uint))
(ok (map-set user-profiles tx-sender { name: name, age: age, is-active: true })))
(ok (map-set UserProfiles tx-sender { name: name, age: age, isActive: true }))
)
(define-read-only (get-profile (user principal))
(default-to
{ name: "", age: u0, is-active: false }
(map-get? user-profiles user)))
(default-to { name: "", age: u0, isActive: false } (map-get? UserProfiles user))
)
(define-public (deactivate-profile)
(match (map-get? user-profiles tx-sender)
profile (ok (map-set user-profiles tx-sender
(merge profile { is-active: false })))
(err u404)))
(match (map-get? UserProfiles tx-sender) profile
(ok (map-set UserProfiles tx-sender (merge profile { isActive: false })))
(err u404)
)
)
;; Usage
(set-profile "Alice" u30)
(set-profile "Ryan" u38)
(get-profile tx-sender)
(deactivate-profile)
```




This example demonstrates:
1. Using `define-map` to create a user profile storage system.
2. Implementing functions to set, get, and update profile data.
Expand All @@ -91,6 +87,7 @@ This example demonstrates:
- `map-set`: Used to set or update a value in a map.
- `map-delete`: Used to remove a key-value pair from a map.
- `map-insert`: Used to insert a new key-value pair only if the key doesn't already exist.
- `merge`: Used to merge two maps, combining their key-value pairs.

## Conclusion

Expand Down
42 changes: 23 additions & 19 deletions content/docs/stacks/clarity/functions/define-non-fungible-token.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,33 @@ Use `define-non-fungible-token` when you need to:
Let's implement a basic digital art NFT system:

```clarity
(define-non-fungible-token digital-art uint)
(define-data-var art-index uint u0)
(define-public (mint-artwork (artist principal) (artwork-uri (string-utf8 256)))
(let ((token-id (var-get art-index)))
(try! (nft-mint? digital-art token-id artist))
(var-set art-index (+ token-id u1))
(ok token-id)))
(define-read-only (get-owner (token-id uint))
(ok (nft-get-owner? digital-art token-id)))
(define-public (transfer (token-id uint) (sender principal) (recipient principal))
(define-non-fungible-token SharkoBarko uint)
(define-data-var index uint u0)
(define-public (mint (who principal) (artwork-uri (string-utf8 256)))
(let
(
(tokenId (var-get index))
)
(try! (nft-mint? SharkoBarko tokenId who))
(var-set index (+ tokenId u1))
(ok tokenId)
)
)
(define-read-only (get-owner (tokenId uint))
(ok (nft-get-owner? SharkoBarko tokenId))
)
(define-public (transfer (tokenId uint) (sender principal) (recipient principal))
(begin
(asserts! (is-eq tx-sender sender) (err u403))
(nft-transfer? digital-art token-id sender recipient)))
(nft-transfer? SharkoBarko tokenId sender recipient)
)
)
```





This example demonstrates:
1. Using `define-non-fungible-token` to create a new NFT class for digital artwork.
2. Implementing basic NFT operations like minting, checking ownership, and transferring.
Expand Down
14 changes: 9 additions & 5 deletions content/docs/stacks/clarity/functions/define-private.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ Let's implement a private function for input validation:

```clarity
(define-private (validate-amount (amount uint))
(and (> amount u0) (<= amount u1000000)))
(and (> amount u0) (<= amount u1000000))
)
(define-public (transfer (recipient principal) (amount uint))
(if (validate-amount amount)
(begin
;; Perform transfer logic here
(ok true))
(err u1)))
(begin
;; Perform transfer logic here
(ok true)
)
(err u1)
)
)
```

This example demonstrates:
Expand Down
51 changes: 38 additions & 13 deletions content/docs/stacks/clarity/functions/define-public.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,50 @@ Use `define-public` when you need to:
- Handle potential errors and edge cases within the function.
- Consider gas costs when designing public functions.

## Practical Example: Token Transfer Function
## Practical Examples

Let's implement a public function for transferring tokens:
### Example 1: Simple Counter

Let's implement a simple counter that can be incremented by users:

```clarity
(define-data-var counter int 0)
(define-public (increment-counter)
(begin
(var-set counter (+ (var-get counter) 1))
(ok (var-get counter))
)
)
```

This example demonstrates:
1. Using `define-public` to create a function that can be called externally.
2. Incrementing a counter and returning the new value.
3. Returning a response type to indicate success.

### Example 2: Setting a Value

Let's implement a function that sets a value if it meets certain conditions:

```clarity
(define-public (transfer (amount uint) (recipient principal))
(let ((sender tx-sender))
(if (>= (get-balance sender) amount)
(begin
(as-contract (transfer-tokens amount sender recipient))
(ok true))
(err u1))))
(define-data-var storedValue uint u0)
(define-public (set-value (value uint))
(if (> value u0)
(begin
(var-set storedValue value)
(ok value)
)
(err u1)
)
)
```

This example demonstrates:
1. Using `define-public` to create a function that can be called externally.
2. Taking parameters for the amount and recipient of the transfer.
3. Implementing basic balance checking logic.
4. Returning a response type to indicate success or failure.
2. Setting a value only if it meets a condition.
3. Returning a response type to indicate success or failure.

## Common Pitfalls

Expand All @@ -77,4 +102,4 @@ This example demonstrates:

## Conclusion

The `define-public` function is a fundamental building block for creating interactive and composable smart contracts in Clarity. By defining public functions, developers can create contracts that users and other contracts can interact with, enabling complex decentralized applications. However, it's crucial to design these functions with security, efficiency, and usability in mind.
The `define-public` function is a fundamental building block for creating interactive and composable smart contracts in Clarity. By defining public functions, developers can create contracts that users and other contracts can interact with, enabling complex decentralized applications. However, it's crucial to design these functions with security, efficiency, and usability in mind.
9 changes: 3 additions & 6 deletions content/docs/stacks/clarity/functions/define-read-only.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ Use `define-read-only` when you need to:
Let's implement a read-only function for checking token balances:

```clarity
(define-map balances principal uint)
(define-map Balances principal uint)
(define-read-only (get-balance (account principal))
(default-to u0 (map-get? balances account)))
(define-read-only (get-total-supply)
(fold + (map-values balances) u0))
(default-to u0 (map-get? Balances account))
)
```

This example demonstrates:
1. Using `define-read-only` to create functions for querying token balances.
2. Implementing a getter function for individual account balances.
3. Creating a function to calculate the total token supply without modifying state.

## Common Pitfalls

Expand Down
3 changes: 2 additions & 1 deletion content/docs/stacks/clarity/functions/define-trait.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ Let's define a simple trait for a token contract:
(transfer? (principal principal uint) (response bool uint))
(get-balance (principal) (response uint uint))
(get-token-uri () (response (optional (string-utf8 256)) uint))
))
)
)
```

This example demonstrates:
Expand Down
Loading

0 comments on commit 448f597

Please sign in to comment.