Really Understand Ethereum Smart Contracts
Editor's Note: This article comes fromBlue Fox NotesEditor's Note: This article comes from
Blue Fox Notes
Blue Fox Notes
(ID: lanhubiji), author: GjermundBjaanes, compilation: Dyna, released with authorization.
You've probably heard of "smart contracts," and you might even know that they're code that runs on a 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 work on the Ethereum blockchain. A basic understanding of programming will help, as this article includes some simple code for example purposes. Some of the technical details are slightly simplified for clarity, but the concepts are valid.
secondary title
Blockchain - Quick Start
Without going into too many details here, the core concept of blockchain technology is a distributed ledger. It is a special type of database that is shared among many participants.

This special database is a transaction list, which records every transaction that occurs in the network. Everyone has a copy. This decentralized distribution, combined with strong monetary incentives, removes the need for trust between parties.
Traditionally, the trust between the parties is resolved through an intermediary, that is, a third party, such as Paypal and banks. Transactions with people you don't trust will go through intermediaries that both parties trust.
Because everyone has the same list, it is very difficult to trick the network into accepting the wrong transaction. Combine that with some encryption and monetary rewards for following the rules, and you have a very secure network.
What is a smart contract?
All of this also makes the blockchain nearly immutable, since the only way to change history is with the consent of a majority of the entire network.
secondary title
What is a smart contract?
The biggest difference between Ethereum and Bitcoin is the introduction of the concept of smart contracts. Bitcoin is a digital currency, Ethereum is a digital currency, but it is much more than that.
The name "smart contract" is a bit misleading. They are not real contracts, nor are they particularly smart. They are just pieces of code — or computer logic operations — that can run on a blockchain.
First, I'll explain about smart contracts being a special kind of account on the Ethereum network. You have user accounts, and you also have smart contract accounts.
A user account consists of:
An address (similar to your bank account number - it also exists on Bitcoin)
Balance (how much money do I have)
A smart contract account includes:
an address
Balance (Ethereum)
a state
the code
The address is the same as that of a normal account, and it is the unique identifier of the account.
Balance and ordinary account are also the same concept. The only exciting thing is that the balance on the smart contract means that code can own money. It can handle the money, or it can mishandle it because of a coding error.

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

Fun fact about smart contracts: they can call other smart contracts. This opens up the ability to create autonomous agents that can spend money and conduct transactions on their own.
Suppose I create a smart contract with the code above. The code has a field called counter which is of type uint (integer). The content of the counter variable is the state of the contract. Whenever I call the count() function, anyone can see that the state of this smart contract on the blockchain will increase by 1.
We'll explain how it works later with more examples, but first I want to go back to Ethereum and Bitcoin transactions and use that to explain a few things.
secondary title
At the transaction level, Ethereum VS Bitcoin
Bitcoin transactions are very simple. You can do just one thing. A type of transaction. Leaving aside some details, it all boils down to TO (to, who is receiving money), FROM (from, who is paying) and AMOUNT (amount, how much). This makes Bitcoin a store of value, capable of transferring value between network participants.
The difference in Ethereum is that the transaction also has a "DATA" (data) field. The "DATA" field supports three types of transactions:
Value transfer (same as Bitcoin)
DATA field is empty or contains any message to append
Create a smart contract
FROM you
AMOUNT is the amount of Ethereum you want to send
Create a smart contract
TO field is empty (it triggers the creation of the smart contract)
The DATA field contains the smart contract code compiled to byte code
FROM you
AMOUNT can be 0 or any amount of Ethereum you want to put in the contract
call smart contract
The 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 amount of Ethereum, such as the amount you need to pay for a service contract
Ethereum transaction
Create a smart contract

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

As mentioned above, an empty TO field indicates the creation of a smart contract. The DATA field contains the smart contract compiled to byte code.
call contract
Will come back to this later, but the main concept is that you send the transaction to the smart contract address you want to call, and then put the function call in the DATA field.
secondary title
Pay Attention to Cost and Execution
Execution of code is paid for by the caller in something called gas. Gas is the fuel for running the Ethereum Virtual Machine. You can think of it as a fee per execution of an instruction (like a 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 forever loop, it will ensure that the gas spent during execution will not exceed the set maximum gas.
The cost of gas (execution) is determined by the miners (nodes running the code) of the network. There is still a lot to learn about gas and execution. But the above are worth keeping in mind.
secondary title
How do smart contracts work?
myObjectReference.myFunction(parameters);
When a smart contract is deployed to the Ethereum network, anyone can call the functions of the smart contract. Although this function may prevent people from calling it for security reasons, you are free to experiment.
myVariable = myObject.myFunction(parameters);
Suppose there is an object of type MyObject. This object has a function called myFunction. To call it, just refer to the instance of the object, which function to call and with which parameters to call.
like this:
Calling a smart contract is conceptually the same. The only difference is that you have to put all the information about the call in a transaction, sign it and send it to the network for execution.
Suppose you want to call the function myFunction, which contains some parameters in the smart contract "0x0123456". There are four steps to call a smart contract:
Now, when a transaction is put into a block in the blockchain, that state change is recorded throughout the network.
secondary title
world computer
But remember: while smart contracts are Turing complete and can theoretically do anything, they are not well suited for heavy computational work.

The more calculations a contract requires, the more expensive it is to run. The more complex the contract, the more likely there is a security hole. And security holes in smart contracts are hard to deal with — blockchains are immutable, after all.
secondary title
Example: Token
text
Back to the point, I want to explain how tokens work.
Most of these tokens are created on Ethereum, and the concept is very simple (it works well, but so simple that it is almost stupid).
How do you write a simple currency system using Javascript or another programming language? You can do it all in one file. What you really need to log is:
1. Total supply
2. Account

3. Balance in the account
4. Fund flow

With a simple mapping between users and balances, you can get the answer to 123:

The map just maps an account to a sum of money.
Using the constructor, you can set the initial supply in your own account (or spread it across any number of accounts)

The movement of funds is done through simple functions that subtract from one account and add to another.


