Smart Contracts of the Future? Vyper and Plutus

By Laura Desmond

As a developer, one thing that you should realize is the importance of constant education. The world of cryptocurrency and blockchain is in a constant state of motion. The norm a year back ago could be completely irrelevant months from now.  So, in the spirit of this constant change, we are going to give you an idea of what Vyper and Plutus smart contracts and code will look like. Ethereum is planning to use Vyper for its future contracts, while Cardano has chosen Plutus as its choice of smart contract programming language.

Smart Contracts of the Future? Vyper and Plutus

Solidity is the Smart contract language of choice for Ethereum as of right now. Solidity was developed by Gavin Wood, Christian Reitwiessner, Alex Beregszaszi, Yoichi Hirai and several former Ethereum core contributors to enable writing smart contracts on blockchain platforms such as Ethereum.

It has been influenced by many popular programs and programming concepts. It has elements of object-oriented programming, web development, assembly language, etc. and covers a wide range of use cases. However, auditing solidity contracts can be really difficult since they can be extremely permissive and complicated.

Vyper is a Python-based language that is built solely for readability and simplicity. Since python itself is one of the most easily understood and widely-used programming languages, it is hoped that non-technical people will be able to hop on and start coding their own smart contracts. Vyper has been developed as a minimalistic, simple language which focuses on trivial use cases (eg. it can’t be used to code a complex Dapp) and is not as flexible as solidity.

So, while it may somewhat curb the flexibility of the contract, it provides a more

“a straightforward, pragmatic, and easily auditable regime for reading and writing smart contracts, which tries to clearly capture the intent of whoever writes the code.”

It looks like Ethereum and Ethereum Classic bigwigs are completely behind Vyper for two main reasons:

 

  • Ethereum’s sharding implementation and Casper FFG have been coded in Vyper.

 

  • The Ethereum Commonwealth which maintains and develops Ethereum Classic has also stated that they want to adopt Vyper as a default smart contract language.

 

Vyper’s minimalist approach does away with inheritance, state modifiers recursive calling, and Operator overloading which gives way to gas limit vector attacks. Plus, it also has tons of overflow checks for large arithmetic operations. Because of its approach, not only is Vyper resource efficient, it is also difficult to write malicious code without it being spotted during auditing.

Vyper Smart Contract

Let’s take a look at a Vyper smart contract courtesy CoinPupil. This is a simple and straightforward ERC-20 contract.

# Events
Transfer: event({_from: indexed(address), _to: indexed(address), _value: uint256})
Approval: event({_owner: indexed(address), _spender: indexed(address), _value: uint256})
​
# State Variables
name: public(bytes32)
symbol: public(bytes32)
totalSupply: public(uint256)
decimals: public(int128)
balances: int128[address]
allowed: int128[address][address]
​
# Constructor Function / Contract Instantiation
@public
def __init__(_name: bytes32, _symbol: bytes32, _decimals: uint256, _initialSupply: uint256):
    self.name = _name
    self.symbol = _symbol
    self.decimals = _decimals
    self.totalSupply =_initialSupply * convert(10, 'uint256') ** _decimals
    self.balances[msg.sender] = convert(self.totalSupply, 'int128')
​
# Account Balance
@public
@constant
def balanceOf(_owner: address) -> uint256:
​
    return convert(self.balances[_owner], 'uint256')
​
# Send _amount of tokens _to address
@public
def transfer(_to: address, _amount: int128(uint256)) -> bool:
​
    if self.balances[msg.sender] >= _amount and 
       self.balances[_to] + _amount >= self.balances[_to]:
        self.balances[msg.sender] -= _amount  # Subtract from the sender
        self.balances[_to] += _amount  # Add the same to the recipient
        
        return True
    else:
        return False
​
# Transfer of allowed tokens 
@public
def transferFrom(_from: address, _to: address, _value: int128(uint256)) -> bool:
​
    if _value <= self.allowed[_from][msg.sender] and 
       _value <= self.balances[_from]:
​
        self.balances[_from] -= _value  # decrease balance of from address.
        self.allowed[_from][msg.sender] -= _value  # decrease allowance.
        self.balances[_to] += _value  # incease balance of to address.
