Learn to write and deploy hello world contract on a local Polygon Edge setup
Learn to write and deploy hello world contract on a local Polygon Edge setup
Step 1: In this lab, we will write a hello world smart-contract for Polygon Blockchain in Solidity language and deploy it on a local Polygon-edge Blockchain.
Step 2: Open 'VS Code' first and then open the terminal in 'VS Code'.
Step 3: The first step is to clone the official Polygon-Edge github repository for to setup the local environment.
Step 4: Now change the directory to the cloned repository.
Step 5: Now start the local Polygon-edge blockchain. Run the given command to start the local Polygon-edge blockchain. It can get stuck sometimes, if that happens just press 'Ctrl + C' to stop the blockchain and then run the command again.
Step 6: You will soon see the local Polygon-edge blockchain running on your system. You can see the logs in the terminal.
Step 7: Now create a new directory for our smart contract. You will need to create a hardhat project for this purpose but before that run the given command to create a new directory named 'hello_world_contract'.
Step 8: Now change the directory to the newly created directory.
Step 9: Next step is to initialize a new hardhat project. Run the given command to initialize a new hardhat project. Follow the on screen instructions to initialize the project(Mark 'Yes' or 'Y' whenever asked). Note: Create a javascript project when asked.
Step 10: Now switch to the file explorer in Vs Code. Inside the root directory of our project (hello_world_contract) we have a file named 'hardhat.config.js' , this file carries the configuration settings for our hardhat project. Inside the 'contracts' folder we have a '.sol' file this is the main file for our smart-contract.
Step 11: Open the 'hardhat.config.js' and add this code. This code adds the locally deployed polygon blockchain configuration settings, so that our smart contract is deployed to the correct network. All the codes has been heavily commented so that you can understand what each line of code does.
Step 12: Change the file name of the smart-contract inside the contracts directory from 'lock.sol' to 'HelloWorld.sol' and replace the file content with the one we have given. This is the code for the Hello World smart-contract which has a getGreeting() function which return the value(Hello World) stored in greet variable.
Step 13: Next, move to the scripts folder and replace the file's contents with the one we have provided. This is a javascript code to deploy the smart-contract to the local Polygon-edge blockchain.
Step 14: Now run the given command to compile the smart-contract. This will compile the smart-contract and create an artifacts folder.
Step 15: Once the contract is compiled you will see a new artifacts folder is created. Explore the folder and you will find a folder named 'contracts' within which there is a folder named 'HelloWorld.sol' and inside that folder you will find a file named 'HelloWorld.json'. This is the ABI (Application Binary Interface) file for our smart-contract.
Step 16: Once the contract is compile the next step is to deploy it to the local Polygon-edge blockchain. Run the given command to deploy the smart-contract to the local Polygon-edge blockchain. Notice the '--network development' flag this specifies the network where we want to deploy our smart-contract.
Step 17: Once the contract is deployed successfully, you will get a message with the contract address. This is the address of our smart-contract on the local Polygon-edge blockchain. Keep it safe with you as we will require it in the next step as we try to interact with our contract.
Step 18: Now we will write a javascript code to interact with our smart-contract. Create a file named 'interact.js' in the scripts folder. Add the code into the file which we have provided. This code will interact with our smart-contract and call the getGreeting() function to get the value stored in the greet variable. Make sure to add the contract address (which you got in the previous step) in the code.
Step 19: Now run the given command to interact with our smart-contract. You should be able to see 'Hello World!' message coming on the terminal showing that we have successfully interacted with our smart-contract.
Step 20: We have successfully coded, compiled and deployed our smart-contract on a local Polygon-edge blockchain.