Autonomy and safety. How smart contracts work

How to create a “smart” contract, what advantages and disadvantages they have. RBC-Crypto answers all questions about new promising technology

A smart contract is a computer program that executes agreements concluded between two or more parties, as a result of which, when certain conditions are met, certain actions occur. That is, when a previously programmed condition is activated, the smart contract automatically executes the corresponding agreement. If we consider a regular contract and a smart contract, then we can definitely say that they are both agreements in which two or more parties agree to abide by a set of conditions. Their fundamental elements are the same: voluntary consent of all parties, the object of the contract (goods or services) and a single goal. However, both differ on three factors: the manner of writing, its legal implications and the concept of compliance.

History of smart contracts

The term "smart contracts" was coined by computer scientist Nick Szabo, probably in 1993, to explain the purpose of introducing what he called a "highly advanced" level of contract law and related business practices into e-commerce protocol development. Szabo, inspired by researchers such as David Chaum, also believed that developing smart contracts through the execution of cryptographic protocols and other digital security mechanisms could be a significant improvement over traditional legal contracts.

Szabo used the word “smart” in quotation marks and stated that artificial intelligence would not be involved. He gave a classic example of a smart contract: this is a vending machine. If the terms of the “contract” are satisfactory to the buyer (i.e., he puts money into the machine), then the machine automatically follows the terms of the unwritten agreement and provides the purchase.

Currently, several formal languages ​​have been developed and proposed to define the terms of a contract. There are currently many working groups specializing in smart contracts that are facilitating the continuation of this research in the future. Before the advent of blockchain, there was no platform that could make smart contracts a reality, so it was only defined conceptually.

Choosing an IDE and Solidity version

Before we begin, we need a proper integrated development environment (IDE). In other words, we need a convenient terminal with the necessary tools to write our code. For the purposes of this tutorial, we'll choose Remix, an IDE created by the Ethereum Foundation that allows you to write, test, debug, run smart contracts, and more. You can either use it directly in your browser or download it locally if you prefer.

Once you launch Remix, you'll see a code editor in the center, a file manager on the left, and a compiler on the right.

Initial Remix window

There will be some pre-written code - we won’t need it. To create your first unique smart contract, let's click on the plus icon in the top left corner of the terminal and give it a name.

Creating a new project in Remix

Because we have an empty document. sol, we must specify the version of Solidity that the compiler will run. At the time of writing this guide, the latest version was 0.5.7. If you are not sure which version to use, you can specify a version range.

2 types of Solidity version indication

Let's give our smart contract a name followed by parentheses.

Naming the contract

How do smart contracts work?

Smart contracts are completely digital and written in a programming language. In addition to establishing obligations and consequences in the same way as in a normal physical document, the code can be executed automatically. Therefore, it can receive and process information relevant to the negotiations while already taking action in accordance with the rules of the contract. BTC is limited in its use of tokens for financial transfers.

The Ethereum platform replaces the more limited BTC language (a scripting language of about a hundred) with a language that allows developers to define their own scripts. Ethereum allows developers to program their own smart contracts. The language is "Turing complete", meaning it supports a wider range of computational instructions.

The Ethereum platform has been used to distribute decentralized applications (DApps). Instead of multiple applications driven by multiple protocols, Ethereum allows all applications to be controlled by a single protocol.

Ethereum is a platform that allows developers to create any program and run it on the core functions of the blockchain, using smart contracts to automatically execute their actions using predefined conditions built into the algorithm. If the conditions are met, the specified function will automatically complete without the need to take any action.

Objects of smart contracts and working conditions

Any smart contract must have the following attributes:

  • Signatories are parties to a smart contract (at least two participants) who participate in the process of signing a contract or waiving the prescribed conditions through the use of digital signatures;
  • Subject of the contract - the subject of a smart contract can only be an object that is located in the decentralized network itself or on other sources that the program can access without involving a person. It was the absence of this attribute that prevented the implementation of smart contracts in 1998, since at that time the concept of blockchain as a virtual decentralized environment had not yet been formed and developed;
  • Conditions of the contract - the conditions appear in the form of a mathematical description, which must be programmed in the working environment of the smart contract itself. They are prescribed according to the algorithm;
  • Decentralized platform - data about each smart contract must be recorded and stored in a distributed ledger.

