Vyper:Ethereum開発のための新しい言語


Solidityで1ヶ月間Ethereumを開発した後、私はDappを開発するための他の方法を模索したいです。 VyperはEthereumを開発するための新しい言語です。 面白いようですので、勉強しましょう。

Ethereumとは

Ethereumはパブリックブロックチェーンネットワークです。Ethereumを使用すると、開発者は独自のDappを構築して展開できます。EtherはEthereumブロックチェーンの通貨です。Etherは、Ethereumネットワーク上で取引やサービスを支払うために使用されます。

Ethereumの詳細については、リンクを参照してください:
https://blockgeeks.com/guides/what-is-ethereum/
https://www.ethereum.org/

Vyperとは

Vyperは、Ethereumブロックチェーンでスマートコントラクトを開発するために使用できる新しい言語です。Vyperはスマートコントラクトの作成プロセスを大幅に簡素化するように設計されています。VyperはSolidityと同じロジックを持ちますが、Pythonと同じ構文です。Vyperは2018年6月現在でもv0.1.0 - beta.1にあります!

Vyperをよりよく理解するために、それをSolidityと比較しましょう!

Vyperの目標

Simplicity
Vyperには、クラスの継承、関数のオーバーロード、演算子のオーバーロード、再帰など、ほとんどの開発者がよく知っている多くのコンポーネントは含まれていません。 これらは複雑さを増やすことによってセキュリティリスクを表します。

Security
Vyperと安全なスマートコントラクトを結ぶのは簡単です。

Vyperの特徴

Vyperは以下の機能を提供します:
1. Bounds and overflow checking: On array accesses as well as on arithmetic level.
2. Support for signed integers and decimal fixed point numbers
3. Decidability: Possible to compute a precise upper bound for the gas consumption.
4. Strong typing
5. Small and understandable compiler code
6. Limited support for pure functions

Vyperは以下の機能を提供していません:
1. Modifiers
2. Class inheritance
3. Inline assembly
4. Function overloading
5. Operator overloading
6. Recursive calling
7. Infinite-length loops
8. Binary fixed point

バイパー変数

Vyperは静的型言語です。つまり、各変数の型を指定する必要があります。Vyperはいくつかの基本変数型を提供します。変数を宣言するには、次の形式「_name: _type」を使用します。

Boolean

Booleanは論理値を格納する変数型です。可能な値は「True」と「False」だけです。

// Vyper Boolean Variable Declaration
varName: bool

// Solidity Boolean Variable Declaration
bool varName;

Signed Integer (int128)

正と負の数値を格納できる可変型です。 それは128ビットの記憶域を持っています。

// Vyper Integer Variable Declaration
varName: int128

// Solidity Integer Variable Declaration
int128 varName;

Unsigned Integer (uint256)

正と負の数値を格納できる可変型です。 それは256ビットの記憶域を持っています。

// Vyper Integer Variable Declaration
varName: uint256

// Solidity Integer Variable Declaration
uint256 varName;

Decimals

10進数を格納できる変数型です。

// Vyper Decimal Variable Declaration
varName: decimal

// Solidity has no decimal variable type

Address

ブロックチェーンアドレスを格納できる変数型です。

// Vyper Address Variable Declaration
varName: address

// Solidity Address Variable Declaration
address varName;

String

一連の文字を格納できる変数型です。

// Vyper String Variable Declaration
varName: bytes32
varName: bytes

// Solidity String Variable Declaration
String varName;

List

同じ変数型の情報の配列です。

// Vyper List Declaration
varName: int256[]

// Solidity String Declaration
int256[] varName;

Structures

これはデータ構造です。 接続されたデータが含まれます。

// Vyper Structure Declaration
structName: {
  varName: int128
}

// Solidity Structure Declaration
struct structName {
  int128 varName;
}

Mappings

これはデータの配列です。 キーは各データに割り当てられます。 このキーは、データにアクセスするために使用されます。

// Vyper Mapping Declaration
mapName: information[uint256]

// Solidity Mapping Declaration
mapping (uint256 => information) mapName;

