Discover how one can write a sensible contract utilizing the Vyper programming language as an alternative of utilizing Solidity

Many programmers who’re studying how one can write a sensible contract will be taught concerning the Solidity language. There are considerable sources of on-line tutorials and books which is able to educate you about Solidity. When mixed with the Truffle framework, Solidity types a killer combo for creating a sensible contract. Virtually all good contracts that dwell on the Ethereum blockchain are written within the Solidity programming language.
On this article, we’ll discover how one can write a sensible contract within the Vyper programming language.
- Introduction
- Setting Surroundings
- Creating a sensible contract utilizing Vyper
- Deploying a sensible contract to Ganache
- Interacting with good contracts
- Interacting with different good contracts
- Compiling code programmatically
What’s Vyper?
Vyper is a contract-oriented, pythonic programming language that targets the Ethereum Virtual Machine (EVM). Vyper has quite simple/intelligible syntax; one of many rules of Vyper is to make it nearly inconceivable for builders to code deceptive packages.
Why Vyper?
- Safety: It must be potential and pure to construct safe good contracts in Vyper.
- Language and compiler simplicity: The language and the compiler implementation ought to try to be easy.
- Auditability: Vyper code must be maximally human-readable. Moreover, it must be maximally tough to jot down deceptive code. Simplicity for the reader is extra vital than simplicity for the author, and ease for readers with low prior expertise with Vyper (and low prior expertise with programming usually) is especially vital.
Due to this Vyper offers the next options:
- Bounds and overflow checking: On array accesses and arithmetic.
- Help for signed integers and decimal fixed-point numbers
- Decidability: It’s potential to compute a exact higher certain for the gasoline consumption of any Vyper operate name.
- Robust typing
- Small and comprehensible compiler code
- Restricted assist for pure capabilities: Something marked fixed is just not allowed to alter the state.
Vyper wants Python 3.6 software program. So, when you don’t have Python 3.6 software program put in, you could set up it here, then comply with the under steps:
$ python3 -m pip set up --user virtualenv
$ pip set up vyper
$ virtualenv -p python3.6 vyper-venv
$ supply vyper-venv/bin/activate
(vyper-venv) $ pip set up vyper
(vyper-venv) $ vyper --version
Now let’s create a sensible contract with Vyper. First, we’ll create a file with the .vy extension and title it whats up.vy
, as follows:
That is the way you compile this vyper file:
(vyper-venv) $ vyper whats up.vy
From this, you’re going to get the next output:

That is the bytecode of the good contract. Take into account that to deploy a sensible contract, you want bytecode, however to entry a sensible contract, you want abi. So how do you get abi? You are able to do this by working the next command:
(vyper-venv) $ vyper -f json whats up.vy
From this, you’re going to get the next output:

If you wish to get each abi and bytecode collectively in a single compilation course of, you might mix each flags within the compilation course of as follows:
(vyper-venv) $ vyper -f json,bytecode whats up.vy
This offers you the next output:

So how do you deploy this good contract to the Ethereum blockchain? There are few methods to do that, however let’s make use of a well-known manner utilizing Truffle:
- Create a listing and initialize it with truffle init as follows:
$ mkdir hello_project
$ cd hello_project
$ truffle init
2. Set truffle-config.js as the next:
module.exports =
networks:
"improvement":
network_id: "*",
host: "127.0.0.1",
port: 8545 // port at Ganache working
,
;
3. Create a construct listing, as follows:
$ mkdir -p construct/contracts
$ cd construct/contracts
4. Then create a Hiya.json file there, as follows:
“abi”:
”bytecode”:
5. Then fill the abi subject with abi or json output from the compilation course of, and fill the bytecode subject with the bytecode output from the compilation course of. It’s good to quote the bytecode worth with double-quote marks. Don’t overlook to place a comma between the abi subject and the bytecode subject. This offers you one thing much like the next:
6. You may then create a migration file to deploy this good contract by creating a brand new file in migrations/2_deploy_hello.js
, as follows:
var Hiya = artifacts.require("Hiya");
module.exports = operate(deployer)
deployer.deploy(Hiya);
;
After every thing is ready up, fireplace up Ganache!
7. Then, contained in the hello_project listing, you might simply run the migration course of, as follows:
$ truffle migrate
You will notice one thing much like the next:

Your good contract written with Vyper has been deployed to Ganache. Your good contract deal with is as follows:
0x4AB3935Df0E224771663984f3617F1a78beA4E8D
Simply as we did earlier than, you should use the Truffle console to work together along with your good contract, as follows:
$ truffle console
Your good contract is at all times given the title Contract. We are able to entry the good contract utilizing the next assertion:
truffle(improvement)> Contract.at("0x4AB3935Df0E224771663984f3617F1a78beA4E8D")
You’ll get a protracted output in which you’ll be able to see abi, bytecode, and so forth, as proven within the following screenshot:

