Browse Source

fixes: removes/fixes outdated clarity syntax

feat/clarity-updates
Hank Stoever 5 years ago
parent
commit
9be3f83be2
  1. 2
      _core/smart/clarityRef.md
  2. 40
      _core/smart/functions.md
  3. 4
      _core/smart/principals.md
  4. 34
      _core/smart/sdk-quickstart.md
  5. 6
      _data/clarityRef.json

2
_core/smart/clarityRef.md

@ -26,7 +26,7 @@ Anywhere a developer wishes to use a literal unsigned integer (for example, incr
### Bool type
Supports values of `'true` or `'false`.
Supports values of `true` or `false`.
### Buffer type

40
_core/smart/functions.md

@ -11,9 +11,9 @@ Clarity includes _defines_ and native functions for creating user-defined funct
* TOC
{:toc}
## define and define-public functions
## define-private and define-public functions
Functions specified via `define-public` statements are public functions. Functions without these designations, simple `define` statements, are private functions. You can run a contract's public functions directly via the `clarity-cli execute` command line directly or from other contracts. You can use the `clarity eval` or `clarity eval_raw` commands to evaluate private functions via the command line.
Functions specified via `define-public` statements are public functions. Functions without these designations, simple `define-private` statements, are private functions. You can run a contract's public functions directly via the `clarity-cli execute` command line directly or from other contracts. You can use the `clarity eval` or `clarity eval_raw` commands to evaluate private functions via the command line.
Public functions return a Response type result. If the function returns an `ok` type, then the function call is considered valid, and any changes made to the blockchain state will be materialized. If the function returns an `err` type, it is considered invalid, and has no effect on the smart contract's state.
@ -71,19 +71,19 @@ The `define-map` interface, as described, disallows range-queries and queries-by
<th>Description</th>
</tr>
<tr>
<td><code>(fetch-entry map-name key-tuple)</code></td>
<td>Fetches the value associated with a given key in the map, or returns <code>'null</code> if there is none.</td>
<td><code>(map-get? map-name key-tuple)</code></td>
<td>Fetches the value associated with a given key in the map. Returns <code>(optional (tuple))</code></td>
</tr>
<tr>
<td><code>(set-entry! map-name key-tuple value-tuple)</code></td>
<td><code>(map-set map-name key-tuple value-tuple)</code></td>
<td>Sets the value of key-tuple in the data map.</td>
</tr>
<tr>
<td><code>(insert-entry! map-name key-tuple value-tuple)</code></td>
<td><code>(map-insert map-name key-tuple value-tuple)</code></td>
<td>Sets the value of <code>key-tuple</code> in the data map if and only if an entry does not already exist.</td>
</tr>
<tr>
<td><code>(delete-entry! map-name key-tuple)</code></td>
<td><code>(map-delete map-name key-tuple)</code></td>
<td>Removes the value associated with the input key for the given map.</td>
</tr>
</table>
@ -92,9 +92,9 @@ Data maps make reasoning about functions easier. By inspecting a given function
## List operations and functions
Lists may be multi-dimensional. However, note that runtime admission checks on typed function-parameters and data-map functions like `set-entry!` are charged based on the _maximal_ size of the multi-dimensional list.
Lists may be multi-dimensional. However, note that runtime admission checks on typed function-parameters and data-map functions like `map-set` are charged based on the _maximal_ size of the multi-dimensional list.
You can call `filter` `map` and `fold` functions with user-defined functions (that is, functions defined with `(define ...)`, `(define-read-only ...)`, or `(define-public ...)`) or simple, native functions (for example, `+`, `-`, `not`).
You can call `filter` `map` and `fold` functions with user-defined functions (that is, functions defined with `(define-public ...)`, `(define-read-only ...)`, or `(define-public ...)`) or simple, native functions (for example, `+`, `-`, `not`).
## Intra-contract calls
@ -112,27 +112,5 @@ For intra-contract calls dynamic dispatch is not supported. When a contract is l
A smart contract may not modify other smart contracts' data directly; it can read data stored in those smart contracts' maps. This read ability does not alter any confidentiality guarantees of Clarity. All data in a smart contract is inherently public, andis readable through querying the underlying database in any case.
### Reading from other smart contracts
To read another contract's data, use `(fetch-contract-entry)` function. This behaves identically to `(fetch-entry)`, though it accepts a contract principal as an argument in addition to the map name:
```cl
(fetch-contract-entry
'contract-name
'map-name
'key-tuple) ;; value tuple or none
```
For example, you could do this:
```cl
(fetch-contract-entry
names
name-map
1) ;;returns owner principal of name
```
Just as with the `(contract-call)` function, the map name and contract principal arguments must be constants, specified at the time of publishing.
Finally, and importantly, the `tx-sender` variable does not change during inter-contract calls. This means that if a transaction invokes a function in a given smart contract, that function is able to make calls into other smart contracts on your behalf. This enables a wide variety of applications, but it comes with some dangers for users of smart contracts. However, the static analysis guarantees of Clarity allow clients to know a priori which functions a given smart contract will ever call. Good clients should always warn users about any potential side effects of a given transaction.

4
_core/smart/principals.md

@ -82,10 +82,10 @@ faucet" could be implemented as so:
```scheme
(define-public (claim-from-faucet)
(if (is-none? (fetch-entry claimed-before (tuple (sender tx-sender))))
(if (is-none? (map-get? claimed-before (tuple (sender tx-sender))))
(let ((requester tx-sender)) ;; set a local variable requester = tx-sender
(begin
(insert-entry! claimed-before (tuple (sender requester)) (tuple (claimed true)))
(map-set claimed-before (tuple (sender requester)) (tuple (claimed true)))
(as-contract (stx-transfer? u1 tx-sender requester))))
(err 1)))
```

34
_core/smart/sdk-quickstart.md

@ -100,10 +100,10 @@ Your project should contain three directories:
The `contracts` directory contains a single file in `sample/hello-world.clar` file.
```cl
(define (say-hi)
(define-public (say-hi)
"hello world")
(define (echo-number (val int))
(define-public (echo-number (val int))
val)
```
@ -191,10 +191,10 @@ Edit the `contracts/hello-world.clar` file.
```cl
;; Functions
(define (hello-world)
(define-public (hello-world)
"hello world")
(define (echo-number (val int))
(define-public (echo-number (val int))
val)
```
@ -206,13 +206,13 @@ Use the `+` function to create a `increment-number-by-10` function.
<pre>
;; Functions
(define (say-hi)
(define-public (say-hi)
"hello world")
(define (increment-number (number int))
(define-public (increment-number (number int))
(+ 1 number))
(define (increment-number-by-10 (number int))
(define-public (increment-number-by-10 (number int))
(+ 10 number))
</pre>
</div>
@ -226,16 +226,16 @@ Use the `+` and `-` function to create a `decrement-number` user-defined method.
<pre>
;; Functions
(define (say-hi)
(define-public (say-hi)
"hello world")
(define (increment-number (number int))
(define-public (increment-number (number int))
(+ 1 number))
(define (increment-number-by-10 (number int))
(define-public (increment-number-by-10 (number int))
(+ 10 number))
(define (decrement-number (number int))
(define-public (decrement-number (number int))
(- number 1))
</pre>
</div>
@ -259,22 +259,22 @@ Finally, try adding a `counter` variable and be sure to store it. Increment `cou
;; Functions
(define (say-hi)
(define-public (say-hi)
"hello world")
(define (increment-number (number int))
(define-public (increment-number (number int))
(+ 1 number))
(define (increment-number-by-10 (number int))
(define-public (increment-number-by-10 (number int))
(+ 10 number))
(define (decrement-number (number int))
(define-public (decrement-number (number int))
(- number 1))
(define (increment-counter)
(define-public (increment-counter)
(set-var! counter (+ 1 counter)))
(define (get-counter)
(define-public (get-counter)
(counter))
</pre>

6
_data/clarityRef.json

@ -534,7 +534,7 @@
"output_type": "Not Applicable",
"signature": "(define-map map-name ((key-name-0 key-type-0) ...) ((val-name-0 val-type-0) ...))",
"description": "`define-map` is used to define a new datamap for use in a smart contract. Such\nmaps are only modifiable by the current smart contract.\n\nMaps are defined with a key tuple type and value tuple type. These are defined using a list\nof name and type pairs, e.g., a key type might be `((id int))`, which is a tuple with a single \"id\"\nfield of type `int`.\n\nLike other kinds of definition statements, `define-map` may only be used at the top level of a smart contract\ndefinition (i.e., you cannot put a define statement in the middle of a function body).",
"example": "\n(define-map squares ((x int)) ((square int)))\n(define (add-entry (x int))\n (map-insert squares ((x 2)) ((square (* x x)))))\n(add-entry 1)\n(add-entry 2)\n(add-entry 3)\n(add-entry 4)\n(add-entry 5)\n"
"example": "\n(define-map squares ((x int)) ((square int)))\n(define-public (add-entry (x int))\n (map-insert squares ((x 2)) ((square (* x x)))))\n(add-entry 1)\n(add-entry 2)\n(add-entry 3)\n(add-entry 4)\n(add-entry 5)\n"
},
{
"name": "define-data-var",
@ -542,7 +542,7 @@
"output_type": "Not Applicable",
"signature": "(define-data-var var-name type value)",
"description": "`define-data-var` is used to define a new persisted variable for use in a smart contract. Such\nvariable are only modifiable by the current smart contract.\n\nPersisted variable are defined with a type and a value.\n\nLike other kinds of definition statements, `define-data-var` may only be used at the top level of a smart contract\ndefinition (i.e., you cannot put a define statement in the middle of a function body).",
"example": "\n(define-data-var size int 0)\n(define (set-size (value int))\n (var-set size value))\n(set-size 1)\n(set-size 2)\n"
"example": "\n(define-data-var size int 0)\n(define-public (set-size (value int))\n (var-set size value))\n(set-size 1)\n(set-size 2)\n"
},
{
"name": "define-fungible-token",
@ -584,7 +584,7 @@
"name": "none",
"output_type": "(optional ?)",
"description": "Represents the _none_ option indicating no value for a given optional (analogous to a null value).",
"example": "\n(define (only-if-positive (a int))\n (if (> a 0)\n (some a)\n none))\n(only-if-positive 4) ;; Returns (some 4)\n(only-if-positive (- 3)) ;; Returns none\n"
"example": "\n(define-public (only-if-positive (a int))\n (if (> a 0)\n (some a)\n none))\n(only-if-positive 4) ;; Returns (some 4)\n(only-if-positive (- 3)) ;; Returns none\n"
}
]
}

Loading…
Cancel
Save