Guide to Writing and Deploying Your First Smart Contract
From code to action, learn how to deploy a smart contract on the Ethereum network with ease
Smart Contracts are self-executing contracts that can automate the transfer of digital assets or the execution of specific actions on various blockchains, including Ethereum, Binance Smart Chain, Polkadot, and Cardano.
After writing a smart contract, the next step is to deploy it onto a blockchain network for execution. To complete this process, developers often utilize development tools such as Remix-IDE.
Remix-IDE is a web-based Integrated Development Environment (IDE) that facilitates the development, testing, and deployment of smart contracts on the Ethereum blockchain.
In this blog, we will walk you through making a simple Increment-Decrement smart contract and deploying it to the Ethereum network using Remix.
Setting up Development Environment in Remix-IDE
- Preparing Your Smart Contract Development Environment in Remix-IDE:
Before writing smart contract code, you’ll need to set up your development environment.
This process starts by opening Remix-IDE (https://remix.ethereum.org/) in your browser. Once you’ve opened up the Remix-IDE interface, locate and click on the “Contracts” tab situated on the left-hand side of the screen. Click the “File” icon and create a new file for your smart contract code.
2. Creating Your Smart Contract File:
After you have set up your development environment, it’s time to create a new file to start writing your Solidity smart contract code. Choose any name for your file and start writing your code in the editor. For example, you could name your file “IncrementDecrement.sol” and begin writing your contract code in the editor.
Remember to include the “.sol” file extension when saving your Solidity smart contract file in Remix-IDE.
With these steps complete, you’re ready to begin writing your smart contract code.
Write the contract code:
Check out this simple example smart contract that lets you increase or decrease a value with ease!
This is a simple example of a smart contract written in Solidity, a programming language used to create smart contracts on the Ethereum blockchain.
Explanation:
The first line of the contract pragma solidity ^0.8.7;
specifies the version of Solidity that the contract is written in. In this case, it’s version “0.8.7”.
The contract is named IncrementDecrement
, and it contains one state variable named Value
of type uint256 (default value of 0). State variables are stored on the blockchain, and their values persist between functions: increment()
and decrement()
, as well as a getter function called getValue()
.
The increment()
increments the value
by 1, and the decrement()
decreases the value
by 1. Both functions are marked as external
(only other contracts and accounts can call them).
Both functions emit events: the Increment
event when increment()
is called, and the Decrement
event when decrement()
is called. The events emit a string message to the blockchain, indicating whether the value was incremented or decremented and by how much.
Finally, the getValue()
is a getter function that allows anyone to view the currentvalue
without being able to modify it. Since this function does not modify the state of the contract, the “view” keyword is used to indicate that it is a read-only function. It is marked as public (meaning it can be called from outside the contract) and returns a uint256.
In short, the smart contract is called IncrementDecrement and has three functions:
- increment() — adds 1 to a value stored on the blockchain
- decrement() — subtracts 1 from the same value stored on the blockchain
- getValue() — returns the current value stored on the blockchain
The value is stored as a state variable, which means it is permanently stored on the blockchain and can be accessed by anyone with the appropriate permissions.
Overall, this contract provides a simple way to increment or decrement a value on the Ethereum blockchain and can be used as a building block for more complex applications.
Once you’ve finished writing your code, you can move on to the next step of compiling your code.
Compile the contract:
After the code is written, you need to compile it into bytecode that can be executed on the Ethereum network. To do this, go to the “Solidity Compiler” tab on the left-hand side of the screen. This will open the Solidity compiler, select the version of Solidity you’re using (e.g., 0.8.7), and click “Compile IncrementDecrement.sol.”
Once your code is compiled, you can see the bytecode and ABI (Application Binary Interface) in the “Compiled Contracts” section on the right-hand side of the screen. The bytecode is what will be executed on the Ethereum network, and the ABI is the interface that allows other contracts or applications to interact with your smart contract.
Deploy smart contract
To deploy your smart contract, go to the “Deploy & Run Transactions” tab, and select “IncrementDecrement” from the dropdown menu.
In the “Environment” dropdown, select the network you want to deploy your contract to (e.g., “Remix VM” for a local testing network or “Injected Web3” for the main Ethereum network).
Before deploying your contract, you may want to set any initial values or parameters for your contract. You can do this in the “Deploy” section on the right-hand side of the screen. In our case do not need to set any initial values.
Once you’re ready to deploy your contract, click on the “Deploy” button. This will initiate the deployment process, which may take some time depending on the network and gas prices, and will deploy the contract to the local testing network.
Interact with the deployed contract (testing)
Once your contract is deployed, you can interact with it by calling its functions from the “Deployed Contracts” section on the bottom-left side of the screen. This section shows all of the contracts that have been deployed to the selected network, along with their addresses and function interfaces.
To call a function on your deployed contract, select the contract from the “Deployed Contracts” section, then click on the function you want to call. This will open a dialog box where you can enter any required parameters for the function and submit the transaction to the Ethereum network. In our case, no parameter is required to call the functions.
Deploy Smart Contract to Testnet (Goerli)
Pre-requisites: have MetaMask Extension installed
To deploy our smart contract to Testnet, go to the “Deploy & Run Transactions” tab, and select “IncrementDecrement” from the dropdown menu. Under the “Environment” dropdown menu, select “Injected Provider — MetaMask”. This will prompt MetaMask to connect your wallet.
Note: Deploying a smart contract to the Ethereum testnet requires you to have Ether (ETH) in your wallet to pay for the gas costs of the deployment.
If You don’t have any testnet ETH:
- Go to the Goerli Testnet Faucet website at https://goerli-faucet.slock.it/.
- Follow the instructions on the page to request some test Ether for your account. You will need to provide your Ethereum address (the one you want to receive the test Ether on) and complete a CAPTCHA.
- Once you have completed the CAPTCHA, click the “Request” button to submit your request for test Ether.
- Wait a few moments for the transaction to be processed. Once the transaction is confirmed, you should receive the requested amount of test Ether in your Ethereum wallet.
Keep in mind that test Ether on a testnet has no real-world value and cannot be used on the main Ethereum network.
Click the “Deploy” button to deploy your smart contract to the testnet. Follow the prompts in your Ethereum wallet to confirm the transaction.
Once the transaction is confirmed, you should see a new “Deployed Contracts” section appear at the bottom of the “Deploy & Run Transactions” panel. You can interact with your deployed contract by clicking on the contract’s address.
That’s it! Your smart contract is now deployed to the Ethereum testnet and can be used by anyone with an Ethereum wallet connected to that testnet.
Bonus Tip: When writing smart contracts, it’s important to keep security in mind. One common vulnerability in smart contracts is the “reentrancy attack,” which allows an attacker to repeatedly call a contract function and drain its funds. To prevent this, you can use the “check-effects-interaction” pattern, which ensures that all state changes are made before any external calls are made.
Conclusion
Writing and deploying smart contracts can seem challenging at first, but with the right tools and knowledge, it can be a straightforward process. In this blog post, we have walked through the process of setting up a development environment using Remix-IDE, writing a simple Increment-Decrement smart contract using Solidity, compiling the code, and deploying the smart contract on the Ethereum network.
By following this step-by-step guide, you can start building your own smart contracts and harnessing the power of blockchain technology. Remember to always test your code thoroughly and take appropriate security measures to ensure the safety of your contracts and digital assets.
Stay tuned and follow Simform Engineering for important updates on various tools and technologies.