Coding a Smart World — Introduction to Vyper Language

Open Auction Smart Contract in Vyper Language

Before we begin with the smart contract let’s take a quick look at Vyper, the new smart contract language in Ethereum.

Vyper is written for Python lovers as it is derived from Python 3. Vyper is not a replacement for Solidity programing language but an alternative to cater to the tastes of developers who prefer Python style language. Saying that Vyper has some advantages compared to Solidity when it comes to Smart Contract programming. The key feature in that regard is Auditability

Vyper code is simple and more human-readable like Python which helps the different parties involved in a smart contract to read the contract without extensive documentation.

Introduction to some basic syntax in Vyper

State Variables

State variables are values which are permanently stored in the smart contract storage. Vyper is a statically typed language, which means that the type of each variable (state and local) needs to be specified or at least known at compile-time. If no value is specified the variable will take the default value specific to the type.

Some important data types

Lists

Lists are reference types and are the same as arrays. Lists are declared as:

_name: _ValueType[_Integer]

Multidimensional lists are declared with multiple square brackets.

Structs

Structs are custom made data type made by grouping other value types. The values of value types are accessed using a dot operator like below.

Mappings

Mappings are like hash tables where every key data is mapped to a value. The key data is stored as a hash function used to look up the value which gives an index. Mappings are only allowed as state variables and modify them inside functions. Mappings are declared as:

map(_KeyType, _ValueType)

Functions

Functions in Vyper are first-class objects which means that they can be referenced by a variable, added in the lists, passed as arguments to another function, etc. Functions have all the rights like any other object variable declared in the contract. Functions decorators are used and the visibility function decorators ‘@public’ or ‘@private’ is mandatory in function declarations.

Functions in Vyper can be invoked as variables to return the function reference or as regular functions to return the function output.

The return value type is indicated by the arrow syntax ->

Constructor Function

For the construction function, it is the same as in Python. Use the __init__() method in Python to initialize a contract class. For instance: The __init__() method below initializes the Auction contract with the beneficiary address and the auction duration.

Default Function

The Default function is the same as the Fallback function in Solidity. It is executed on a call to the contract if no other functions match the given function identifier. This function is always named __default__ and must be annotated with @public. It cannot have arguments and cannot return anything. This function is usually used when making Ether (ETH) transfers to the contract.

self keyword

self represents the instance of the class. By using the “self” keyword we can access the attributes and methods of the class in python. State variables are accessed via the self variable. Private functions are called via the self variable.

Events

Events are primarily used to return smart contract return values to the front-end user interface. Events are also logged into blockchain as some indexed data which can be searched. An event can be executed with the function :

log.<event>()

Events in Vyper work similar to that in Solidity. Events must be declared before the state variables and functions.

A good read about how events are used in Ethereum is here

Some quick facts about Vyper

  • There is no need to mention the compiler version in a Vyper source code file because the right version is automatically picked up by the compiler.
  • There is no null option in Vyper and hence every variable has a default value.
  • Vyper provides a feature to calculate the precise upper bound of gas cost for any function call. This is achieved by preventing programming capabilities like recursive calling, infinite loops, etc.
  • There is no Class inheritance and inline assembler code in Vyper.
  • There are no function modifiers as in Solidity or function overloading or operator overloading to keep simpler compiler code.
  • Vyper is strongly-typed like Python and many new data types are added for better readability.
  • In Vyper each smart contract is written in a separate file and they are independent.
  • In Vyper you can’t give a value to the variable when defining the type of the variable.

The Vyper source code files are saved with extension .vy. Here I have named my file as open_auction.vy. The remix IDE can be opened here. In order to configure the compiler, we can either use a remote compiler or install one locally. The options are listed on the left panel of the remix home page. The local compiler needs some configuration and somehow did not work for me. The remote compiler works fine but only recommended for demo projects.

来源

What do you think?

发表评论

电子邮件地址不会被公开。 必填项已用*标注

Loading…

0

Comments

0 comments

DeFi Money Market (DMM) Introduces mETH — Earn a stable 6.25% on ETH Deposits

Pay-with-stake