Skip to content

Commit

Permalink
add enum
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfnjust committed Apr 1, 2024
1 parent ed8070a commit 1e8a6f7
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 19 deletions.
34 changes: 17 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"mocha": "^10.1.0",
"prettier": "^2.8.2",
"rimraf": "^3.0.2",
"scrypt-cli": "^0.1.72",
"scrypt-cli": "^0.1.73",
"ts-node": "^10.9.1",
"typescript": "^5.3.3"
}
Expand Down
61 changes: 61 additions & 0 deletions src/contracts/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import {
method,
prop,
SmartContract,
assert,
hash256,
SigHash,
} from 'scrypt-ts'

// Enum representing shipping 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 uint 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'
)
}
}
5 changes: 4 additions & 1 deletion tests/counter.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { expect } from 'chai'
import { expect, use } from 'chai'
import { Counter } from '../src/contracts/counter'
import { getDefaultSigner } from './utils/helper'
import { MethodCallOptions } from 'scrypt-ts'

import chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)

describe('Test SmartContract `Counter`', () => {
before(() => {
Counter.loadArtifact()
Expand Down
37 changes: 37 additions & 0 deletions tests/enum.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { expect, use } from 'chai'
import { Enum, Status } from '../src/contracts/enum'
import { getDefaultSigner } from './utils/helper'
import { MethodCallOptions } from 'scrypt-ts'
import chaiAsPromised from 'chai-as-promised'
use(chaiAsPromised)

describe('Test SmartContract `Enum`', () => {
before(() => {
Enum.loadArtifact()
})

it('should pass the public method unit test successfully.', async () => {
const balance = 1

const instance = new Enum()
await instance.connect(getDefaultSigner())

await instance.deploy(1)

// create the next instance from the current
const nextInstance = instance.next()

// apply updates on the next instance off chain
nextInstance.set(Status.Accepted)

// call the method of current instance to apply the updates on chain
const callContract = async () =>
instance.methods.unlock({
next: {
instance: nextInstance,
balance,
},
} as MethodCallOptions<Enum>)
await expect(callContract()).not.rejected
})
})

0 comments on commit 1e8a6f7

Please sign in to comment.