How to execute the smart contract of Ethereum?

Pen Pavilion 2021-05-15 01:52:14 阅读数:602

本文一共[544]字,预计阅读时长:1分钟~
execute smart contract ethereum

The main reason why blockchain technology dominates the top technology is its decentralization . Although the main purpose of blockchain is to maintain transaction records without a center , But for Automation , Smart contracts are introduced . So after writing the smart contract ? In this tutorial of Ethereum smart contract , We will learn how to use Truffle Ethereum And Ethereum private network to execute smart contracts .

We'll look at the following topics in the Ethereum smart contract tutorial :

  • Use cases : Smart contracts in the insurance process
  • The benefits of smart contracts
  • Installation prerequisites
  • To configure Genesis Block
  • Run Ethereum private network
  • Create an Ethereum account
  • Create Ethereum smart contract
  • Execute the Ethereum smart contract

Use cases : Smart contracts in the insurance process

Blockchain follows “ There is no central authority ”, That's why smart contracts are introduced . But have you ever thought about how to use smart contracts ? that , In this part of the Ethereum smart contract , I'll explain the use of smart contracts in the insurance process .

Let's consider an example of flight delay insurance . Suppose you want to start from A Destination C Fly , But you didn't fly directly . that , You decide to pass B Let's take a turn . Now? , Your route will be from A To B, And then from B To C, among B It's the airport , You're going to change the flight . Unfortunately , from A To B And from B To C There's not a big time gap between our flights . therefore , If there are any questions , Once from A To B The flight delay of , Then you will miss from B To C The flight . When you realize that , To avoid heavy losses , You may need a flight delay insurance .

Now? , If you A To B The flight delay of ( This will make you miss from B To C The flight ), You will get the amount of insurance . It's normal , If your flight is delayed , You can apply for insurance . then , Someone will verify and approve the insurance , Last , You will get the amount of your insurance . But it's a long process .

How do you use smart contracts to improve the insurance process ?

When it comes to financial transactions , Especially when you take the money ,“ The sooner the better ”, isn't it? ? that , Let's see how smart contracts can enhance the insurance process . Smart contracts are digital contracts , Automatically when certain conditions are met . If the flight is delayed , Smart contracts can be written to pay the insured amount to those who choose to take out flight delay insurance . therefore , When the flight is delayed and the system records this delay , The insurance will be paid immediately .

OK! The insurance is paid in seconds . This is how smart contracts can be implemented simply and quickly .

The benefits of smart contracts

You've seen in the example above how smart contracts enhance financial processes . In addition to fast trading , Smart contracts have more benefits . ad locum , I listed some other benefits of using smart contracts :

  • automation : Smart contracts automate all the steps .
  • There is no intermediary : When you use smart contracts , You don't need an intermediary to do the work , Because everything will be handled by smart contracts .
  • Cost effectiveness : The use of smart contracts can save the transaction fees and intermediaries charged by banks ( If any ) The service charge of .

Now? , We know how to use smart contracts to make the world faster , Let's experience this Ethereum smart contract tutorial .

Installation prerequisites

For the Ethereum smart contract tutorial , We need to 5 An important application :

  • NodeJS
  • NPM
  • Ethereum
  • Truffle
  • Solidity Compiler

install NodeJS

NodeJS It's a tool for building server applications JavaScript frame . Because we use a private network , therefore NodeJS It's easy to build web applications .

To install Nodejs, Please run the following command in the terminal :

$ sudo apt-get install nodejs

install NPM

NPM representative Node Package Manager, Used to run the Nodejs Applications .

To install NPM, Please run the following command in the terminal :

$ sudo apt-get install npm

Install Ethereum

Ethereum is a distributed computing platform based on open source and public blockchain , For building decentralized applications .

To install Ethereum , Please run the following command in the terminal :

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum

install Truffle

Truffle It's Ethereum Blokchains Development environment of , Test framework and asset pipeline .

To install Truffle, Please run the following command in the terminal :

$ npm install -g truffle

install Solidity Compiler

Solidity It's a programming language for writing smart contracts . To run smart contracts on our systems , We have to install Solidity Compiler.

To install Solidity Compiler, Please run the following command in the terminal :

$ sudo npm install -g solc

To configure Genesis Block

Genesis Block yes Blockchain The starting point of the , We need a genesis File to start Blockchain. In this part of the Ethereum smart contract , We will write a Genesis File and configure it to allow us to run smart contracts .

Let's first create a new directory , It is then created in that directory genesis file :

$ mkdir ethereum-network
$ cd ethereum-network
$ nano genesis.json

Now? , stay genesis.json Enter the following line in the file :

