- Getting Started With Ethereum Programming
Ethereum is currently the most widely adopted public blockchain for decentralised applications development. I found that most information on the internet regarding Ethereum development is slightly fragmented. Therefore, in this article I would like to provide a comprehensive overview on getting started with programming on Ethereum, from setting up tools to deploying your smart contract. If you are a complete beginner and would like to learn about Ethereum, you are now at the right place. If you are already learning Ethereum development and got stuck somewhere, this article might give you a better overview.
First of all, we need to understand what Ethereum is and what it means for developers. You could think of Ethereum as a server, except that it is managed by many independent parties. For example, if you are on Facebook, what you see on your news feed is sent to you by the Facebook server. When you click on something on your news feed, you are sending a request to the Facebook server and the server will return you with a response. These responses are what made up the view you are seeing. Therefore, for applications development on Ethereum, the Ethereum blockchain will act as the “server”. However this “server” is not managed by a single party like Facebook, but is maintained by many miners. When you are interacting with the application, you are sending requests to Ethereum and it will return you with responses.
If you would like to build an application on Ethereum, first you have to code the “server” logic. This logic will determine how the system returns responses. This logic is coded in the form of a smart contract, with Solidity as the programming language. Let’s start with a simple Solidity smart contract which acts as a bank. Solidity code has a file extension of .sol, so let’s name this file as Bank.sol.
A simple smart contract.
Learning Solidity is another topic, however as long as you understand any programming language, it should be fairly easy to be understood. For this article, we wouldn’t go into the fine details of Solidity, but instead the main motivation is for you to have an overview of the entire Ethereum programming stack. One resource which I find to be really helpful in learning Solidity is from CryptoZombies.
Now that you have your first Solidity smart contract, you would want to deploy it to the Ethereum platform. What does deploying actually mean in this context? Basically, you make this smart contract public so that everyone will be able to access and interact with it. Every deployed smart contract will have a corresponding address, which is an identifier to it. Truffle is a framework which is suitable for decentralised application development, so let’s install it!
npm install -g truffle
(Prerequisite: you need to have npm and node installed)
After you have installed truffle, run `truffle init` to set up a project directory. You will see a few directories: contracts, migrations and test. Now navigate into the contracts directory: `cd contracts`. You will see an existing smart contract Migration.sol. Copy the smart contract file you’ve written just now to place it in this same directory. Run `truffle compile`. What it is doing is to compile your Solidity code into bytecodes. The Ethereum platform could only execute bytecodes, therefore it is essential to compile them.
Before you are able to interact with the Ethereum network (deploying contract is considered as an interaction), you will first need to set up an Ethereum address. I would recommend using Metamask and to obtain some test ether beforehand. (You could follow this simple tutorial if you don’t have Metamask yet.)
To deploy your smart contract to Ethereum, you will need to connect to an Ethereum node. Since Ethereum is a distributed network participated by many nodes, you could think of connecting to any node as finding the entry point to the network. There are various ways to connect to an Ethereum node, including setting up your own node, but for now let’s just keep it simple and connect to an existing node using Infura. Infura is a useful tool for Ethereum development as it abstracts away the low level details of establishing a connection to the Ethereum network. Go to https://infura.io/ and create an account. Once you’ve done that, on the left panel, you should see an Ethereum icon, go ahead and click on that. Then, create a new project and give it a name. Click on the project and go to settings.
Copy the endpoint
As you can see, the settings will show you the endpoints which are used to connect to an Ethereum node. We will be using the Ropsten network for this article, so let’s select the Ropsten endpoint and copy it.
Now, update your truffle-config.js in the repository with the following code, with the <ID> being replaced by the ID shown in your infura project settings:
In line 15, you will have to change `https://ropsten.infura.io/v3/<ID>` to the endpoint you obtained from Infura.
You’ll need a few more things. If you look at the truffle-config.js file, we need to read a .secret file. This file should contain the seed phrase to your Ethereum address. Let’s do this:
Then copy your seed phrase from Metamask and paste it inside. IMPORTANT: the seed phrase to your account should always be kept private and nobody else should have access to it, so make sure you are very careful with this. If you are pushing the repository to Github, please remember not to push the .secret file.
We also need a library. In the project directory (same directory as truffle-config.js), run
npm install @truffle/hdwallet-provider
We are good to go. Next, run
truffle migrate --network ropsten
This is the command to deploy your contracts to the Ethereum blockchain.
Now, you might be very confused on a few things — what is truffle-config.js file doing and what is Ropsten? Basically, Ethereum is a platform with many networks. Only the main network involved real money, while the other networks are test networks. As a developer, we wouldn’t want to launch our code into production without fully testing it, which is why Ethereum provides many test networks for developers to try out their code without any real cost. Ropsten is one of the test networks. The config file specifies your deployment details.
Congratulations, you’ve now deployed your first smart contract on the Ethereum network. If you look at your terminal, you will be able to see the deployed contract address. This address is needed whenever you would like to interact with your contract. I’m hoping to write a second part to this article, guiding you through interacting with your smart contract using a simple user interface. In the meantime, if you encounter any issues, feel free to drop a comment.
- Date of publication:
- Thu, 01/14/2021 - 09:58
Click on the link - it will be copied to clipboard