How to debug Brownie program in Visual Studio Code? I suppose there should be some launch.json created but don't know which one.
4 Answers
You can use the VSCode debugger for brownie
This is is an example of a VSCode launch.json
debug configuration for running the currently opened script on brownie:
{
"name": "Brownie: Current Script",
"type": "python",
"request": "launch",
"program": "venv/bin/brownie",
"console": "integratedTerminal",
"args": ["run","${file}"]
}
To debug something other than scripts or add arguments, just modify the "args"
array.
This works if you installed brownie using venv.
If you used pipx you can use $ whereis brownie
to get brownie's executable and put that in "program"
. You would also need to set your python interpreter to what's being used in the script (it's on the first line of the brownie file you found). I tried, it works, but there's probably a better way to do it.
Another thing to take into consideration is that brownie catches exceptions so you may want to enable breakpoints at Raised Exceptions to land on your code's errors.
You can debug in brownie a few different ways.
Use the Built in tools.
Brownie has several tools like checking the transaction trace, revert messages, and call traces. You can use any one of these to see why a transaction did/didn't go through.
For example:
>>> tx.call_trace()
Call trace for '0xd31c1c8db46a5bf2d3be822778c767e1b12e0257152fcc14dcf7e4a942793cb4':
SecurityToken.transfer 0:5198 (0xea53cB8c11f96243CE3A29C55dd9B7D761b2c0BA)
└─SecurityToken._transfer 170:5198
├─IssuingEntity.transferTokens 608:4991 (0x40b49Ad1B8D6A8Df6cEdB56081D51b69e6569e06)
│ ├─IssuingEntity.checkTransfer 834:4052
│ │ ├─IssuingEntity._getID 959:1494
│ │ │ └─KYCRegistrar.getID 1186:1331 (0xa79269260195879dBA8CEFF2767B7F2B5F2a54D8)
│ │ ├─IssuingEntity._getID 1501:1635
│ │ ├─IssuingEntity._getID 1642:2177
│ │ │ └─KYCRegistrar.getID 1869:2014 (0xa79269260195879dBA8CEFF2767B7F2B5F2a54D8)
│ │ ├─IssuingEntity._getInvestors 2305:3540
│ │ │ └─KYCRegistrar.getInvestors 2520:3483 (0xa79269260195879dBA8CEFF2767B7F2B5F2a54D8)
│ │ │ ├─KYCBase.isPermitted 2874:3003
│ │ │ │ └─KYCRegistrar.isPermittedID 2925:2997
│ │ │ └─KYCBase.isPermitted 3014:3143
│ │ │ └─KYCRegistrar.isPermittedID 3065:3137
│ │ └─IssuingEntity._checkTransfer 3603:4037
│ ├─IssuingEntity._setRating 4098:4162
│ ├─IssuingEntity._setRating 4204:4268
│ ├─SafeMath32.add 4307:4330
│ └─IssuingEntity._incrementCount 4365:4770
│ ├─SafeMath32.add 4400:4423
│ ├─SafeMath32.add 4481:4504
│ ├─SafeMath32.add 4599:4622
│ └─SafeMath32.add 4692:4715
└─SecurityToken._checkTransfer 5071:5198
Use pdb
When running tests, you can pass any flag you'd normally pass to pytest, and debug the same way you'd debug python.
For example, if you have a test like:
def test_something():
transaction = contract.do_something({"from":account})
assert transaction == 1
You can test it and step into the console like:
brownie test -k test_something --pdb
If there is an error, it will place you into a console where the error last was, and you can just run python commands to see what's going on. This is (in my opinion) one of the best ways.
Use the console
The final way is to just use the brownie console. In the brownie shell you can do "anything".
brownie console
And just try to run and debug things yourself.
-
Thanks for answer! However, since I start brownie program from terminal, can I for example step trace its python code in VSC? Or brownie console is the only way?– VitalyCommented Nov 4, 2021 at 16:52
-
You can! Using
--pdb
. Any python debugger you use you can also use in brownie to step through. Commented Nov 4, 2021 at 22:21 -
I think the answer was more if there is a possibility to use the in vscode integrated debugger (which stops at the breakpoints from vscode(red dots). If i use brownie with --pdb as described it does not stop at the vscode-breakpoints. Commented Nov 14, 2021 at 6:12
-
i meant question not answer :-), can't edit the first comment anymore Commented Nov 14, 2021 at 6:23
Adding to the comment made by @Eyon above, there is a small change needed for me.
If you just set "type" as "python" , it didn't work for me.
I also had to make sure my local workspace settings for VS code points to python in pipx folder.
Launch.json
{
"name": "Brownie aave files",
"type": "python",
"request": "launch",
"program": "/Users/myuser/.local/pipx/venvs/eth-brownie/bin/brownie",
"console": "integratedTerminal",
"env": {
"Path": "${env:Path}"
},
"args": [
"run",
"/Users/myuser/Documents/Code/RND/blockchain/solidity-youtube-course/aave-brownie/scripts/aave_borrow.py"
]
}
settings.json
{
"python.defaultInterpreterPath": "/Users/myuser/.local/pipx/venvs/eth-brownie/bin/python"
}
-
Exactly, this is what I meant. I usually just do it in the VSCode command runner, but this is better because it gets persisted in the code.– EyonCommented Oct 21, 2022 at 22:54
A couple of additional remarks as I was having issues with the above setup since my interpreter wasn't set up correctly.
Make sure you go into your VS Code Settings and search for ">python: select interpreter". Then select your new python interpreter based on the location of your newly created virtual env.
Here's a separate example of running your python file on the Goerli network:
{
"name": "Brownie: Current File (Goerli)",
"type": "python",
"request": "launch",
"program": "./venv/bin/brownie",
"args": [
"run",
"${file}",
"--network",
"goerli"
],
"console": "integratedTerminal",
},