Getting Started with Blockchain | Really Understanding Ethereum Smart Contracts

Foreword: You may have heard the term smart contract many times, but how many people really understand what a smart contract is? This article will help you understand the Ethereum Smart Contract. The writer is Gjermund Bjaanes, translated by Dyna of the Blue Fox Notes community.

You may have heard of "smart contracts" and may even know that they are code that runs on the blockchain.

But how can you run code on the blockchain? This is not a simple concept that can be easily understood.

This article explains how smart contracts run on the Ethereum blockchain. A basic understanding of programming will help, as this article contains some simple code for examples. For the sake of clarity, some of the technical details are somewhat simplified, but the concepts are valid.

 

Blockchain – Quick Start

 

There is not much detail here, and the core concept of blockchain technology is distributed ledgers. It is a special type of database that is shared among many participants.

This particular database is a list of transactions that record every transaction that takes place on the network. Everyone has a copy. This decentralized distribution combined with strong monetary incentives eliminates the need for trust between the parties.

Traditionally, trust between parties is resolved through intermediaries, such as third parties, such as Paypal and banks. Transactions with people you don't trust will be conducted by an intermediary that both parties trust.

With the blockchain, this demand has disappeared. Because you can put your trust on the web, where strong incentives eliminate the desire to cheat and deceive (in short: compliance with rules is more profitable).

More specifically: a blockchain network is a group of machines that record the same copy of a transaction list (for example, money transferred from A to B).

Because everyone has the same list, it's hard to trick the network into accepting the wrong transaction. Combined with some encryption algorithms and monetary rewards to comply with the rules, your network will be very secure.

All of this also makes the blockchain almost immutable, because the only way to change the history is to get the majority of the network's consent.

 

What is a smart contract?

 

Different from Bitcoin, the biggest difference in Ethereum is the introduction of the concept of smart contracts. Bitcoin is a digital currency, and Ethereum is also a digital currency, but it is much more than that.

The name "smart contract" is a bit misleading. They are not real contracts and are not particularly smart. They are just some code that can run on the blockchain – or computer logic.

First, I will introduce a special account on the smart contract Ethereum network. You have a user account and you have a smart contract account.

A user account includes:

  • An address (similar to your bank account – it also exists on Bitcoin)
  • Balance (how much I have)

A smart contract account includes:

  • An address
  • Balance (Ethereum)
  • One state
  • Code

The address is the same as the address of a normal account, which is the unique identifier for the account.

The balance is the same concept as a regular account. The only exciting thing is that the balance on a smart contract means that the code can have money. It can handle the money, or it can be mishandled due to coding errors.

The status of a smart contract account is the current state of all fields and variables declared in the smart contract. It works in the same way as field variables in most programming languages. In fact, a materialized object of a class may be the easiest way to understand a smart contract. The only difference is that this object is permanent (unless programmed to self-destruct).

The code for the smart contract is the compiled bytecode, and the Ethereum client and node can run on it. It is the code that is executed when the smart contract is created and contains functions that can be called. Just like any object in an object oriented programming language.

Anecdotes about smart contracts: they can call other smart contracts. This opens up the ability to create autonomous agents that can spend and trade on their own .

Suppose I created a smart contract with the code above. The code has a field called counter, of type uint (integer). The content of the counter variable is the state of this contract. Whenever I call the count() function, anyone can see that the state of the smart contract on the blockchain will increase by one.

Later, we will explain more about how it works, but first I want to go back to Ethereum and Bitcoin transactions and explain some things.

 

At the transaction level, Ethereum V S bitcoin

 

Bitcoin trading is very simple. You can only do one thing. One type of transaction. Skip some details, everything can be attributed to T O (paid, who is collecting money), F ROM (from, who is paying) and A MOUNT (quantity, how much). This makes Bitcoin a value storage tool that delivers value among network participants.

The difference between Ethereum is that the transaction also has a “ D ATA ” (data) field. The " D ATA " field supports three types of transactions:

  • Value transfer (same as bitcoin)
  • TO receiving address
  • The DATA field is empty or contains any messages to attach
  • F ROM you
  • A MOUNT is the number of Ethereum you want to send.
  • Create a smart contract
  • The T O field is empty (it triggers the creation of a smart contract)
  • The DATA field contains the smart contract code compiled into bytecode
  • FROM you
  • AMOUNT can be 0 or any number of Ethereum you want to put in the contract.
  • Call smart contract
      • TO field is the smart contract account address
      • The DATA field contains the function name and parameters – how to call the smart contract
      • FROM you
      • AMOUNT can be 0 or any number of Ethereum, such as the number you need to pay for a service contract

There are more fields and complexities in these transactions, but these have well explained the core concepts. Let's take a look at some more specific examples of these transactions.

