Creating a Smart Contract

Apr 29, 2024 11:46:57 PM

Creating a Smart Contract

Let’s begin by creating a contract project using the extension. This will generate the project files you need to get started.

Make sure you have the Linkd Dev Tools extension installed.

Create a New Project

First, create a new folder for your project. Then, open the folder in Visual Studio Code.

Next, use the extension to create a new smart contract project.

  • Press Ctrl+Shift+P to open the command palette.
  • Type Linkd and select Linkd: Create New Project.
  • Select the language you want to use for your contract.
  • Name it Coin and press Enter.

The extension will create a new project in your folder and download the tools and dependencies required to build the project. It will also create a README.md file with instructions on how to build and deploy your project.

Your new smart contract should look like this:

Coin.py

Neo Go Configuration File

Users utilizing Go will find a separate file for the contract’s configuration. The configuration file is named Coin.yml and it’s stored next to the contract file. Here’s the content of the Coin.yml file for the Go contract:

Coin.yml

Users of other languages will not have this file.

Template Methods

The initial project template includes a few methods that are used to manage the contract’s lifecycle and authorization:

  • _deploy: Called when the contract is deployed. Methods starting with an underscore can only be called by native contracts.

  • update: Used to update the smart contract. The extension will look for a method called update in your contract. This is not mandatory but highly recommended. If your contract doesn’t have an update method, the extension will add a default one for you.

  • verify: The verify method is used by other contracts to verify if someone can act on behalf of the contract. In the template, the verify method checks if the contract owner has signed the transaction.

  • Owner Key: We store the owner script hash in the contract’s storage under the storage key "owner". This value is initialized inside the _deploy method.

Running the Hello World Contract

Use the extension to invoke the Hello World method. Click Run above the method name.

Run Extension Use the Run button above the method name.

The extension will compile and deploy the contract into your local blockchain. Internally, the extension uses Neo Express.

Hello World Output

The output window will display the result of the transaction. You should see the following message:

Now let’s update the contract code to add some functionality.