Spaces:
Runtime error
Runtime error
File size: 2,253 Bytes
711e9c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
---
sidebar_position: 7
---
# How to Debug a Contract
Debugging an sCrypt contract is as easy as debugging TypeScript, since it is just TypeScript.
## Use `console.log()`
You can use `console.log()` to print to the console.
```ts
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')
}
}
```
[Try it on Replit](https://replit.com/@msinkec/scryptTS-console-logging)
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](installation.md), you should have an auto-generated [launch.json](https://github.com/sCrypt-Inc/boilerplate/blob/master/.vscode/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](https://code.visualstudio.com/docs/TypeScript/TypeScript-debugging).

You can set some breakpoints and choose `Launch demo` from the `Run and Debug` view (or press **F5**) to start the debugger instantly.

:::note
You need to change the contract file name in [launch.json](https://github.com/sCrypt-Inc/boilerplate/blob/master/.vscode/launch.json#L13) if needed.
:::
### Debug a test
If you want to debug a unit test written with the [Mocha](https://mochajs.org) testing framework, choose `Launch demo test` from the `Run and Debug` view.

:::note
You need to change the contract test file name in [launch.json](https://github.com/sCrypt-Inc/boilerplate/blob/master/.vscode/launch.json#L25) if needed.
::: |