A smart contract is a program that is stored on the blockchain and executes an agreement when certain conditions are met. It can be created using a programming language like Vyper, which executes on the Ethereum Virtual Machine (EVM), a decentralized computer with millions of executable projects. Vyper is a Python-based language and Vyper smart contracts are saved with .vy extensions. This article will look at how to build secure smart contracts using Vyper.
Smart contracts store rules for negotiating the terms of a contract. The conditions are directly written into the code. They automatically verify the conditions when fulfilled and then execute the agreed-upon terms. There is no third party; those who are involved in the contract can directly sign it. This results in transparency, efficiency, security, speed, and cost reduction. Small wonder that companies are increasingly turning to smart contracts instead of physical ones.
Let’s understand how a smart contract works using a couple of examples.
Consider a crowdfunding platform where anyone can donate money to save patients in need of expensive treatment. Typically, patients’ families must pay the platform to advertise their needs and raise funds. The donated money goes to the platform and then to the patients. Clearly, the platform serves as a third party.
If a smart contract was included in the above scenario, it would include the patients’ health problems, hospital reports for proof, and facilitate donation collection. There would be no need for a third party.
A Vyper smart contract could be programmed to automatically execute all the actions with respect to certain terms and conditions. If enough funds are raised before the deadline, all the contributions are sent to the patients’ families and treatment can be administered quickly. If the treatment cannot be given, the funds collected are returned to the donors. In smart contracts, data is stored in a distributed manner. Hence, in the above example, no one person has control over the money.
Consider a scenario where Tanya buys Maria’s flat. Maria must provide her flat’s address while Tanya must pay the rent. In this case, a smart contract will work in the following manner:
First, independent storage is created where Tanya and Maria can put their values but cannot easily take them out.
Tanya puts her rent in her storage while Maria puts her address.
When Maria receives the payment confirmation, Tanya receives the address.
If Tanya goes to the flat and finds that the address provided by Maria is wrong, Tanya’s rent is returned.
If Tanya suddenly changes her plan and doesn’t want the flat anymore, she will have to pay Maria some penalty as liquidated damages, and the remaining amount of her rent will be transferred back to her.
At the end of the agreement, the smart contract is considered fulfilled and is stored in the blockchain network.
As smart contracts are designed and implemented within blockchains, they inherit their properties.
The following steps are used to implement a Vyper smart contract.
Download Python 3.6 or higher versions from Python’s official website.
The pip package manager is essential to install Vyper using the following command:
pip install vyper
Let’s build a smart contract to store and display a user’s address.
Note: You will need to register with MetaMask to interact with Ethereum.
Create a folder, vyper_contract, and a file, contract.vy, inside the folder. Contract.vy is the Vyper code file to create the necessary Vyper smart contract.
The corresponding Vyper code is as follows:
# @version ^0.3.1 address: public(String) @external def __init__(addr: String): self.address = addr @view @external def getAddress() -> String: return self.address
The first line is the version pragma. It informs the Vyper compiler to use version 0.3.1 to compile the Vyper smart contract. address is the state variable. As the name suggests, it stores the address of the user in the form of a string. An @external is used to create an external function which can be invoked from the outside. The
__init__ function is the constructor of the Vyper smart contract. It comes into play when the contract is deployed.
The @view informs Vyper that the getAddress function does not change the state of the blockchain. It returns the address state variable which holds the address of the user.
This step checks to make sure the contract works as desired.
<filename>.vy, or in this case, vyper contract.vy, is the command to compile the contract.
The output is a hexadecimal string that is the bytecode of the contract.
Click the Ethereum icon in the sidebar.
From the ENVIRONMENT dropdown, select Injected Web3. A small UI beneath will change to the Ropsten Testnet you are connected to.
Click on Deploy and input a word beside the Deploy button. This word is the user’s address - the desired input.
Clicking the Deploy button will make the MetaMask UI appear.
The Vyper smart contract will be deployed to the Ropsten Testnet.
Now, interact with the contract deployed in the previous step to call the methods in the Remix browser IDE or view the deployed contract on the Ropsten Ethereum Faucet.
Click on the CONTRACT AT 0x26… dropdown. You will see the functions and state variables, getAddress and address. Choose getAddress.
It will return the word passed to the constructor during deployment in step 4. The action called the getAddress function in the contract.
A Vyper smart contract that can store and display user addresses has successfully been tested and deployed! Try your hand at it by using the codes above. You can also try it with different variables.
In this article, we looked at how to build a smart contract using the pythonic language, Vyper. We also touched on some of the features of a smart contract, which are similar to those of a blockchain.
Web 3.0 is the future of web technologies and blockchain technology is one of its main principles. As trust in blockchain grows, smart contracts - being secure, transparent, fast, and cheap - will soon become a lot more mainstream.
Author is a seasoned writer with a reputation for crafting highly engaging, well-researched, and useful content that is widely read by many of today's skilled programmers and developers.
Tell us the skills you need and we'll find the best developer for you in days, not weeks.