​
        return True
    else:
        return False
​
# Allow _spender to withdraw from the account up to the _value amount. 
@public
def approve(_spender: address, _amount: int128(uint256)) -> bool:
​
    self.allowed[msg.sender][_spender] = _amount
    log.Approval(msg.sender, _spender, convert(_amount, 'uint256'))
​
    return True
​
# Get the allowance of an address
@public
def allowance(_owner: address, _spender: address) -> uint256:
    return convert(self.allowed[_owner][_spender], 'uint256')

In this contract:

  • The “self” method is used to show the instance variables of its class for clarification purposes.

 

  • @public and @private are used to set the visibility and exposure of the contracts ABI (Application Binary Interface) interface which allows external actors (other contracts or wallet addresses) to call it.

Plutus

Cardano has chosen Haskell and Plutus as their languages of choice. Haskell will be used to code Cardano, while Plutus will be used for the smart contract creation. Both of them are functional languages.

What do we mean by that?

The Two Families of Languages (A Slight Detour)

When it comes to programming languages, there are two families:

  • Imperative
  • Functional.

 

Imperative Programming Languages

In an imperative approach, the coder needs to put down all the steps that the computer needs to take in order to reach a goal. All of our traditional programming languages like C++, Java and even Solidity are imperative programming languages. This kind of programming approach is also called algorithmic programming.

Let’s take an example of what we mean by that. Let’s look at C++. Suppose we want to add 5 and 3.

 

int a = 5;

 

int b = 3;

 

int c;

 

c= a + b;

So, as you can see, the addition process takes over multiple steps and each step is constantly changing the state of the program as they are all being executed in turn individually.

An addition process took four steps and the steps are:

  • Declaring an integer a and assigning the value 5 to it.

 

  • Declaring an integer b and assigning the value 3 to it.

 

  • Declaring an integer c.

 

  • Adding the values of and b and storing them in c.

Functional Programming Languages

The second family of programming languages is Functional languages. This style of programming was created to build a functional approach to problem-solving. This kind of approach is called declarative programming.

So, how does functional programming work?

Suppose there is a function f(x) that we want to use to calculate a function g(x) and then we want to use that to work with a function h(x). Instead of solving all of those in a sequence, we can simply club all of them together in a single function like this:

h(g(f(x)))

This makes the functional approach easier to reason mathematically. This is why functional programs are supposed to be a more secure approach to smart contract creation. This also aids in simpler Formal Verification which pretty much means that it is easier to mathematically prove what a program does and how it acts out. This gives Cardano its “High Assurance Code” property.

This is precisely why the functional approach is so desirable.

And that is exactly what Cardano is using Haskell to code their ecosystem and Plutus for their smart contracts. Both Haskell and Plutus are functional languages.

Back to Plutus

Plutus is a higher level and simplified version of Haskell. This means, that like Haskell, it has more similarities to mathematics rather than programming. Plutus is intended for use by

“financial institutions, places that do electronic trading and other such financial operations with money at stake.”

So, let’s take a look at some basic Plutus programming. The following code has been taken from Cardano docs.

This simple Plutus program shows addition, multiplication, factorial, and Fibonacci:

add : Nat -> Nat -> Nat {
  add Zero n = n ;
  add (Suc m) n = Suc (add m n)
}

mul : Nat -> Nat -> Nat {
  mul Zero _ = Zero ;
  mul (Suc m) n = add (mul m n) n
}

fac : Nat -> Nat {
  fac Zero = Suc Zero ;
  fac (Suc n) = mul (Suc n) (fac n)
}

fib : Nat -> Nat {
  fib Zero = Suc Zero ;
  fib (Suc Zero) = Suc Zero ;
  fib (Suc (Suc n)) = add (fib n) (fib (Suc n))
}

You can see in the code itself the differences between a functional approach and an imperative approach.

Conclusion

The ever-changing world of crypto and blockchain means that we need to be on our toes constantly. Having knowledge of these new programming languages is absolutely critical for all bounty hunters and developers.

Comments

comments