Writing Your First Contract In Solidity
An Integrated Development Environment, or IDE, is a tool that enables Solidity-based smart contract development. Does this sound familiar? No? We were in the same situation back then, but let's reduce a few important details. An IDE, to put it simply, is a piece of application development software that combines standard developer tools into a single GUI.
**Remix is a well-known JavaScript IDE for developing Ethereum smart contracts that provides full testing, deployment, and debugging of all smart contracts. **
There are many methods to get access to Remix:
- You can either access it through your browser at https://remix.ethereum.org
- You can do so by clicking the https://github.com/ethereum/remix-desktop/releases and installing it on your computer.
- The ETH Dapp called Mist is the final choice.
Click on the file explorer tab indicated by the white arrow in the image above. Select Solidity as the default environment (if prompted) and then click on the plus icon next to the browser (indicated by the white arrowhead)
Click on File Icon and name your file as ' 'HelloWorld.sol'' (whereby ".sol" refers to Solidity programs).
Copy the Following Code... //SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.9;
contract HelloWorld {
event messagechanged(string oldmsg, string newmsg); string public message;
constructor(string memory firstmessage) { message = firstmessage; }
function update(string memory newmesssage) public {
string memory oldmsg = message;
message = newmesssage;
emit messagechanged(oldmsg, newmesssage);
} }
Explaination of the Code
- It is best practise to announce our Solidity contract as a comment in the first line of code because it is not licenced:
- The Solidity compiler version is then declared for our contract.
- We pick a number greater than 0.8.0 at random.
- Next, we declare that our contract is called "Hello World," as follows:
- The most recent value of our message is then declared as a public variable with the name "message" and the data type "string". For debugging reasons, Ethereum provides a built-in data type called "event." It is comparable to JavaScript's "console.log" but is sent out during transactions on Ethereum and is recorded in its blockchain instead.
- Since we'll be changing the value of our "message" from "Hello World" to "Hello Metaschool," this applies to our situation.
- When the value of our old and new message changes, it would be a good idea to save the value in an event for later use.
- The line of code needed and run when the contract is deployed in Solidity is known as a function Object() { [native code] }. We declare our function Object() { [native code] } and assign the string "firstmessage" to our "message" variable as input. This indicates that the contract will request an introductory message when it is deployed.
- In order to alter the value of the string public message, we will pass "Hello World" as the first message while deploying the contract.
- We now require a method for periodically updating the content of our "message."
- We create the "update" function, which accepts our "newmessage" as input, for this reason.
- The value of our current "message" is then declared as a temporary variable (note "memory").
- Then, in order to maintain track of changes on the blockchain, we emit our previously declared event "messagechanged" and update the value of the "message" to the "newmessage".
Leave Writing Your First Contract In Solidity to:
Read more #solidity posts
Best Posts From param1252
We have not curated any of param1252's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.