The easiest way to install Vyper on your system is by using Python’s PIP package manager. Vyper requires Python 3.6 or higher installed on your system; you can check if Python is installed on not by typing the following in your terminal/cmd:
how to write and deploy a smart contract in vyper
$ python --version
$ python --version
Now, let’s install Vyper using PIP. Type the following in your terminal/cmd:
how to write and deploy a smart contract in vyper
$ pip install vyper
$ pip install vyper
If everything goes right, Vyper will be installed in your system.
You can check if Vyper is installed completely or not by typing the following in your terminal/cmd:
how to write and deploy a smart contract in vyper
$ vyper --version
$ vyper --version
Now, open your text editor and make a new Vyper file contract.vy (vy is the file extension for Vyper) and paste the following:
how to write and deploy a smart contract in vyper
# @version ^0.2.0
greet: public(String[100])
@external
def __init__():
self.greet = "Hello World"
# @version ^0.2.0
greet: public(String[100])
@external
def __init__():
self.greet = "Hello World"
# @version ^0.2.0
greet: public(String[100])
@external
def __init__():
self.greet = "Hello World"
Explanation of the code above:
Line 1: Specifying that version of Vyper this contract is meant for.
Line 3: Declaring a public variable greet, which will be stored on the blockchain, the data type of greet is a string with a maximum length of the string to be 100.
Line 5-6: Marking our init function as @external means that it can only be called via transactions or other contracts. Defining our __init__ function, a pythonic constructor called upon object creation will initialize the greet variable.
Line 7: Passing a string “Hello World” to our variable greet.
Now go to your terminal/cmd and cd into the directory where you saved your contract and compile your Vyper contract by typing the following:
how to write and deploy a smart contract in vyper
$ vyper contract.vy
$ vyper contract.vy
how to write and deploy a smart contract in vyper
$ vyper -f abi contract.vy
$ vyper -f abi contract.vy
$ vyper -f abi contract.vy
If the contract doesn’t have any errors, it will be compiled to a byte code and abi, which will look something like this::
Connect your Metamask wallet with the MyEtherWallet platform and copy-paste the Byte code and ABI from the terminal/cmd into the respective fields, type in the contract's name, and click on Sign Transaction, confirm the transaction from Metamask.
Now that our contract is deployed let’s interact with it. Go to the Activity section in your Metamask plugin, click on the last transaction (which deployed the contract), click on the Etherscan button from the top-right, and then copy the contract address.
Select the function from the drop named Select an Item and click on greet. You’ll see the Hello World message under the Result section.