logo

Service

Smart Contract Service#

When writing a smart contract in aelf, the first step is to define it so our tools can generate it. aelf contracts are defined and generated using gRPC and protobuf.

Example: Multi-Token Contract#

Here is a simplified part of our multi-token contract definition:

1
syntax = "proto3";
2
3
package token;
4
option csharp_namespace = "AElf.Contracts.MultiToken.Messages";
5
6
service TokenContract {
7
option (aelf.csharp_state) = "AElf.Contracts.MultiToken.TokenContractState";
8
9
// Actions
10
rpc Create (CreateInput) returns (google.protobuf.Empty) { }
11
rpc Transfer (TransferInput) returns (google.protobuf.Empty) { }
12
13
// Views
14
rpc GetBalance (GetBalanceInput) returns (GetBalanceOutput) {
15
option (aelf.is_view) = true;
16
}
17
}

Service Methods#

There are two types of methods in a service:

Actions#

  • These methods take input and output protobuf messages.
  • They usually modify the state of the chain.
  • Example:

    1
    rpc Create (CreateInput) returns (google.protobuf.Empty) { }
  • Takes a protobuf message as input and returns a protobuf message.
  • google.protobuf.Empty signifies returning nothing.
  • Convention: append Input to protobuf types used as parameters.
  • Views#

  • These methods do not modify the state of the chain.
  • They are used to query the state.
  • Example:

    1
    rpc GetBalance (GetBalanceInput) returns (GetBalanceOutput) {
    2
    option (aelf.is_view) = true;
    3
    }
  • Annotated with a view option to indicate it's read-only.
  • Edited on: 15 July 2024 03:24:10 GMT+0