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
Install aelf-command Linux and macOs
1sudo npm i -g aelf-command
Window
1npm 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#
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.
1aelf-command create
1? Save account info into a file? (Y/n) Y
Make sure to choose Y to save your account information.
ℹ️ Note: If you do not save your account information (by selecting n or N), do not export the wallet password. Only proceed to the next step if you have saved your account information.
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
1. Get Testnet ELF Tokens:
To receive testnet ELF tokens, run this command after replacing $WALLET_ADDRESS and $WALLET_PASSWORD with your wallet details:
1export WALLET_ADDRESS="YOUR_WALLET_ADDRESS"2curl -X POST "https://faucet.aelf.dev/api/claim?walletAddress=$WALLET_ADDRESS" -H "accept: application/json" -d ""
2. Check ELF Balance:
To check your ELF balance, use:
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.
Deploy Smart Contract:
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/
1export CONTRACT_ADDRESS="YOUR_SMART_CONTRACT_ADDRESS e.g. 2LUmicHyH4RXrMjG4beDwuDsiWJESyLkgkwPdGTR8kahRzq5XS"
TIP
ℹ️ Note: You are to copy the smart contract address as we will be referencing it in the next quest!
INFO
🎉 You have successfully deployed your Voting dApp smart contract on the aelf testnet! In the next quest, we will be building the frontend components that allow us to interact with our deployed smart contract!
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: 18 July 2024 03:46:33 GMT+0