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#
Install Required Packages
1dotnet new --install AElf.ContractTemplates
AELF.ContractTemplates contains various predefined templates for the ease of developing smart contracts on the aelf blockchain.
1dotnet 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.
Note: If you have installed aelf.deploy and your terminal says that there is no such command available, please uninstall and install aelf.deploy.
Install Node.js and Yarn
1sudo 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:
1mkdir hello-world2cd hello-world3dotnet 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.
1cd src
The implementation of file src/HelloWorldState.cs is as follows:
1using AElf.Sdk.CSharp.State;23namespace AElf.Contracts.HelloWorld4{5// The state class is access the blockchain state6public class HelloWorldState : ContractState7{8// A state that holds string value9public StringState Message { get; set; }10}11}
The implementation of file src/HelloWorld.cs is as follows:
1// contract implementation starts here2namespace AElf.Contracts.HelloWorld3{4public class HelloWorld : HelloWorldContainer.HelloWorldBase5{6// A method that updates the contract state, Message with a user input7public override Empty Update(StringValue input)8{9State.Message.Value = input.Value;10Context.Fire(new UpdatedMessage11{12Value = input.Value13});14return new Empty();15}1617// A method that reads the contract state, Message18public override StringValue Read(Empty input)19{20var value = State.Message.Value;21return new StringValue22{23Value = value24};25}26}27}
Building Smart Contract#
Build the new code with the following commands inside src folder:
1dotnet 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.
1aelf-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:
1export 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
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.
1export WALLET_ADDRESS="YOUR_WALLET_ADDRESS"2curl -X POST "https://faucet.aelf.dev/api/claim?walletAddress=$WALLET_ADDRESS" -H "accept: application/json" -d ""
To check your wallet's current ELF balance:
1aelf-command call ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://tdvw-test-node.aelf.io GetBalance
You will be prompted for the following:
1Enter the required param <symbol>: ELF2Enter 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.
1export CONTRACT_PATH=$(find ~+ . -path "*patched*" | head -n 1)
1aelf-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:
1export 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.
1aelf-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.
1aelf-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