Skip to content

Commit

Permalink
more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanwaits committed Aug 13, 2024
1 parent b21660e commit 6dccf8c
Show file tree
Hide file tree
Showing 27 changed files with 70 additions and 154 deletions.
23 changes: 12 additions & 11 deletions content/docs/stacks/clarity/functions/pow.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ Use `pow` when you need to:
- Combine with other mathematical functions for comprehensive calculations.
- Be aware of the performance implications of large exponentiation operations.

## Practical Example: Calculating Powers
## Practical Example: Calculating Token Balances in Decimal Format

Let's implement a function that calculates the power of a number:
Let's implement a function that calculates the power of a number, specifically for converting integer representations of tokens or uStx:

```clarity
(define-read-only (calculate-power (base int) (exponent int))
(pow base exponent)
(define-constant MICRO_TOKENS (pow u10 u6)) ;; 6 decimal places
(define-data-var userBalance uint u100) ;; Amount reprented in a clear and readable format
(define-read-only (get-total-micro-balance (userAddress principal))
(* (var-get userBalance) MICRO_TOKENS)
)
;; Usage
(calculate-power 2 3) ;; Returns 8
(calculate-power 5 2) ;; Returns 25
(calculate-power 10 0) ;; Returns 1
(get-total-micro-balance tx-sender)
```

This example demonstrates:
1. Using `pow` to calculate the power of a number.
2. Implementing a read-only function to return the result of the exponentiation.
3. Handling both small and large input values.
1. Using `pow` to define a constant for micro tokens with 6 decimal places.
2. Implementing a read-only function to calculate the total balance in decimal format.
3. Handling balances from different versions of a token contract.

## Common Pitfalls

Expand All @@ -73,4 +74,4 @@ This example demonstrates:

## Conclusion

The `pow` function is a fundamental tool for performing exponentiation in Clarity smart contracts. It allows developers to raise numbers to a power, enabling robust and comprehensive mathematical operations. When used effectively, `pow` enhances the reliability and maintainability of your smart contract code by providing a clear and concise way to manage power calculations.
The `pow` function is a fundamental tool for performing exponentiation in Clarity smart contracts. It allows developers to raise numbers to a power, enabling robust and comprehensive mathematical operations. When used effectively, `pow` enhances the reliability and maintainability of your smart contract code by providing a clear and concise way to manage power calculations.
6 changes: 3 additions & 3 deletions content/docs/stacks/clarity/functions/principal-construct.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Use `principal-construct?` when you need to:
Let's implement a function that constructs a standard principal:

```clarity
(define-public (create-standard-principal (version-byte (buff 1)) (hash-bytes (buff 20)))
(principal-construct? version-byte hash-bytes)
(define-public (create-standard-principal (versionByte (buff 1)) (hashBytes (buff 20)))
(principal-construct? versionByte hashBytes)
)
;; Usage
Expand All @@ -57,7 +57,7 @@ This example demonstrates:

## Common Pitfalls

1. Using `principal-construct?` with incorrectly formatted `version-byte` or `hash-bytes`, causing the operation to fail.
1. Using `principal-construct?` with incorrectly formatted `versionByte` or `hashBytes`, causing the operation to fail.
2. Assuming the principal will always be valid, leading to unhandled error cases.
3. Not handling all possible conditions, resulting in incomplete principal management.
4. Overlooking the need for proper error handling and validation.
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/principal-of.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Let's implement a function that derives a principal from a public key:
)
;; Usage
(derive-principal 0x03adb8de4bfb65db2cfd6120d55c6526ae9c52e675db7e47308636534ba7786110) ;; Returns (ok 'ST1AW6EKPGT61SQ9FNVDS17RKNWT8ZP582VF9HSCP)
(derive-principal 0x0390a5cac7c33fda49f70bc1b0866fa0ba7a9440d9de647fecb8132ceb76a94dfa) ;; Returns (ok 'ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM)
```

This example demonstrates:
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/print.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Use `print` when you need to:
Let's implement a function that prints the result of an addition operation:

