aelf Tech Talks — AElf Smart Contract Development-The First AElf Smart Contract — Part 1

aelf Developer
aelf
Published in
5 min readJan 7, 2020

By Yiqi Zhao, aelf Senior Developer

The first Aelf smart contract

In this 4 part series we will go through the development of an aelf smart contract.

If the AELf staging project can be started normally and use a single node to generate blocks, then, you can start working on the first smart contract: Hello World.

Developing AELf smart contracts only requires C # syntax, and these syntaxes will be clearly demonstrated through the demo in the following tutorial. Readers with minimal programming experience should be able to start developing smart contracts after understanding the basic development process.

Now, let’s start creating a smart contract project.

Assume we have an AElf.Boilerplate solution that has not yet created any smart contract projects. After opening the AElf.Boilerplate.sln file with IDE such as VS or Rider, the directory structure should be the following:

The Directory.Build.props file in the contract folder is there to help all projects in the contract directory to reference the same version of AELf.Sdk.CSharp, thus you will not need to manually add a reference to the latter version after creating a smart contract project. In addition, the props file imports AELf.Contract.Tools.targets, which uses MSBUILD to define some methods and structure code generation scripts defined in Protobuf.

The src folder contains items such as staging startup, system contract deployment, and simulated transaction testing, which will be explained in another section.

1. Define the services and structure of the smart contract (Proto file)

The first step in developing a smart contract is to define some interfaces (corresponding to services in GRpc) that the smart contract can provide externally and data structures (corresponding to messages in GRpc).

When using AElf’s staging to develop AElf smart contracts, we recommend placing the proto files that define contract services and data structures under the protobuf folder of the AElf.Boilerplate project:

We can see that a considerable number of proto files already exist in the protobuf folder, including the proto files of AELf system contract and a series of AELf contract standards (acs * .proto). In the AELF folder, we can find core.proto and options.proto, those are public types and extensions defined by AELF to make the development process more convenient. The AELF smart contract development inevitably requires the importing of these two files (and some other proto files defined by Google).

Regardless of the method, developers will need to create a file named hello_world_contract.proto in the protobuf folder (we recommend contract-related proto files named “contract name_contract.proto”, although this is not necessary), and then you can start defining the contract services and data structures:

csharp_namespace = “AElf.Contracts.HelloWorld” means that the corresponding C# code should be under the AElf.Contracts.HelloWorld namespace.

option (aelf.csharp_state) = “AElf.Contracts.HelloWorld.HelloWorldContractState” means that the state of the contract should be defined in the HelloWorldContractState class under the AElf.Contracts.HelloWorld namespace.

In hello_world_contract.proto, two Action services are defined (the state of the blockchain may be modified after the service is called, which can be understood as the global ledger will change after the call): Greet and GreetTo. Greet requires that the input is google.protobuf.Empty type(can be understood as a placeholder for empty input), and the output is google.protobuf.StringValue (the traditional string); GreetTo requires that the input is google.protobuf.StringValue, which outputs GreetToOutput type customized for this proto file.

In addition, a View service is defined (only used as a method to query the current state of the blockchain): GetGreetedList, which requires the input to be google.protobuf.Empty type and the output to be a customized GreetingsList type. You can see that the GreetingsList is essentially a string Lists.

TIPs:

  • The premise of using google.protobuf.Empty is to import google/protobuf/empty.proto;
  • The premise of using google.protobuf.Timestamp is to import google/protobuf/timestamp.proto;
  • The premise of using google.protobuf.StringValue is to import google/protobuf/wrappers.proto;
  • When using option (aelf.is_view) = true; declare that the service will not modify the state of the blockchain. This operation needs to be imported into aelf/options.proto. (You also need to import aelf/options.proto before using aelf.csharp_state).

After the Proto file is created, we can start to implement the three services defined in it. Part 2 will continue this process for smart contract development.

Other Topics in aelf Tech Talks:

aelf Tech Talks: Dependency Injection Part 1

— Join the Community:

· Get on our Telegram Discord Slack and Kakao channel

· Follow us on Twitter Reddit and Facebook

· Read weekly articles on the aelf blog

· Catch up with the develop progress on Github

· Telegram community in 한국 ,日本語 ,русский ,العربية ,Deutsch ,ItalianoandTiếng Việt,

· Instagram: aelfblockchain

· YouTube Channel: aelf

For more information, visit aelf.io

--

--