@prop
DecoratorThe data types used by the TicTacToe
contract include:
boolean
: true
or false
bigint
: arbitrarily large signed integer
PubKey
:public key
Sig
:signature
ByteString
: bytes
If you want to know more data types, you can reference the documentation.
sCrypt
only allows fixed-size array. When you declare an array, you must use FixedArray<T, N>
. T
is the type of array element and N
is the array size.
let a: FixedArray<bigint, 3> = [0n, 1n, 2n]; const b: FixedArray<boolean, 3> = [false, false && true, (1n > 2n)];
@prop
decoratorUse this decorator to mark any property that intends to be stored on chain. This decorator accepts a boolean parameter. By default, it is set to false
, meaning the property cannot be changed after the contract is deployed. If it is true
, the property is a so-called stateful property and its value can be updated in subsequent contract calls. Only specific types can be used in @prop
.
Contract properties without the @prop
decorator are regular TypeScript properties without any special requirement, meaning they can use any type. But accessing these member variables is prohibited in methods decorated with @method
.
class Test extends SmartContract { @prop() x: bigint; @prop(true) y: FixedArray<boolean, 2>; @prop(false) z: ByteString; // define a constant @prop() static readonly N: bigint = 3n; // suffix `n` means bigint literal. constructor(x: bigint, y: FixedArray<boolean, 2>, z: ByteString) { super(...arguments); this.x = x; this.y = y; this.z = z; } @method() public unlock(x: bigint) { assert(this.x === x, "incorrect input x"); } }
The tic-tac-toe game contract supports two players and their public keys need to be saved.
Add the following contract properties:
alice
and bob
, both of which are PubKey
type.is_alice_turn
: a boolean
. It represents whether it is alice
's turn to play.board
: a fixed-size array FixedArray<bigint, 9>
with a size of 9
. It represents the state of every square in the board.EMPTY
, type bigint
, value 0n
. It means that a square in the board is emptyALICE
, type bigint
, value 1n
. Alice places symbol X
in a square.BOB
, type bigint
, value 2n
. Bob places symbol O
in a square.Initialize all non-static properties in the constructor. Specifically, the entire board is empty at first.