Smart contracts will only be able to execute if certain conditions are met:

  • Availability of a digital signature, the basis for which is public and private keys;
  • Decentralized platform, which is served by independent nodes;
  • Trusted source of digital data;
  • Mathematically proven Turing completeness.

Advantages of smart contracts

Using smart contracts, there is no longer any need to resort to the help of a third party, for example, a lawyer or a notary, which, in addition to possible errors, entails significant costs. Blockchain is able to protect information in an encrypted network that can be accessed from anywhere in the world, so speed and security are obvious. The most important advantages of contracts are:

Autonomy

These contracts are always concluded between one or more individuals or legal entities, but without intermediaries. A lawyer is not required to confirm the contract. Therefore, the parties reduce and may even eliminate any extra person who is not involved in the contract.

Cost reduction

Because contracts are not dependent on a third party, costs are reduced. Less human intervention leads to lower costs.

Speed

Smart contracts use software code to automate tasks that would otherwise be performed manually. Therefore, they increase the speed of business processes and are less prone to manual errors.

Safety

By basing contracts on the Ethereum blockchain, they cannot be lost. Everything is unchangeable. Nothing and no one can make it disappear, and you always have access to them.

The decentralized governance process eliminates the risk of manipulation because execution is automatically managed by the entire network rather than by a single part.

conclusions

We have described the operating principles and features of smart contracts in general terms, but this is a promising area for research. Despite the shortcomings of distributed ledger technology and the problems of scaling a number of projects, their advantages over traditional centralized systems are obvious. This will change not only the financial industry, but also government practices.

Today, many powers are allocating multibillion-dollar funds for the development of blockchain technologies and their implementation in various fields. The success of smart contracts will be the key to the growth of the ETH rate, so you should not lose sight of this cryptocurrency; its rate against the dollar may increase significantly over time.

Disadvantages of smart contracts

On the other hand, programs of this type also have certain disadvantages. The main one is mainly the use of technologies that it involves: Internet of Things and Blockchain.

IoT can provide connectivity to assets, but it still has a long way to go in terms of security. IoT devices are easy to hack. Blockchain is more than secure, but it is immutable. Once the terms are agreed upon, they cannot be subsequently changed, which is disadvantageous for either party.

Create and deploy your own ERC20 token. Issue of tokens and creation of ICO.

Let's face it, most blockchain developers who are just starting out are looking to play big and create their own blockchains and tokens. Although this is an extremely complex topic that has attracted some of the best software developers from other fields, creating a basic ERC20 token is not a difficult task.

First we need to create another file in Remix and load the ERC20 interface, namely:

How to create a smart contract?

The contract is developed in a specific language (Solidity, Serpent or Mutan), compiled in EVM and, with some exceptions, follows the standard ERC-20 interface. Solidity is a language that is based on JS, Python and C++.

To approach creating a smart contract, you need to know Solidity. Just as paper contracts are created by people who know the entire legal environment around them, smart contracts require knowledge of computer code called Solidity.

A Lawyer's Introduction to Smart Contracts describes two "layers" for smart contracts: the Smart Contract Platform (SCP), which is the infrastructure that allows them to be created and managed on the blockchain, and the Smart Contract Management System (SCMS). ). This protocol is added to this infrastructure to make it easier for the user to interact with contracts. That is, it is a graphical interface.

The contract is executed online, inside the Ethereum Virtual Machine (EVM). This execution is not free, but is paid for in the form of "gas", a small amount of ether currency contributed by one or more parties to the contract. An important detail is that execution is decentralized and occurs in all nodes of the network. This is one of the reasons why EVM is so slow and can only process about 15 transactions per second.

Waves Enterprise SDK

We at Waves Enterprise decided to put together a set of tools in one box that would encapsulate all the best practices for developing public blockchains. We deal specifically with enterprise blockchain and understand the basic requirements of customers for tools:

  • use of a common technology stack;
  • lack of several languages ​​within one project;
  • predictable, guaranteed support from the vendor.

We took them into account when creating Waves Enterprise SDK