```clarity
(define-public (add-and-print (a int) (b int))
(define-read-only (add-and-print (a int) (b int))
(print (+ a b))
)
Expand Down
8 changes: 4 additions & 4 deletions content/docs/stacks/clarity/functions/replace-at.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Use `replace-at?` when you need to:
Let's implement a function that replaces an element in a list at a specific index:

```clarity
(define-public (replace-element (lst (list 5 int)) (idx uint) (new-element int))
(replace-at? lst idx new-element)
(define-read-only (replace-element (items (list 5 int)) (index uint) (newElement int))
(replace-at? items index newElement)
)
;; Usage
(replace-element (list 1 2 3 4 5) u2 10) ;; Returns (ok (1 2 10 4 5))
(replace-element (list 1 2 3 4 5) u5 10) ;; Returns (err u1) because the index is out of bounds
(replace-element (list 1 2 3 4 5) u2 10) ;; Returns (some (list (1 2 10 4 5)))
(replace-element (list 1 2 3 4 5) u5 10) ;; Returns (none) because the index is out of bounds
```

This example demonstrates:
Expand Down
4 changes: 2 additions & 2 deletions content/docs/stacks/clarity/functions/secp256k1-recover.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Use `secp256k1-recover?` when you need to:
Let's implement a function that recovers the public key from a message hash and signature:

```clarity
(define-public (recover-public-key (message-hash (buff 32)) (signature (buff 65)))
(secp256k1-recover? message-hash signature)
(define-read-only (recover-public-key (messageHash (buff 32)) (signature (buff 65)))
(secp256k1-recover? messageHash signature)
)
;; Usage
Expand Down
4 changes: 2 additions & 2 deletions content/docs/stacks/clarity/functions/secp256k1-verify.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ Use `secp256k1-verify` when you need to:
Let's implement a function that verifies a signature against a message hash and public key:

```clarity
(define-public (verify-signature (message-hash (buff 32)) (signature (buff 65)) (public-key (buff 33)))
(secp256k1-verify message-hash signature public-key)
(define-read-only (verify-signature (messageHash (buff 32)) (signature (buff 65)) (publicKey (buff 33)))
(secp256k1-verify messageHash signature publicKey)
)
;; Usage
Expand Down
6 changes: 3 additions & 3 deletions content/docs/stacks/clarity/functions/sha256.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Use `sha256` when you need to:
Let's implement a function that computes the SHA-256 hash of a given buffer:

```clarity
(define-public (compute-sha256 (input (buff 32)))
(define-read-only (compute-sha256 (input (buff 32)))
(sha256 input)
)
;; Usage
(compute-sha256 0x68656c6c6f20776f726c6421212121212121212121212121212121212121212121)
;; Returns 0x2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
(compute-sha256 0x68656c6c6f20776f726c64000000000000000000000000000000000000000000)
;; Returns 0x28effae679c457da1e5158c063b3dfa78d0ade721b9aa9f1fc3f46dba4c0ea15
```

This example demonstrates:
Expand Down
6 changes: 3 additions & 3 deletions content/docs/stacks/clarity/functions/sha512-256.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Use `sha512/256` when you need to:
Let's implement a function that computes the SHA-512/256 hash of a given buffer:

```clarity
(define-public (compute-sha512-256 (input (buff 32)))
(define-read-only (compute-sha512-256 (input (buff 32)))
(sha512/256 input)
)
;; Usage
(compute-sha512-256 0x68656c6c6f20776f726c6421212121212121212121212121212121212121212121)
;; Returns 0x515a7e92e7c60522db968d81ff70b80818fc17aeabbec36baf0dda2812e94a86
(compute-sha512-256 0x68656c6c6f20776f726c64000000000000000000000000000000000000000000)
;; Returns 0xcf0edb437886eae39b21ebad0caeea342d2bd61c98e9d09d0e89109a546d01fc
```

This example demonstrates:
Expand Down
6 changes: 3 additions & 3 deletions content/docs/stacks/clarity/functions/sha512.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Use `sha512` when you need to:
Let's implement a function that computes the SHA-512 hash of a given buffer:

```clarity
(define-public (compute-sha512 (input (buff 32)))
(define-read-only (compute-sha512 (input (buff 32)))
(sha512 input)
)
;; Usage
(compute-sha512 0x68656c6c6f20776f726c6421212121212121212121212121212121212121212121)
;; Returns 0x6fcee9a7b7a7b821d241c03c82377928bc6882e7a08c78a4221199bfa220cdc55212273018ee613317c8293bb8d1ce08d1e017508e94e06ab85a734c99c7cc34
(compute-sha512 0x68656c6c6f20776f726c64000000000000000000000000000000000000000000)
;; Returns 0x638f0da7489fae1f981a47199a2854d0fa117cea82bd86049930aa86e565c6cdccd52fc0e6bba5a135961ed5b7360d5e2b0ff65889acbac01361f5e291a6da45
```

This example demonstrates:
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/slice.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `slice?` when you need to:
Let's implement a function that extracts a sub-sequence from a string:

```clarity
(define-public (extract-substring (input (string-ascii 20)) (start uint) (end uint))
(define-read-only (extract-substring (input (string-ascii 20)) (start uint) (end uint))
(slice? input start end)
)
Expand Down
42 changes: 1 addition & 41 deletions content/docs/stacks/clarity/functions/some.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ description: Constructing an optional type from a value in Clarity smart contrac
```clarity
(some value)
```





















- Input: `A`
- Output: `(optional A)`
Expand Down Expand Up @@ -62,34 +42,14 @@ Use `some` when you need to:
Let's implement a function that constructs an optional type from a given integer:

```clarity
(define-public (wrap-in-optional (input int))
(define-read-only (wrap-in-optional (input int))
(some input)
)
;; Usage
(wrap-in-optional 42) ;; Returns (some 42)
(wrap-in-optional -1) ;; Returns (some -1)
```





















This example demonstrates:
1. Using `some` to construct an optional type from a given integer.
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/sqrti.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `sqrti` when you need to:
Let's implement a function that calculates the integer square root of a given number:

```clarity
(define-public (calculate-sqrti (input uint))
(define-read-only (calculate-sqrti (input uint))
(sqrti input)
)
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/string-to-int.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `string-to-int?` when you need to:
Let's implement a function that converts a string to an optional integer:

```clarity
(define-public (convert-string-to-int (input (string-ascii 20)))
(define-read-only (convert-string-to-int (input (string-ascii 20)))
(string-to-int? input)
)
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/string-to-uint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `string-to-uint?` when you need to:
Let's implement a function that converts a string to an optional unsigned integer:

```clarity
(define-public (convert-string-to-uint (input (string-ascii 20)))
(define-read-only (convert-string-to-uint (input (string-ascii 20)))
(string-to-uint? input)
)
Expand Down
4 changes: 2 additions & 2 deletions content/docs/stacks/clarity/functions/stx-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ Use `stx-account` when you need to:
Let's implement a function that queries the STX account information for a given principal:

```clarity
(define-public (get-stx-account-info (account principal))
(define-read-only (get-stx-account-info (account principal))
(stx-account account)
)
;; Usage
(get-stx-account-info 'SZ2J6ZY48GV1EZ5V2V5RB9MP66SW86PYKKQ9H6DPR)
;; Returns (tuple (balance u1000) (nonce u0) (stx-locked u0))
;; Returns (tuple (balance u0) (nonce u0) (stx-locked u0))
```

This example demonstrates:
Expand Down
3 changes: 1 addition & 2 deletions content/docs/stacks/clarity/functions/stx-burn.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ Let's implement a function that burns a specified amount of STX from the `tx-sen
)
;; Usage
(burn-stx u60) ;; Returns (ok true) if successful
(burn-stx u50) ;; Returns (err u1) if the sender does not have enough balance
(burn-stx u50) ;; Returns (ok true)
```

This example demonstrates:
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/stx-get-balance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `stx-get-balance` when you need to:
Let's implement a function that queries the STX balance of a given principal:

```clarity
(define-public (get-stx-balance (account principal))
(define-read-only (get-stx-balance (account principal))
(stx-get-balance account)
)
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/to-int.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `to-int` when you need to:
Let's implement a function that converts an unsigned integer to a signed integer:

```clarity
(define-public (convert-to-int (input uint))
(define-read-only (convert-to-int (input uint))
(to-int input)
)
Expand Down
2 changes: 1 addition & 1 deletion content/docs/stacks/clarity/functions/to-uint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Use `to-uint` when you need to:
Let's implement a function that converts a signed integer to an unsigned integer:

```clarity
(define-public (convert-to-uint (input int))
(define-read-only (convert-to-uint (input int))
(to-uint input)
)
Expand Down
6 changes: 4 additions & 2 deletions content/docs/stacks/clarity/functions/try.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@ Let's implement a function that transfers STX and handles errors using `try!`:

```clarity
(define-public (transfer-stx (amount uint) (recipient principal))
(try! (stx-transfer? amount tx-sender recipient))
(ok true)
(begin
(try! (stx-transfer? amount tx-sender recipient))
(ok true)
)
)
;; Usage
Expand Down
8 changes: 5 additions & 3 deletions content/docs/stacks/clarity/functions/tuple.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,13 @@ Let's implement a function that stores and retrieves user information using tupl
(define-map UserInfo { userId: principal } { name: (string-ascii 20), age: uint })
(define-public (set-user-info (user principal) (name (string-ascii 20)) (age uint))
(map-set UserInfo { userId: user } { name: name, age: age })
(ok true)
(begin
(map-set UserInfo { userId: user } { name: name, age: age })
(ok true)
)
)
(define-public (get-user-info (user principal))
(define-read-only (get-user-info (user principal))
(map-get? UserInfo { userId: user })
)
Expand Down
7 changes: 5 additions & 2 deletions content/docs/stacks/clarity/functions/unwrap-err-panic.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ Let's implement a function that processes an error response using `unwrap-err-pa

```clarity
(define-public (process-error-response (input (response int int)))
(let ((error-value (unwrap-err-panic input)))
(ok error-value)
(let
(
(errorValue (unwrap-err-panic input))
)
(ok errorValue)
)
)
Expand Down
7 changes: 5 additions & 2 deletions content/docs/stacks/clarity/functions/unwrap-err.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ Let's implement a function that processes an error response using `unwrap-err!`:

```clarity
(define-public (process-error-response (input (response int int)))
(let ((error-value (unwrap-err! input (err "No error found"))))
(ok error-value)
(let
(
(errorValue (unwrap-err! input (err "No error found")))
)
(ok errorValue)
)
)
Expand Down
Loading

0 comments on commit 6dccf8c

Please sign in to comment.