How to Debug a Contract
Debugging a sCrypt contract is as easy as debugging TypeScript, since it is just TypeScript.
Use console.log()
You can use console.log()
to print output to the console.
export class Demo extends SmartContract {
@prop()
readonly x: bigint
@prop()
readonly y: bigint
constructor(x: bigint, y: bigint) {
super(...arguments)
this.x = x
this.y = y
}
@method()
sum(a: bigint, b: bigint): bigint {
return a + b
}
@method()
public add(z: bigint) {
console.log(`z: ${z}`) // print the value of z
console.log(`sum: ${this.x + this.y}`) // print the value of this.x + this.y
assert(z == this.sum(this.x, this.y), 'incorrect sum')
}
}
After running the code, you should see the following output:
z: 3
sum: 3
Use Visual Studio Code debugger
You can use VS Code to debug sCrypt contracts, the same way as any other TypeScript programs. If you have created a project with the sCrypt CLI, you should have an auto-generated launch.json, containing everything needed for the debugger out of the box. To learn more about the VS Code TypeScript debugger, please refer to the official documentation.
You can set some breakpoints and choose Launch demo
from the Run and Debug
view (or press F5) to start the debugger instantly.
You need to change the contract file name in launch.json if needed.
Debug a test
If you want to debug a unit test written with the Mocha testing framework, choose Launch demo test
from the Run and Debug
view.
You need to change the contract test file name in launch.json if needed.
Debug a ScriptContext Failure
One common failure is caused by ScriptContext assertions, like
assert(this.ctx.hashOutputs == hash256(outputs), 'hashOutputs mismatch')
Refer to this guide to debug such failures.