Words written in front:I believe that the readers who are reading this article must have heard of blockchain, and believe that blockchain is a promising and even disruptive technology. But they may also feel confused. Although they have some general concepts about blockchain, they will find that it is not so easy to apply blockchain technology and how to program blockchain. Then congratulations! You have come to the right place. This tutorial mainly introduces a major school of blockchain technology: Ethereum programming. I hope that after reading this book, readers can learn and master Ethereum and its smart contract programming, and be able to implement blockchain technology in actual project development. Finally, let me introduce myself: My online name is "Xingyun", I am a senior Internet practitioner, an explorer of blockchain technology for many years, and the founder of Ethereum Chinese Network. Friends who are interested in creating a new world in the blockchain field are welcome to communicate with each other and make progress together. Okay, enough nonsense, let’s get to the point! Blockchain IntroductionWhat exactly is blockchain?Blockchain is a distributed database that originated from Bitcoin. Blockchain is a series of data blocks generated by cryptographic methods. Each data block contains information about several Bitcoin network transactions, which is used to verify the validity of the information (anti-counterfeiting) and generate the next block. (From Wikipedia) Definition of blockchain technology:Blockchain is a distributed ledger , a technical solution for collectively maintaining a reliable database in a decentralized and trustless manner. From a data perspective:Blockchain is a distributed database that is almost impossible to change. "Distributed" has two meanings: one is distributed storage, and the other is that all participants maintain it together. Several characteristics of blockchain technology
Cited from Based on the above advantages, the Bitcoin system has realized a self-operating trading system with billions of transactions, and has been running stably for many years 24/7 globally. The Bitcoin transactions between any two accounts are faithfully recorded in a large number of redundant ledgers. In the Bitcoin network, all accounts are anonymous, and transactions between any accounts cannot be tampered with and will be recorded on every node. Then, through the Bitcoin incentive mechanism for mining, the network is self-operating without any centralized transaction system. EthereumSo what is Ethereum?Ethereum is an open source public blockchain platform with smart contract functions. It provides a decentralized virtual machine (EVM) to process peer-to-peer contracts through its dedicated cryptocurrency Ether (from wiki) The simplest way to put it is: blockchain technology + smart contract. Ethereum inherits blockchain technology and implements support for smart contracts, allowing blockchain technology to be combined with commercial applications and realize project implementation. In the Ethereum network, smart contracts are also regarded as special accounts, so that users can call the properties and methods of the account by trading with it, thus supporting the implementation of smart contracts from the underlying technology. Technical architecture diagramWhat are smart contracts?We have mentioned the five characteristics of blockchain technology before. Ethereum inherits all of the above blockchain technologies and provides support for smart contracts. This expands blockchain technology from the original transaction function between accounts to a platform that can implement smart contracts . This smart contract can be a crowdfunding contract, a mathematical formula, or a completely random number. As long as the smart contract is deployed to the Ethereum network, it inherently has the five characteristics of blockchain technology. At the same time, because it is written in a JavaScript-like language, it can implement a lot of complex business logic. This tutorial mainly introduces the programming of smart contracts. By writing smart contracts that conform to your own business logic, you can easily implement various blockchain-based projects. In the next chapter, we will start with the simplest smart contract and give you a quick introduction to what a smart contract looks like.The simplest smart contractpragma solidity 0.4.9; contract DemoTypes { function f(uint a) returns (uint b) { uint result = a * 8; return result; } } The above is the simplest smart contract. This smart contract implements the most basic function, that is, input N and return 8*N. Official address: https://ethereum.github.io/browser-solidity/#version=soljson-v0.4.9+commit.364da425.js
Paste the code above and you can see the result as shown below: At this time, click the red Create button You can deploy this simplest smart contract on the blockchain network (on memory) Here we can see a few things
This time we enter 100, and then click the f button, we can see the result The result is clear.
The above introduces the simplest smart contract. The next chapter will introduce you to the smart contract language Solidity, which is also the focus of the book. Solidity In the previous article, we can see The Solidity here is the core language of Ethereum smart contracts, Solidity, which is also the focus of this tutorial. What is Solidity?Solidity is the programming language of Ethereum smart contracts. By compiling and deploying smart contracts, you can create, execute and view smart contracts, thereby realizing certain commercial applications. A few simple Solidity examplesThrough the following smart contracts, we can blockchain some business applications well, thus realizing a decentralized, trustless and highly transparent business model. In the following tutorials, we will gradually analyze Solidity programming to help you quickly master the Solidity language and implement blockchain on the front-end web page. I implements the sum function of 1+2+3+..+npragma solidity 0.4.9; contract Demo1 { /*Calculate the sum from 1 to N*/ function f(uint n) returns (uint sum) { if (n == 0) throw; uint result = 0; for (uint i=0; i<=n; i++) { result +=i; } return result; } } II implements a token function and comes with the functions of mining and transferring tokens.pragma solidity ^0.4.0; contract Coin { // The keyword "public" makes those variables // readable from outside. address public minter; mapping (address => uint) public balances; // Events allow light clients to react on // changes efficiently. event Sent(address from, address to, uint amount); // This is the constructor whose code is // run only when the contract is created. function Coin() { minter = msg.sender; } function mint(address receiver, uint amount) { if (msg.sender != minter) return; balances[receiver] += amount; } function send(address receiver, uint amount) { if (balances[msg.sender] < amount) return; balances[msg.sender] -= amount; balances[receiver] += amount; Sent(msg.sender, receiver, amount); } } III Implement a crowdfunding smart contract, where each user can raise funds, and if the fundraising is successful, the proceeds can be transferred to the beneficiary, and each crowdfunding participant can obtain tokens.pragma solidity ^0.4.2; contract token { function transfer(address receiver, uint amount){ } } contract Crowdsale4 { address public beneficiary; uint public fundingGoal; uint public amountRaised; uint public deadline; uint public price; token public tokenReward; mapping(address => uint256) public balanceOf; bool public fundingGoalReached = false; event GoalReached(address beneficiary, uint amountRaised); event FundTransfer(address backer, uint amount, bool isContribution); bool public crowdsaleClosed = false; /* data structure to hold information about campaign contributors */ /* at initialization, setup the owner */ function Crowdsale4 ( address ifSuccessfulSendTo, uint fundingGoalInEthers, uint durationInMinutes, uint etherCostOfEachToken, token addressOfTokenUsedAsReward ) { beneficiary = ifSuccessfulSendTo; fundingGoal = fundingGoalInEthers * 1 ether; deadline = now + durationInMinutes * 1 minutes; price = etherCostOfEachToken * 1 ether; tokenReward = token(addressOfTokenUsedAsReward); } /* The function without name is the default function that is called whenever anyone sends funds to a contract */ function () payable { if (crowdsaleClosed) throw; uint amount = msg.value; balanceOf[msg.sender] += amount; amountRaised += amount; tokenReward.transfer(msg.sender, amount / price); FundTransfer(msg.sender, amount, true); } modifier afterDeadline() { if (now >= deadline) _; } /* checks if the goal or time limit has been reached and ends the campaign */ function checkGoalReached() afterDeadline { if (amountRaised >= fundingGoal){ fundingGoalReached = true; GoalReached(beneficiary, amountRaised); } crowdsaleClosed = true; } function safeWithdrawal() afterDeadline { if (!fundingGoalReached) { uint amount = balanceOf[msg.sender]; balanceOf[msg.sender] = 0; if (amount > 0) { if (msg.sender.send(amount)) { FundTransfer(msg.sender, amount, false); } else { balanceOf[msg.sender] = amount; } } } if (fundingGoalReached && beneficiary == msg.sender) { if (beneficiary.send(amountRaised)) { FundTransfer(beneficiary, amountRaised, false); } else { //If we fail to send the funds to beneficiary, unlock funders balance fundingGoalReached = false; } } } } This is the end of the introduction to Solidity. We will analyze the mysteries of these contracts in detail later. As mentioned above, smart contracts are deployed on the Ethereum network. So how to build an Ethereum network requires the official tool Geth. The next chapter will explain in detail. |
<<: How financial services giants invest in blockchain companies
>>: BTCC CEO Li Qiyuan: Why do some Chinese miners dislike isolated verification?
When a man marries a wife, he wants to marry one ...
Super big eyes man speed dating The eyes are big,...
Your Nose Reveals Your Fortune and Luck 1. A broa...
The beard-like hair above women's lips is call...
Where to look for a man’s fortune line? The wealt...
For many people, in some cases, if it is not conv...
Girls who treat pets as companions When it comes ...
Not everyone is suitable for this great and diffi...
In physiognomy, different facial features represe...
Women with upturned chins usually have better look...
Stand firm at 2850 and hit 3000? 1. Price trend &...
Teeth tell a person's wealth and honor Teeth ...
For women, in fact, these are also many aspects t...
On July 13th local time, U.S. federal judge Anali...
Each of us has moles. Depending on the shape and ...