A beginner's tutorial on Ethereum Smart Contract programming

Source: Internet
Author: User
Tags manual writing

The original is the Consensys developer blog, the original author of Eva and Consensys development team. If you want to get more timely information, you can visit the Consensys home page and click Newsletter to subscribe to the email. The translation of this article has been authorized by Mr. Lubin, founder of Consensys.

Some people say that ethereum is too difficult to deal with, so we wrote this article to help you learn how to use Ethereum to write smart contracts and applications, Consensys said. The tools, wallets, applications, and entire ecosystems used here are still in development, and they will work better in the future. The first part outlines the key concepts, the major Ethereum clients, and the programming language used to write the smart contract. The second section discusses the overall workflow, as well as some of the DAPP frameworks and tools that are currently popular. Part three focuses on programming, and we'll learn how to use truffle to write tests and build Dapp for smart contracts. The first part. Overview

If you are completely unfamiliar with the concept of cryptocurrency, such as bitcoin and how it works, we recommend that you take a look at the first few chapters of Bitcoin book by Andreas Antonopoulos and then read the Ethereum white paper. (see Http://ethfans.org/posts/ethereum-whitepaper for the Chinese version of Ethereum white paper)

If you think the chapters in the White paper are too obscure, you can also get to know Ethereum directly. Doing development on Ethereum does not require you to understand all those "cryptography economics" (Crypto economic computer science), and much of the white paper is about Ethereum's improvements to the Bitcoin architecture. Beginner Tutorials

Ethereum.org provides an official beginner's introductory tutorial, as well as a tutorial on token contracts and crowdfunding contracts. The contract language solidity also has official documentation. Another good piece of information for learning smart contracts (and my entry) is dappsforbeginners, but it may be a bit out of date.

The purpose of this article is to complement the above information and to introduce some basic developer tools that make it easier to get started with Ethereum, smart contracts, and build dapps (decentralized apps, distributed applications). I will try to explain in my own (still novice) understanding what each step of the workflow is doing, and I get a lot of help from Consensys's cool developers. Basic Concepts

Knowing these nouns is a good place to start:

Public key cryptography System. Alice has a public key and a private key. She can create a digital signature with her private key, and Bob can use her public key to verify that the signature was actually created with Alice's private key, that is, Alice's signature. When you create an ethereum or Bitcoin wallet, that long 0xdf ... The 5f address is essentially a public key, and the corresponding private key is saved somewhere. An online wallet similar to Coinbase can help you keep your private key, and you can keep it for yourself. If you lose the private key of a wallet that has money, you will lose that money forever, so you'd better make a backup of the private key. "It is very painful to learn this by stepping on the pit," said the experienced.

Point-to-point network. Like BitTorrent, all the nodes in the Ethereum distributed network have equal status and no hub servers. (In the future there will be semi-centric hybrid services for users and developers to facilitate, as we'll talk about later.) )

Block chain. Blockchain is like a world-only book, or database, that records all the trading history in the network.

Ethereum virtual Machine (EVM). It allows you to write more powerful programs on the Ethereum (you can also write scripts on Bitcoins). It is sometimes used to refer to the Ethereum blockchain, which is responsible for performing smart contracts and everything.

Node. You can run the node, read and write the Ethereum blockchain, and even use the virtual machine. The full node needs to download the entire chunk chain. The light node is still under development.

Miners. Mining, which is the node that handles chunks on the blockchain. This page can see the current active part of the Ethereum miner: stats.ethdev.com.

Proof of workload. The miners are always competing to solve some mathematical problems. The first to solve the answer (figure out the next chunk) will receive the aether as a reward. All nodes are then updated with their own blockchain. All miners who want to work out the next chunk have to keep up with the rest of the nodes and maintain the power of the same blockchain, so the entire network can always reach a consensus. (Note: Ethereum is planning to turn to a proof of entitlement system (POS) without miners, but that is not covered in this article.) )

Ether coins. Abbreviation ETH. A real digital currency that you can buy and use. Here is the chart of one of the exchanges that can trade the etheric currency. At the time of writing this article, 1 etheric coins were worth 65 cents.

Gas. (petrol) The implementation of the program on the Ethereum and the preservation of data will consume a certain amount of the etheric currency, gas is the conversion of the etheric currency. This mechanism is used to ensure efficiency.

Dapp. The Ethereum community calls applications based on smart contracts a decentralized app. The goal of Dapp is (or should be) to have a friendly interface to your smart contract, plus something extra, such as IPFs (a central network that can store and read data, not from the Ethereum team but with a similar spirit). Dapp can run on a centralized server that can interact with the Ethereum node, or it can run on one of the Ethereum equal nodes. (Take a minute to think about it: Unlike normal web sites, Dapp can't run on a regular server.) They need to commit transactions to the blockchain and read important data from the blockchain rather than the centralized database. Compared to a typical user login system, the user may be represented as a wallet address while other user data is stored locally. Many things will have different architectures than the current Web application. )

If you want to see how these concepts can be understood from another novice perspective, read just enough Bitcoin for Ethereum. Ethereum Client, smart contract language

Writing and deploying a smart contract does not require you to run an Ethereum node. The following is a list of browser-based Ides and APIs. But if you are learning, you should run an Ethereum node to understand the basic components, not to mention running the nodes. run a client that is available on the Ethereum node

Ethereum has many client implementations in different languages (i.e., multiple ways to interact with the Ethereum network), including C + +, Go, Python, Java, Haskell, and more. Why so many implementations are needed. Different implementations can meet different requirements (for example, the goal of the Haskell implementation can be mathematically validated), making ethereum safer and enriching the entire ecosystem.

While writing this article, I used the Go language implementation of the client Geth (go-ethereum), other times also use a tool called TESTRPC, which uses the Python client pyethereum. These tools are used in the following examples.

Note: I've used a C + + client, and I'm still digging with the Ethminer component and the Geth, so these different components work together.
About mining: Digging is interesting, a bit like taking care of your indoor potted plant, and a way to understand the whole system. Although the current price of the etheric currency may not even be able to fill the electricity bill, but who knows later. People are creating a lot of cool dapp that might make ethereum more and more popular.

Interactive consoles. After the client runs, you can synchronize the blockchain, build the wallet, and send and receive the etheric currency. One way to use Geth is through the JavaScript console (JavaScript console, like the one you press F12 in Chrome, just run in the terminal). You can also use the curl-like command to interact with the client through JSON RPC. The goal of this article is to take everyone through the process of dapp development, so this piece is not much to say. But we should keep in mind that these command-line tools are debugging, configuring nodes, and using wallets.

You run the node in the test network. If you are running the Geth client on the official network, it will take quite a while to download the entire blockchain to sync with the network. (You can determine whether you have synchronized by comparing the last block number printed in the node log and the latest block listed on stats.ethdev.com.) Another problem is that running a smart contract on the official network requires a real etheric currency. There is no problem with running nodes on the test network. There is no need to synchronize the entire blockchain at this time, create a private chain of their own tick, for development is more time-saving.

Testrpc. With Geth you can create a test network, and another way to create a test network faster is to use TESTRPC. TESTRPC can help you create a pile of test accounts with funds at startup. It runs faster and is therefore more suitable for development and testing. You can start from Testrpc, and then move on to the test network created by Geth as the contract slowly takes shape-the startup method is simple, just specify a networkid:geth--networkid "12345". This is TESTRPC's code warehouse, and we'll talk about it later.

Next we talk about the available programming languages, and then we can start to really program. programming language for writing smart contracts

It's good to use solidity. There are several languages available to write smart contracts: a bit like JavaScript solidity, the file extension is. Sol. And Python close to the serpent, with the file name ending with. SE. There are also lll like Lisp. Serpent has been popular for some time, but now the most popular and most stable to be considered solidity, so with solidity good. I heard you like python? With solidity.

Solc compiler. After writing a smart contract with solidity, you need to compile it with SOLC. It is a component from the C + + client implementation (again, different implementations are complementary), here is the installation method. If you do not want to install SOLC you can also use the browser-based compiler directly, such as solidity real-time compiler or Cosmo. The section on programming later assumes that you have installed the SOLC.

Note: Ethereum is in active development, and sometimes the new version will be out of sync. Make sure you're using the latest dev version, or a stable version. If you have a problem, go to the gitter chat room or forums.ethereum.org on the Ethereum project and ask what version the other person is using.

Web3.js API. Once the solidity contract is compiled and sent to the network, you can use the Ethereum web3.js JavaScript API to invoke it and build a Web application that interacts with it.

These are the basic tools needed to write smart contracts on Ethereum and build dapp that interact with them. The second part. Dapp framework, tools and workflow DAPP Development Framework

Although the tools mentioned above can be developed, the use of the framework created by the Community gods makes development easier.

Truffle and Embark. It was truffle that led me into the door. The summer before the truffle, I saw how a bunch of talented students had been in a hackathon (programming marathon) activity, but I was scared, though the results were pretty good. Then truffle appears to help you get rid of a lot of insignificant small things, so you can quickly enter the process of writing code-compile-deploy-test-package Dapp. Another similar dapp build-and-test framework is embark. I have only used truffle, but the two camps have a lot of dapp gods.

Meteor. A different set of development stacks used by many Dapp developers is made up of web3.js and Meteor, Meteor is a common webapp development framework (Ethereum-meteor-wallet project provides a great example of getting started, and Silentciero is building a large number of Meteor and W Eb3.js and Dapp integrated templates). I have downloaded and run some nice dapp that are constructed in this way. There will be some interesting discussions at the Ethereum developer Conference, Ðξvcon1, from November 9 to 13th, on the use of these tools to build dapp and related best practices (the meeting is broadcast live on YouTube).

APIs. Blockapps.net intends to provide a RESTful API for Dapp to use in order to eliminate the hassle of developers running local nodes, a hub-and-spoke service based on Ethereum Haskell. This runs counter to Dapp's de-centric model, but is useful when you can't run an Ethereum node locally, such as when you want a browser or a mobile device to use your dapp. Blockapps provides a command-line tool bloc that can be used after registering a developer account.

Many people fear that the need to run Ethereum nodes to use Dapp will scare users away, but many tools, including Blockapps, can solve the problem. Metamask allows you to use the Ethereum functionality in your browser without nodes, and the Alethzero or Alethone provided by Ethereum is a client with an easy-to-use interface in development, Consensys is building a light purse lightwallet, These tools will make dapp easier to use. Light clients and horizontal shards (sharding) are also in the planning and development. This is an ecosystem of peers that can evolve a hybrid architecture. Intelligent Contract Integrated development Environment (IDE)

Ide. Ethereum officially produced the mix IDE used to write smart contracts, which I didn't use but would try as soon as possible.

Browser-based IDE. Solidity real-time compiler and Cosmo all allow you to quickly start writing smart contracts in your browser. You can even have these tools use your local node, just let the local node open a port (pay attention to security.) These tools sites must be trustworthy, and never put all of your assets in such a local node. Cosmo UI has instructions on how to use Geth to do this). After your smart contract has been debugged, you can use the development framework to add a user interface to it and package it into Dapp, which is exactly what truffle is doing, and the programming chapters that follow are explained in detail.

Ether.camp is developing another powerful enterprise browser IDE. Their IDE will support the sandbox test network, automatically generate the user interface for testing (instead of the manual writing test that will be shown later), and a test trading browser test.ether.camp. Before your contract is ready to go live, using their test network will be a good way to make sure that your smart contract works in a near-real environment. They also provide a trading browser Frontier.ether.camp for the official network, which can see the details of each transaction. At the time of writing, Ether.camp's IDE can only be registered by invitation and is expected to be released soon.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.