There are many articles about the deployment of smart contracts on the network , But they all have one common characteristic , It is to deploy by command line , First, establish SOLC Compile environment for , Then deploy Geth perhaps Eth node , And then generate the wallet step by step ,ABI, Contract address for deployment , For beginners, it is difficult to understand and easy to fail , This paper mainly introduces how to deploy and call smart contract in the graphical interface . For other blockchain knowledge , Please refer to my other articles :

One ,        Smart contracts and DAPP summary

1,  Basic concepts of smart contract

A smart contract is a collection of code and data , Can be deployed to run on Ethereum network . If you make a metaphor, smart contracts are more like JAVA program ,JAVA Procedure passed JAVA virtual machine (JVM) The code interprets the byte for execution , Ethereum smart contract through Ethereum virtual machine (EVM) Interpreted as bytecode for execution , If you've studied assembly , You will find that the compiled bytecode is very similar to the assembly . At the same time, smart contracts have their own accounts , It can automatically perform some functions driven by time or events , If you can pass information between each other , Modify the status of the blockchain, such as account information . The biggest feature of Ethereum smart contract is Turing complete , Generally speaking, it can simulate all the things that a computer can do , Bitcoin, as we all know, can actually execute some simple scripts , But he's not Turing complete , For example, bitcoin can't execute the circular instruction .

Ethereum virtual machine (EVM)

Ethereum virtual machine (EVM) It is the operating environment of smart contract in Ethereum . It's not just sandboxed , In fact, it runs completely isolated , That is to say, it runs in the EVM The internal code cannot touch the network , File system or other process , Even there are limited calls between smart contracts .

2,  DAPP Basic concepts

        Beginners often combine smart contracts with DAPP Confused , The Ethereum community refers to smart contract based applications as decentralized applications (Decentralized
App).DApp The goal is to make your smart contract have a friendly interface , Add something extra that's good for the user . typical DApp The example consists of a html Interface ,web3 Runtime , a section JS Code and a smart contract deployed on the blockchain . And general CS The architecture of the site is different ,DApp Cannot run on a normal server ,DApp It must be running on a server that can interact with Ethereum nodes , Or any Ethereum node .DApp The transaction is submitted to the blockchain network to interact with the corresponding smart contract , And from the blockchain network instead of a centralized database, such as (MYSQL database ) Read important data . Compared with typical BS Architecture user login system is different , Ethereum users are represented as a hexadecimal address and all user data and other data are stored locally , Compared with the current web There are many different application architectures .

3,  Smart contract high level language

        It is impossible for users to write Ethereum virtual machine directly (EVM) Bytecode , So Ethereum provides several high-level languages for writing smart contracts .

Solidity: similar JavaScript, This is the flagship language recommended by Ethereum , It is also the most popular smart contract language . Please refer to Solidity file , address :

Serpent: similar Python style , Document address :

LLL: similar Lisp style , It's now terminated .

You can choose different high-level languages according to different habits , At present, the most popular Solidity.

Two ,        Deploying the first smart contract on Ethereum private chain

        The smart contract in this paper adopts the official example contract of Ethereum , The function is to store a number on the blockchain , And be able to read it out . The code is as follows :

