generated from readthedocs/tutorial-template
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
splitting ergoscript section and readding solo faq
- Loading branch information
1 parent
d60cbe6
commit 8c9a1ec
Showing
13 changed files
with
239 additions
and
202 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
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Anyone-Can-Spend Scripts | ||
|
||
The simplest ErgoScript program is a single boolean predicate such as: | ||
|
||
true | ||
|
||
This corresponds to the [address](https://wallet.plutomonkey.com/p2s/?source=dHJ1ZQ==) `4MQyML64GnzMxZgm`. | ||
|
||
**Notes:** | ||
|
||
1. Any funds sent to this address can be spent by anyone because the script always evaluates to `true`. | ||
2. Scripts that always evaluate to `true` (and the corresponding boxes) are called **anyone-can-spend**. | ||
|
||
A slightly more complex "anyone-can-spend" script is: | ||
|
||
true && (false || true) // address NwAyzZpF2KcXAGBJvPrAH |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
|
||
# Box Structure | ||
|
||
Both the `INPUTS` and `OUTPUTS` arrays are a collection of `Box` type, which has the following important fields: | ||
|
||
1. The amount (in nanoErgs) contained in the box: `value` | ||
2. The serialized script as an array of bytes: `propositionBytes` | ||
3. An array of tokens (optional assets): `tokens` | ||
4. The registers of a box `R4..R9` used to store arbitrary data | ||
|
||
Each element of `tokens` is a pair of type `(tokenId, amount)`, where `tokenId` is an array of 32 bytes and the amount is `Long`. | ||
An example of using tokens is the script: | ||
|
||
{ | ||
val out = OUTPUTS(0) | ||
val token = out.tokens(0) | ||
token._1 == fromBase64("nZdrGUBMAfIO6lmSRJq2zEUKGCOeYOYzAeIqbfYs8sg=") && | ||
token._2 == 1 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
# Code-blocks | ||
|
||
Multiple lines must be put inside a code-block enclosed in braces as in: | ||
|
||
{ | ||
val out = OUTPUTS(0) | ||
val in = INPUTS(0) | ||
in.value == out.value | ||
} | ||
|
||
Note that arrays in Scala are accessed using round parentheses, not square brackets like in Java or Python. | ||
Thus, `OUTPUTS(0)` refers to the first element of the `OUTPUTS` array. As in Scala, the last line of a block is the returned value of that block. In the above example, the value returned is the boolean predicate | ||
`in.value == out.value`. | ||
|
||
The above script, [corresponding to the address](https://wallet.plutomonkey.com/p2s/?source=eyAgCiAgdmFsIG91dCA9IE9VVFBVVFMoMCkKICB2YWwgaW4gPSBJTlBVVFMoMCkKICBpbi52YWx1ZSA9PSBvdXQudmFsdWUKfQ==) `2EUTBShk4TbLWJNwGpkVYh8dAPqbrfvb3p`, | ||
allows anyone to spend the corresponding box provided that the first input and first output of the transaction have the same value. | ||
|
||
Observe that we used the `val` keyword to define intermediate variables. As in Scala, a `val` defines an immutable object. Therefore, the following snippet is invalid: | ||
|
||
... | ||
val out = OUTPUTS(0) // define an immutable value and set it to the first output. | ||
out = OUTPUTS(1) // cannot reassign a val (will give error) | ||
... | ||
|
||
Unlike Scala, ErgoScript does not support the `var` keyword, and thus everything is immutable. | ||
|
||
|
||
See below how to use lambda syntax to emulate mutable variables. | ||
|
||
Multiple blocks can be joined as in: | ||
|
||
{ | ||
INPUTS(0).id == SELF.id | ||
} || { | ||
INPUTS(0).value == 100000 | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Context Variables | ||
|
||
More interesting ErgoScript programs contain predicates defined on context, such as: | ||
|
||
HEIGHT < 4000000 // address 2fp75qcgMrTNR2vuLhiJYQt | ||
|
||
This uses the context variable `HEIGHT`, representing the height of the block in which the transaction gets mined. | ||
A box with this address is "anyone-can-spend" if the blockchain height is less than 4000000 and "no-one-can-spend" otherwise. | ||
There are other context variables such as `OUTPUTS, INPUTS, minerPubKey`. See the [documentation](https://github.com/ScorexFoundation/sigmastate-interpreter/blob/develop/docs/LangSpec.md) for details. |
Oops, something went wrong.