Ethereum trading

 

Value transfer

very simple. TO sends a certain number of Ethereum tokens to an address. You can also add a message to a transaction.

Create a smart contract

As mentioned above, an empty TO field means creating a smart contract. The DATA field contains a smart contract compiled into bytecode.

Call contract

We will return to this question later, but the main idea is that you send the transaction to the smart contract address you want to call and then place the function call in the DATA field.

Pay attention to cost and execution

As you can imagine, you can't always run a computationally intensive program for free on the blockchain.

The execution of the code is paid by the caller with something called gas. Gas is the fuel for running the Ethereum virtual machine. You can think of it as the cost of executing an instruction each time (like a single line of code).

You need to set the maximum gas that can be spent for a particular contract call. For example, if the code you call enters a permanent loop, it will ensure that the gas spent during execution will not exceed the maximum gas set.

The cost of gas is determined by the miners of the network (the nodes that run the code). There is a lot of knowledge about gas and execution. But these are worth keeping in mind.

How does a smart contract work?

 

When a smart contract is deployed to the Ethereum network, anyone can call the smart contract function. Although this function may prevent people from calling for security reasons, you are free to try.

Suppose there is an object of type MyObject. This object has a function called myFunction. To call it, simply reference the instance of the object, which function to call, and which parameter to call.

like this:

myObjectReference.myFunction(parameters);

Any value returned by this function can be stored in the variable:

myVariable = myObject.myFunction(parameters);

Calling a smart contract is conceptually the same. The only difference is that you have to put all the information related to the call in the transaction, sign it and send it to the network for execution.

Suppose you want to call the function myFunction, which contains some of the parameters in the smart contract "0x0123456". Calling a smart contract is a four-step process:

Now, when a transaction is placed in a block in the blockchain, the state change is recorded throughout the network.

World computer

 

Many people call Ethereum a world computer. This is a good analogy. It is like a virtual machine maintained by the entire world.

But keep in mind: While smart contracts are Turing-complete and theoretically can do anything, they are not well suited for heavy computing.

The Ethereum world computer is like an old slow computer running a simple program. Because of the cost and security, it is crucial to keep Ethereum's smart contracts small and simple.

The more calculations a contract requires, the greater the cost of running. The more complex the contract, the more likely it is that there is a security breach. And security holes in smart contracts are hard to deal with – after all, blockchains are immutable.

Example: Pass

 

Back to the point, I want to explain how the certificate works.

Most of these passes were created on Ethereum, and the concept is very simple (it works fine, but it's too simple to be stupid).

How to write a simple currency system using Javascript or other programming languages? You can do all the work in one file. What you really need to record is:

Total supply

2. Account

3. Balance in the account

4. Flow of funds

With a simple mapping between the user and the balance, you can get the answer to 123:

The map simply maps an account to a sum of money.

Using the constructor, you can set the initial supply in your own account (or distribute it in any number of accounts)

The flow of funds is done through a simple function, which is subtracted from one account and then added to another.

Creating a pass is exactly the same as the one we used at Ethereum. Of course, there are some more complicated and extra features, but the basic concept is very simple.

Here's what the basic pass is presented in the Ethereum programming language (again: simplified for clarity):

This is the basic programming concept. I think this illustrates the power of Ethereum as a platform. With some simple code, you can generate a token out of thin air, which is essentially some variable recorded by the world computer. Welcome to the new internet.

——

Risk Warning: All articles in Blue Fox Notes do not constitute investment recommendations . Investment is risky . Investment should consider individual risk tolerance . It is recommended to conduct in-depth inspections of the project and carefully make your own investment decisions.

We will continue to update Blocking; if you have any questions or suggestions, please contact us!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

A picture to understand the difference between Bakkt's bitcoin futures and "traditional" futures

According to the delivery method, futures contracts are usually divided into cash delivery and physical delivery. At ...

Blockchain

Data Analysis | Exchange Risk, Exchange Capital Reserve and Platform Coin Valuation Geometry

Analyst | Carol Editor | Bi Tongtong | PANews The FCoin thunderstorm event caused widespread concern. In the last par...

Blockchain

The history of the rise and fall of the exchange: an important silhouette of the development of blockchain

Original from: blog.nomics.com Author: Nathaniel Whittemore & Clay Collins Compilation: Orange Book There may be ...

Opinion

SBF Trial Records Fully Exposed Blame-shifting, Amnesia, Contradictions

Today is the real highlight, as the prosecution lawyer will conduct a half-day long cross-examination of SBF after th...

Opinion

Former close friend to testify against SBF, list of other witnesses revealed.

SBF's trial started on October 2nd, and his once closest friends will testify in court, becoming his biggest threat. ...