How to program smart contracts with Ethereum and Webassembly?

Ethereum is behind one of the most popular crypto-currencies – and yet it's much more than that. Thanks to algorithms, the cryptocurrency can be managed completely autonomously. But how do "smart contracts" work, and how far have we got down this road?

How do smart contracts work in Ethereum?
To understand the principle of smart contracts in Ethereum, we first need to take a little excursion into the world of programming languages. Common programming languages are essentially divided into two groups: interpreted languages and compiled languages. In the first group, programs are written in a high-level language and then executed by an interpreter. Two popular examples are Python and JavaScript. Interpreted languages are common in many application domains (such as the web), as they enable you to get started straight away. They are also very universal and can be used on different platforms without the need for additional tooling.

In contrast, there are compiled languages, where a compiler first converts the program text into another language – often binary machine code. This binary code is platform-dependent and is executed directly on a processor. The compiler can (and must) produce customized code for the processor's instruction set, e.g. for ARM- or Intel-compatible CPUs. Well-known representatives of this type are C and Rust.

However, the reality is, as ever, more complex than these simple categories suggest. For some time now, there have been hybrid forms, such as Java. The Java compiler does not translate Java code directly into "real" machine code, but into a special intermediate format. This intermediate format, in turn, is then executed by an interpreter – the Java virtual machine – on the processor's concrete architecture.

Ethereum's smart contracts also work in a similar way. All the nodes that validate transactions and extract new currency in Ethereum contain an instance of the Ethereum Virtual Machine (EVM). The Yellow Paper, Ethereum's technical specification, defines in detail the instructions that support the EVM – and how they should be executed.

This is a proprietary development with a number of special features:

Interaction with the outside world is not possible: all algorithmic decisions must result from the blockchain and its transactions.
Arithmetic is based on 256-bit values to facilitate the manipulation of larger addresses and amounts.
Special operations such as hash functions are integrated to increase performance.
All instructions are assigned a cost function (fuel), which corresponds approximately to the execution time required and the amount of memory required. The term metering is commonly used in English.
Programming on EVM
As with the Java ecosystem, there are several programming languages for which EVM compilers are available. The most common language is Solidity, which is superficially (syntactically) reminiscent of JavaScript. At the end of 2020, Ethereum's documentation lists two additional languages: Vyper, which is based on Python, and Yul Plus, a completely independent development.

What all these languages have in common is that they are domain-specific because, unlike general-purpose languages, they occupy a niche with particular characteristics and, in particular, a special execution engine: EVM. Of course, these domain-specific languages (DSLs) are basically a good idea for reducing application complexity.

But in the case of EVM, this seems to make little sense. After all, it can – regardless of the lack of possibilities for interaction with the world outside the blockchain – execute arbitrary algorithms, so it is (to put it simply) Turing-complete.

Why not use an existing language and execution environment?
If necessary, we'd have to remove some functionality, but we'd be able to draw on longer experience, more stable tools and – much more importantly – a broader base of programmers. Indeed, it's common knowledge that today, the popularity of a programming language is determined not only by whether it is particularly concise, type-safe or dynamic to program in, but also by the ease with which the numerous existing libraries and packages can be accessed. This change of era is best illustrated by JavaScript, which is often criticized for its crude semantics, but which must be considered the most popular programming language at the latest since the millionth NPM package.

Webassembly, a universal intermediate language
The people behind the Ethereum specification came to the conclusion that certain problems could be solved by abandoning custom development in favor of a universal language. How convenient that development is underway on the web to establish an alternative to the big dog JavaScript: Webassembly (WASM). This is both a universal intermediate language and a binary format, combined with a specification for interpreters. As its name suggests, this open standard was originally designed for the web. In the meantime, other application domains (e.g. smartphone applications) have been explored.

WASM development is led by industry giants such as Microsoft, Google and Apple. A key feature of the language is that it was designed from the outset for portability. This can be seen from the fact that many existing programming languages such as Rust, C++ or Go can already compile for web assembly.

Suivez l’actualité au quotidien

Disclaimer en:


Le trading est risqué et vous pouvez perdre tout ou partie de votre capital. Les informations fournies ne constituent en aucun cas un conseil financier et/ou une recommandation d’investissement.

Summary

You might also like :

Nos Partenaire

BingX

BTC Trading Platform

Bitpanda

BTC Trading Platform

Coinbase

BTC Trading Platform

In the same topic

Discover our tools