logo

Hello World Contract

Description: This is the simplest contract to get you started. It introduces the basic structure of a smart contract and shows how to deploy and interact with it.

Purpose: To familiarize you with the basic syntax and deployment process of smart contracts on aelf blockchain.

Difficulty Level: Easy

Step 1 - Setting up your development environment#

  • Basic knowledge of terminal commands
  • IDE - Install VS Code
  • Install Required Packages

  • Install dotnet 6.0 SDK
  • Install aelf contract templates
  • 1
    dotnet new --install AElf.ContractTemplates

    AELF.ContractTemplates contains various predefined templates for the ease of developing smart contracts on the aelf blockchain.

  • Install aelf deploy tool
  • 1
    dotnet tool install --global aelf.deploy

    aelf.deploy is a utility tool for deploying smart contracts on the aelf blockchain. Please remember to export PATH after installing aelf.deploy.

    Install Node.js and Yarn

  • Install Node.js
  • Install aelf-command
  • 1
    sudo npm i -g aelf-command

    aelf-command is a CLI tool for interacting with the aelf blockchain, enabling tasks like creating wallets and managing transactions. Provide required permissions while installing aelf-command globally.

    Step 2 - Develop Smart Contract#

    Start Your Smart Contract Project#

    Open your Terminal.

    Enter the following command to generate a new project:

    1
    mkdir hello-world
    2
    cd hello-world
    3
    dotnet new aelf -n HelloWorld

    Adding Your Smart Contract Code#

    Now that we have a template hello world project, we can customise the template to incorporate our own contract logic. Lets start by implementing methods to provide basic functionality for updating and reading a message stored persistently in the contract state.

    1
    cd src

    The implementation of file src/HelloWorldState.cs is as follows:

    1
    using AElf.Sdk.CSharp.State;
    2
    3
    namespace AElf.Contracts.HelloWorld
    4
    {
    5
    // The state class is access the blockchain state
    6
    public class HelloWorldState : ContractState
    7
    {
    8
    // A state that holds string value
    9
    public StringState Message { get; set; }
    10
    }
    11
    }

    The implementation of file src/HelloWorld.cs is as follows:

    1
    // contract implementation starts here
    2
    namespace AElf.Contracts.HelloWorld
    3
    {
    4
    public class HelloWorld : HelloWorldContainer.HelloWorldBase
    5
    {
    6
    // A method that updates the contract state, Message with a user input
    7
    public override Empty Update(StringValue input)
    8
    {
    9
    State.Message.Value = input.Value;
    10
    Context.Fire(new UpdatedMessage
    11
    {
    12
    Value = input.Value
    13
    });
    14
    return new Empty();
    15
    }
    16
    17
    // A method that reads the contract state, Message
    18
    public override StringValue Read(Empty input)
    19
    {
    20
    var value = State.Message.Value;
    21
    return new StringValue
    22
    {
    23
    Value = value
    24
    };
    25
    }
    26
    }
    27
    }

    Building Smart Contract#

    Build the new code with the following commands inside src folder:

    1
    dotnet build

    Step 3 - Deploy Smart Contract#

    Create A Wallet#

    To send transactions on the aelf blockchain, you must have a wallet.

    Run this command to create aelf wallet.

    1
    aelf-command create

    You will be prompted to save your account, please do save your account as shown below:

    1
    ? Save account info into a file? (Y/n) Y

    Next, enter and confirm your password. Then export your wallet password as shown below:

    1
    export WALLET_PASSWORD="YOUR_WALLET_PASSWORD"

    Acquire Testnet Tokens (Faucet) for Development#

    To deploy smart contracts or execute on-chain transactions on aelf, you'll require testnet ELF tokens.

    Get ELF Tokens

  • CLI
  • Web
  • Run the following command to get testnet ELF tokens from faucet. Remember to either export your wallet address and wallet password or replace your wallet address and wallet password respectively.

    1
    export WALLET_ADDRESS="YOUR_WALLET_ADDRESS"
    2
    curl -X POST "https://faucet.aelf.dev/api/claim?walletAddress=$WALLET_ADDRESS" -H "accept: application/json" -d ""

    To check your wallet's current ELF balance:

    1
    aelf-command call ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io GetBalance

    You will be prompted for the following:

    1
    Enter the required param <symbol>: ELF
    2
    Enter the required param <owner>: $WALLET_ADDRESS

    You should see the result displaying your wallet's ELF balance.

    The smart contract needs to be deployed on the chain before users can interact with it.

    Run the following command to deploy a contract. Remember to export the path of LotteryGame.dll.patched to CONTRACT_PATH.

    1
    export CONTRACT_PATH=$(find ~+ . -path "*patched*" | head -n 1)
    1
    aelf-deploy -a $WALLET_ADDRESS -p $WALLET_PASSWORD -c $CONTRACT_PATH -e https://tdvw-test-node.aelf.io/

    Please wait for approximately 1 to 2 minutes. If the deployment is successful, it will provide you with the contract address.

    Export your smart contract address:

    1
    export CONTRACT_ADDRESS="YOUR_SMART_CONTRACT_ADDRESS e.g. 2LUmicHyH4RXrMjG4beDwuDsiWJESyLkgkwPdGTR8kahRzq5XS"

    Step 4 - Interact with Your Deployed Smart Contract#

    Lets try to call methods on your newly-deployed smart contract using aelf-command.

    Firstly, we will set a message using the Update method. Run the following command, and enter the message argument as test. This will set test into the Message contract state. Remember to export CONTRACT_ADDRESS equals to your deployed contract address.

    1
    aelf-command send $CONTRACT_ADDRESS -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io Update

    After that, we can use Read method to retrieve the value previously set for the Message contract state. Running the following command should yield test.

    1
    aelf-command call $CONTRACT_ADDRESS -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io Read

    Edited on: 15 July 2024 05:03:03 GMT+0