Vyper関数

Function

関数は、特定のタスクを実行するコードのセクションです

// Vyper Mapping Declaration
@public 
def functionName(parameterName: bytes32):
  // some codes

// Solidity Mapping Declaration
function functionName(String parameterName) public {
  // some codes
}

Constructor

コンストラクタは、プログラムの起動時に直ちに呼び出される関数です。

// Vyper Mapping Declaration
@public 
def __init__(parameterName: bytes32):
  // some codes

// Solidity Mapping Declaration
constructor(String parameterName) public {
  // some codes
}

Default Function

デフォルト関数は、関数呼び出しが存在しない関数につながるたびに呼び出されます。

// Vyper Mapping Declaration
@public
@payable 
def __default__():
  // some codes

サンプルコード

これはサンプルのVyperコードです。 このスマートコントラクトは、図書館システム用に設計されています。 それは3つの主な機能を持っています:本の借り、本の返却、本の追加。私の他のポストで元のSolidityコードを見ることができます。https://qiita.com/Binsu/items/2772570d67d915148411

# data needed
# 必要なデータです
totalNumberOfBooks: public(int128)
totalNumberOfBorrowers: public(int128)
currentNumberOfBorrowers: public(int128)

bookDetails : public({
    id: int128,
    titleOfBook: bytes,
    authorOfBook: bytes,
    isBorrowed: bool}[uint256])

borrowerDetails  : public({
    id: int128,
    bookID: int128,
    borrowerName: bytes}[uint256])

# Constructor
@public
def __init__():
    totalNumberOfBooks = 0
    totalNumberOfBorrowers = 0
    currentNumberOfBorrowers = 0

// function to add books
// 本を追加すること機能です
@public
def addBooks(_title: bytes, _author: bytes):
    totalNumberOfBooks = totalNumberOfBooks+ 1
    bookDetails[totalNumberOfBooks] = {id: totalNumberOfBooks, titleOfBook: _title, authorOfBook: _author, isBorrowed: false}

# function to borrow a book
# the book to be borrowed should be available
# 本を借りること機能です
# 借りる本は利用可能になるはずです
@public
def borrowBook(_bookID: int128, name: bytes)
    assert books[bookID].isBorrowed == false
    assert currentNumberOfBorrowers < totalNumberOfBooks

    totalNumberOfBorrowers = totalNumberOfBorrowers + 1
    currentNumberOfBorrowers= currentNumberOfBorrowers + 1

    borrowerDetails[totalNumberOfBorrowers] = {id: totalNumberOfBorrowers, bookID: _bookID, borrowerName: name}
    bookDetails[bookID].isBorrowed = true

# function to return a book
# you cannot return an unborrowed book
# 本を返すこと関数です
# 借りてない本を返すことはできません
@public
def returnBook(bookID: int128)  
    assert books[bookID].isBorrowed == true
    currentNumberOfBorrowers= currentNumberOfBorrowers - 1
    bookDetails[bookID].isBorrowed = false

Vyperコントラクトを展開する

Vyperは新しい言語であるため、Vyperスマートコントラクトを展開する方法はほとんどありません。

  1. Vyperコンパイラによって生成されたバイトコードを取得して、それを手動でデプロイします。「MistかGeth」
  2. myetherwalletを使用してVyperスマートコントラクトを展開します。

まとめ

Vyperはスマート契約を作成するためのシンプルで安全な言語です。 それは新しいので、ドキュメントやコミュニティのサポートのためのソースはまだありません。 しかし、Vyperを勉強して言語の先駆者になれるのはまだ良いです。

VyperはSolidityを置き換えるものではありません。 Ethereumコミュニティは、より強力なセキュリティを必要とし、Vyperはそれを提供したいと考えています。

Vyperを練習したい場合は、このリンクにアクセスしてください。https://vyper.online/

Sources:
https://blockgeeks.com/guides/understanding-vyper/
https://vyper.readthedocs.io/en/latest/testing-deploying-contracts.html#deploying-a-contract