Skip to main content

Define RPC Methods and Messages

Step 1: Defining Methods and Messages

To begin, open the src/Protobuf/contract/lottery_game_contract.proto file and replace its contents with the following code snippet:

syntax = "proto3";

import "aelf/core.proto";
import "aelf/options.proto";
import "google/protobuf/empty.proto";
import "google/protobuf/wrappers.proto";
import "acs12.proto";
// The namespace of this class
option csharp_namespace = "AElf.Contracts.LotteryGame";

service LotteryGame {
// The name of the state class the smart contract is going to use to access blockchain state
option (aelf.csharp_state) = "AElf.Contracts.LotteryGame.LotteryGameState";
option (aelf.base) = "acs12.proto";

rpc Initialize (google.protobuf.Empty) returns (google.protobuf.Empty) {
}

rpc Play (google.protobuf.Int64Value) returns (google.protobuf.Empty) {
}

rpc Withdraw (google.protobuf.Int64Value) returns (google.protobuf.Empty) {
}

rpc Deposit (google.protobuf.Int64Value) returns (google.protobuf.Empty) {
}

rpc TransferOwnership (aelf.Address) returns (google.protobuf.Empty) {
}

rpc GetPlayAmountLimit (google.protobuf.Empty) returns (PlayAmountLimitMessage) {
option (aelf.is_view) = true;
}

rpc GetContractBalance (google.protobuf.Empty) returns (google.protobuf.Int64Value) {
option (aelf.is_view) = true;
}

rpc GetOwner (google.protobuf.Empty) returns (google.protobuf.StringValue) {
option (aelf.is_view) = true;
}
}

// An event that will be emitted from contract method call when Play is called.
message PlayOutcomeEvent {
option (aelf.is_event) = true;
int64 amount = 1;
int64 won = 2;
}

// An event that will be emitted from contract method call when Withdraw is called.
message WithdrawEvent {
option (aelf.is_event) = true;
int64 amount = 1;
aelf.Address from = 2;
aelf.Address to = 3;
}

// An event that will be emitted from contract method call when Deposit is called.
message DepositEvent {
option (aelf.is_event) = true;
int64 amount = 1;
aelf.Address from = 2;
aelf.Address to = 3;
}

// The message containing the play amount limits
message PlayAmountLimitMessage {
int64 minimumAmount = 1;
int64 maximumAmount = 2;
}

Step 2: Understanding the Code

Interface Declarations:

  • Initialize - Initializes the owner of the contract and sets the reference to the MultiToken contract.
  • Play - Allows a player to draw from the lottery, resulting in either a payout to the player or a deduction of tokens.
  • Withdraw - Allows the contract owner to withdraw a specified amount of tokens from the contract's token pool.
  • Deposit - Allows the contract owner to deposit a specified amount of tokens into the contract's token pool.
  • TransferOwnership - Transfers ownership of the contract to another address. This can only be called by the current contract owner.
  • GetPlayAmountLimit - Returns the minimum and maximum token amounts that players can use per session.
  • GetContractBalance - Returns the current balance of the token pool.
  • GetOwner - Returns the current owner of the LotteryGame contract.

Message definitions:

  • PlayOutcomeEvent - Emitted to indicate the outcome of the Play method.
  • WithdrawEvent - Emitted to indicate the outcome of the Withdraw method, including the amount of tokens withdrawn and the receiver's address.
  • DepositEvent - Emitted to indicate the outcome of the Deposit method, including the amount of tokens deposited and the sender's address.
  • PlayAmountLimitMessage - Contains the upper and lower limits for the amount of tokens that can be used per play session.

Line 7 and 14 introduces acs12 to the proto file. We will learn more about it in the next step.

Step 3: Understanding ACS12

In the aelf blockchain ecosystem, ACS stands for "AElf Contract Standard." ACS defines a set of standardized interfaces and protocols that smart contracts on the aelf platform should follow to ensure interoperability and consistency across different contracts. These standards cover various functionalities, enabling developers to create more reliable and compatible smart contracts.

ACS12 is a crucial standard for defining User Smart Contracts on the aelf blockchain. Compliance with ACS12 is necessary for the automatic code checker to execute upon deployment of your contract. Therefore, all smart contracts must implement the ACS12 standards to ensure successful deployment in any of aelf's blockchain.

tip

You can acquire the latest acs12.proto from the aelf GitHub repository.

With the interface declarations and message definitions established, we can now proceed to define the smart contract states in the next section.