-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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' | ||
) | ||
} | ||
} |
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,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 | ||
}) | ||
}) |