. It encapsulates the best patterns from the world of blockchain development, uses standard enterprise programming languages ​​(Java, Kotlin) and a single set of tools for both smart contracts and applications that take data from them.

Let's look at an example of a smart contract in our SDK - this is a port of an ERC-20 token:

override fun transferFrom(from: String, to: String, amount: Long) { require(allowance(from, ca11.ca11er) >= amount) { "INSUFFICIENT_ALLOWANCE" } require(balance0f(from) >= amount) { "INSUFFICIENT_BALANCE" } modifyBalance(from) { it – amount } modifyBaIance(to) { it + amount } modifyAlIowance(from, call.alleg) { it – amount } } fun modifyBalance(address: String, fn: (Long) –> Long) { balance.put(address, fn(balance0f(address))) } fun modifyAllowance(from: String, to: String, fn: (Long) –> Long) { allowances.put(allowanceKey(from, to), fn(allowance (from, then))) }

This is a Kotlin code that uses low-level functions to check who gave whom how many rights to transfer tokens, and track the balance for which permission was given. What is the advantage of this code compared to Solidity?

  • no hidden vulnerabilities: re-entrancy attacks are impossible.
  • no need to think about gas optimization - the commission is fixed.
  • written in a more common language - after our training, within one day, Java developers were already starting to write their contracts.

But, as we discussed, smart contracts are not everything. We need to obtain data and information on contract renewal. We have a library for this in the SDK that replaces The Graph, and in this library everything is also written in Kotlin. It is enough to create a BlockListener rotation, specify which keys in the blockchain we are listening to, and you can write arbitrary code that will decompose transaction data into an object model for further work.

@VstBlockListener fun handleEvent( @VstKeyFiIter(keyPrefix = "BALANCE_") event: VstKeyEvent ) { val balance = event.payload val tx = event.tx // transaction val contractID = tx.sender }

In classic Ethereum, to call a contract, you need to use the Web3j library in JS. You need a separate code to call the contract and collect the transaction. We have already implemented this within a single language using standard tools. That is, our SDK implements the full life cycle of software development on the blockchain in one box.

How does the process of registering smart contracts take place?

The smart contract infrastructure can be implemented using replicated records, and contract execution can be done using a Merkle tree, which works through cryptographic hash functions and replication of the Byzantine general problem (BFT), which is a generalized two general problem. Each node in a peer-to-peer network acts as a registry of trust or assurance, performing changes to contract holders and automatically checking the rules imposed by the transaction, as well as verifying the same operation of other nodes. Cryptocurrencies like BTC have implemented special cases for those records or nodes where the property and core of the transaction is money. BTC and many of its derivatives contain more generalized mechanisms for ownership and contract enforcement. Code maintenance is a very important part of the BTC protocol, based on state machine replication.

Solving the Problem of Programming Language Complexity

A barrier arises between a person and smart contracts - knowledge of programming languages. There are several ways to solve this problem:

  1. Studying the language. The problem is that this takes a lot of time.
  2. Using ready-made solutions. The problem is uncertainty about the correctness of code editing.
  3. Using the templates that Nxt offers. The problem is limited capabilities.
  4. Payment for programmer work. The problem is that a high-quality smart contract programmer charges a lot of money for his work, since there is now enough work in the crypto world.

The most optimal solution is created in the iOlite , which makes an engine for converting regular language into contract code.

This tool is called Fast Adaptation Engine and aims to replace the work of coders. Structures of language expressions are attached to parts of code, then combined and reworked to create a complete program. A huge database is connected for this purpose. Data in this database is entered by developers. If part of the code contributed by the programmer was successfully applied in the development of a smart contract, he receives a bonus in iOlite tokens.

On the one hand, it is more profitable for specialists in this field to carry out private orders, but, on the other hand, it is obvious that programming will eventually be automated as much as possible. At the moment, work is underway to translate English into Solidity.

To summarize, it is necessary to highlight Hyperledger Fabric as a platform that is already being actively used, providing developers with flexible tools for using smart contracts.

Rating
( 2 ratings, average 4.5 out of 5 )
Did you like the article? Share with friends:
For any suggestions regarding the site: [email protected]
Для любых предложений по сайту: [email protected]