Write & Compile Your First Solidity Code

Sneha Thakare
4 min readMar 24, 2022

--

Solidity is the programming language to write smart contracts on the Ethereum blockchain. It’s a statically-typed, object-oriented programming language.

Contracts in Solidity are similar to classes in object-oriented languages. They contain persistent data in state variables, and functions that can modify these variables. A contract and its functions need to be called for anything to happen.

IDEs, such as Remix, make the creation process seamless using UI elements.

Remix IDE

Remix is an online IDE that enables you to write and debug your Solidity code. When you first visit Remix, you should see a landing page similar to the one below.

Writing Your First Smart Contract

Click the Create New File icon in the File Explorers tab. Name the new file and use the .sol extension to show that the file contains Solidity code.

Line 1: Specifying SPDX license type, the Solidity compiler encourages the use of machine-readable SPDX license identifiers. Every source file should start with a comment indicating its license. If you skip the whole comment (it won’t result in an error, just a warning).

Line 2: On the first line we are declaring which Solidity compiler we want to use. The pragma keyword is used to enable certain compiler features or check.

Line 4: We are declaring our contract here and naming it as MyFirstContract. It is normal practice to use the same filename as the contract name. For example — this contract will be saved in the file name MyFirstContract.sol (.sol is the file extension for solidity smart contracts).

Line 5: We are declaring an array of type uint (Unsigned Integer) named numbers, this variable will be used to store data. State variables are variables whose values are permanently stored in contract storage.

Line 7–17: Next, we will add a function, using which we will get the count of even numbers.Functions are usually defined inside a contract. Functions accept parameters and return variables to pass parameters and values between them. Here the function is returning a parameter unsigned integer count. In addition, the function is marked as public which means that the function can be called by anyone.

Line 19–25: We will add another function to check if the number is even and return true or false based on the result. This function is marked as view which tells Solidity compiler that this is a read-only function. Other than that the function also has returns (bool), which means that the function will return a boolean value.

Compile and Deploy

To compile your code, click on the Solidity compiler button. When you hover over the buttons on the left side of the editor, you should be able to see the button’s name.

Now click on the button that reads Compile MyFirstContract.sol. If the compiler doesn’t encounter any errors, then you’ll have successfully compiled your first smart contract.

To deploy your code, click on the Deploy & run transactions button. This button is just below the Solidity compiler button in the left-hand menu. While on this page, ensure that your contract name displays correctly above the Deploy button.

You can now click Deploy to run your code on the current local test network, with no transaction fees.

You can test your code from the REMIX IDE itself. Under the Deployed Contracts section, click on your deployed contract and you will see your functions. The compiler automatically generates getter functions for public state variables, which allows other contracts to read their values.You can add sample input values and test your code.

Conclusion

So we have successfully created and deployed our smart contract written in Solidity to the blockchain, You can refer the Solidity Documentation for more.

--

--

Sneha Thakare
Sneha Thakare

Written by Sneha Thakare

Frontend Engineer (Javascript)

No responses yet