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

aelf Developer
aelf
Published in
6 min readJan 14, 2020

Part 1 of this series looked at creating the framework for a smart contract on aelf. This post will focus on the creation of the smart contracts.

2. Create a smart contract project

Create a file named “AElf.Contracts.HelloWorld” in the contract folder, and then modify the csproj file as follows:

If the current project wants to implement a service defined in a proto file, it uses the ContractCode tag to reference the proto file.

Before compiling the AELf.Contracts.HelloWorld project, create a C# code file named HelloWorldContractState in the project (or rename Class1 to HelloWorldContractState) and let HelloWorldContractState inherit from ContractState, otherwise the compilation will fail with an error:

… The type or namespace name ‘HelloWorldContractState’ does not exist in the namespace ‘AElf.Contracts.HelloWorld’ (are you missing an assembly reference?)

After compiling successfully, the directory structure of the project should be:

HelloWorldContractState.cs is used to define the contract state. The current code is:

Finally, in this project, a C# code file HelloWorldContract.cs is given to provide service implementation, and the classes in it are inherited from HelloWorldContractContainer.HelloWorldContractBase, and C#’s override mechanism can be used to implement services.

Before implementing the code of HelloWorldContract, we analyze the functions of those three services.

The Greet service is relatively simple, that is, a “Hello World!” It is returned as the transaction execution result after the call.

GreetTo is similar to Greet, except that the execution result returned includes the string specified by the transaction sender. GetGreetedList is used to query the record of transaction parameters of previous GreetTo transactions. You can ignore data cleaning up problems for now. The records must be stored as status. Therefore, you need to define a SingletonState type of a GreetedList by means of attributes in HelloWorldContractState:

For the GreetedList above, you can use State.GreetedList directly in HelloWorldContract. You may consider State.GreetedList as the entrance to the database. Use State.GreetedList.Value to get the current value of this SingletonState <GreetedList> type of database (through accessing the value property of its SingletonState, the AELf contract development SDK will complete the assembly key and read the cache and database operations in sequence).

Next, we look at how to implement those three services.

3. Create Smart Contract Testing Project

Use TestKit

AElf Contract TestKit is a testing framework specifically used to test AElf smart contracts. In this framework, it constructs a Stub and uses the methods provided by the Stub instance to simulate transaction executions (generally corresponding to the contract’s Action method). It also queries (usually corresponding to the View method of the contract), in addition to querying the transaction execution results in the test case. Following this, you complete the test task of the contract method.

In the test folder, create an xUnit project as the AELf smart contract test project, or modify the csproj file to:

If the current project needs to use a Stub of a contract to simulate sending or querying transactions, use ContractStub tag to reference the proto file.

TIPs

  • RootNamespace explicitly specifies a default namespace under this project. The default namespace is changed to be consistent with the contract code. This is not necessary.
  • You can decide whether to add a reference to the third-party class library.
  • A reference to the AELf.Contracts.TestKit of the main chain needs to be added. At the time of writing this document, the latest AELf released version is 0.9.0.
  • Because the purpose of this project is to test the HelloWorld contract, we need to add a reference to the contract project.
  • When the test environment is initialized, the HelloWorld contract needs to be deployed with zero contracts, which means that it is also necessary to use the ContractStub tag to reference the stub of the zero contract.

Test Module

XXModule is a unit for modular management of the code by the ABP framework. For contract test case projects, it only needs to rely on ContractTestModule because AELf has closed the permission to arbitrarily deploy the contract by default. When preparing the test environment, you need to manually turn on the permission to deploy the contract.

Test Base

Test Base is used to initialize the variables used in the test case (such as the contract stub and contract address, etc.) and to deploy the contract for the test.

In HelloWorldContractTestBase, we deployed the HelloWorld contract by calling the zero contract DeploySystemSmartContract method, and initialized the two important variables in the contract test case, HelloWorldContractStub and HelloWorldContractAddress.

Test Cases

When the Test Base is fully prepared, the writing part would be easy.

If you want to simulate the process of sending transactions in the test case, for example, you want to send a Greet transaction in the HelloWorld contract, you can directly use the initialized HelloWorldContractStub in Test Base, and call await HelloWorldContractStub.Greet.SendAsync (new Empty ()). After the call is over, a variable of type TransactionResult is used to receive the return value, and the execution result of this transaction is checked.

The following are the most basic test cases for the three methods of Greet, GreetTo, and GetGreetedList:

Please note that the premise of using SendAsync is to assume that the corresponding transaction must be successfully executed when writing the test case. If you want to test the exception of transaction execution failure, you need to use another method: SendWithExceptionAsync.

In Part 3 of this series, we will discuss the deployment of the smart contracts we have developed.

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

--

--