{
"config": {
"chainId": 2019,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc": {},
"difficulty" : "200"
"gasLimit" : "99999999999999"
}

Save and exit .

Run Ethereum private network

Here it is Etheruem In the smart contract tutorial , We're going to deploy Ethereum smart contracts on private networks . therefore , To start this network , We will use the following command :

$ geth --datadir ./dataDir init ./genesis.json

$ geth --port 4321 --networkid 1234 --datadir=./dataDir --rpc --rpcport 8543 --rpcaddr 127.0.0.1 --rpcapi "eth,net,web3,personal,miner"

Before further discussion , Let me explain some of the important parameters used in the above command :

  • datadir: Storage and Blockchain Directory of related data .
  • rpc: Enable HTTP-RPC The server .
  • rpcport and rpcaddr It is used to set the port and address of the network respectively .
  • rpcapi: Allow us to use different API Interact with Ethereum network .

take Geth Connect to Ethereum private blockchain

Geth The console is a console that we can interact with Ethereum private blockchain . To put Geth Connect to Ethereum private blockchain , Please open a new terminal and run the following command :

$ geth attach http://127.0.0.1:8543

Now? , We are Geth Console , We can communicate with blockchain .

Create an Ethereum account

To make any deal , We need an account . In this part of Ethereum's smart contract tutorial , We'll see how to start from Geth The console creates a new Ethereum account .

Follow the steps so far , We've been Geth Console . To create a new account , Please be there. Geth Run the following command in the console :

> personal.newAccount('seedphrase')

take seedphrase Replace with the password you want to set for this account .

We created a new account , But this account doesn't have ether. We need to start mining . To start digging , We need to unlock the account first . Let's unlock the account and start digging .

> personal.unlockAccount(web3.eth.coinbase, "seedphrase")
> miner.start()

As mining continues to happen , some ether Will be deposited into the account .

Be careful : these ether Is a virtual ether, No real value .

To check for ether balance , We will run the following command :

web3.fromWei(eth.getBalance(eth.coinbase), "ether")

When you run this command on a regular basis , You'll see that as a result of mining ether increase .

To stop digging , Please run the following command :

> miner.stop()

Create Ethereum smart contract

Create a truffle project

Now we're ready for our private blockchain , We will see how to use Truffle Create Ethereum smart contract . In this tutorial , We will create a simple “Hello World” Ethereum smart contract .

First , Let's first create a new directory to store Truffle project . And then in that directory , We will create a new Truffle project . Open a new terminal and run the following command :

$ mkdir truffle
$ cd truffle
$ truffle init

truffle init The command will create Truffle All necessary documents required for the project .

Now we are ready to deploy all the content of the Ethereum smart contract , Let's write a “Hello World” Intelligent contract .

writing “Hello World” Intelligent contract

All contracts should be written in contracts Directory . We'll switch to this directory and create a directory called HelloWorld.sol The contract of , And add the following line to this file :

pragma solidity ^0.4.15;
contract HelloWorld {
string public message;
function Hello() public{
message = "Hello World!";
}
}

This is this. ! But this smart contract can't be executed alone . We're going to have to do some configuration for it .

To configure Truffle transfer

To move our smart contract , We have to truffle The directory migrations Add a file to the directory . In this directory , We're going to add a name called 2_deploy_contracts.js The file of , It includes the following :

var HelloWorld = artifacts.require("./HelloWorld.sol");
module.exports = function(deployer) {
deployer.deploy(HelloWorld);
};

Save and exit .

To run on our network Truffle, We need editors truffle In the directory truffle.js file . Open this file and enter :

module.exports = {
rpc: {
host:"localhost",
port:8543
},
networks: {
development: {
host: "localhost", 
port: 8543,
network_id: "*",
from: "0xfa2361236b5ac8079cb6cf250e5284922ed9ba9a",
gas: 20000000
}
}
};

Be careful : take from Replace the address with the address of the account you created in the previous step .

Execute the Ethereum smart contract

In the last section of the Ethereum smart contract tutorial , We will learn how to deploy our Hello World Intelligent contract .

Compiling and deploying smart contracts

Before we execute the smart contract , We have to first compile and deploy it to our Ethereum private network . We will do this with the following command :

$ truffle compile

Now? , We have to unlock our account and start digging . Use Geth The console returns to the terminal and runs the following command :

> personal.unlockAccount(web3.eth.coinbase)
> miner.start()

then , Return to with truffle As the terminal of the current working directory and run the following command :

$ truffle migrate

Wait for the deployment to complete .

Executing smart contracts on private Ethernet block chains

To execute Hello World Intelligent contract , We have to get into Truffle Console . Run the following command to do this :

$ truffle console

You are going to be in Truffle Console . To execute smart contracts , Please run the following command :

> var first_contract
> HelloWorld.deployed().then(function(instance) { first_contract = instance; })
> dApp.message.call()

Congratulations ! You've created the first Ethereum smart contract and executed it . I hope this Ethereum smart contract tutorial can provide information , And help you understand how to execute the Ethereum smart contract . Now? , Keep trying to write other smart contracts and execute them .

If you want to learn blockchain and Blockchain Technologies Build a career , So please see some Ethereum we share 、 The currency 、EOS、Fabric And other interactive online programming practical courses related to blockchain :

  • java Ethereum development tutorial , Mainly aimed at java and android Programmers develop blockchain Ethereum web3j Detailed explanation .
  • python The etheric fang , Mainly aimed at python Used by the Engineer web3.py Detailed explanation of blockchain Ethereum development .
  • php The etheric fang , It mainly introduces the use of php Conduct smart contract development interaction , Create an account 、 transaction 、 Transfer accounts 、 Token development, filters and transactions .
  • Introduction to Ethereum , It mainly introduces smart contract and dapp application development , Suitable for entry .
  • Advanced course of Ethereum development , It mainly introduces the use of node.js、mongodb、 Blockchain 、ipfs Realize decentralization of e-commerce DApp actual combat , Suitable for advanced .
  • ERC721 Ethereum proves the actual battle , The course is a digital art creation and sharing DApp The actual development of the main line , In depth to explain the concept of Ethereum heterogeneity pass 、 Standards and development programs . It contains ERC-721 Autonomous implementation of standards , Explain OpenZeppelin Second development of contract code base , The actual project adopts Truffle,IPFS, It has realized the general stock exchange and decentralized general stock exchange .
  • C# The etheric fang , It mainly explains how to use C# Based on Development .Net The Ethereum application of , Including account management 、 Status and transactions 、 Smart contract development and interaction 、 Filters and trading etc .
  • java Bitcoin development tutorial , This course is for beginners , It covers the core concepts of bitcoin , For example, blockchain storage 、 Decentralized consensus mechanism 、 Keys and scripts 、 Trade with UTXO etc. , At the same time, it also explains in detail how to Java Bitcoin support function is integrated in the code , For example, create an address 、 Manage your wallet 、 Construct naked transactions, etc , yes Java A rare bitcoin development learning course for Engineers .
  • php Bitcoin development tutorial , This course is for beginners , It covers the core concepts of bitcoin , For example, blockchain storage 、 Decentralized consensus mechanism 、 Keys and scripts 、 Trade with UTXO etc. , At the same time, it also explains in detail how to Php Bitcoin support function is integrated in the code , For example, create an address 、 Manage your wallet 、 Construct naked transactions, etc , yes Php A rare bitcoin development learning course for Engineers .
  • c# Bitcoin development tutorial , This course is for beginners , It covers the core concepts of bitcoin , For example, blockchain storage 、 Decentralized consensus mechanism 、 Keys and scripts 、 Trade with UTXO etc. , At the same time, it also explains in detail how to C# Bitcoin support function is integrated in the code , For example, create an address 、 Manage your wallet 、 Construct naked transactions, etc , yes C# A rare bitcoin development learning course for Engineers .
  • EOS Introductory tutorial , This course helps you get a quick introduction to EOS Development of decentralized application of blockchain , Content covered EOS Tool chain 、 Accounts and wallets 、 Issue tokens 、 Smart contract development and deployment 、 Use code to interact with smart contracts and other core knowledge points , At last, we use all the knowledge points to complete a note DApp Development of .
  • Play in depth EOS Wallet development , This course uses mobile phones EOS The whole development process of wallet is the main line , Learn more EOS Blockchain application development , The course covers accounts 、 Computing resources 、 Intelligent contract 、 Actions and transactions, etc EOS The core concept of blockchain , It also explains how to use eosjs and eosjs-ecc Development package access EOS Blockchain , And how to React Integration of front-end applications EOS Blockchain support . The content of the course is simple , It's very suitable for front-end engineers to study deeply EOS Blockchain application development .
  • Hyperledger Fabric Blockchain development details , This course is for beginners , The content includes Hyperledger Fabric The identity certificate and MSP service 、 Authority strategy 、 Channel configuration and startup 、 Chain code communication interface and other core concepts , Also contains Fabric Network design 、nodejs Chain code and operation practice of application development , yes Nodejs Engineers learn Fabric The best choice for blockchain development .
  • Hyperledger Fabric java Blockchain development details , The course is for beginners , The content includes Hyperledger Fabric The identity certificate and MSP service 、 Authority strategy 、 Channel configuration and startup 、 Chain code communication interface and other core concepts , Also contains Fabric Network design 、java Chain code and operation practice of application development , yes java Engineers learn Fabric The best choice for blockchain development .
  • tendermint Blockchain development details , This course is suitable for the students who want to use tendermint Engineers who develop blockchain , The course includes tendermint The core concepts in the application development model , for example ABCI Interface 、 Ms merkel tree 、 Multi version state library, etc , It also includes rich practical codes such as token issuance , yes go The best choice for language engineers to get started with blockchain development .

Original translation of Huizhi , Reprint please indicate the source . Here is How to execute the smart contract of Ethereum ?

版权声明:本文为[Pen Pavilion]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/05/20210515012020260T.html