Create a Chaincode Project with the Blockchain App Builder VS Code Extension

To create a Chaincode Project when using the Blockchain App Builder, you need to scaffold a chaincode project from a detailed specification file. This generates a project with all the files you need.

Background

Blockchain App Builder initializes and scaffolds a chaincode project right out of the box for you. Based on simple input, Create New Chaincode can generate complex chaincode projects with features such as:
  • Multiple assets (models) and their behaviors (controllers)
  • Auto-generate CRUD (Create/Read/Update/Delete) and non-CRUD methods
  • Automatic validation of arguments
  • Marshalling/unmarshalling of arguments
  • Transparent persistence capability (ORM)
  • Calling rich queries
  • Transient and private data support
  • Identity management

The generated project follows model/controller and decorator pattern, which allows an asset's properties maintained on the ledger to be specified as typed fields and extended with specific behaviors and validation rules. This reduces the number of lines of code which helps in readability and scalability.

Create a Specification File

Before you begin, you need to create an input specification file. Note that you cannot alter the sample specification files that were installed as part of Blockchain App Builder, but you can duplicate them or use them as a reference file for your own specification files.

  1. In the Specifications pane, select Create New Spec File.
  2. The Specifications Details pane opens:
    • Enter the name for the specification file.
    • Select the file type - YAML and JSON are supported.
    • Optionally enter a description for the file.
    • The Reference File drop-down allows you to generate your specification file from a pre-existing file in your workspace if you have a file you'd like to use as a template. If nothing is selected, the created file will be empty and you can enter your specification from scratch.
    • Enter the location where you want the specification file to be stored on your system.
    Click Save.
The new specification file is created and appears in the Specifications pane. Click on it to open it in the editor.

Import a Specification File

If you have a pre-existing specification file, you can import it:
  1. In the Specifications pane, click More Actions and select Import Specification.
  2. Browse to your file and click Import Specification.
The specification file is imported and appears in the Specifications pane. Click on it to open it in the editor.

Duplicate a Specification File

You can also duplicate a specification file that's already in your Specifications pane by right-clicking it and selecting Duplicate. Right-click the file and select Rename to update the name.

The details about the contents of specifications files are described here: Input Specification File. Use this information and the sample specification templates to create your specification content.

Validate a Specification File

If you installed the YAML Language Support extension, the specification file is automatically validated as you type. The following validation features are supported automatically:
  • Command completion
  • Completion of scalar nodes to schema defaults
  • Context-sensitive help when you hover over a property or method
  • Indents for array items as you type
  • Syntax error detection

Scaffolding the Chaincode Project

When you have a specification file that meets your needs, generate your chaincode project.
  1. In the Chaincodes pane, select Create New Chaincode.
  2. The Chaincode Details pane opens:
    • Enter the name of your chaincode project.
    • Select the language: TypeScript or Go.
    • Select the specification file that you're using to create the chaincode.
    • Enter the location or Go domain where you want the project to be created within your local development environment.
    Click Create.
When your project is created, it will be shown in the Chaincodes pane. All the files required by the chaincode will be in the project. For a detailed overview of the files created, see: For a detailed overview of a token-based project, see also:

Note:

  • The Chaincodes pane allows you open and work with content within the chaincode project, but won't let you add, delete, or rename files within the project. To do that, right-click your project and select Open in Explorer. This opens the project in the VS Code Explorer view.
  • Deleting or renaming files in the chaincode project can potentially break the link between the project files and the specification file used to create it. If you plan to synchronize your code between the two, don't change the file names.

Import an Existing Chaincode Project

If you've created a chaincode project through the CLI or you've cleaned your VS Code blockchain content and want to import a locally saved project, in the Chaincodes pane click the More Actions... icon and select Import Chaincode. Browse to the project and click Import Chaincode.