How can you debug a TON smart contract in FunC and print logs or dump variables?
How can you debug a TON smart contract in FunC and print logs or dump variables?
The TVM has a special function for [dumping variables](https://ton.org/docs/#/func/builtins?id=dump-variable) in debug - `~dump`
Run `~dump(variable_name);` to print a variable's contents.
Run `~dump(12345);` to print the number `12345`.
Example:
```clike
() recv_internal(int msg_value, cell in_msg, slice in_msg_body) impure {
;; let's say I want to print the value of the variable msg_value
~dump(msg_value);
}
```
Please note that this command will not run on mainnet, so do not deploy production contracts with it. My favorite way to test smart contracts locally is using [ton-contract-executor](https://github.com/Naltox/ton-contract-executor) - this awesome library run a local version of the TVM in web-assembly right inside Node.js, which is very convenient for writing JavaScript/TypeScript tests.
To enable debug prints in ton-contract-executor, when you create your contract instance pass `debug: true` in SmartContractConfig and print the logs after interacting with the contract:
```js
import { SmartContract } from "ton-contract-executor";
const contract = await SmartContract.fromCell(codeCell, dataCell, {
debug: true // enable debug
});
const send = await contract.sendInternalMessage(...);
console.log(send.logs); // print the logs
```
The TVM has a special function for [dumping variables](https://ton.org/docs/#/func/builtins?id=dump-variable) in debug - `~dump`
Run `~dump(variable_name);` to print a variable's contents.
Run `~dump(12345);` to print the number `12345`.
Example:
```
() recv_internal(int msg_value, cell in_msg, slice in_msg_body) impure {
;; let's say I want to print the value of the variable msg_value
~dump(msg_value);
}
```
Please note that this command will not run on mainnet, so do not deploy production contracts with it. My favorite way to test smart contracts locally is using [ton-contract-executor](https://github.com/Naltox/ton-contract-executor) - this awesome library run a local version of the TVM in web-assembly right inside Node.js, which is very convenient for writing JavaScript/TypeScript tests.
To enable debug prints in ton-contract-executor, when you create your contract instance pass `debug: true` in SmartContractConfig and print the logs after interacting with the contract:
```js
import { SmartContract } from "ton-contract-executor";
const contract = await SmartContract.fromCell(codeCell, dataCell, {
debug: true // enable debug
});
const send = await contract.sendInternalMessage(...);
console.log(send.logs); // print the logs
```
How can you debug a TON smart contract in FunC and print logs or dump variables?
The TVM has a special function for [dumping variables](https://ton.org/docs/#/func/builtins?id=dump-variable) in debug - `~dump`
Run `~dump(variable_name);` to print a variable's contents.
Run `~dump(12345);` to print the number `12345`.
Example:
```
() recv_internal(int msg_value, cell in_msg, slice in_msg_body) impure {
;; let's say I want to print the value of the variable msg_value
~dump(msg_value);
}
```
Please note that this command will not run on mainnet, so do not deploy production contracts with it. My favorite way to test smart contracts locally is using [ton-contract-executor](https://github.com/Naltox/ton-contract-executor) - this awesome library run a local version of the TVM in web-assembly right inside Node.js, which is very convenient for writing JavaScript/TypeScript tests.
To enable debug prints in ton-contract-executor, when you create your contract instance pass `debug: true` in SmartContractConfig and print the logs after interacting with the contract:
```js
import { SmartContract } from "ton-contract-executor";
const contract = await SmartContract.fromCell(codeCell, dataCell, {
debug: true // enable debug
});
const send = await contract.sendInternalMessage(...);
console.log(send.logs); // print the logs
```