|
1 | 1 | ---
|
2 | 2 | sidebar_position: 2
|
3 | 3 | ---
|
| 4 | + |
4 | 5 | # Basic Syntax
|
| 6 | + |
| 7 | +Introduction to the basic grammar and rules of the Scrypt language. |
| 8 | + |
| 9 | +Reference: https://scryptdoc.readthedocs.io/en/latest/ |
| 10 | + |
| 11 | +## Constructor |
| 12 | + |
| 13 | +Each contract has only one constructor, which is used to initialize member variables. If no constructor is specified, a |
| 14 | +default constructor will be generated to initialize all members. |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Require() |
| 19 | + |
| 20 | +The `require` function specifies contract conditions and accepts a boolean parameter. If the parameter is false, the |
| 21 | +contract execution will be interrupted, and it will return a failure. |
| 22 | + |
| 23 | +When the parameter is true, the verification will pass. |
| 24 | + |
| 25 | +## Public Functions |
| 26 | + |
| 27 | +Each contract has at least one public function. This function does not return a value (void return). It can be |
| 28 | +considered the locking script or the entry point of the entire contract. Only when all the `require` conditions in the |
| 29 | +public function are met and end normally, can the contract be unlocked. |
| 30 | + |
| 31 | +In the case of multiple public functions, the contract can be considered to have multiple unlocking methods, each |
| 32 | +capable of unlocking the output. |
| 33 | + |
| 34 | +## Basic Data Structures |
| 35 | + |
| 36 | +- Bool: true or false |
| 37 | +- Int: Signed integers of any length in decimal or hexadecimal |
| 38 | +- Byte: Hexadecimal format starting with b and single quotes, or UTF-8 characters enclosed in double quotes |
| 39 | + - Array type: separated by commas, with a fixed length |
| 40 | + |
| 41 | +```c |
| 42 | +bool[3] b = [false, false && true || false, true || (1 > 2)]; |
| 43 | +int[3] c = [72, -4 - 1 - 40, 833 * (99 + 9901) + 8888]; |
| 44 | +bytes[3] a = [b'ffee', b'11', b'22']; |
| 45 | +int[2][3] d = [[11, 12, 13], [21, 22, 23]]; |
| 46 | +// array dimension can be omitted when declared |
| 47 | +int[] e = [1, 4, 2]; // e is of type int[3] |
| 48 | +int[][] f = [[11, 12, 13], [21, 22, 23]]; // f is of type int[2][3] |
| 49 | +``` |
| 50 | + |
| 51 | +Arrays can use the `repeat` function to set all values to the same. |
| 52 | + |
| 53 | +```text |
| 54 | +// a == [0, 0, 0] |
| 55 | +int[3] a = repeat(0, 3); |
| 56 | +``` |
| 57 | + |
| 58 | +Arrays can be indexed using indices starting from 0. If out of bounds, the contract will fail. |
| 59 | + |
| 60 | +Two arrays are considered equal if they have the same number of elements and each element is the same. |
| 61 | + |
| 62 | +## Struct Type |
| 63 | + |
| 64 | +A struct is a group of member variables, which can be basic data structures or nested structs. |
| 65 | + |
| 66 | +When using arrays, members can be accessed directly using the dot (.) notation. |
| 67 | + |
| 68 | +Type inference: The `auto` keyword can automatically infer basic data types. |
| 69 | + |
| 70 | +Type alias: You can assign aliases to specific data types. |
| 71 | + |
| 72 | +## Generics |
| 73 | + |
| 74 | +Generics can be defined in libraries and structs. When using generics, the corresponding type must be explicitly |
| 75 | +declared. |
| 76 | + |
| 77 | +```c |
| 78 | +library HashedMap<K, V> { |
| 79 | + // use them as function parameter's type |
| 80 | + function set(K k, V v, int idx) { |
| 81 | + ... |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +struct ST<T, P> { |
| 86 | + T t; |
| 87 | + P p; |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Domain Types |
| 92 | + |
| 93 | +Domain types are subtypes of basic types, mainly used for types related to Bitcoin transactions. Using domain types can |
| 94 | +enhance type safety. |
| 95 | + |
| 96 | +- PublicKey: PubKey(b'0200112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100'); |
| 97 | +- Sig: Sig( |
| 98 | + b'3045022100b71be3f1dc001e0a1ad65ed84e7a5a0bfe48325f2146ca1d677cf15e96e8b80302206d74605e8234eae3d4980fcd7b2fdc1c5b9374f0ce71dea38707fccdbd28cf7e41'); |
| 99 | +- Ripemd160: Ripemd160(b'0011223344556677889999887766554433221100'); |
| 100 | +- PubKeyHash: PubKeyHash(b'0011223344556677889999887766554433221100'); |
| 101 | +- Sha1: Sha1(b'0011223344556677889999887766554433221100'); |
| 102 | +- Sha256: Sha256(b'00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100'); |
| 103 | +- SigHashType: SigHashType(b'01'); SigHashType s = SigHash.ALL | SigHash.ANYONECANPAY; |
| 104 | +- SigHashPreimage: SigHashPreimage(b'0100000028bcef7e73248aa273db19d73'); |
| 105 | +- OpCodeType: OpCode.OP_DUP + OpCode.OP_ADD; |
| 106 | +- PrivKey: PrivKey(0x00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100); |
| 107 | + |
| 108 | +## Constants |
| 109 | + |
| 110 | +Constants cannot be modified once declared. |
| 111 | + |
| 112 | +```text |
| 113 | +contract Test { |
| 114 | + const int x; |
| 115 | + |
| 116 | + constructor(int x) { |
| 117 | + this.x = x; |
| 118 | + } |
| 119 | + |
| 120 | + public function equal(const int y) { |
| 121 | + y = 1; // error |
| 122 | + const int a = 36; |
| 123 | + a = 11; // error |
| 124 | + } |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +## If Condition |
| 129 | + |
| 130 | +The `if` condition is used to control the program logic flow. Besides bool, int, and bytes can also use `if`. Int 0 and |
| 131 | +byte b'', b'00' are considered false. |
| 132 | + |
| 133 | +## Exit Method |
| 134 | + |
| 135 | +The `exit` method ends the function logic early. The parameter can be true or false. |
| 136 | + |
| 137 | +## Code Separator |
| 138 | + |
| 139 | +Three or more asterisks (*) will insert an OP_CODESEPARATOR operation, which affects the signature calculation. |
| 140 | + |
| 141 | +## Access Control |
| 142 | + |
| 143 | +- Default: no keyword required |
| 144 | +- Private |
| 145 | +- Public: only applies to functions |
| 146 | + |
| 147 | +| Access Level | Same Contract | Other Contract | Externally | |
| 148 | +|--------------|---------------|----------------|------------| |
| 149 | +| Default | Yes | Yes | No | |
| 150 | +| Private | Yes | No | No | |
| 151 | +| Public | Yes | Yes | Yes | |
0 commit comments