Skip to content

Latest commit

 

History

History
103 lines (71 loc) · 3.33 KB

File metadata and controls

103 lines (71 loc) · 3.33 KB

Introduction

Setup

To easily follow this tutorial you will need to open the AElf Boilerplate root folder in Visual Studio Code and also open the Integrated Terminal.

In the previous image you can see that the repository is composed of a chain and a web folder. The following content will help you understand the content of the chain folder. Every path in the following tutorial will be relative to this folder.

Folder structure

This section will introduce you to the structure of a smart contract developed with Boilerplate by introducing the Hello World contract example. Here's an overview of the folders and files you will find in the chain folder:

  • protobuf: contains some protobuf message definitions, some common to all contracts, some for specific contracts.
  • scripts: build scripts - not important for this guide.
  • src:
    • AElf.Boilerplate.Launcher: console project to launch the node.
    • AElf.Boilerplate.Mainchain: a library for the node.
    • HelloWorldContract: the implementation of the Hello World contract.
  • test:
    • HelloWorldContract.Test: the tests for the Hello World contract.

This guide will focus on the HelloWorldContract and HelloWorldContract.Test folders since the code we will modify is located in these.

syntax = "proto3";

import "aelf_options.proto";
import "google/protobuf/empty.proto";

option csharp_namespace = "HelloWorldContract";

service HelloWorldContract {

    option (aelf.csharp_state) = "HelloWorldContractState";

    rpc Hello (google.protobuf.Empty) returns (HelloReturn) { }
}

message HelloReturn {
    string Value = 1;
}

It's a simple contract that defines one method Hello and one type HelloReturn.

Implementation: the implementation of the contract is located in the src/HelloWorldContract/ folder, it contains two important files: HelloWorldContract.cs and HelloWorldContractState.cs that implement respectively the contract's implementation and its state.

using Google.Protobuf.WellKnownTypes;

namespace HelloWorldContract
{
    public partial class HelloWorldContract : HelloWorldContractContainer.HelloWorldContractBase
    {
        public override HelloReturn Hello(Empty input)
        {
            return new HelloReturn {Value = "Hello world!"};
        }
    }
}

The above code, represents the implementation of the Hello method of the smart contract. It returns a HelloReturn object that holds an "Hello world!" string.

using AElf.Sdk.CSharp.State;
namespace HelloWorldContract
{
    public class HelloWorldContractState : ContractState
    {
    }
}

This class represents the state of the contract. It is empty now, but you'll find out how to add some code in here in Adding some methods (below).

Testing

Now lets look at the test :

public class HelloWorldContractTest : HelloWorldContractTestBase
{
    [Fact]
    public async Task HelloCall_ReturnsHelloWorldMessage()
    {
        var result = await HelloWorldContractStub.Hello.CallAsync(new Empty());
        result.Value.ShouldBe("Hello world!");
    }
}

This is a simple test that uses AElf's test framework to validate that the method does what it's supposed to - in this case return an "Hello world!" string.