Skip to content

Commit

Permalink
Merge pull request #295 from yusufidimaina9989/master
Browse files Browse the repository at this point in the history
Add Todo List
  • Loading branch information
yusufidimaina9989 authored Oct 10, 2023
2 parents 60d5c59 + 477046a commit 47b1437
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/contracts/todoList.ts
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')
}
}
63 changes: 63 additions & 0 deletions tests/todoList.test.ts
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
})
})

0 comments on commit 47b1437

Please sign in to comment.