官术网_书友最值得收藏!

Creating a smart contract with Vyper

Now let's create a smart contract with Vyper. First, we will create a file with the .vy extension and name it hello.vy, as follows:

name: public(bytes[24])

@public
def __init__():
self.name = "Satoshi Nakamoto"

@public
def change_name(new_name: bytes[24]):
self.name = new_name

@public
def say_hello() -> bytes[32]:
return concat("Hello, ", self.name)

If you come from a Solidity or Python background, you will notice a peculiarity: there is no class (as in the Python programming language) and there is no contract (as in the Solidity programming language) in a smart contract written with the Vyper programming language. However, there is an initializer function. The name of the initializer function is the same as it is in the Python programming language, which is __init__.

While using Python, you can create as many classes as you want in one file. In Vyper, the rule is one smart contract per one file. There is also no class or contract here; the file itself is a class.

This is how you compile this vyper file:

(vyper-venv) $ vyper hello.vy

From this, you will get the following output:

This is the bytecode of the smart contract. Keep in mind that to deploy a smart contract, you need bytecode, but to access a smart contract, you need abi. So how do you get abi? You can do this by running the following command:

(vyper-venv) $ vyper -f json hello.vy

From this, you will get the following output:

If you want to get both abi and bytecode together in a single compilation process, you could combine both flags in the compilation process as follows:

(vyper-venv) $ vyper -f json,bytecode hello.vy

This will give you the following output:

主站蜘蛛池模板: 临沂市| 德保县| 卢氏县| 邵东县| 淮南市| 青神县| 寻甸| 海门市| 黔东| 乌拉特前旗| 泸西县| 霍州市| 尉氏县| 大洼县| 黄浦区| 于田县| 察隅县| 丹巴县| 土默特左旗| 嘉荫县| 新平| 虎林市| 仲巴县| 清流县| 如皋市| 恩施市| 赤壁市| 齐齐哈尔市| 金溪县| 高台县| 洛南县| 浪卡子县| 边坝县| 大石桥市| 顺平县| 上虞市| 永泰县| 鄂托克旗| 辰溪县| 商南县| 柯坪县|