-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #295 from yusufidimaina9989/master
Add Todo List
- Loading branch information
Showing
2 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
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,52 @@ | ||
import { | ||
SmartContract, | ||
ByteString, | ||
prop, | ||
method, | ||
assert, | ||
FixedArray, | ||
fill, | ||
toByteString, | ||
hash256, | ||
} from 'scrypt-ts' | ||
|
||
export type Task = { | ||
task: ByteString | ||
isCompleted: boolean | ||
} | ||
export class TodoList extends SmartContract { | ||
static readonly MAX_TASKCOUNT = 10 | ||
@prop(true) | ||
tasks: FixedArray<Task, typeof TodoList.MAX_TASKCOUNT> | ||
|
||
constructor() { | ||
super(...arguments) | ||
this.tasks = fill( | ||
{ | ||
task: toByteString(''), | ||
isCompleted: false, | ||
}, | ||
TodoList.MAX_TASKCOUNT | ||
) | ||
} | ||
|
||
@method() | ||
public addTask(task: Task, taskIdx: bigint) { | ||
task = this.tasks[Number(taskIdx)] | ||
const output = | ||
this.buildStateOutput(this.ctx.utxo.value) + | ||
this.buildChangeOutput() | ||
assert(hash256(output) == this.ctx.hashOutputs, 'HashOutput Mismatch') | ||
} | ||
|
||
@method() | ||
public taskCompleted(taskIdx: bigint) { | ||
const task = this.tasks[Number(taskIdx)] | ||
this.tasks[Number(taskIdx)].isCompleted = true | ||
|
||
const output = | ||
this.buildStateOutput(this.ctx.utxo.value) + | ||
this.buildChangeOutput() | ||
assert(hash256(output) == this.ctx.hashOutputs, 'HashOutput Mismatch') | ||
} | ||
} |
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,63 @@ | ||
import { expect, use } from 'chai' | ||
import { TodoList, Task } from '../src/contracts/todoList' | ||
import { getDefaultSigner} from './utils/helper' | ||
import chaiAsPromised from 'chai-as-promised' | ||
import { describe } from 'mocha' | ||
import { MethodCallOptions, toByteString } from 'scrypt-ts' | ||
|
||
use(chaiAsPromised) | ||
|
||
describe('Test SmartContract `Demo', () => { | ||
let instance: TodoList | ||
let taskIdx | ||
const Todo: Task = { | ||
task: toByteString('first task', true), | ||
isCompleted: false, | ||
} | ||
|
||
before(async () => { | ||
await TodoList.loadArtifact() | ||
|
||
instance = new TodoList() | ||
|
||
instance.connect(getDefaultSigner()) | ||
}) | ||
|
||
it('should pass the public `addTask` sucessfully', async () => { | ||
await instance.deploy(1) | ||
const nextInstance = instance.next() | ||
taskIdx = 0n | ||
console.log('Task Added : ', Buffer.from(Todo.task, "hex").toString("utf8")) | ||
console.log('isCompleted : ', Todo.isCompleted) | ||
const call = async () => { | ||
await instance.methods.addTask(Todo, taskIdx, { | ||
next: { | ||
instance: nextInstance, | ||
balance: instance.balance, | ||
}, | ||
} as MethodCallOptions<TodoList>) | ||
} | ||
return expect(call()).not.be.rejected | ||
}) | ||
|
||
it('should pass the public `completeTask` sucessfully', async () => { | ||
await instance.deploy(1) | ||
// Create the next instance from the current. | ||
const nextInstance = instance.next() | ||
taskIdx = 0 | ||
nextInstance.tasks[taskIdx].isCompleted = true | ||
|
||
console.log('Task Completed : ', Buffer.from(Todo.task, "hex").toString("utf8")) | ||
console.log('isCompleted : ', nextInstance.tasks[taskIdx].isCompleted) | ||
|
||
const call = async () => { | ||
await instance.methods.taskCompleted(BigInt(taskIdx), { | ||
next: { | ||
instance: nextInstance, | ||
balance: instance.balance, | ||
} as MethodCallOptions<TodoList>, | ||
}) | ||
} | ||
return expect(call()).not.be.rejected | ||
}) | ||
}) |