Introduction to Bitcoin Smart Contracts (3)-sCrypt Development Tools-Visual Studio Code Plugin

The previous article mainly introduced some basic knowledge of the high-level language sCypt for Bitcoin smart contracts. Some students may have been eager to try. As the saying goes, a worker must first sharpen his tools if he wants to do his job well. Before the actual coding work begins, it is still necessary to spend some time learning how to use the right tools.

I believe that many developers and friends are using Visusal Studio Code, an IDE from Microsoft. Its excellent performance and flexible plug-in mechanism bring a very silky programming experience. To help you write Bitcoin smart contracts more easily and efficiently, sCrypt provides a feature-rich and practical tool application: sCrypt Visual Studio Code plug-in . You can directly search for "sCrypt" in the Extensions view of VS Code to install it, or you can download the independent installation package here .

Plug-in download

Below I will introduce the functions and usage of this plug-in according to the three typical scenarios in the development process (coding -> debugging -> testing).

coding

The main functions involved in the coding phase include:

  • Syntax highlighting : automatically highlight keywords, function names, and data types;
  • Jump to definition : Right-click a function or variable in the document editing area, and select "Go to Definition" in the pop-up menu to jump the cursor to the function or variable definition;
  • Find variable references : Right-click a function or variable in the document editing area and select "Go to References" from the pop-up menu to display all references;
  • Rename : Right-click the function or variable in the document editing area and select "Rename Symbol" in the pop-up menu to rename it;
  • Hovering prompt : display the definition of the variable or function where the mouse is hovering;
  • Compilation result output : Right-click in the document editing area and select "Compile to Bitcoin Script" in the pop-up menu to output the compilation result to a file;
  • Automatic prompt : In the process of code editing, prompts for possible function or variable names are automatically given according to the information currently entered;

Auto prompt

  • Function signature help : In the process of code editing, prompts related to function signature are automatically given according to the currently entered information;

Signature help

  • Code formatting : Right-click in the document editing area and select "Format Document" in the pop-up menu to format the file;

Code formatting

  • Automatic error checking : During the code editing process, the compilation error information will be displayed in the "Problems" window automatically in real time;

Error checking

debugging

The debugging function can help developers quickly locate and resolve errors in the code. Before usually start debugging, you need to .vscode/launch.jsonadd something like the following configurations:

{
            "type": "scrypt",
            "request": "launch",
            "name": "Debug sCrypt",
            "program": "${workspaceFolder}/contracts/xxx.scrypt",
            "constructorParams": "123456",
            "entryMethod": "unlock",
            "entryMethodParams": "123, 456",
            "txContext": {
                "hex": "01000000015884e5db9de218238671572340b207ee85b628074e7e467096c267266baf77a40000000000ffffffff0000000000",
                "inputIndex": 0,
                "inputSatoshis": 100000
            }
        }

During the debugging process, the available functions include:

  • Set/cancel breakpoint ;
  • Single step debugging/continue running ;
  • Enter/exit the objective function ;
  • View variable value ;
  • View the call stack ;
  • Add/delete observation expression or variable value ;
  • Support REPL 1 (that is, allow input and execution of sCrypt statement in "DEBUG CONSOLE", as shown below);

REPL

test

sCrypt provides an npm package named scrypttest2 to help developers perform contract testing. You can use Javascript or Typescript to write unit tests, and you can execute a single test file by right-clicking in the editor and selecting "Run sCrypt Test". Of course, you can also run by the command line similar npm testtest case execution order.

Note : If you want to run the test file by right-clicking in the editor, you need to meet two prerequisites:

  1. The file extension can only be .scrypttest.jsor .scrypttest.ts;
  2. In package.json test directory need to have a named single-testscript specifies the specific operational test command. E.g:
  "scripts": {
    "single-test": "mocha -r ts-node/register --timeout 120000"
  },

The above is a brief introduction to the sCrypt Visual Studio Code plug- in. In the next article, we will enter the practical phase and complete the entire process of a simple contract from development to deployment.

appendix


  1. REPL (Read-Eval-Print Loop): Interactive interpreter, convenient for debugging, testing or experimenting some codes; ↩︎

  2. scrypttest : sCrypt test toolkit, encapsulates some tool functions needed to support JS and TS unit test scenarios; ↩︎

Guess you like

Origin blog.csdn.net/freedomhero/article/details/107127341