Skip to content

Commit

Permalink
Merge pull request #255 from sCrypt-Inc/enum
Browse files Browse the repository at this point in the history
add enum
  • Loading branch information
zhfnjust authored Apr 1, 2024
2 parents dd56e78 + 065e277 commit 50e815b
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions docs/how-to-write-a-contract/how-to-write-a-contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,8 @@ assert(arrayA[0] = 0n)
### User-defined Types
#### `type` or `interface`
Users can best define customized types using `type` or `interface`, made of basic types.[^2]
```ts
Expand Down Expand Up @@ -503,6 +505,73 @@ function printCoord(pt: Point2) {
[^2]: A user-defined type is also passed by value on chain, and by reference off chain, same as a `FixedArray`. It is thus strongly recommended to NEVER mutate the field of a parameter, which is of a user-defined type, inside a function.
#### `enum`
sCrypt supports enumerables and they are useful to model choice and keep track of state.
Users can define enums outside of a contract.
**Declaring and using Enum**
```ts

// Enum status
// Pending - 0
// Shipped - 1
// Accepted - 2
// Rejected - 3
// Canceled - 4
export enum Status {
Pending,
Shipped,
Accepted,
Rejected,
Canceled,
}


export class Enum extends SmartContract {
@prop(true)
status: Status

constructor() {
super(...arguments)
this.status = Status.Pending
}

@method()
get(): Status {
return this.status
}

// Update status by passing Int into input
@method()
set(status: Status): void {
this.status = status
}

@method(SigHash.ANYONECANPAY_SINGLE)
public unlock() {
let s = this.get()
assert(s == Status.Pending, 'invalid stauts')

this.set(Status.Accepted)

s = this.get()

assert(s == Status.Accepted, 'invalid stauts')

assert(
this.ctx.hashOutputs ==
hash256(this.buildStateOutput(this.ctx.utxo.value)),
'hashOutputs check failed'
)
}
}
```
### Domain Types
There are several domain types, specific to the Bitcoin context, used to further improve type safety. They are all subtypes of `ByteString`. That is, they can be used where a `ByteString` is expected, but not vice versa.
Expand Down

0 comments on commit 50e815b

Please sign in to comment.