Let’s take a look at the worth of the title variable of the good contract utilizing the next assertion:
truffle(improvement)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(operate(occasion)return occasion.title.name(); );'Satoshi Nakamoto'
Let’s change the title as follows:
truffle(improvement)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(operate(occasion) return occasion.change_name(“Vitalik Buterin”), from: “0xb28Fc17791bf66719FBFCc65846B01Fe2726e9E2” );
The worth within the
from
subject is taken from one of many accounts in Ganache. You may simply take a look at the Ganache window and select any account you want.
Output:
from: ‘0xb28Fc17791bf66719FBFCc65846B01Fe2726e9E2’
Now has the title been modified? Let’s discover out. Run the next command:
truffle(improvement)> Contract.at(“0x4AB3935Df0E224771663984f3617F1a78beA4E8D”).then(operate(occasion)return occasion.title.name(); );‘Vitalik Buterin’
Your good contract can work together with different good contracts on the blockchain.
The deal with knowledge sort is just not solely used for regular accounts however may also be used for good contract accounts. So, a sensible contract can donate ethers to our donatee through the donation good contract!
Restart your Ganache. Keep in mind your whats up.vy
file? We need to deploy our Hiya good contract with a customized title.
Our migration file, migrations/2_deploy_hello.js
continues to be the identical, as proven within the following code:
var Hiya = artifacts.require("Hiya");
module.exports = operate(deployer)
deployer.deploy(Hiya);
;
Compile your whats up.vy
file once more to get the interface and the bytecode. Open our contracts JSON file, the construct/contracts/Hiya.json
file. Take away all of the content material and change it with the next code:
"contractName": "Hiya",
"abi": <your Hiya good contract's interface>,
"bytecode": "<your Hiya good contract's bytecode>"
You must give a reputation to your good contract as a result of this time, you will deploy two good contracts. When you don’t give a reputation to your good contract, it’s going to have a default title, Contract. It’s not an issue when you solely need to deploy one good contract.
Then, to your donation.vy
, edit it, and add the next strains of code:
Utilizing the next code, create one other migration file for our Donation good contract, migrations/3_deploy_donation.js
:
var Donation = artifacts.require("Donation");
module.exports = operate(deployer)
deployer.deploy(Donation);
;
Compile your donation.vy
and get the interface and the bytecode of the good contract. Then, utilizing the next code, create one other contract JSON file for our Donation good contract, construct/contracts/Donation.json
:
"contractName": "Donation",
"abi": <your Donation good contract's interface>,
"bytecode": "<your Donation good contract's bytecode>"
Run the migration. You could have to make use of —- reset
flag, as follows:
$ truffle migrate --reset
You’ll get the next output:

Be aware the deal with of the Donation
good contract “0x25aFF89B8a72aB4Ba6F4C831f5B1375f9BCe76A9” and Hiya
good contract “0x772138489eD34095FBA6a0Af70d3C9115813CFcA”. Yours may very well be completely different.
Run the Truffle console as follows:
$ truffle console
Now our good contract is just not lonely anymore😄, as proven within the following code:
truffle(improvement)> Donation.at(“0x25aFF89B8a72aB4Ba6F4C831f5B1375f9BCe76A9”).then(operate(occasion) return occasion.donation_smart_contract_call_hello_smart_contract_method.name(“0x772138489eD34095FBA6a0Af70d3C9115813CFcA”); );
Output:
‘0x48656c6c6f2c205361746f736869204e616b616d6f746f’
You can create a script to compile Vyper code, as an alternative of utilizing a command-line utility. Just remember to are in the identical listing containing Hiya.vy
and donation.vy
. Create a script named compiler.vy
, as follows:
When you execute this script utilizing the next command, you’re going to get a Hiya.json
file that you might use with Truffle, as proven within the following code:
(vyper-venv) $ python compiler.vy
On this article, we noticed how one can write a sensible contract utilizing the Vyper programming language. First, we put in the Vyper compiler. Then we developed a sensible contract.
By doing this, we realized about many of the options of the Vyper programming language, together with the operate decorator, initialization operate, and performance permission modifier.
There are additionally some knowledge sorts equivalent to deal with, integer, timestamp, map, array, and array of bytes (string). We realized how one can compile a Vyper supply to a sensible contract after which deploy it to Ganache with the Truffle instrument. We additionally interacted with that good contract by way of the Truffle console.