contract SimpleStorage {

    uint storedData;

    function set(uint x) {

        storedData = x;


    function get() constant returns (uintretVal) {

        return storedData;



Even if I haven't learned it Solidity Language can also be roughly seen , The contract set Function stores a number in the X Variable ,get Function from X Read this number from the variable , Let's deploy this contract :

1,  Start private chain

        Start Ethereum private chain Geth and Ethereum-Wallet Graphical interface ( This article uses Geth 1.41 edition , Ethereum-Wallet
0.8.1 edition ). If you don't know how to start , Please refer to my last article 《 Blockchain development ( One ) Building private chain environment based on Ethereum 》 address :
<>, After startup, the interface is as follows ,
Ethereum-Wallet It will show in red PRIVTE-NET sign .


2, Create two wallets

        click ”ADD ACCOUNT”
Button , Add a wallet , A dialog box will pop up , Prompt for password twice , After entering the password , The account is created successfully . Create another account , The same operation , You can see from the screenshot above , There are three accounts , One is MAIN

3, Mining for some ether

        After account creation , There is no ether yet , Mining in the private chain is needed , Switch to Geth Interface , input


        miner The 1 Represents a single thread for mining , If not configured , Will let CPU Full speed operation , Affect the use of computers .

          After running for a while , The primary account will get a lot of ether , At this time, the screen will quickly swipe the screen , Don't worry , Enter the command miner.stop() Stop mining .

4,  Transfer Ethernet from one wallet to another

        Click first ACCOUNT2 account number , Enter the account details interface , Click on the right “COPY
address”, hold ACCOUNT2 Copy your address , The system will prompt you that you are in a test network , Don't transfer real Ethernet to this account .

        Click in wallet “SEND” Button , from MAINACCOUNT to ACCOUNT2 Transfer into certain ether , At the same time, we can see that the cost of executing this transaction is 0.00042 Ethers .



Click send and you will be prompted to enter the password , But it hasn't been sent yet , According to the transaction rules of blockchain , Need miner's confirmation , And every transaction needs to be confirmed 12 Pieces , One block is 16 Second generation time . Switch to Geth program , Enter the mining command , until ACCOUNT2 Display on 100 Ethers , Then stop mining .

5,  Deploying smart contracts

        spot “CONTRACTS”, Enter the smart contract management interface , click “DEPOLY NEW
CONTRACT”, Start deploying smart contracts , Select the account to deploy the smart contract , And input the code of the smart contract , As shown in the figure below

          After input, click “DEPLOY”, The system will prompt for the password , Because there is a cost to deploy a smart contract .



    There is no smart contract deployed at this time , Switch to Geth Interface , Mining , stay 12 After a block , The smart contract can be confirmed and displayed .

Three ,        Running smart contracts

1, Run smart contract on this node

click “CONTRACTS” Enter the smart contract interface , You can see the smart contract just deployed “SimpleStorage”, Click to enter the smart contract , Enter the details interface , There are write area and read area of smart contract , Start first Geth Mining , Then select the corresponding smart contract function in the write area SET, Enter the value you want to set in the value input box below , After running for a while, you can see the smart contract function in the read area GET in Retval The return value of has changed .

          The same is true for other smart contracts , It's just a function of many points , Input multipoint .


2, Running smart contracts on other nodes

At this time, the smart contract can only be seen by itself , Others can't see and run , If someone else is going to run your deployed smart contract, you need to provide some information , It's what other tutorials call smart contracts ABI And address .

Enter the newly deployed “SimpleStorage” Smart contract interface , There are four buttons on the right

A.“Deposit Eher”: Send Ethernet to the smart contract

B.“Copy address”: Copy the address of the smart contract

C.“Show QR Code”: Display a QR code , If you scan with a mobile phone , Displays the address of the smart contract

D.“Show Interface”: Display the JSON Interface , that is ABI

        First of all, let's order “Copy address”, Copy the address of the smart contract , Then click “Show
Interface” Smart contract JSON Copy all the interfaces , Open at another node that needs to run the smart contract Ethereum-Wallet, open “CONTRACTS” Interface Click “WATCH
CONTRACTS” Add a smart contract .

        As shown in the figure above ,CONTRACT NAME Fill in as you like ,CONTRACT ADDRESS Fill in smart contract address ,JSON
INTERFACE Fill in the ”Show Interface “ Content copied in . spot OK after , You can see the smart contract and run it .

Four ,        Deployment principle of smart contract

1, Deployment architecture of smart contract

  Although the deployment of smart contract introduced in this paper is compiled and executed in graphical interface , But in fact, the most important thing is to rely on the background operation Geth Node of , here Geth Provides a RPC Provides blockchain information to the wallet with graphical interface .

RPC Interface

         Geth stay 8545 Port provides JSON RPC API
, Data transmission adopts JSON format , Can be executed Web3 Library commands , For example, to the front end , such as mist And other graphical clients provide blockchain information , The default access address is
http://localhost:8545 <http://localhost:8545/>.

When we deploy a smart contract , first Ethereum-Wallet call SOLC The smart contract compiler compiles code into EVM Bytecode , Then the EVM Bytecode passing Geth Of RPC Interface to Ethereum network , After verification of the whole network , Write to each at the same time Geth In the managed blockchain , The structure is as follows


2, Deployed data flow

  First, the code goes through SOLC Compile into binary code , Then create a smart contract through a transaction , The transaction contains the creator's account number , Content of smart contract , Address of smart contract , The smart contract address is generated by the creator's account number and the number of transactions sent as random numbers , adopt Kecca-256 The encryption algorithm recreates an address as an account .


        During deployment , It needs to be deployed through transactions , At the same time, the data should be stored on the blockchain , These need to be used to GAS.


transaction (Transactions)

        A deal is a message , Send from one account to another . Transactions can contain binary data (payload) And Ethernet .

        If the target account contains a code , The code and input data are executed .

If the target account is a zero account ( The account address is 0), The deal will create a new contract . As mentioned above , This smart contract address is not a zero address , It is calculated from the address of the contract creator and the number of transactions sent out by that address . Create the payload Be treated as EVM Bytecode execution . The output executed is stored permanently as contract code . This means that , To create a contract , You don't need to send a real contract code to the contract , Instead, it sends code that returns executable code .


Each transaction on Ethereum will be charged a certain amount gas,gas Is to limit the amount of work required to execute the transaction , At the same time, it pays for the execution . When EVM When executing smart contract ,gas Will be gradually consumed according to specific rules , actually GAS It's a smaller unit of ether , If ether is compared to 100 element , that GAS It can be seen as 1 Cents . If only ether , There will be problems , Ethers need to be bought and sold , There will be price fluctuations in the market . There may be a situation like bitcoin , One day down 50% rise 50%. This is not acceptable for the calculated cost , For example, it costs ten yuan to do an addition today , It will cost 100 yuan to do an addition tomorrow . So here's the introduction gas To decouple . Decouple market volatility from computational overhead , In other words, ether and gas There is an exchange rate between them , It doesn't matter if ether goes up ,gas Just drop the price . It makes sure I do the same calculations , The consumption of fiat money is consistent .

        gas price(gas Price , Etherometer ) It is set by the transaction creator , Transaction costs to be prepaid for sending account = gas price * gas
amount. If the execution is over, there are gas surplus , these ones here gas Will be returned to the sending account .

        The deployment of smart contracts was mentioned in the previous article 0.00042 Ethers , Converted into gas namely 21000 individual gas.

        No matter where the execution is , once gas Be exhausted ( For example, it is reduced to a negative value ), Will trigger a out-of-gas abnormal . All status changes made by the current call frame will be rolled back. .

Five ,        Operation principle of smart contract

Smart contract is the code deployed in the blockchain , The blockchain itself cannot execute code , Code execution is local EVM in , actually , The code deployed on the blockchain is the code that can generate the original smart contract code locally , It can be understood that blockchain is a database ,而客户端从数据库中读取了存储的运行代码,并在本地运行后,将结果写入到了区块链这个数据库中.